-
Notifications
You must be signed in to change notification settings - Fork 90
/
web.go
248 lines (202 loc) · 5.95 KB
/
web.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package kotsadm
import (
"bytes"
"os"
"strconv"
"strings"
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
)
var (
executableMode = int32(511) // hex 777
)
func getWebYAML(deployOptions DeployOptions) (map[string][]byte, error) {
docs := map[string][]byte{}
s := json.NewYAMLSerializer(json.DefaultMetaFactory, scheme.Scheme, scheme.Scheme)
var config bytes.Buffer
if err := s.Encode(webConfig(deployOptions), &config); err != nil {
return nil, errors.Wrap(err, "failed to marshal web config")
}
docs["web-config.yaml"] = config.Bytes()
var deployment bytes.Buffer
if err := s.Encode(webDeployment(deployOptions.Namespace), &deployment); err != nil {
return nil, errors.Wrap(err, "failed to marsha web deployment")
}
docs["web-deployment.yaml"] = deployment.Bytes()
var service bytes.Buffer
if err := s.Encode(webService(deployOptions), &service); err != nil {
return nil, errors.Wrap(err, "failed to marshal web service")
}
docs["web-service.yaml"] = service.Bytes()
return docs, nil
}
func ensureWeb(deployOptions *DeployOptions, clientset *kubernetes.Clientset) error {
if deployOptions.Hostname == "" {
hostname, err := promptForHostname()
if err != nil {
return errors.Wrap(err, "failed to prompt for hostname")
}
deployOptions.Hostname = hostname
}
if deployOptions.ServiceType == "" {
serviceType, err := promptForWebServiceType(deployOptions)
if err != nil {
return errors.Wrap(err, "failed to prompt for service type")
}
deployOptions.ServiceType = serviceType
}
if err := ensureWebConfig(deployOptions, clientset); err != nil {
return errors.Wrap(err, "failed to ensure web configmap")
}
if err := ensureWebDeployment(deployOptions.Namespace, clientset); err != nil {
return errors.Wrap(err, "failed to ensure web deployment")
}
if err := ensureWebService(deployOptions, clientset); err != nil {
return errors.Wrap(err, "failed to ensure web service")
}
return nil
}
func ensureWebConfig(deployOptions *DeployOptions, clientset *kubernetes.Clientset) error {
_, err := clientset.CoreV1().ConfigMaps(deployOptions.Namespace).Get("kotsadm-web-scripts", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing config map")
}
_, err := clientset.CoreV1().ConfigMaps(deployOptions.Namespace).Create(webConfig(*deployOptions))
if err != nil {
return errors.Wrap(err, "failed to create configmap")
}
}
return nil
}
func ensureWebDeployment(namespace string, clientset *kubernetes.Clientset) error {
_, err := clientset.AppsV1().Deployments(namespace).Get("kotsadm-web", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing deployment")
}
_, err := clientset.AppsV1().Deployments(namespace).Create(webDeployment(namespace))
if err != nil {
return errors.Wrap(err, "failed to create deployment")
}
}
return nil
}
func ensureWebService(deployOptions *DeployOptions, clientset *kubernetes.Clientset) error {
_, err := clientset.CoreV1().Services(deployOptions.Namespace).Get("kotsadm-web", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing service")
}
_, err := clientset.CoreV1().Services(deployOptions.Namespace).Create(webService(*deployOptions))
if err != nil {
return errors.Wrap(err, "Failed to create service")
}
}
return nil
}
func promptForWebServiceType(deployOptions *DeployOptions) (string, error) {
prompt := promptui.Select{
Label: "Web/UI Service Type:",
Items: []string{"ClusterIP", "NodePort", "LoadBalancer"},
}
for {
_, result, err := prompt.Run()
if err != nil {
if err == promptui.ErrInterrupt {
os.Exit(-1)
}
continue
}
if result == "NodePort" {
nodePort, err := promptForWebNodePort()
if err != nil {
return "", errors.Wrap(err, "failed to prompt for node port")
}
deployOptions.NodePort = int32(nodePort)
}
return result, nil
}
}
func promptForWebNodePort() (int, error) {
templates := &promptui.PromptTemplates{
Prompt: "{{ . | bold }} ",
Valid: "{{ . | green }} ",
Invalid: "{{ . | red }} ",
Success: "{{ . | bold }} ",
}
prompt := promptui.Prompt{
Label: "Node Port:",
Templates: templates,
Default: "30000",
Validate: func(input string) error {
_, err := strconv.Atoi(input)
return err
},
}
for {
result, err := prompt.Run()
if err != nil {
if err == promptui.ErrInterrupt {
os.Exit(-1)
}
continue
}
nodePort, err := strconv.Atoi(result)
if err != nil {
return 0, errors.Wrap(err, "failed to convert nodeport")
}
return nodePort, nil
}
}
func promptForHostname() (string, error) {
templates := &promptui.PromptTemplates{
Prompt: "{{ . | bold }} ",
Valid: "{{ . | green }} ",
Invalid: "{{ . | red }} ",
Success: "{{ . | bold }} ",
}
prompt := promptui.Prompt{
Label: "Hostname for the Admin Console:",
Templates: templates,
Default: "localhost:8800",
Validate: func(input string) error {
if !strings.Contains(input, ":") {
errs := validation.IsDNS1123Subdomain(input)
if len(errs) > 0 {
return errors.New(errs[0])
}
return nil
}
split := strings.Split(input, ":")
if len(split) != 2 {
return errors.New("only hostname or hostname:port are allowed formats")
}
errs := validation.IsDNS1123Subdomain(split[0])
if len(errs) > 0 {
return errors.New(errs[0])
}
_, err := strconv.Atoi(split[1])
if err != nil {
return err
}
return nil
},
}
for {
result, err := prompt.Run()
if err != nil {
if err == promptui.ErrInterrupt {
os.Exit(-1)
}
continue
}
return result, nil
}
}