/
setup_nuvolaris.go
154 lines (131 loc) · 3.71 KB
/
setup_nuvolaris.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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"
"os"
"time"
)
type SetupPipeline struct {
kubeClient *KubeClient
k8sContext string
apiHost string
operatorDockerImage string
err error
logger *Logger
}
type setupStep func(sp *SetupPipeline)
func (sp *SetupPipeline) step(f setupStep) {
if sp.err != nil {
return
}
f(sp)
time.Sleep(2 * time.Second)
}
func setupNuvolaris(logger *Logger, cmd *SetupCmd) error {
setupWithNoFlags := !cmd.Devcluster &&
!cmd.Configure &&
cmd.Uninstall == "" &&
cmd.Context == ""
if setupWithNoFlags {
err := listAvailableContexts()
if err != nil {
return err
}
return nil
}
if cmd.Configure {
err := checkApiHost(cmd)
if err != nil {
return err
}
return configureCrd(cmd.Apihost)
}
sp := SetupPipeline{
operatorDockerImage: cmd.OperatorImage + ":" + cmd.OperatorTag,
logger: logger,
}
if cmd.Uninstall != "" {
sp.k8sContext = cmd.Uninstall
sp.kubeClient, sp.err = initClients(sp.k8sContext)
sp.step(resetNuvolaris)
return sp.err
}
if cmd.Devcluster {
sp.err = startDevCluster(sp.logger)
sp.k8sContext = "kind-nuvolaris"
} else if cmd.Context != "" {
err := checkApiHost(cmd)
if err != nil {
return err
}
sp.k8sContext = cmd.Context
sp.apiHost = cmd.Apihost
} else {
fmt.Println("Specify Kubernetes context with --context flag")
return nil
}
sp.kubeClient, sp.err = initClients(sp.k8sContext)
sp.step(createNuvolarisNamespace)
sp.step(deployServiceAccount)
sp.step(deployClusterRoleBinding)
sp.step(runNuvolarisOperatorPod)
sp.step(waitForCrdDefinitionReady)
sp.step(deployOperatorObject)
sp.step(waitForOpenWhiskReady)
sp.step(manifestDeploy)
return sp.err
}
func checkApiHost(cmd *SetupCmd) error {
if cmd.Apihost == "" {
return fmt.Errorf("please specify the public IP of your Kubernetes cluster - if your Kubernetes has a load balancer, specify --apihost=auto")
}
return nil
}
func createNuvolarisNamespace(sp *SetupPipeline) {
sp.err = sp.kubeClient.createNuvolarisNamespace()
}
func deployServiceAccount(sp *SetupPipeline) {
sp.err = sp.kubeClient.createServiceAccount()
}
func deployClusterRoleBinding(sp *SetupPipeline) {
sp.err = sp.kubeClient.createClusterRoleBinding()
}
func runNuvolarisOperatorPod(sp *SetupPipeline) {
sp.err = sp.kubeClient.createOperatorPod(sp.operatorDockerImage)
}
func waitForCrdDefinitionReady(sp *SetupPipeline) {
sp.err = crdProbe(sp.kubeClient)
}
func deployOperatorObject(sp *SetupPipeline) {
sp.err = createWhiskOperatorObject(sp.kubeClient, sp.apiHost)
}
func waitForOpenWhiskReady(sp *SetupPipeline) {
sp.err = readinessProbe(sp.kubeClient)
}
func manifestDeploy(sp *SetupPipeline) {
if _, err := os.Stat("./manifest.yaml"); err == nil {
fmt.Println("Deploying manifest.yaml")
sp.err = Wsk([]string{"wsk", "project", "deploy"})
} else {
sp.err = nil
}
}
func resetNuvolaris(sp *SetupPipeline) {
sp.err = sp.kubeClient.cleanup()
}