/
installer.go
185 lines (161 loc) · 5.45 KB
/
installer.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
// Copyright 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 istio
import (
"bytes"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strconv"
"sync"
"k8s.io/client-go/rest"
"istio.io/istio/operator/cmd/mesh"
"istio.io/istio/operator/pkg/util/clog"
"istio.io/istio/pkg/kube"
"istio.io/istio/pkg/log"
testenv "istio.io/istio/pkg/test/env"
"istio.io/istio/pkg/test/framework/components/cluster"
"istio.io/istio/pkg/test/framework/resource"
"istio.io/istio/pkg/test/scopes"
)
var _ resource.Dumper = &installer{}
type installArgs struct {
ComponentName string
Revision string
Files []string
Set []string
}
func (args *installArgs) AppendSet(key, value string) {
args.Set = append(args.Set, fmt.Sprintf("%s=%s", key, value))
}
type installer struct {
ctx resource.Context
workDir string
manifests map[string][]string
mu sync.Mutex
}
func newInstaller(ctx resource.Context, workDir string) *installer {
return &installer{
ctx: ctx,
workDir: workDir,
manifests: make(map[string][]string),
}
}
func (i *installer) Install(c cluster.Cluster, args installArgs) error {
kubeConfigFile, err := kubeConfigFileForCluster(c)
if err != nil {
return err
}
iArgs := &mesh.InstallArgs{
InFilenames: args.Files,
Set: args.Set,
ManifestsPath: filepath.Join(testenv.IstioSrc, "manifests"),
Revision: args.Revision,
}
if i.ctx.Settings().Ambient {
iArgs.InFilenames = append(iArgs.InFilenames, filepath.Join(testenv.IstioSrc, IntegrationTestAmbientDefaultsIOP))
}
if i.ctx.Settings().PeerMetadataDiscovery && len(args.ComponentName) == 0 {
iArgs.InFilenames = append(iArgs.InFilenames, filepath.Join(testenv.IstioSrc, IntegrationTestPeerMetadataDiscoveryDefaultsIOP))
}
rc, err := kube.DefaultRestConfig(kubeConfigFile, "", func(config *rest.Config) {
config.QPS = 50
config.Burst = 100
})
if err != nil {
return err
}
kubeClient, err := kube.NewCLIClient(kube.NewClientConfigForRestConfig(rc), "")
if err != nil {
return fmt.Errorf("create Kubernetes client: %v", err)
}
// Generate the manifest YAML, so that we can uninstall it in Close.
var stdOut, stdErr bytes.Buffer
if err := mesh.ManifestGenerate(kubeClient, &mesh.RootArgs{}, &mesh.ManifestGenerateArgs{
InFilenames: iArgs.InFilenames,
Set: iArgs.Set,
Force: iArgs.Force,
ManifestsPath: iArgs.ManifestsPath,
Revision: iArgs.Revision,
}, cmdLogOptions(), cmdLogger(&stdOut, &stdErr)); err != nil {
return err
}
yaml := stdOut.String()
// Store the generated manifest.
i.mu.Lock()
i.manifests[c.Name()] = append(i.manifests[c.Name()], yaml)
i.mu.Unlock()
// Actually run the install command
iArgs.SkipConfirmation = true
componentName := args.ComponentName
if len(componentName) == 0 {
componentName = "Istio components"
}
scopes.Framework.Infof("Installing %s on cluster %s: %s", componentName, c.Name(), iArgs)
stdOut.Reset()
stdErr.Reset()
if err := mesh.Install(kubeClient, &mesh.RootArgs{}, iArgs, cmdLogOptions(), &stdOut,
cmdLogger(&stdOut, &stdErr),
mesh.NewPrinterForWriter(&stdOut)); err != nil {
return fmt.Errorf("failed installing %s on cluster %s: %v. Details: %s", componentName, c.Name(), err, &stdErr)
}
return nil
}
func (i *installer) Close(c cluster.Cluster) error {
i.mu.Lock()
manifests := i.manifests[c.Name()]
delete(i.manifests, c.Name())
i.mu.Unlock()
if len(manifests) > 0 {
return i.ctx.ConfigKube(c).YAML("", removeCRDsSlice(manifests)).Delete()
}
return nil
}
func (i *installer) Dump(resource.Context) {
manifestsDir := path.Join(i.workDir, "manifests")
if err := os.Mkdir(manifestsDir, 0o700); err != nil {
scopes.Framework.Errorf("Unable to create directory for dumping install manifests: %v", err)
}
for clusterName, manifests := range i.manifests {
clusterDir := path.Join(manifestsDir, clusterName)
if err := os.Mkdir(manifestsDir, 0o700); err != nil {
scopes.Framework.Errorf("Unable to create directory for dumping %s install manifests: %v", clusterName, err)
}
for i, manifest := range manifests {
err := os.WriteFile(path.Join(clusterDir, "manifest-"+strconv.Itoa(i)+".yaml"), []byte(manifest), 0o644)
if err != nil {
scopes.Framework.Errorf("Failed writing manifest %d/%d in %s: %v", i, len(manifests)-1, clusterName, err)
}
}
}
}
func cmdLogOptions() *log.Options {
o := log.DefaultOptions()
// These scopes are, at the default "INFO" level, too chatty for command line use
o.SetOutputLevel("validation", log.ErrorLevel)
o.SetOutputLevel("processing", log.ErrorLevel)
o.SetOutputLevel("analysis", log.WarnLevel)
o.SetOutputLevel("installer", log.WarnLevel)
o.SetOutputLevel("translator", log.WarnLevel)
o.SetOutputLevel("adsc", log.WarnLevel)
o.SetOutputLevel("default", log.WarnLevel)
o.SetOutputLevel("klog", log.WarnLevel)
o.SetOutputLevel("kube", log.ErrorLevel)
return o
}
func cmdLogger(stdOut, stdErr io.Writer) clog.Logger {
return clog.NewConsoleLogger(stdOut, stdErr, scopes.Framework)
}