istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/translate/translate_test.go (about) 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 15 package translate 16 17 import ( 18 "fmt" 19 "testing" 20 21 "istio.io/api/operator/v1alpha1" 22 "istio.io/istio/operator/pkg/name" 23 "istio.io/istio/operator/pkg/object" 24 "istio.io/istio/operator/pkg/tpath" 25 "istio.io/istio/operator/pkg/util" 26 "istio.io/istio/pkg/test/util/assert" 27 ) 28 29 func Test_skipReplicaCountWithAutoscaleEnabled(t *testing.T) { 30 const valuesWithHPAndReplicaCountFormat = ` 31 values: 32 pilot: 33 autoscaleEnabled: %t 34 gateways: 35 istio-ingressgateway: 36 autoscaleEnabled: %t 37 istio-egressgateway: 38 autoscaleEnabled: %t 39 components: 40 pilot: 41 k8s: 42 replicaCount: 2 43 ingressGateways: 44 - name: istio-ingressgateway 45 enabled: true 46 k8s: 47 replicaCount: 2 48 egressGateways: 49 - name: istio-egressgateway 50 enabled: true 51 k8s: 52 replicaCount: 2 53 ` 54 55 cases := []struct { 56 name string 57 component name.ComponentName 58 values string 59 expectSkip bool 60 }{ 61 { 62 name: "hpa enabled for pilot without replicas", 63 component: name.PilotComponentName, 64 values: fmt.Sprintf(valuesWithHPAndReplicaCountFormat, false, false, false), 65 expectSkip: false, 66 }, 67 { 68 name: "hpa enabled for ingressgateway without replica", 69 component: name.IngressComponentName, 70 values: fmt.Sprintf(valuesWithHPAndReplicaCountFormat, false, false, false), 71 expectSkip: false, 72 }, 73 { 74 name: "hpa enabled for pilot without replicas", 75 component: name.EgressComponentName, 76 values: fmt.Sprintf(valuesWithHPAndReplicaCountFormat, false, false, false), 77 expectSkip: false, 78 }, 79 { 80 name: "hpa enabled for pilot with replicas", 81 component: name.PilotComponentName, 82 values: fmt.Sprintf(valuesWithHPAndReplicaCountFormat, true, false, false), 83 expectSkip: true, 84 }, 85 { 86 name: "hpa enabled for ingressgateway with replicas", 87 component: name.IngressComponentName, 88 values: fmt.Sprintf(valuesWithHPAndReplicaCountFormat, false, true, false), 89 expectSkip: true, 90 }, 91 { 92 name: "hpa enabled for egressgateway with replicas", 93 component: name.EgressComponentName, 94 values: fmt.Sprintf(valuesWithHPAndReplicaCountFormat, true, false, true), 95 expectSkip: true, 96 }, 97 } 98 for _, tt := range cases { 99 t.Run(tt.name, func(t *testing.T) { 100 var iop *v1alpha1.IstioOperatorSpec 101 if tt.values != "" { 102 iop = &v1alpha1.IstioOperatorSpec{} 103 if err := util.UnmarshalWithJSONPB(tt.values, iop, true); err != nil { 104 t.Fatal(err) 105 } 106 } 107 108 got := skipReplicaCountWithAutoscaleEnabled(iop, tt.component) 109 assert.Equal(t, tt.expectSkip, got) 110 }) 111 } 112 } 113 114 func Test_fixMergedObjectWithCustomServicePortOverlay_withAppProtocol(t *testing.T) { 115 const serviceString = ` 116 apiVersion: v1 117 kind: Service 118 metadata: 119 name: istio-ingressgateway 120 namespace: istio-system 121 spec: 122 type: LoadBalancer 123 ` 124 const iopString = ` 125 components: 126 ingressGateways: 127 - name: istio-ingressgateway 128 enabled: true 129 k8s: 130 service: 131 ports: 132 - name: http2 133 port: 80 134 targetPort: 8080 135 ` 136 const iopString2 = ` 137 components: 138 ingressGateways: 139 - name: istio-ingressgateway 140 enabled: true 141 k8s: 142 service: 143 ports: 144 - name: http2 145 port: 80 146 targetPort: 8080 147 appProtocol: HTTP 148 ` 149 cases := []struct { 150 name string 151 iopString string 152 expectValue string 153 expectExist bool 154 }{ 155 { 156 name: "without appProtocol", 157 iopString: iopString, 158 expectExist: false, 159 }, 160 { 161 name: "with appProtocol", 162 iopString: iopString2, 163 expectExist: true, 164 expectValue: "HTTP", 165 }, 166 } 167 168 for _, tt := range cases { 169 t.Run(tt.name, func(t *testing.T) { 170 var iop *v1alpha1.IstioOperatorSpec 171 if tt.iopString != "" { 172 iop = &v1alpha1.IstioOperatorSpec{} 173 if err := util.UnmarshalWithJSONPB(tt.iopString, iop, true); err != nil { 174 t.Fatal(err) 175 } 176 } 177 translator := NewTranslator() 178 serviceObj, err := object.ParseYAMLToK8sObject([]byte(serviceString)) 179 assert.NoError(t, err) 180 obj, err := translator.fixMergedObjectWithCustomServicePortOverlay(serviceObj, 181 iop.Components.IngressGateways[0].GetK8S().GetService(), serviceObj) 182 assert.NoError(t, err) 183 val := obj.UnstructuredObject().Object["spec"].(map[string]interface{})["ports"].([]interface{})[0] 184 apVal, found, _ := tpath.GetFromStructPath(val, "appProtocol") 185 if !tt.expectExist { 186 assert.Equal(t, found, false) 187 } else { 188 if apVal != nil { 189 assert.Equal(t, apVal.(string), tt.expectValue) 190 } else { 191 assert.Equal(t, "", tt.expectValue) 192 } 193 } 194 }) 195 } 196 }