-
Notifications
You must be signed in to change notification settings - Fork 90
/
api.go
193 lines (155 loc) · 5.77 KB
/
api.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
189
190
191
192
193
package kotsadm
import (
"bytes"
"time"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
)
var timeoutWaitingForAPI = time.Duration(time.Minute * 2)
func getApiYAML(namespace string) (map[string][]byte, error) {
docs := map[string][]byte{}
s := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var role bytes.Buffer
if err := s.Encode(apiRole(namespace), &role); err != nil {
return nil, errors.Wrap(err, "failed to marshal api role")
}
docs["api-role.yaml"] = role.Bytes()
var roleBinding bytes.Buffer
if err := s.Encode(apiRoleBinding(namespace), &roleBinding); err != nil {
return nil, errors.Wrap(err, "failed to marshal api role binding")
}
docs["api-rolebinding.yaml"] = roleBinding.Bytes()
var serviceAccount bytes.Buffer
if err := s.Encode(apiServiceAccount(namespace), &serviceAccount); err != nil {
return nil, errors.Wrap(err, "failed to marshal api service account")
}
docs["api-serviceaccount.yaml"] = serviceAccount.Bytes()
var deployment bytes.Buffer
if err := s.Encode(apiDeployment(namespace), &deployment); err != nil {
return nil, errors.Wrap(err, "failed to marshal api deployment")
}
docs["api-deployment.yaml"] = deployment.Bytes()
var service bytes.Buffer
if err := s.Encode(apiService(namespace), &service); err != nil {
return nil, errors.Wrap(err, "failed to marshal api service")
}
docs["api-service.yaml"] = service.Bytes()
return docs, nil
}
func waitForAPI(deployOptions *DeployOptions, clientset *kubernetes.Clientset) error {
start := time.Now()
for {
pods, err := clientset.CoreV1().Pods(deployOptions.Namespace).List(metav1.ListOptions{LabelSelector: "app=kotsadm-api"})
if err != nil {
return errors.Wrap(err, "failed to list pods")
}
for _, pod := range pods.Items {
if pod.Status.Phase == corev1.PodRunning {
if pod.Status.ContainerStatuses[0].Ready == true {
return nil
}
}
}
time.Sleep(time.Second)
if time.Now().Sub(start) > timeoutWaitingForAPI {
return errors.New("timeout waiting for api pod")
}
}
}
func ensureAPI(deployOptions *DeployOptions, clientset *kubernetes.Clientset) error {
if err := ensureApiRBAC(deployOptions.Namespace, clientset); err != nil {
return errors.Wrap(err, "failed to ensure api rbac")
}
if err := ensureApplicationMetadata(*deployOptions, clientset); err != nil {
return errors.Wrap(err, "failed to ensure custom branding")
}
if err := ensureAPIDeployment(*deployOptions, clientset); err != nil {
return errors.Wrap(err, "failed to ensure api deployment")
}
if err := ensureAPIService(deployOptions.Namespace, clientset); err != nil {
return errors.Wrap(err, "failed to ensure api service")
}
return nil
}
func ensureApiRBAC(namespace string, clientset *kubernetes.Clientset) error {
if err := ensureApiRole(namespace, clientset); err != nil {
return errors.Wrap(err, "failed to ensure api role")
}
if err := ensureApiRoleBinding(namespace, clientset); err != nil {
return errors.Wrap(err, "failed to ensure api role binding")
}
if err := ensureApiServiceAccount(namespace, clientset); err != nil {
return errors.Wrap(err, "failed to ensure api service account")
}
return nil
}
func ensureApiRole(namespace string, clientset *kubernetes.Clientset) error {
_, err := clientset.RbacV1().Roles(namespace).Get("kotsadm-api-role", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get role")
}
_, err := clientset.RbacV1().Roles(namespace).Create(apiRole(namespace))
if err != nil {
return errors.Wrap(err, "failed to create role")
}
}
return nil
}
func ensureApiRoleBinding(namespace string, clientset *kubernetes.Clientset) error {
_, err := clientset.RbacV1().RoleBindings(namespace).Get("kotsadm-api-rolebinding", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get rolebinding")
}
_, err := clientset.RbacV1().RoleBindings(namespace).Create(apiRoleBinding(namespace))
if err != nil {
return errors.Wrap(err, "failed to create rolebinding")
}
}
return nil
}
func ensureApiServiceAccount(namespace string, clientset *kubernetes.Clientset) error {
_, err := clientset.CoreV1().ServiceAccounts(namespace).Get("kotsadm-api", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get serviceaccouont")
}
_, err := clientset.CoreV1().ServiceAccounts(namespace).Create(apiServiceAccount(namespace))
if err != nil {
return errors.Wrap(err, "failed to create serviceaccount")
}
}
return nil
}
func ensureAPIDeployment(deployOptions DeployOptions, clientset *kubernetes.Clientset) error {
_, err := clientset.AppsV1().Deployments(deployOptions.Namespace).Get("kotsadm-api", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing deployment")
}
_, err := clientset.AppsV1().Deployments(deployOptions.Namespace).Create(apiDeployment(deployOptions.Namespace))
if err != nil {
return errors.Wrap(err, "failed to create deployment")
}
}
return nil
}
func ensureAPIService(namespace string, clientset *kubernetes.Clientset) error {
_, err := clientset.CoreV1().Services(namespace).Get("kotsadm-api", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing service")
}
_, err := clientset.CoreV1().Services(namespace).Create(apiService(namespace))
if err != nil {
return errors.Wrap(err, "Failed to create service")
}
}
return nil
}