forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest_shared_test.go
333 lines (292 loc) · 10.1 KB
/
manifest_shared_test.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// 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 mesh
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/gogo/protobuf/proto"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"istio.io/istio/operator/pkg/apis/istio/v1alpha1"
"istio.io/istio/operator/pkg/cache"
"istio.io/istio/operator/pkg/controller/istiocontrolplane"
"istio.io/istio/operator/pkg/helmreconciler"
"istio.io/istio/operator/pkg/manifest"
"istio.io/istio/operator/pkg/name"
"istio.io/istio/operator/pkg/object"
"istio.io/istio/operator/pkg/util"
"istio.io/istio/operator/pkg/util/clog"
"istio.io/pkg/log"
)
// cmdType is one of the commands used to generate and optionally apply a manifest.
type cmdType string
const (
// istioctl manifest generate
cmdGenerate cmdType = "istioctl manifest generate"
// istioctl install
cmdApply cmdType = "istioctl install"
// in-cluster controller
cmdController cmdType = "operator controller"
)
// Golden output files add a lot of noise to pull requests. Use a unique suffix so
// we can hide them by default. This should match one of the `linuguist-generated=true`
// lines in istio.io/istio/.gitattributes.
const (
goldenFileSuffixHideChangesInReview = ".golden.yaml"
goldenFileSuffixShowChangesInReview = ".golden-show-in-gh-pull-request.yaml"
)
var (
// By default, tests only run with manifest generate, since it doesn't require any external fake test environment.
testedManifestCmds = []cmdType{cmdGenerate}
// Only used if kubebuilder is installed.
testenv *envtest.Environment
testClient client.Client
testReconcileOperator *istiocontrolplane.ReconcileIstioOperator
allNamespacedGVKs = append(helmreconciler.NamespacedResources,
schema.GroupVersionKind{Group: "", Version: "v1", Kind: "Endpoints"})
// CRDs are not in the prune list, but must be considered for tests.
allClusterGVKs = append(helmreconciler.ClusterResources,
schema.GroupVersionKind{Group: "apiextensions.k8s.io", Version: "v1beta1", Kind: "CustomResourceDefinition"})
)
func init() {
if kubeBuilderInstalled() {
// TestMode is required to not wait in the go client for resources that will never be created in the test server.
helmreconciler.TestMode = true
// Add install and controller to the list of commands to run tests against.
testedManifestCmds = append(testedManifestCmds, cmdApply, cmdController)
}
}
// recreateTestEnv (re)creates a kubebuilder fake API server environment. This is required for testing of the
// controller runtime, which is used in the operator.
func recreateTestEnv() error {
// If kubebuilder is installed, use that test env for apply and controller testing.
log.Infof("Recreating kubebuilder test environment\n")
if testenv != nil {
testenv.Stop()
}
var err error
testenv = &envtest.Environment{}
testRestConfig, err = testenv.Start()
if err != nil {
return err
}
testK8Interface, err = kubernetes.NewForConfig(testRestConfig)
testRestConfig.QPS = 50
testRestConfig.Burst = 100
if err != nil {
return err
}
s := scheme.Scheme
s.AddKnownTypes(v1alpha1.SchemeGroupVersion, &v1alpha1.IstioOperator{})
testClient, err = client.New(testRestConfig, client.Options{Scheme: s})
if err != nil {
return err
}
testReconcileOperator = istiocontrolplane.NewReconcileIstioOperator(testClient, testRestConfig, s)
return nil
}
// runManifestCommands runs all testedManifestCmds commands with the given input IOP file, flags and chartSource.
// It returns an ObjectSet for each cmd type.
// nolint: unparam
func runManifestCommands(inFile, flags string, chartSource chartSourceType) (map[cmdType]*ObjectSet, error) {
out := make(map[cmdType]*ObjectSet)
for _, cmd := range testedManifestCmds {
log.Infof("\nRunning test command using %s\n", cmd)
switch cmd {
case cmdApply, cmdController:
if err := cleanTestCluster(); err != nil {
return nil, err
}
if err := fakeApplyExtraResources(inFile); err != nil {
return nil, err
}
default:
}
var objs *ObjectSet
var err error
switch cmd {
case cmdGenerate:
m, _, err := generateManifest(inFile, flags, chartSource)
if err != nil {
return nil, err
}
objs, err = parseObjectSetFromManifest(m)
if err != nil {
return nil, err
}
case cmdApply:
objs, err = fakeApplyManifest(inFile, flags, chartSource)
case cmdController:
objs, err = fakeControllerReconcile(inFile, chartSource)
default:
}
if err != nil {
return nil, err
}
out[cmd] = objs
}
return out, nil
}
// fakeApplyManifest runs istioctl install.
func fakeApplyManifest(inFile, flags string, chartSource chartSourceType) (*ObjectSet, error) {
inPath := filepath.Join(testDataDir, "input", inFile+".yaml")
manifest, err := runManifestCommand("install", []string{inPath}, flags, chartSource)
if err != nil {
return nil, fmt.Errorf("error %s: %s", err, manifest)
}
return NewObjectSet(getAllIstioObjects()), nil
}
// fakeApplyExtraResources applies any extra resources for the given test name.
func fakeApplyExtraResources(inFile string) error {
reconciler, err := helmreconciler.NewHelmReconciler(testClient, testRestConfig, nil, nil)
if err != nil {
return err
}
if rs, err := readFile(filepath.Join(testDataDir, "input-extra-resources", inFile+".yaml")); err == nil {
if err := applyWithReconciler(reconciler, rs); err != nil {
return err
}
}
return nil
}
func fakeControllerReconcile(inFile string, chartSource chartSourceType) (*ObjectSet, error) {
l := clog.NewDefaultLogger()
_, iop, err := manifest.GenerateConfig(
[]string{inFileAbsolutePath(inFile)},
[]string{"installPackagePath=" + string(chartSource)},
false, testRestConfig, l)
if err != nil {
return nil, err
}
iop.Spec.InstallPackagePath = string(chartSource)
reconciler, err := helmreconciler.NewHelmReconciler(testClient, testRestConfig, iop, nil)
if err != nil {
return nil, err
}
if err := fakeInstallOperator(reconciler, chartSource, iop); err != nil {
return nil, err
}
if _, err := reconciler.Reconcile(); err != nil {
return nil, err
}
return NewObjectSet(getAllIstioObjects()), nil
}
// fakeInstallOperator installs the operator manifest resources into a cluster using the given reconciler.
// The installation is for testing with a kubebuilder fake cluster only, since no functional Deployment will be
// created.
func fakeInstallOperator(reconciler *helmreconciler.HelmReconciler, chartSource chartSourceType, iop proto.Message) error {
ocArgs := &operatorCommonArgs{
manifestsPath: string(chartSource),
istioNamespace: istioDefaultNamespace,
watchedNamespaces: istioDefaultNamespace,
operatorNamespace: operatorDefaultNamespace,
// placeholders, since the fake API server does not actually pull images and create pods.
hub: "fake hub",
tag: "fake tag",
}
_, mstr, err := renderOperatorManifest(nil, ocArgs)
if err != nil {
return err
}
if err := applyWithReconciler(reconciler, mstr); err != nil {
return err
}
iopStr, err := util.MarshalWithJSONPB(iop)
if err != nil {
return err
}
if err := saveIOPToCluster(reconciler, iopStr); err != nil {
return err
}
return err
}
// applyWithReconciler applies the given manifest string using the given reconciler.
func applyWithReconciler(reconciler *helmreconciler.HelmReconciler, manifest string) error {
m := name.Manifest{
// Name is not important here, only Content will be applied.
Name: name.IstioOperatorComponentName,
Content: manifest,
}
_, _, err := reconciler.ApplyManifest(m, false)
return err
}
// runManifestCommand runs the given manifest command. If filenames is set, passes the given filenames as -f flag,
// flags is passed to the command verbatim. If you set both flags and path, make sure to not use -f in flags.
func runManifestCommand(command string, filenames []string, flags string, chartSource chartSourceType) (string, error) {
var args string
if command == "install" {
args = "install"
} else {
args = "manifest " + command
}
for _, f := range filenames {
args += " -f " + f
}
if flags != "" {
args += " " + flags
}
args += " --set installPackagePath=" + string(chartSource)
return runCommand(args)
}
// runCommand runs the given command string.
func runCommand(command string) (string, error) {
var out bytes.Buffer
rootCmd := GetRootCmd(strings.Split(command, " "))
rootCmd.SetOut(&out)
err := rootCmd.Execute()
return out.String(), err
}
// cleanTestCluster resets the test cluster.
func cleanTestCluster() error {
// Needed in case we are running a test through this path that doesn't start a new process.
cache.FlushObjectCaches()
if !kubeBuilderInstalled() {
return nil
}
return recreateTestEnv()
}
// getAllIstioObjects lists all Istio GVK resources from the testClient.
func getAllIstioObjects() object.K8sObjects {
var out object.K8sObjects
for _, gvk := range append(allClusterGVKs, allNamespacedGVKs...) {
objects := &unstructured.UnstructuredList{}
objects.SetGroupVersionKind(gvk)
if err := testClient.List(context.TODO(), objects); err != nil {
log.Error(err.Error())
continue
}
for _, o := range objects.Items {
no := o.DeepCopy()
out = append(out, object.NewK8sObject(no, nil, nil))
}
}
return out
}
// readFile reads a file and returns the contents.
func readFile(path string) (string, error) {
b, err := ioutil.ReadFile(path)
return string(b), err
}
// inFileAbsolutePath returns the absolute path for an input file like "gateways".
func inFileAbsolutePath(inFile string) string {
return filepath.Join(testDataDir, "input", inFile+".yaml")
}