-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (157 loc) · 4.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2018 The Kubeflow Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"math"
"os"
"time"
"github.com/kubeflow/arena/pkg/util"
yaml "gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
std_errors "errors"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
)
var (
namespace string
jobName string
statefulSetName string
ErrNoNeedUpgrade = std_errors.New("No need upgrade. It's already the latest version.")
ErrStillNeedWait = std_errors.New("Need waited.")
)
// Receive Namespace, Job Name, Statefulset name
func main() {
// 1. Get the job, statefulset and namespace
err := updateInputFromEnv()
if err != nil {
panic(err.Error())
}
log.Infof("jobName: %s, namespace: %s, statefulset %s", jobName, namespace, statefulSetName)
// 2. creates the in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
// 3. creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// 4. Wait the job complete, only when job completes, clean up the statefulset
err = WaitJobComplete(clientset, namespace, jobName, math.MaxInt64, 5*time.Second)
if err != nil {
panic(err.Error())
}
// 5. Delete the statefulset
err = DeleteStatefulSet(clientset, namespace, statefulSetName)
if err != nil {
panic(err.Error())
}
}
// get the job to monitor from environment
func updateInputFromEnv() error {
namespace = os.Getenv("NAMESPACE")
if len(namespace) == 0 {
return fmt.Errorf("Failed to get namespace from env NAMESPACE")
}
jobName = os.Getenv("JOBNAME")
if len(jobName) == 0 {
return fmt.Errorf("Failed to get jobName from env JOBNAME")
}
statefulSetName = os.Getenv("STATEFULSETNAME")
if len(statefulSetName) == 0 {
return fmt.Errorf("Failed to get statefulsetName from env STATEFULSETNAME")
}
return nil
}
func WaitJobComplete(client *kubernetes.Clientset, namespace string, jobName string, duration time.Duration, tick time.Duration) error {
log.Infof("Duration %v, tick %v", duration, tick)
return util.RetryDuring(duration, tick, func() (err error) {
job, err := client.BatchV1().Jobs(namespace).Get(jobName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
log.Infof("Job %s doesn't exist, need to wait.", jobName)
return ErrStillNeedWait
}
}
output, err := yaml.Marshal(job)
if err != nil {
log.Warnf("Failed to parse the job %s due to %v", job.Name, err)
}
log.Infof("Check the job status %s", string(output))
// wait the job to start
startTime := job.Status.StartTime
if startTime.IsZero() {
return ErrStillNeedWait
}
succeed := false
if job.Status.Succeeded > 0 {
succeed = true
}
if !succeed {
log.Warnf("Failed due to %v", job.Status.Conditions)
return ErrStillNeedWait
} else {
return nil
}
})
}
func DeleteStatefulSet(client *kubernetes.Clientset, namespace string, stsName string) error {
sts, err := client.AppsV1beta1().StatefulSets(namespace).Get(stsName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
log.Infof("The statefulset %s in namespace %s is not found, it has been deleted.", stsName, namespace)
return nil
} else {
return err
}
}
// Delete statefulset when the job is completed.
deletePolicy := metav1.DeletePropagationForeground
svcName := sts.Spec.ServiceName
err = client.CoreV1().Services(namespace).Delete(svcName, &metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
})
if err != nil {
if errors.IsNotFound(err) {
log.Infof("The svcName %s in namespace %s has been deleted.", svcName, namespace)
return nil
} else {
return err
}
}
err = client.AppsV1beta1().StatefulSets(namespace).Delete(stsName, &metav1.DeleteOptions{
PropagationPolicy: &deletePolicy,
})
if err != nil {
return err
}
return util.RetryDuring(10*time.Minute, 10*time.Second, func() (err error) {
sts, err = client.AppsV1beta1().StatefulSets(namespace).Get(stsName, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
log.Infof("The statefulset %s in namespace %s has been deleted.", stsName, namespace)
return nil
} else {
log.Infof("Unexpected the err %v", err)
return err
}
}
log.Errorf("still can get statefulset: %s/%s, [%+v]\n", namespace, stsName, sts)
return ErrStillNeedWait
})
}