|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package aws |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + v1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | +) |
| 26 | + |
| 27 | +func TestValidateServiceAnnotationTargetGroupAttributes(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + annotations map[string]string |
| 31 | + servicePorts []v1.ServicePort |
| 32 | + expectedError string |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "no target group attributes annotation", |
| 36 | + annotations: map[string]string{}, |
| 37 | + servicePorts: []v1.ServicePort{ |
| 38 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 39 | + }, |
| 40 | + expectedError: "", |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "empty target group attributes annotation", |
| 44 | + annotations: map[string]string{ |
| 45 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "", |
| 46 | + }, |
| 47 | + servicePorts: []v1.ServicePort{ |
| 48 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 49 | + }, |
| 50 | + expectedError: "", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "valid preserve_client_ip.enabled=true", |
| 54 | + annotations: map[string]string{ |
| 55 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled=true", |
| 56 | + }, |
| 57 | + servicePorts: []v1.ServicePort{ |
| 58 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 59 | + }, |
| 60 | + expectedError: "", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "valid preserve_client_ip.enabled=false", |
| 64 | + annotations: map[string]string{ |
| 65 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled=false", |
| 66 | + }, |
| 67 | + servicePorts: []v1.ServicePort{ |
| 68 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 69 | + }, |
| 70 | + expectedError: "", |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "valid proxy_protocol_v2.enabled=true", |
| 74 | + annotations: map[string]string{ |
| 75 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "proxy_protocol_v2.enabled=true", |
| 76 | + }, |
| 77 | + servicePorts: []v1.ServicePort{ |
| 78 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 79 | + }, |
| 80 | + expectedError: "", |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "valid proxy_protocol_v2.enabled=false", |
| 84 | + annotations: map[string]string{ |
| 85 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "proxy_protocol_v2.enabled=false", |
| 86 | + }, |
| 87 | + servicePorts: []v1.ServicePort{ |
| 88 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 89 | + }, |
| 90 | + expectedError: "", |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "valid multiple attributes", |
| 94 | + annotations: map[string]string{ |
| 95 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled=true,proxy_protocol_v2.enabled=false", |
| 96 | + }, |
| 97 | + servicePorts: []v1.ServicePort{ |
| 98 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 99 | + }, |
| 100 | + expectedError: "", |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "duplicate attribute in annotation (last one wins - no error expected)", |
| 104 | + annotations: map[string]string{ |
| 105 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled=true,preserve_client_ip.enabled=false", |
| 106 | + }, |
| 107 | + servicePorts: []v1.ServicePort{ |
| 108 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 109 | + }, |
| 110 | + expectedError: "", // getKeyValuePropertiesFromAnnotation overwrites, so no duplicate detection |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "empty attribute value", |
| 114 | + annotations: map[string]string{ |
| 115 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled=", |
| 116 | + }, |
| 117 | + servicePorts: []v1.ServicePort{ |
| 118 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 119 | + }, |
| 120 | + expectedError: "attribute value is empty for \"preserve_client_ip.enabled\"", |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "invalid preserve_client_ip.enabled value", |
| 124 | + annotations: map[string]string{ |
| 125 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled=invalid", |
| 126 | + }, |
| 127 | + servicePorts: []v1.ServicePort{ |
| 128 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 129 | + }, |
| 130 | + expectedError: "invalid attribute value for \"preserve_client_ip.enabled\": invalid", |
| 131 | + }, |
| 132 | + { |
| 133 | + name: "invalid proxy_protocol_v2.enabled value", |
| 134 | + annotations: map[string]string{ |
| 135 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "proxy_protocol_v2.enabled=yes", |
| 136 | + }, |
| 137 | + servicePorts: []v1.ServicePort{ |
| 138 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 139 | + }, |
| 140 | + expectedError: "invalid attribute value for \"proxy_protocol_v2.enabled\": yes", |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "unsupported attribute", |
| 144 | + annotations: map[string]string{ |
| 145 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "unsupported_attribute=value", |
| 146 | + }, |
| 147 | + servicePorts: []v1.ServicePort{ |
| 148 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 149 | + }, |
| 150 | + expectedError: "the attribute \"unsupported_attribute\" is not supported by the controller or is invalid", |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "preserve_client_ip.enabled=false with UDP port should fail", |
| 154 | + annotations: map[string]string{ |
| 155 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled=false", |
| 156 | + }, |
| 157 | + servicePorts: []v1.ServicePort{ |
| 158 | + {Port: 53, Protocol: v1.ProtocolUDP}, |
| 159 | + }, |
| 160 | + expectedError: "client IP preservation can't be disabled for UDP ports", |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "preserve_client_ip.enabled=false with TCP_UDP port should fail", |
| 164 | + annotations: map[string]string{ |
| 165 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled=false", |
| 166 | + }, |
| 167 | + servicePorts: []v1.ServicePort{ |
| 168 | + {Port: 53, Protocol: "TCP_UDP"}, |
| 169 | + }, |
| 170 | + expectedError: "client IP preservation can't be disabled for UDP ports", |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "preserve_client_ip.enabled=true with UDP port should succeed", |
| 174 | + annotations: map[string]string{ |
| 175 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled=true", |
| 176 | + }, |
| 177 | + servicePorts: []v1.ServicePort{ |
| 178 | + {Port: 53, Protocol: v1.ProtocolUDP}, |
| 179 | + }, |
| 180 | + expectedError: "", |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "preserve_client_ip.enabled=false with mixed TCP and UDP ports should fail", |
| 184 | + annotations: map[string]string{ |
| 185 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled=false", |
| 186 | + }, |
| 187 | + servicePorts: []v1.ServicePort{ |
| 188 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 189 | + {Port: 53, Protocol: v1.ProtocolUDP}, |
| 190 | + }, |
| 191 | + expectedError: "client IP preservation can't be disabled for UDP ports", |
| 192 | + }, |
| 193 | + { |
| 194 | + name: "multiple attributes with preserve_client_ip.enabled=false and UDP port should fail", |
| 195 | + annotations: map[string]string{ |
| 196 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled=false,proxy_protocol_v2.enabled=true", |
| 197 | + }, |
| 198 | + servicePorts: []v1.ServicePort{ |
| 199 | + {Port: 53, Protocol: v1.ProtocolUDP}, |
| 200 | + }, |
| 201 | + expectedError: "client IP preservation can't be disabled for UDP ports", |
| 202 | + }, |
| 203 | + { |
| 204 | + name: "case sensitivity - preserve_client_ip.enabled with True should fail", |
| 205 | + annotations: map[string]string{ |
| 206 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled=True", |
| 207 | + }, |
| 208 | + servicePorts: []v1.ServicePort{ |
| 209 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 210 | + }, |
| 211 | + expectedError: "invalid attribute value for \"preserve_client_ip.enabled\": True", |
| 212 | + }, |
| 213 | + { |
| 214 | + name: "case sensitivity - proxy_protocol_v2.enabled with FALSE should fail", |
| 215 | + annotations: map[string]string{ |
| 216 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "proxy_protocol_v2.enabled=FALSE", |
| 217 | + }, |
| 218 | + servicePorts: []v1.ServicePort{ |
| 219 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 220 | + }, |
| 221 | + expectedError: "invalid attribute value for \"proxy_protocol_v2.enabled\": FALSE", |
| 222 | + }, |
| 223 | + { |
| 224 | + name: "whitespace in attribute values should fail", |
| 225 | + annotations: map[string]string{ |
| 226 | + ServiceAnnotationLoadBalancerTargetGroupAttributes: "preserve_client_ip.enabled= true ", |
| 227 | + }, |
| 228 | + servicePorts: []v1.ServicePort{ |
| 229 | + {Port: 80, Protocol: v1.ProtocolTCP}, |
| 230 | + }, |
| 231 | + expectedError: "invalid attribute value for \"preserve_client_ip.enabled\":", |
| 232 | + }, |
| 233 | + } |
| 234 | + |
| 235 | + for _, tt := range tests { |
| 236 | + t.Run(tt.name, func(t *testing.T) { |
| 237 | + // Create a test service with the specified ports |
| 238 | + service := &v1.Service{ |
| 239 | + ObjectMeta: metav1.ObjectMeta{ |
| 240 | + Name: "test-service", |
| 241 | + Namespace: "test-namespace", |
| 242 | + Annotations: tt.annotations, |
| 243 | + }, |
| 244 | + Spec: v1.ServiceSpec{ |
| 245 | + Type: v1.ServiceTypeLoadBalancer, |
| 246 | + Ports: tt.servicePorts, |
| 247 | + }, |
| 248 | + } |
| 249 | + |
| 250 | + // Create validation input |
| 251 | + input := &awsValidationInput{ |
| 252 | + apiService: service, |
| 253 | + annotations: tt.annotations, |
| 254 | + } |
| 255 | + |
| 256 | + // Execute the validation |
| 257 | + err := validateServiceAnnotationTargetGroupAttributes(input) |
| 258 | + |
| 259 | + // Verify the result |
| 260 | + if tt.expectedError == "" { |
| 261 | + assert.NoError(t, err, "Expected no error for test case: %s", tt.name) |
| 262 | + } else { |
| 263 | + assert.Error(t, err, "Expected error for test case: %s", tt.name) |
| 264 | + assert.Contains(t, err.Error(), tt.expectedError, "Error message should contain expected text for test case: %s", tt.name) |
| 265 | + } |
| 266 | + }) |
| 267 | + } |
| 268 | +} |
0 commit comments