sigs.k8s.io/kueue@v0.6.2/apis/config/v1beta1/defaults_test.go (about) 1 /* 2 Copyright 2022 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 v1beta1 18 19 import ( 20 "testing" 21 "time" 22 23 "github.com/google/go-cmp/cmp" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 componentconfigv1alpha1 "k8s.io/component-base/config/v1alpha1" 26 "k8s.io/utils/ptr" 27 ) 28 29 const ( 30 overwriteNamespace = "kueue-tenant-a" 31 overwriteWebhookPort = 9444 32 overwriteMetricBindAddress = ":38081" 33 overwriteHealthProbeBindAddress = ":38080" 34 overwriteLeaderElectionID = "foo.kueue.x-k8s.io" 35 ) 36 37 func TestSetDefaults_Configuration(t *testing.T) { 38 defaultCtrlManagerConfigurationSpec := ControllerManager{ 39 LeaderElection: &componentconfigv1alpha1.LeaderElectionConfiguration{ 40 LeaderElect: ptr.To(true), 41 LeaseDuration: metav1.Duration{Duration: DefaultLeaderElectionLeaseDuration}, 42 RenewDeadline: metav1.Duration{Duration: DefaultLeaderElectionRenewDeadline}, 43 RetryPeriod: metav1.Duration{Duration: DefaultLeaderElectionRetryPeriod}, 44 ResourceLock: "leases", 45 ResourceName: "c1f6bfd2.kueue.x-k8s.io", 46 }, 47 Webhook: ControllerWebhook{ 48 Port: ptr.To(DefaultWebhookPort), 49 }, 50 Metrics: ControllerMetrics{ 51 BindAddress: DefaultMetricsBindAddress, 52 }, 53 Health: ControllerHealth{ 54 HealthProbeBindAddress: DefaultHealthProbeBindAddress, 55 }, 56 } 57 defaultClientConnection := &ClientConnection{ 58 QPS: ptr.To(DefaultClientConnectionQPS), 59 Burst: ptr.To(DefaultClientConnectionBurst), 60 } 61 defaultIntegrations := &Integrations{ 62 Frameworks: []string{defaultJobFrameworkName}, 63 PodOptions: &PodIntegrationOptions{ 64 NamespaceSelector: &metav1.LabelSelector{ 65 MatchExpressions: []metav1.LabelSelectorRequirement{ 66 { 67 Key: "kubernetes.io/metadata.name", 68 Operator: metav1.LabelSelectorOpNotIn, 69 Values: []string{"kube-system", "kueue-system"}, 70 }, 71 }, 72 }, 73 PodSelector: &metav1.LabelSelector{}, 74 }, 75 } 76 defaultQueueVisibility := &QueueVisibility{ 77 UpdateIntervalSeconds: DefaultQueueVisibilityUpdateIntervalSeconds, 78 ClusterQueues: &ClusterQueueVisibility{ 79 MaxCount: 10, 80 }, 81 } 82 83 overwriteNamespaceIntegrations := &Integrations{ 84 Frameworks: []string{defaultJobFrameworkName}, 85 PodOptions: &PodIntegrationOptions{ 86 NamespaceSelector: &metav1.LabelSelector{ 87 MatchExpressions: []metav1.LabelSelectorRequirement{ 88 { 89 Key: "kubernetes.io/metadata.name", 90 Operator: metav1.LabelSelectorOpNotIn, 91 Values: []string{"kube-system", overwriteNamespace}, 92 }, 93 }, 94 }, 95 PodSelector: &metav1.LabelSelector{}, 96 }, 97 } 98 99 defaultMultiKueue := &MultiKueue{ 100 GCInterval: &metav1.Duration{Duration: DefaultMultiKueueGCInterval}, 101 Origin: ptr.To(DefaultMultiKueueOrigin), 102 } 103 104 podsReadyTimeoutTimeout := metav1.Duration{Duration: defaultPodsReadyTimeout} 105 podsReadyTimeoutOverwrite := metav1.Duration{Duration: time.Minute} 106 107 testCases := map[string]struct { 108 original *Configuration 109 want *Configuration 110 }{ 111 "defaulting namespace": { 112 original: &Configuration{ 113 InternalCertManagement: &InternalCertManagement{ 114 Enable: ptr.To(false), 115 }, 116 }, 117 want: &Configuration{ 118 Namespace: ptr.To(DefaultNamespace), 119 ControllerManager: defaultCtrlManagerConfigurationSpec, 120 InternalCertManagement: &InternalCertManagement{ 121 Enable: ptr.To(false), 122 }, 123 ClientConnection: defaultClientConnection, 124 Integrations: defaultIntegrations, 125 QueueVisibility: defaultQueueVisibility, 126 MultiKueue: defaultMultiKueue, 127 }, 128 }, 129 "defaulting ControllerManager": { 130 original: &Configuration{ 131 ControllerManager: ControllerManager{ 132 LeaderElection: &componentconfigv1alpha1.LeaderElectionConfiguration{ 133 LeaderElect: ptr.To(true), 134 }, 135 }, 136 InternalCertManagement: &InternalCertManagement{ 137 Enable: ptr.To(false), 138 }, 139 }, 140 want: &Configuration{ 141 Namespace: ptr.To(DefaultNamespace), 142 ControllerManager: ControllerManager{ 143 Webhook: ControllerWebhook{ 144 Port: ptr.To(DefaultWebhookPort), 145 }, 146 Metrics: ControllerMetrics{ 147 BindAddress: DefaultMetricsBindAddress, 148 }, 149 Health: ControllerHealth{ 150 HealthProbeBindAddress: DefaultHealthProbeBindAddress, 151 }, 152 LeaderElection: &componentconfigv1alpha1.LeaderElectionConfiguration{ 153 LeaderElect: ptr.To(true), 154 LeaseDuration: metav1.Duration{Duration: DefaultLeaderElectionLeaseDuration}, 155 RenewDeadline: metav1.Duration{Duration: DefaultLeaderElectionRenewDeadline}, 156 RetryPeriod: metav1.Duration{Duration: DefaultLeaderElectionRetryPeriod}, 157 ResourceLock: "leases", 158 ResourceName: DefaultLeaderElectionID, 159 }, 160 }, 161 InternalCertManagement: &InternalCertManagement{ 162 Enable: ptr.To(false), 163 }, 164 ClientConnection: defaultClientConnection, 165 Integrations: defaultIntegrations, 166 QueueVisibility: defaultQueueVisibility, 167 MultiKueue: defaultMultiKueue, 168 }, 169 }, 170 "should not default ControllerManager": { 171 original: &Configuration{ 172 ControllerManager: ControllerManager{ 173 Webhook: ControllerWebhook{ 174 Port: ptr.To(overwriteWebhookPort), 175 }, 176 Metrics: ControllerMetrics{ 177 BindAddress: overwriteMetricBindAddress, 178 }, 179 Health: ControllerHealth{ 180 HealthProbeBindAddress: overwriteHealthProbeBindAddress, 181 }, 182 LeaderElection: &componentconfigv1alpha1.LeaderElectionConfiguration{ 183 LeaderElect: ptr.To(true), 184 LeaseDuration: metav1.Duration{Duration: DefaultLeaderElectionLeaseDuration}, 185 RenewDeadline: metav1.Duration{Duration: DefaultLeaderElectionRenewDeadline}, 186 RetryPeriod: metav1.Duration{Duration: DefaultLeaderElectionRetryPeriod}, 187 ResourceLock: "leases", 188 ResourceName: overwriteLeaderElectionID, 189 }, 190 }, 191 InternalCertManagement: &InternalCertManagement{ 192 Enable: ptr.To(false), 193 }, 194 Integrations: defaultIntegrations, 195 QueueVisibility: defaultQueueVisibility, 196 }, 197 want: &Configuration{ 198 Namespace: ptr.To(DefaultNamespace), 199 ControllerManager: ControllerManager{ 200 Webhook: ControllerWebhook{ 201 Port: ptr.To(overwriteWebhookPort), 202 }, 203 Metrics: ControllerMetrics{ 204 BindAddress: overwriteMetricBindAddress, 205 }, 206 Health: ControllerHealth{ 207 HealthProbeBindAddress: overwriteHealthProbeBindAddress, 208 }, 209 LeaderElection: &componentconfigv1alpha1.LeaderElectionConfiguration{ 210 LeaderElect: ptr.To(true), 211 LeaseDuration: metav1.Duration{Duration: DefaultLeaderElectionLeaseDuration}, 212 RenewDeadline: metav1.Duration{Duration: DefaultLeaderElectionRenewDeadline}, 213 RetryPeriod: metav1.Duration{Duration: DefaultLeaderElectionRetryPeriod}, 214 ResourceLock: "leases", 215 ResourceName: overwriteLeaderElectionID, 216 }, 217 }, 218 InternalCertManagement: &InternalCertManagement{ 219 Enable: ptr.To(false), 220 }, 221 ClientConnection: defaultClientConnection, 222 Integrations: defaultIntegrations, 223 QueueVisibility: defaultQueueVisibility, 224 MultiKueue: defaultMultiKueue, 225 }, 226 }, 227 "should not set LeaderElectionID": { 228 original: &Configuration{ 229 ControllerManager: ControllerManager{ 230 LeaderElection: &componentconfigv1alpha1.LeaderElectionConfiguration{ 231 LeaderElect: ptr.To(false), 232 }, 233 }, 234 InternalCertManagement: &InternalCertManagement{ 235 Enable: ptr.To(false), 236 }, 237 }, 238 want: &Configuration{ 239 Namespace: ptr.To(DefaultNamespace), 240 ControllerManager: ControllerManager{ 241 Webhook: ControllerWebhook{ 242 Port: ptr.To(DefaultWebhookPort), 243 }, 244 Metrics: ControllerMetrics{ 245 BindAddress: DefaultMetricsBindAddress, 246 }, 247 Health: ControllerHealth{ 248 HealthProbeBindAddress: DefaultHealthProbeBindAddress, 249 }, 250 LeaderElection: &componentconfigv1alpha1.LeaderElectionConfiguration{ 251 LeaderElect: ptr.To(false), 252 LeaseDuration: metav1.Duration{Duration: DefaultLeaderElectionLeaseDuration}, 253 RenewDeadline: metav1.Duration{Duration: DefaultLeaderElectionRenewDeadline}, 254 RetryPeriod: metav1.Duration{Duration: DefaultLeaderElectionRetryPeriod}, 255 ResourceLock: "leases", 256 ResourceName: "c1f6bfd2.kueue.x-k8s.io", 257 }, 258 }, 259 InternalCertManagement: &InternalCertManagement{ 260 Enable: ptr.To(false), 261 }, 262 ClientConnection: defaultClientConnection, 263 Integrations: defaultIntegrations, 264 QueueVisibility: defaultQueueVisibility, 265 MultiKueue: defaultMultiKueue, 266 }, 267 }, 268 "defaulting InternalCertManagement": { 269 original: &Configuration{ 270 Namespace: ptr.To(overwriteNamespace), 271 }, 272 want: &Configuration{ 273 Namespace: ptr.To(overwriteNamespace), 274 ControllerManager: defaultCtrlManagerConfigurationSpec, 275 InternalCertManagement: &InternalCertManagement{ 276 Enable: ptr.To(true), 277 WebhookServiceName: ptr.To(DefaultWebhookServiceName), 278 WebhookSecretName: ptr.To(DefaultWebhookSecretName), 279 }, 280 ClientConnection: defaultClientConnection, 281 Integrations: overwriteNamespaceIntegrations, 282 QueueVisibility: defaultQueueVisibility, 283 MultiKueue: defaultMultiKueue, 284 }, 285 }, 286 "should not default InternalCertManagement": { 287 original: &Configuration{ 288 Namespace: ptr.To(overwriteNamespace), 289 InternalCertManagement: &InternalCertManagement{ 290 Enable: ptr.To(false), 291 }, 292 }, 293 want: &Configuration{ 294 Namespace: ptr.To(overwriteNamespace), 295 ControllerManager: defaultCtrlManagerConfigurationSpec, 296 InternalCertManagement: &InternalCertManagement{ 297 Enable: ptr.To(false), 298 }, 299 ClientConnection: defaultClientConnection, 300 Integrations: overwriteNamespaceIntegrations, 301 QueueVisibility: defaultQueueVisibility, 302 MultiKueue: defaultMultiKueue, 303 }, 304 }, 305 "should not default values in custom ClientConnection": { 306 original: &Configuration{ 307 Namespace: ptr.To(overwriteNamespace), 308 InternalCertManagement: &InternalCertManagement{ 309 Enable: ptr.To(false), 310 }, 311 ClientConnection: &ClientConnection{ 312 QPS: ptr.To[float32](123.0), 313 Burst: ptr.To[int32](456), 314 }, 315 }, 316 want: &Configuration{ 317 Namespace: ptr.To(overwriteNamespace), 318 ControllerManager: defaultCtrlManagerConfigurationSpec, 319 InternalCertManagement: &InternalCertManagement{ 320 Enable: ptr.To(false), 321 }, 322 ClientConnection: &ClientConnection{ 323 QPS: ptr.To[float32](123.0), 324 Burst: ptr.To[int32](456), 325 }, 326 Integrations: overwriteNamespaceIntegrations, 327 QueueVisibility: defaultQueueVisibility, 328 MultiKueue: defaultMultiKueue, 329 }, 330 }, 331 "should default empty custom ClientConnection": { 332 original: &Configuration{ 333 Namespace: ptr.To(overwriteNamespace), 334 InternalCertManagement: &InternalCertManagement{ 335 Enable: ptr.To(false), 336 }, 337 ClientConnection: &ClientConnection{}, 338 }, 339 want: &Configuration{ 340 Namespace: ptr.To(overwriteNamespace), 341 ControllerManager: defaultCtrlManagerConfigurationSpec, 342 InternalCertManagement: &InternalCertManagement{ 343 Enable: ptr.To(false), 344 }, 345 ClientConnection: defaultClientConnection, 346 Integrations: overwriteNamespaceIntegrations, 347 QueueVisibility: defaultQueueVisibility, 348 MultiKueue: defaultMultiKueue, 349 }, 350 }, 351 "defaulting waitForPodsReady values": { 352 original: &Configuration{ 353 WaitForPodsReady: &WaitForPodsReady{ 354 Enable: true, 355 }, 356 InternalCertManagement: &InternalCertManagement{ 357 Enable: ptr.To(false), 358 }, 359 }, 360 want: &Configuration{ 361 WaitForPodsReady: &WaitForPodsReady{ 362 Enable: true, 363 BlockAdmission: ptr.To(true), 364 Timeout: &podsReadyTimeoutTimeout, 365 RequeuingStrategy: &RequeuingStrategy{ 366 Timestamp: ptr.To(EvictionTimestamp), 367 }, 368 }, 369 Namespace: ptr.To(DefaultNamespace), 370 ControllerManager: defaultCtrlManagerConfigurationSpec, 371 InternalCertManagement: &InternalCertManagement{ 372 Enable: ptr.To(false), 373 }, 374 ClientConnection: defaultClientConnection, 375 Integrations: defaultIntegrations, 376 QueueVisibility: defaultQueueVisibility, 377 MultiKueue: defaultMultiKueue, 378 }, 379 }, 380 "set waitForPodsReady.blockAdmission to false when enable is false": { 381 original: &Configuration{ 382 WaitForPodsReady: &WaitForPodsReady{ 383 Enable: false, 384 }, 385 InternalCertManagement: &InternalCertManagement{ 386 Enable: ptr.To(false), 387 }, 388 }, 389 want: &Configuration{ 390 WaitForPodsReady: &WaitForPodsReady{ 391 Enable: false, 392 BlockAdmission: ptr.To(false), 393 Timeout: &podsReadyTimeoutTimeout, 394 RequeuingStrategy: &RequeuingStrategy{ 395 Timestamp: ptr.To(EvictionTimestamp), 396 }, 397 }, 398 Namespace: ptr.To(DefaultNamespace), 399 ControllerManager: defaultCtrlManagerConfigurationSpec, 400 InternalCertManagement: &InternalCertManagement{ 401 Enable: ptr.To(false), 402 }, 403 ClientConnection: defaultClientConnection, 404 Integrations: defaultIntegrations, 405 QueueVisibility: defaultQueueVisibility, 406 MultiKueue: defaultMultiKueue, 407 }, 408 }, 409 "respecting provided waitForPodsReady values": { 410 original: &Configuration{ 411 WaitForPodsReady: &WaitForPodsReady{ 412 Enable: true, 413 Timeout: &podsReadyTimeoutOverwrite, 414 RequeuingStrategy: &RequeuingStrategy{ 415 Timestamp: ptr.To(CreationTimestamp), 416 }, 417 }, 418 InternalCertManagement: &InternalCertManagement{ 419 Enable: ptr.To(false), 420 }, 421 }, 422 want: &Configuration{ 423 WaitForPodsReady: &WaitForPodsReady{ 424 Enable: true, 425 BlockAdmission: ptr.To(true), 426 Timeout: &podsReadyTimeoutOverwrite, 427 RequeuingStrategy: &RequeuingStrategy{ 428 Timestamp: ptr.To(CreationTimestamp), 429 }, 430 }, 431 Namespace: ptr.To(DefaultNamespace), 432 ControllerManager: defaultCtrlManagerConfigurationSpec, 433 InternalCertManagement: &InternalCertManagement{ 434 Enable: ptr.To(false), 435 }, 436 ClientConnection: defaultClientConnection, 437 Integrations: defaultIntegrations, 438 QueueVisibility: defaultQueueVisibility, 439 MultiKueue: defaultMultiKueue, 440 }, 441 }, 442 "integrations": { 443 original: &Configuration{ 444 InternalCertManagement: &InternalCertManagement{ 445 Enable: ptr.To(false), 446 }, 447 Integrations: &Integrations{ 448 Frameworks: []string{"a", "b"}, 449 }, 450 }, 451 want: &Configuration{ 452 Namespace: ptr.To(DefaultNamespace), 453 ControllerManager: defaultCtrlManagerConfigurationSpec, 454 InternalCertManagement: &InternalCertManagement{ 455 Enable: ptr.To(false), 456 }, 457 ClientConnection: defaultClientConnection, 458 Integrations: &Integrations{ 459 Frameworks: []string{"a", "b"}, 460 PodOptions: defaultIntegrations.PodOptions, 461 }, 462 QueueVisibility: defaultQueueVisibility, 463 MultiKueue: defaultMultiKueue, 464 }, 465 }, 466 "queue visibility": { 467 original: &Configuration{ 468 InternalCertManagement: &InternalCertManagement{ 469 Enable: ptr.To(false), 470 }, 471 QueueVisibility: &QueueVisibility{ 472 UpdateIntervalSeconds: 10, 473 ClusterQueues: &ClusterQueueVisibility{ 474 MaxCount: 0, 475 }, 476 }, 477 }, 478 want: &Configuration{ 479 Namespace: ptr.To(DefaultNamespace), 480 ControllerManager: defaultCtrlManagerConfigurationSpec, 481 InternalCertManagement: &InternalCertManagement{ 482 Enable: ptr.To(false), 483 }, 484 ClientConnection: defaultClientConnection, 485 Integrations: defaultIntegrations, 486 QueueVisibility: &QueueVisibility{ 487 UpdateIntervalSeconds: 10, 488 ClusterQueues: &ClusterQueueVisibility{ 489 MaxCount: 0, 490 }, 491 }, 492 MultiKueue: defaultMultiKueue, 493 }, 494 }, 495 "multiKueue": { 496 original: &Configuration{ 497 InternalCertManagement: &InternalCertManagement{ 498 Enable: ptr.To(false), 499 }, 500 MultiKueue: &MultiKueue{ 501 GCInterval: &metav1.Duration{Duration: time.Second}, 502 Origin: ptr.To("multikueue-manager1"), 503 }, 504 }, 505 want: &Configuration{ 506 Namespace: ptr.To(DefaultNamespace), 507 ControllerManager: defaultCtrlManagerConfigurationSpec, 508 InternalCertManagement: &InternalCertManagement{ 509 Enable: ptr.To(false), 510 }, 511 ClientConnection: defaultClientConnection, 512 Integrations: defaultIntegrations, 513 QueueVisibility: defaultQueueVisibility, 514 MultiKueue: &MultiKueue{ 515 GCInterval: &metav1.Duration{Duration: time.Second}, 516 Origin: ptr.To("multikueue-manager1"), 517 }, 518 }, 519 }, 520 "multiKueue GCInterval 0": { 521 original: &Configuration{ 522 InternalCertManagement: &InternalCertManagement{ 523 Enable: ptr.To(false), 524 }, 525 MultiKueue: &MultiKueue{ 526 GCInterval: &metav1.Duration{}, 527 Origin: ptr.To("multikueue-manager1"), 528 }, 529 }, 530 want: &Configuration{ 531 Namespace: ptr.To(DefaultNamespace), 532 ControllerManager: defaultCtrlManagerConfigurationSpec, 533 InternalCertManagement: &InternalCertManagement{ 534 Enable: ptr.To(false), 535 }, 536 ClientConnection: defaultClientConnection, 537 Integrations: defaultIntegrations, 538 QueueVisibility: defaultQueueVisibility, 539 MultiKueue: &MultiKueue{ 540 GCInterval: &metav1.Duration{}, 541 Origin: ptr.To("multikueue-manager1"), 542 }, 543 }, 544 }, 545 } 546 547 for name, tc := range testCases { 548 t.Run(name, func(t *testing.T) { 549 SetDefaults_Configuration(tc.original) 550 if diff := cmp.Diff(tc.want, tc.original); diff != "" { 551 t.Errorf("unexpected error (-want,+got):\n%s", diff) 552 } 553 }) 554 } 555 }