istio
163 строки · 5.2 Кб
1// Copyright Istio Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package helmreconciler
16
17import (
18_ "embed"
19"sync"
20"testing"
21
22v1 "k8s.io/api/core/v1"
23metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24"k8s.io/apimachinery/pkg/runtime/schema"
25"sigs.k8s.io/controller-runtime/pkg/client/fake"
26"sigs.k8s.io/yaml"
27
28"istio.io/api/label"
29"istio.io/istio/operator/pkg/apis/istio/v1alpha1"
30"istio.io/istio/operator/pkg/name"
31"istio.io/istio/operator/pkg/object"
32"istio.io/istio/operator/pkg/util/clog"
33"istio.io/istio/operator/pkg/util/progress"
34"istio.io/istio/pkg/kube"
35"istio.io/istio/pkg/test/util/assert"
36)
37
38var (
39//go:embed testdata/iop-test-gw-1.yaml
40iopTestGwData1 []byte
41//go:embed testdata/iop-test-gw-2.yaml
42iopTestGwData2 []byte
43)
44
45func applyResourcesIntoCluster(t *testing.T, h *HelmReconciler, manifestMap name.ManifestMap) {
46for cn, ms := range manifestMap.Consolidated() {
47objects, err := object.ParseK8sObjectsFromYAMLManifest(ms)
48if err != nil {
49t.Fatalf("failed parse k8s objects from yaml: %v", err)
50}
51for _, obj := range objects {
52obju := obj.UnstructuredObject()
53if err := h.applyLabelsAndAnnotations(obju, cn); err != nil {
54t.Errorf("failed to apply label and annotations: %v", err)
55}
56if err := h.ApplyObject(obj.UnstructuredObject()); err != nil {
57t.Errorf("HelmReconciler.ApplyObject() error = %v", err)
58}
59}
60}
61}
62
63func TestHelmReconciler_GetPrunedResources(t *testing.T) {
64t.Run("get gateway pruned resources", func(t *testing.T) {
65var h1 *HelmReconciler
66cl := fake.NewClientBuilder().WithInterceptorFuncs(interceptorFunc).Build()
67// init two custom gateways with revision
68gateways := [][]byte{iopTestGwData1, iopTestGwData2}
69for i, data := range gateways {
70iop := &v1alpha1.IstioOperator{}
71err := yaml.UnmarshalStrict(data, iop)
72assert.NoError(t, err)
73h := &HelmReconciler{
74client: cl,
75kubeClient: kube.NewFakeClientWithVersion("24"),
76opts: &Options{
77ProgressLog: progress.NewLog(),
78Log: clog.NewDefaultLogger(),
79},
80iop: iop,
81countLock: &sync.Mutex{},
82prunedKindSet: map[schema.GroupKind]struct{}{},
83}
84if i == 0 {
85h1 = h
86}
87manifestMap, err := h.RenderCharts()
88if err != nil {
89t.Fatalf("failed to render manifest: %v", err)
90}
91applyResourcesIntoCluster(t, h, manifestMap)
92}
93// delete one iop: iop-test-gw-1, get its pruned resources
94componentName := string(name.IngressComponentName)
95resources, err := h1.GetPrunedResources(h1.iop.Spec.Revision, false, componentName)
96assert.NoError(t, err)
97assert.Equal(t, true, len(resources) > 0)
98// check resources, only associated with iop-test-gw-1 istiooperator CR,
99// otherwise, the resources of all IngressGateways components will be deleted.
100// See https://github.com/istio/istio/issues/40577 for more details.
101for _, uslist := range resources {
102for _, u := range uslist.Items {
103assert.Equal(t, h1.iop.Spec.Revision, u.GetLabels()[label.IoIstioRev.Name])
104assert.Equal(t, componentName, u.GetLabels()[IstioComponentLabelStr])
105assert.Equal(t, h1.iop.GetName(), u.GetLabels()[OwningResourceName])
106assert.Equal(t, h1.iop.GetNamespace(), u.GetLabels()[OwningResourceNamespace])
107}
108}
109})
110}
111
112func TestPilotExist(t *testing.T) {
113t.Run("exist", func(t *testing.T) {
114cl := fake.NewClientBuilder().WithInterceptorFuncs(interceptorFunc).Build()
115iop := &v1alpha1.IstioOperator{}
116h := &HelmReconciler{
117client: cl,
118kubeClient: kube.NewFakeClientWithVersion("24"),
119opts: &Options{
120ProgressLog: progress.NewLog(),
121Log: clog.NewDefaultLogger(),
122},
123iop: iop,
124countLock: &sync.Mutex{},
125prunedKindSet: map[schema.GroupKind]struct{}{},
126}
127mockClient := kube.NewFakeClient(&v1.Pod{
128ObjectMeta: metav1.ObjectMeta{
129Name: "istiod",
130Namespace: "istio-system",
131Labels: map[string]string{"app": "istiod"},
132},
133})
134
135if exist, err := h.pilotExists(mockClient, "istio-system"); err != nil {
136t.Fatalf("HelmReconciler.pilotExists error = %v", err)
137} else if !exist {
138t.Errorf("HelmReconciler.pilotExists fail")
139}
140})
141
142t.Run("non-exist", func(t *testing.T) {
143cl := fake.NewClientBuilder().WithInterceptorFuncs(interceptorFunc).Build()
144iop := &v1alpha1.IstioOperator{}
145kc := kube.NewFakeClientWithVersion("24")
146h := &HelmReconciler{
147client: cl,
148kubeClient: kc,
149opts: &Options{
150ProgressLog: progress.NewLog(),
151Log: clog.NewDefaultLogger(),
152},
153iop: iop,
154countLock: &sync.Mutex{},
155prunedKindSet: map[schema.GroupKind]struct{}{},
156}
157if exist, err := h.pilotExists(kc, "istio-system"); err != nil {
158t.Fatalf("HelmReconciler.pilotExists error = %v", err)
159} else if exist {
160t.Errorf("HelmReconciler.pilotExists fail")
161}
162})
163}
164