Skip to content

Commit d7c1dad

Browse files
authored
feat: add component config (kubernetes-sigs#609)
* feat: add component config Signed-off-by: rainfd <[email protected]> * feat: override the file configuration with the command line flag Signed-off-by: rainfd <[email protected]> * fix: ignore network configuration tested by config --------- Signed-off-by: rainfd <[email protected]>
1 parent 3d49ae7 commit d7c1dad

20 files changed

+1828
-30
lines changed

PROJECT

+8
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ resources:
2121
defaulting: true
2222
validation: true
2323
webhookVersion: v1
24+
- api:
25+
crdVersion: v1
26+
namespaced: true
27+
domain: jobset.x-k8s.io
28+
group: jobset
29+
kind: Configuration
30+
path: sigs.k8s.io/jobset/api/config/v1alpha1
31+
version: v1alpha1
2432
version: "3"
+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
Copyright 2023 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 v1alpha1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
configv1alpha1 "k8s.io/component-base/config/v1alpha1"
22+
)
23+
24+
// +k8s:defaulter-gen=true
25+
// +kubebuilder:object:root=true
26+
27+
// Configuration is the Schema for the configurations API
28+
type Configuration struct {
29+
metav1.TypeMeta `json:",inline"`
30+
31+
// Namespace is the namespace in which jobset controller is deployed.
32+
// It is used as part of DNSName of the webhook Service.
33+
// If not set, the value is set from the file /var/run/secrets/kubernetes.io/serviceaccount/namespace
34+
// If the file doesn't exist, default value is kueue-system.
35+
Namespace *string `json:"namespace,omitempty"`
36+
37+
// ControllerManager returns the configurations for controllers
38+
ControllerManager `json:",inline"`
39+
40+
// InternalCertManagerment is configuration for internalCertManagerment
41+
InternalCertManagement *InternalCertManagement `json:"internalCertManagement,omitempty"`
42+
43+
ClientConnection *ClientConnection `json:"clientConnection,omitempty"`
44+
}
45+
46+
type ControllerManager struct {
47+
// Webhook contains the controllers webhook configuration
48+
// +optional
49+
Webhook ControllerWebhook `json:"webhook,omitempty"`
50+
51+
// LeaderElection is the LeaderElection config to be used when configuring
52+
// the manager.Manager leader election
53+
// +optional
54+
LeaderElection *configv1alpha1.LeaderElectionConfiguration `json:"leaderElection,omitempty"`
55+
56+
// Metrics contains the controller metrics configuration
57+
// +optional
58+
Metrics ControllerMetrics `json:"metrics,omitempty"`
59+
60+
// Health contains the controller health configuration
61+
// +optional
62+
Health ControllerHealth `json:"health,omitempty"`
63+
}
64+
65+
// ControllerWebhook defines the webhook server for the controller.
66+
type ControllerWebhook struct {
67+
// Port is the port that the webhook server serves at.
68+
// It is used to set webhook.Server.Port.
69+
// +optional
70+
Port *int `json:"port,omitempty"`
71+
72+
// Host is the hostname that the webhook server binds to.
73+
// It is used to set webhook.Server.Host.
74+
// +optional
75+
Host string `json:"host,omitempty"`
76+
77+
// CertDir is the directory that contains the server key and certificate.
78+
// if not set, webhook server would look up the server key and certificate in
79+
// {TempDir}/k8s-webhook-server/serving-certs. The server key and certificate
80+
// must be named tls.key and tls.crt, respectively.
81+
// +optional
82+
CertDir string `json:"certDir,omitempty"`
83+
}
84+
85+
// ControllerMetrics defines the metrics configs.
86+
type ControllerMetrics struct {
87+
// BindAddress is the TCP address that the controller should bind to
88+
// for serving prometheus metrics.
89+
// It can be set to "0" to disable the metrics serving.
90+
// +optional
91+
BindAddress string `json:"bindAddress,omitempty"`
92+
}
93+
94+
// ControllerHealth defines the health configs.
95+
type ControllerHealth struct {
96+
// HealthProbeBindAddress is the TCP address that the controller should bind to
97+
// for serving health probes
98+
// It can be set to "0" or "" to disable serving the health probe.
99+
// +optional
100+
HealthProbeBindAddress string `json:"healthProbeBindAddress,omitempty"`
101+
102+
// ReadinessEndpointName, defaults to "readyz"
103+
// +optional
104+
ReadinessEndpointName string `json:"readinessEndpointName,omitempty"`
105+
106+
// LivenessEndpointName, defaults to "healthz"
107+
// +optional
108+
LivenessEndpointName string `json:"livenessEndpointName,omitempty"`
109+
}
110+
111+
type InternalCertManagement struct {
112+
// Enable controls whether to enable internal cert management or not.
113+
// Defaults to true. If you want to use a third-party management, e.g. cert-manager,
114+
// set it to false. See the user guide for more information.
115+
Enable *bool `json:"enable,omitempty"`
116+
117+
// WebhookServiceName is the name of the Service used as part of the DNSName.
118+
// Defaults to jobset-webhook-service.
119+
WebhookServiceName *string `json:"webhookServiceName,omitempty"`
120+
121+
// WebhookSecretName is the name of the Secret used to store CA and server certs.
122+
// Defaults to jobset-webhook-server-cert.
123+
WebhookSecretName *string `json:"webhookSecretName,omitempty"`
124+
}
125+
126+
type ClientConnection struct {
127+
// QPS controls the number of queries per second allowed for K8S api server
128+
// connection.
129+
QPS *float32 `json:"qps,omitempty"`
130+
131+
// Burst allows extra queries to accumulate when a client is exceeding its rate.
132+
Burst *int32 `json:"burst,omitempty"`
133+
}

api/config/v1alpha1/defaults.go

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 v1alpha1
18+
19+
import (
20+
"os"
21+
"strings"
22+
"time"
23+
24+
configv1alpha1 "k8s.io/component-base/config/v1alpha1"
25+
"k8s.io/utils/ptr"
26+
)
27+
28+
const (
29+
DefaultNamespace = "jobset-system"
30+
DefaultWebhookServiceName = "jobset-webhook-service"
31+
DefaultWebhookSecretName = "jobset-webhook-server-cert"
32+
DefaultWebhookPort = 9443
33+
DefaultHealthProbeBindAddress = ":8081"
34+
DefaultMetricsBindAddress = ":8080"
35+
DefaultLeaderElectionID = "6d4f6a47.jobset.x-k8s.io"
36+
DefaultLeaderElectionLeaseDuration = 15 * time.Second
37+
DefaultLeaderElectionRenewDeadline = 10 * time.Second
38+
DefaultLeaderElectionRetryPeriod = 2 * time.Second
39+
DefaultResourceLock = "leases"
40+
DefaultClientConnectionQPS float32 = 500
41+
DefaultClientConnectionBurst int32 = 500
42+
)
43+
44+
func getOperatorNamespace() string {
45+
if data, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
46+
if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
47+
return ns
48+
}
49+
}
50+
return DefaultNamespace
51+
}
52+
53+
// SetDefaults_Configuration sets default values for ComponentConfig.
54+
//
55+
//nolint:revive // format required by generated code for defaulting
56+
func SetDefaults_Configuration(cfg *Configuration) {
57+
if cfg.Namespace == nil {
58+
cfg.Namespace = ptr.To(getOperatorNamespace())
59+
}
60+
if cfg.Webhook.Port == nil {
61+
cfg.Webhook.Port = ptr.To(DefaultWebhookPort)
62+
}
63+
if len(cfg.Metrics.BindAddress) == 0 {
64+
cfg.Metrics.BindAddress = DefaultMetricsBindAddress
65+
}
66+
if len(cfg.Health.HealthProbeBindAddress) == 0 {
67+
cfg.Health.HealthProbeBindAddress = DefaultHealthProbeBindAddress
68+
}
69+
70+
if cfg.LeaderElection == nil {
71+
cfg.LeaderElection = &configv1alpha1.LeaderElectionConfiguration{}
72+
}
73+
if len(cfg.LeaderElection.ResourceName) == 0 {
74+
cfg.LeaderElection.ResourceName = DefaultLeaderElectionID
75+
}
76+
if len(cfg.LeaderElection.ResourceLock) == 0 {
77+
cfg.LeaderElection.ResourceLock = DefaultResourceLock
78+
}
79+
// Use the default LeaderElectionConfiguration options
80+
configv1alpha1.RecommendedDefaultLeaderElectionConfiguration(cfg.LeaderElection)
81+
82+
if cfg.InternalCertManagement == nil {
83+
cfg.InternalCertManagement = &InternalCertManagement{}
84+
}
85+
if cfg.InternalCertManagement.Enable == nil {
86+
cfg.InternalCertManagement.Enable = ptr.To(true)
87+
}
88+
if *cfg.InternalCertManagement.Enable {
89+
if cfg.InternalCertManagement.WebhookServiceName == nil {
90+
cfg.InternalCertManagement.WebhookServiceName = ptr.To(DefaultWebhookServiceName)
91+
}
92+
if cfg.InternalCertManagement.WebhookSecretName == nil {
93+
cfg.InternalCertManagement.WebhookSecretName = ptr.To(DefaultWebhookSecretName)
94+
}
95+
}
96+
if cfg.ClientConnection == nil {
97+
cfg.ClientConnection = &ClientConnection{}
98+
}
99+
if cfg.ClientConnection.QPS == nil {
100+
cfg.ClientConnection.QPS = ptr.To(DefaultClientConnectionQPS)
101+
}
102+
if cfg.ClientConnection.Burst == nil {
103+
cfg.ClientConnection.Burst = ptr.To(DefaultClientConnectionBurst)
104+
}
105+
}

0 commit comments

Comments
 (0)