sigs.k8s.io/kueue@v0.6.2/apis/config/v1beta1/defaults.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  	"os"
    21  	"strings"
    22  	"time"
    23  
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/client-go/tools/leaderelection/resourcelock"
    26  	configv1alpha1 "k8s.io/component-base/config/v1alpha1"
    27  	"k8s.io/utils/ptr"
    28  )
    29  
    30  const (
    31  	DefaultNamespace                                    = "kueue-system"
    32  	DefaultWebhookServiceName                           = "kueue-webhook-service"
    33  	DefaultWebhookSecretName                            = "kueue-webhook-server-cert"
    34  	DefaultWebhookPort                                  = 9443
    35  	DefaultHealthProbeBindAddress                       = ":8081"
    36  	DefaultMetricsBindAddress                           = ":8080"
    37  	DefaultLeaderElectionID                             = "c1f6bfd2.kueue.x-k8s.io"
    38  	DefaultLeaderElectionLeaseDuration                  = 15 * time.Second
    39  	DefaultLeaderElectionRenewDeadline                  = 10 * time.Second
    40  	DefaultLeaderElectionRetryPeriod                    = 2 * time.Second
    41  	DefaultClientConnectionQPS                  float32 = 20.0
    42  	DefaultClientConnectionBurst                int32   = 30
    43  	defaultPodsReadyTimeout                             = 5 * time.Minute
    44  	DefaultQueueVisibilityUpdateIntervalSeconds int32   = 5
    45  	DefaultClusterQueuesMaxCount                int32   = 10
    46  	defaultJobFrameworkName                             = "batch/job"
    47  	DefaultMultiKueueGCInterval                         = time.Minute
    48  	DefaultMultiKueueOrigin                             = "multikueue"
    49  )
    50  
    51  func getOperatorNamespace() string {
    52  	if data, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
    53  		if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
    54  			return ns
    55  		}
    56  	}
    57  	return DefaultNamespace
    58  }
    59  
    60  // SetDefaults_Configuration sets default values for ComponentConfig.
    61  func SetDefaults_Configuration(cfg *Configuration) {
    62  	if cfg.Namespace == nil {
    63  		cfg.Namespace = ptr.To(getOperatorNamespace())
    64  	}
    65  	if cfg.Webhook.Port == nil {
    66  		cfg.Webhook.Port = ptr.To(DefaultWebhookPort)
    67  	}
    68  	if len(cfg.Metrics.BindAddress) == 0 {
    69  		cfg.Metrics.BindAddress = DefaultMetricsBindAddress
    70  	}
    71  	if len(cfg.Health.HealthProbeBindAddress) == 0 {
    72  		cfg.Health.HealthProbeBindAddress = DefaultHealthProbeBindAddress
    73  	}
    74  
    75  	if cfg.LeaderElection == nil {
    76  		cfg.LeaderElection = &configv1alpha1.LeaderElectionConfiguration{}
    77  	}
    78  	if len(cfg.LeaderElection.ResourceName) == 0 {
    79  		cfg.LeaderElection.ResourceName = DefaultLeaderElectionID
    80  	}
    81  	if len(cfg.LeaderElection.ResourceLock) == 0 {
    82  		// Default to Lease as component-base still defaults to endpoint resources
    83  		// until core components migrate to using Leases. See k/k #80289 for more details.
    84  		cfg.LeaderElection.ResourceLock = resourcelock.LeasesResourceLock
    85  	}
    86  	// Use the default LeaderElectionConfiguration options
    87  	configv1alpha1.RecommendedDefaultLeaderElectionConfiguration(cfg.LeaderElection)
    88  
    89  	if cfg.InternalCertManagement == nil {
    90  		cfg.InternalCertManagement = &InternalCertManagement{}
    91  	}
    92  	if cfg.InternalCertManagement.Enable == nil {
    93  		cfg.InternalCertManagement.Enable = ptr.To(true)
    94  	}
    95  	if *cfg.InternalCertManagement.Enable {
    96  		if cfg.InternalCertManagement.WebhookServiceName == nil {
    97  			cfg.InternalCertManagement.WebhookServiceName = ptr.To(DefaultWebhookServiceName)
    98  		}
    99  		if cfg.InternalCertManagement.WebhookSecretName == nil {
   100  			cfg.InternalCertManagement.WebhookSecretName = ptr.To(DefaultWebhookSecretName)
   101  		}
   102  	}
   103  	if cfg.ClientConnection == nil {
   104  		cfg.ClientConnection = &ClientConnection{}
   105  	}
   106  	if cfg.ClientConnection.QPS == nil {
   107  		cfg.ClientConnection.QPS = ptr.To(DefaultClientConnectionQPS)
   108  	}
   109  	if cfg.ClientConnection.Burst == nil {
   110  		cfg.ClientConnection.Burst = ptr.To(DefaultClientConnectionBurst)
   111  	}
   112  	if cfg.WaitForPodsReady != nil {
   113  		if cfg.WaitForPodsReady.Timeout == nil {
   114  			cfg.WaitForPodsReady.Timeout = &metav1.Duration{Duration: defaultPodsReadyTimeout}
   115  		}
   116  		if cfg.WaitForPodsReady.BlockAdmission == nil {
   117  			defaultBlockAdmission := true
   118  			if !cfg.WaitForPodsReady.Enable {
   119  				defaultBlockAdmission = false
   120  			}
   121  			cfg.WaitForPodsReady.BlockAdmission = &defaultBlockAdmission
   122  		}
   123  		if cfg.WaitForPodsReady.RequeuingStrategy == nil || cfg.WaitForPodsReady.RequeuingStrategy.Timestamp == nil {
   124  			cfg.WaitForPodsReady.RequeuingStrategy = &RequeuingStrategy{
   125  				Timestamp: ptr.To(EvictionTimestamp),
   126  			}
   127  		}
   128  	}
   129  	if cfg.Integrations == nil {
   130  		cfg.Integrations = &Integrations{}
   131  	}
   132  	if cfg.Integrations.Frameworks == nil {
   133  		cfg.Integrations.Frameworks = []string{defaultJobFrameworkName}
   134  	}
   135  	if cfg.QueueVisibility == nil {
   136  		cfg.QueueVisibility = &QueueVisibility{}
   137  	}
   138  	if cfg.QueueVisibility.UpdateIntervalSeconds == 0 {
   139  		cfg.QueueVisibility.UpdateIntervalSeconds = DefaultQueueVisibilityUpdateIntervalSeconds
   140  	}
   141  	if cfg.QueueVisibility.ClusterQueues == nil {
   142  		cfg.QueueVisibility.ClusterQueues = &ClusterQueueVisibility{
   143  			MaxCount: DefaultClusterQueuesMaxCount,
   144  		}
   145  	}
   146  
   147  	if cfg.Integrations.PodOptions == nil {
   148  		cfg.Integrations.PodOptions = &PodIntegrationOptions{}
   149  	}
   150  
   151  	if cfg.Integrations.PodOptions.NamespaceSelector == nil {
   152  		matchExpressionsValues := []string{"kube-system", *cfg.Namespace}
   153  
   154  		cfg.Integrations.PodOptions.NamespaceSelector = &metav1.LabelSelector{
   155  			MatchExpressions: []metav1.LabelSelectorRequirement{
   156  				{
   157  					Key:      "kubernetes.io/metadata.name",
   158  					Operator: metav1.LabelSelectorOpNotIn,
   159  					Values:   matchExpressionsValues,
   160  				},
   161  			},
   162  		}
   163  	}
   164  
   165  	if cfg.Integrations.PodOptions.PodSelector == nil {
   166  		cfg.Integrations.PodOptions.PodSelector = &metav1.LabelSelector{}
   167  	}
   168  
   169  	if cfg.MultiKueue == nil {
   170  		cfg.MultiKueue = &MultiKueue{}
   171  	}
   172  	if cfg.MultiKueue.GCInterval == nil {
   173  		cfg.MultiKueue.GCInterval = &metav1.Duration{Duration: DefaultMultiKueueGCInterval}
   174  	}
   175  	if ptr.Deref(cfg.MultiKueue.Origin, "") == "" {
   176  		cfg.MultiKueue.Origin = ptr.To(DefaultMultiKueueOrigin)
   177  	}
   178  }