Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 5ebb538

Browse files
committed
Revert "checkpoint: make run func generic"
This reverts commit db0d731.
1 parent 8575f8a commit 5ebb538

File tree

1 file changed

+46
-59
lines changed

1 file changed

+46
-59
lines changed

cmd/checkpoint/main.go

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ const (
2525
kubeletAPIPodsURL = "http://127.0.0.1:10255/pods"
2626
ignorePath = "/srv/kubernetes/manifests"
2727
activePath = "/etc/kubernetes/manifests"
28+
manifestFilename = "apiserver.json"
2829
kubeconfigPath = "/etc/kubernetes/kubeconfig"
2930
secretsPath = "/etc/kubernetes/checkpoint-secrets"
30-
31-
tempAPIServer = "temp-apiserver"
32-
kubeAPIServer = "kube-apiserver"
3331
)
3432

3533
var (
36-
secureAPIAddr = fmt.Sprintf("https://%s:%s", os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT_HTTPS"))
34+
tempAPIServer = []byte("temp-apiserver")
35+
kubeAPIServer = []byte("kube-apiserver")
36+
activeManifest = filepath.Join(activePath, manifestFilename)
37+
checkpointManifest = filepath.Join(ignorePath, manifestFilename)
38+
secureAPIAddr = fmt.Sprintf("https://%s:%s", os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT_HTTPS"))
3739
)
3840

3941
var tempAPIServerManifest = v1.Pod{
@@ -43,58 +45,50 @@ var tempAPIServerManifest = v1.Pod{
4345
},
4446
ObjectMeta: v1.ObjectMeta{
4547
Name: "temp-apiserver",
46-
Namespace: api.NamespaceSystem,
48+
Namespace: "kube-system",
4749
},
4850
}
4951

50-
var tempPodSpecMap = map[string]v1.Pod{
51-
tempAPIServer: tempAPIServerManifest,
52-
}
53-
5452
func main() {
5553
flag.Set("logtostderr", "true")
5654
defer glog.Flush()
57-
glog.Info("begin pods checkpointing...")
58-
run(kubeAPIServer, tempAPIServer, api.NamespaceSystem)
55+
glog.Info("begin apiserver checkpointing...")
56+
run()
5957
}
6058

61-
func run(actualPodName, tempPodName, namespace string) {
59+
func run() {
6260
client := newAPIClient()
6361
for {
6462
var podList v1.PodList
6563
if err := json.Unmarshal(getPodsFromKubeletAPI(), &podList); err != nil {
6664
glog.Fatal(err)
6765
}
6866
switch {
69-
case bothRunning(podList, actualPodName, tempPodName, namespace):
70-
glog.Infof("both temp %v and actual %v pods running, removing temp pod", actualPodName, tempPodName)
71-
// Both the temp and actual pods are running.
72-
// Remove the temp manifest from the config dir so that the
67+
case bothAPIServersRunning(podList):
68+
glog.Info("both temp and kube apiserver running, removing temp apiserver")
69+
// Both the self-hosted API Server and the temp API Server are running.
70+
// Remove the temp API Server manifest from the config dir so that the
7371
// kubelet will stop it.
74-
if err := os.Remove(activeManifest(tempPodName)); err != nil {
72+
if err := os.Remove(activeManifest); err != nil {
7573
glog.Error(err)
7674
}
77-
case isPodRunning(podList, client, actualPodName, namespace):
78-
glog.Infof("actual pod %v found, creating temp pod manifest", actualPodName)
79-
// The actual is running. Let's snapshot the pod,
75+
case kubeSystemAPIServerRunning(podList, client):
76+
glog.Info("kube-apiserver found, creating temp-apiserver manifest")
77+
// The self-hosted API Server is running. Let's snapshot the pod,
8078
// clean it up a bit, and then save it to the ignore path for
8179
// later use.
82-
tempSpec, ok := tempPodSpecMap[tempPodName]
83-
if !ok {
84-
glog.Fatalf("cannot find pod spec for %v", tempPodName)
85-
}
86-
tempSpec.Spec = parseAPIPodSpec(podList, actualPodName, namespace)
87-
convertSecretsToVolumeMounts(client, &tempSpec)
88-
writeManifest(tempSpec, tempPodName)
89-
glog.Infof("finished creating temp pod %v manifest at %s\n", tempPodName, checkpointManifest(tempPodName))
80+
tempAPIServerManifest.Spec = parseAPIPodSpec(podList)
81+
convertSecretsToVolumeMounts(client, &tempAPIServerManifest)
82+
writeManifest(tempAPIServerManifest)
83+
glog.Infof("finished creating temp-apiserver manifest at %s\n", checkpointManifest)
9084

9185
default:
92-
glog.Info("no actual pod running, installing temp pod static manifest")
93-
b, err := ioutil.ReadFile(checkpointManifest(tempPodName))
86+
glog.Info("no apiserver running, installing temp apiserver static manifest")
87+
b, err := ioutil.ReadFile(checkpointManifest)
9488
if err != nil {
9589
glog.Error(err)
9690
} else {
97-
if err := ioutil.WriteFile(activeManifest(tempPodName), b, 0644); err != nil {
91+
if err := ioutil.WriteFile(activeManifest, b, 0644); err != nil {
9892
glog.Error(err)
9993
}
10094
}
@@ -124,35 +118,36 @@ func getPodsFromKubeletAPI() []byte {
124118
return pods
125119
}
126120

127-
func bothRunning(pods v1.PodList, an, tn, ns string) bool {
128-
var actualPodSeen, tempPodSeen bool
121+
func bothAPIServersRunning(pods v1.PodList) bool {
122+
var kubeAPISeen, tempAPISeen bool
129123
for _, p := range pods.Items {
130-
actualPodSeen = actualPodSeen || isPod(p, an, ns)
131-
tempPodSeen = tempPodSeen || isPod(p, tn, ns)
132-
if actualPodSeen && tempPodSeen {
124+
kubeAPISeen = kubeAPISeen || isKubeAPI(p)
125+
tempAPISeen = tempAPISeen || isTempAPI(p)
126+
if kubeAPISeen && tempAPISeen {
133127
return true
134128
}
135129
}
136130
return false
137131
}
138132

139-
func isPodRunning(pods v1.PodList, client clientset.Interface, n, ns string) bool {
133+
func kubeSystemAPIServerRunning(pods v1.PodList, client clientset.Interface) bool {
140134
for _, p := range pods.Items {
141-
if isPod(p, n, ns) {
142-
if n == kubeAPIServer {
143-
// Make sure it's actually running. Sometimes we get that
144-
// pod manifest back, but the server is not actually running.
145-
_, err := client.Discovery().ServerVersion()
146-
return err == nil
147-
}
148-
return true
135+
if isKubeAPI(p) {
136+
// Make sure it's actually running. Sometimes we get that
137+
// pod manifest back, but the server is not actually running.
138+
_, err := client.Discovery().ServerVersion()
139+
return err == nil
149140
}
150141
}
151142
return false
152143
}
153144

154-
func isPod(pod v1.Pod, n, ns string) bool {
155-
return strings.Contains(pod.Name, n) && pod.Namespace == ns
145+
func isKubeAPI(pod v1.Pod) bool {
146+
return strings.Contains(pod.Name, "kube-apiserver") && pod.Namespace == api.NamespaceSystem
147+
}
148+
149+
func isTempAPI(pod v1.Pod) bool {
150+
return strings.Contains(pod.Name, "temp-apiserver") && pod.Namespace == api.NamespaceSystem
156151
}
157152

158153
// cleanVolumes will sanitize the list of volumes and volume mounts
@@ -180,18 +175,18 @@ func cleanVolumes(p *v1.Pod) {
180175
// writeManifest will write the manifest to the ignore path.
181176
// It first writes the file to a temp file, and then atomically moves it into
182177
// the actual ignore path and correct file name.
183-
func writeManifest(manifest v1.Pod, name string) {
178+
func writeManifest(manifest v1.Pod) {
184179
m, err := json.Marshal(manifest)
185180
if err != nil {
186181
glog.Fatal(err)
187182
}
188-
writeAndAtomicCopy(m, checkpointManifest(name))
183+
writeAndAtomicCopy(m, checkpointManifest)
189184
}
190185

191-
func parseAPIPodSpec(podList v1.PodList, n, ns string) v1.PodSpec {
186+
func parseAPIPodSpec(podList v1.PodList) v1.PodSpec {
192187
var apiPod v1.Pod
193188
for _, p := range podList.Items {
194-
if isPod(p, n, ns) {
189+
if isKubeAPI(p) {
195190
apiPod = p
196191
break
197192
}
@@ -255,11 +250,3 @@ func writeAndAtomicCopy(data []byte, path string) {
255250
glog.Fatal(err)
256251
}
257252
}
258-
259-
func activeManifest(name string) string {
260-
return filepath.Join(activePath, name+".json")
261-
}
262-
263-
func checkpointManifest(name string) string {
264-
return filepath.Join(ignorePath, name+".json")
265-
}

0 commit comments

Comments
 (0)