This repository was archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathoperator-init.go
279 lines (242 loc) · 9 KB
/
operator-init.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2019 Istio 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 mesh
import (
"fmt"
"io/ioutil"
"sort"
"strings"
"time"
"github.com/spf13/cobra"
"k8s.io/utils/pointer"
"istio.io/operator/pkg/helm"
"istio.io/operator/pkg/kubectlcmd"
"istio.io/operator/pkg/manifest"
"istio.io/operator/pkg/name"
"istio.io/operator/pkg/object"
"istio.io/operator/pkg/util"
"istio.io/operator/version"
"istio.io/pkg/log"
buildversion "istio.io/pkg/version"
)
type operatorInitArgs struct {
// hub is the hub for the operator image.
hub string
// tag is the tag for the operator image.
tag string
// operatorNamespace is the namespace the operator controller is installed into.
operatorNamespace string
// istioNamespace is the namespace Istio is installed into.
istioNamespace string
// inFilename is the path to the input IstioOperator CR.
inFilename string
// kubeConfigPath is the path to kube config file.
kubeConfigPath string
// context is the cluster context in the kube config.
context string
// readinessTimeout is maximum time to wait for all Istio resources to be ready.
readinessTimeout time.Duration
// wait is flag that indicates whether to wait resources ready before exiting.
wait bool
}
const (
istioControllerComponentName = "Operator"
istioNamespaceComponentName = "IstioNamespace"
istioOperatorCRComponentName = "OperatorCustomResource"
)
// manifestApplier is used for test dependency injection.
type manifestApplier func(manifestStr, componentName string, opts *kubectlcmd.Options, verbose bool, l *Logger) bool
var (
defaultManifestApplier = applyManifest
)
func addOperatorInitFlags(cmd *cobra.Command, args *operatorInitArgs) {
hub, tag := buildversion.DockerInfo.Hub, buildversion.DockerInfo.Tag
if hub == "" {
hub = "gcr.io/istio-testing"
}
if tag == "" {
tag = "latest"
}
cmd.PersistentFlags().StringVarP(&args.inFilename, "filename", "f", "", filenameFlagHelpStr)
cmd.PersistentFlags().StringVarP(&args.kubeConfigPath, "kubeconfig", "c", "", "Path to kube config")
cmd.PersistentFlags().StringVar(&args.context, "context", "", "The name of the kubeconfig context to use")
cmd.PersistentFlags().DurationVar(&args.readinessTimeout, "readiness-timeout", 300*time.Second, "Maximum seconds to wait for all Istio resources to be ready."+
" The --wait flag must be set for this flag to apply")
cmd.PersistentFlags().BoolVarP(&args.wait, "wait", "w", false, "Wait, if set will wait until all Pods, Services, and minimum number of Pods "+
"of a Deployment are in a ready state before the command exits. It will wait for a maximum duration of --readiness-timeout seconds")
cmd.PersistentFlags().StringVar(&args.hub, "hub", hub, "The hub for the operator controller image")
cmd.PersistentFlags().StringVar(&args.tag, "tag", tag, "The tag for the operator controller image")
cmd.PersistentFlags().StringVar(&args.operatorNamespace, "operatorNamespace", "istio-operator",
"The namespace the operator controller is installed into")
cmd.PersistentFlags().StringVar(&args.istioNamespace, "istioNamespace", "istio-system",
"The namespace Istio is installed into")
}
func operatorInitCmd(rootArgs *rootArgs, oiArgs *operatorInitArgs) *cobra.Command {
return &cobra.Command{
Use: "init",
Short: "Installs the Istio operator controller in the cluster.",
Long: "The init subcommand installs the Istio operator controller in the cluster.",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
l := NewLogger(rootArgs.logToStdErr, cmd.OutOrStdout(), cmd.OutOrStderr())
operatorInit(rootArgs, oiArgs, l, defaultManifestApplier)
}}
}
// operatorInit installs the Istio operator controller into the cluster.
func operatorInit(args *rootArgs, oiArgs *operatorInitArgs, l *Logger, apply manifestApplier) {
initLogsOrExit(args)
// Error here likely indicates Deployment is missing. If some other K8s error, we will hit it again later.
already, _ := isControllerInstalled(oiArgs.kubeConfigPath, oiArgs.context, oiArgs.operatorNamespace)
if already {
l.logAndPrintf("Operator controller is already installed in %s namespace, updating.", oiArgs.operatorNamespace)
}
l.logAndPrintf("Using operator Deployment image: %s/operator:%s", oiArgs.hub, oiArgs.tag)
mstr, err := renderOperatorManifest(args, oiArgs, l)
if err != nil {
l.logAndFatal(err)
}
log.Infof("Using the following manifest to install operator:\n%s\n", mstr)
// If CR was passed, we must create a namespace for it and install CR into it.
customResource, istioNamespace, err := getCRAndNamespaceFromFile(oiArgs.inFilename, l)
if err != nil {
l.logAndFatal(err)
}
opts := &kubectlcmd.Options{
DryRun: args.dryRun,
Verbose: args.verbose,
WaitTimeout: 1 * time.Minute,
Kubeconfig: oiArgs.kubeConfigPath,
Context: oiArgs.context,
}
if err := manifest.InitK8SRestClient(opts.Kubeconfig, opts.Context); err != nil {
l.logAndFatal(err)
}
success := apply(mstr, istioControllerComponentName, opts, args.verbose, l)
if customResource != "" {
success = success && apply(genNamespaceResource(istioNamespace), istioNamespaceComponentName, opts, args.verbose, l)
success = success && apply(customResource, istioOperatorCRComponentName, opts, args.verbose, l)
}
if !success {
l.logAndPrint("\n*** Errors were logged during apply operation. Please check component installation logs above. ***\n")
return
}
l.logAndPrint("\n*** Success. ***\n")
}
func applyManifest(manifestStr, componentName string, opts *kubectlcmd.Options, verbose bool, l *Logger) bool {
l.logAndPrint("")
// Specifically don't prune operator installation since it leads to a lot of resources being reapplied.
opts.Prune = pointer.BoolPtr(false)
out, objs := manifest.ApplyManifest(name.ComponentName(componentName), manifestStr, version.OperatorBinaryVersion.String(), *opts)
success := true
if out.Err != nil {
cs := fmt.Sprintf("Component %s install returned the following errors:", componentName)
l.logAndPrintf("\n%s\n%s", cs, strings.Repeat("=", len(cs)))
l.logAndPrint("Error: ", out.Err, "\n")
success = false
} else {
l.logAndPrintf("Component %s installed successfully.", componentName)
if opts.Verbose {
l.logAndPrintf("The following objects were installed:\n%s", k8sObjectsString(objs))
}
}
if !ignoreError(out.Stderr) {
l.logAndPrint("Error detail:\n", out.Stderr, "\n")
success = false
}
if !ignoreError(out.Stderr) {
l.logAndPrint(out.Stdout, "\n")
}
return success
}
func getCRAndNamespaceFromFile(filePath string, l *Logger) (customResource string, istioNamespace string, err error) {
if filePath == "" {
return "", "", nil
}
mergedYAML, err := genProfile(false, filePath, "", "", "", true, l)
if err != nil {
return "", "", err
}
mergedIOPS, err := unmarshalAndValidateIOPS(mergedYAML, true, l)
if err != nil {
return "", "", err
}
b, err := ioutil.ReadFile(filePath)
if err != nil {
return "", "", fmt.Errorf("could not read values from file %s: %s", filePath, err)
}
customResource = string(b)
istioNamespace = mergedIOPS.MeshConfig.RootNamespace
return
}
// chartsRootDir, helmBaseDir, componentName, namespace string) (TemplateRenderer, error) {
func renderOperatorManifest(_ *rootArgs, oiArgs *operatorInitArgs, _ *Logger) (string, error) {
r, err := helm.NewHelmRenderer("", "../operator", istioControllerComponentName, oiArgs.operatorNamespace)
if err != nil {
return "", err
}
if err := r.Run(); err != nil {
return "", err
}
tmpl := `
operatorNamespace: {{.OperatorNamespace}}
istioNamespace: {{.IstioNamespace}}
hub: {{.Hub}}
tag: {{.Tag}}
`
tv := struct {
OperatorNamespace string
IstioNamespace string
Hub string
Tag string
}{
OperatorNamespace: oiArgs.operatorNamespace,
IstioNamespace: oiArgs.istioNamespace,
Hub: oiArgs.hub,
Tag: oiArgs.tag,
}
vals, err := util.RenderTemplate(tmpl, tv)
if err != nil {
return "", err
}
log.Infof("Installing operator charts with the following values:\n%s", vals)
return r.RenderManifest(vals)
}
func genNamespaceResource(namespace string) string {
tmpl := `
apiVersion: v1
kind: Namespace
metadata:
labels:
istio-injection: disabled
name: {{.Namespace}}
`
tv := struct {
Namespace string
}{
Namespace: namespace,
}
vals, err := util.RenderTemplate(tmpl, tv)
if err != nil {
return ""
}
return vals
}
func k8sObjectsString(objs object.K8sObjects) string {
var out []string
for _, o := range objs {
out = append(out, fmt.Sprintf("- %s/%s/%s", o.Kind, o.Namespace, o.Name))
}
sort.Strings(out)
return strings.Join(out, "\n")
}