-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubernetes.go
111 lines (105 loc) · 3.1 KB
/
kubernetes.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
package web
import (
"fmt"
"k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
// Needed to add support for GCP authentication
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd/api"
"k8s.io/client-go/util/retry"
)
// UpdateDeployment updates a deployment object by adding a new docker image value
// and triggering a rolling deployment in the process.
func UpdateDeployment(namespace, name, container, docker string, info ClusterInfo) error {
client, err := kubernetes.NewForConfig(&rest.Config{
Host: "https://" + info.Endpoint,
Username: info.Username,
Password: info.Password,
AuthProvider: &api.AuthProviderConfig{
Name: "gcp",
},
TLSClientConfig: rest.TLSClientConfig{
Insecure: false,
CertData: info.CertData,
CAData: info.CAData,
KeyData: info.KeyData,
},
})
if err != nil {
return err
}
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
// Retrieve the latest version of Deployment before attempting update
// RetryOnConflict uses exponential backoff to avoid exhausting the apiserver
res, err := client.AppsV1().Deployments(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return err
}
found := false
for i, c := range res.Spec.Template.Spec.Containers {
if c.Name == container {
res.Spec.Template.Spec.Containers[i].Image = docker
found = true
}
}
if !found {
return fmt.Errorf("container spec for %v not found in %v deployment", container, name)
}
_, err = client.AppsV1().Deployments(namespace).Update(res)
return err
})
return err
}
// UnavailableReplicas returns the number of unavailable deployment instances for a given deployment.
func UnavailableReplicas(namespace, name string, info ClusterInfo) (int32, error) {
client, err := kubernetes.NewForConfig(&rest.Config{
Host: "https://" + info.Endpoint,
Username: info.Username,
Password: info.Password,
AuthProvider: &api.AuthProviderConfig{
Name: "gcp",
},
TLSClientConfig: rest.TLSClientConfig{
Insecure: false,
CertData: info.CertData,
CAData: info.CAData,
KeyData: info.KeyData,
},
})
if err != nil {
return -1, err
}
res, err := client.AppsV1().Deployments(namespace).Get(name, metav1.GetOptions{})
if err != nil {
return -1, err
}
return res.Status.UnavailableReplicas, nil
}
// RollbackDeployment reverts the deployment back to its previous state.
func RollbackDeployment(namespace, name string, info ClusterInfo) error {
client, err := kubernetes.NewForConfig(&rest.Config{
Host: "https://" + info.Endpoint,
Username: info.Username,
Password: info.Password,
AuthProvider: &api.AuthProviderConfig{
Name: "gcp",
},
TLSClientConfig: rest.TLSClientConfig{
Insecure: false,
CertData: info.CertData,
CAData: info.CAData,
KeyData: info.KeyData,
},
})
if err != nil {
return err
}
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
return client.ExtensionsV1beta1().Deployments(namespace).Rollback(&v1beta1.DeploymentRollback{
Name: name,
})
})
return err
}