-
Notifications
You must be signed in to change notification settings - Fork 298
/
deployment.go
176 lines (156 loc) · 5.91 KB
/
deployment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package scheduler
import (
"fmt"
"path"
"strings"
hyperv1 "github.com/openshift/hypershift/api/hypershift/v1beta1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
configv1 "github.com/openshift/api/config/v1"
"github.com/openshift/hypershift/control-plane-operator/controllers/hostedcontrolplane/kas"
"github.com/openshift/hypershift/control-plane-operator/controllers/hostedcontrolplane/manifests"
"github.com/openshift/hypershift/support/config"
"github.com/openshift/hypershift/support/util"
)
const (
schedulerSecurePort = 10259
schedulerConfigHashAnnotation = "hypershift.openshift.io/scheduler-config-hash"
)
var (
volumeMounts = util.PodVolumeMounts{
schedulerContainerMain().Name: {
schedulerVolumeConfig().Name: "/etc/kubernetes/config",
schedulerVolumeCertWorkDir().Name: "/var/run/kubernetes",
schedulerVolumeKubeconfig().Name: "/etc/kubernetes/kubeconfig",
},
}
name = "kube-scheduler"
labels = map[string]string{
"app": name,
hyperv1.ControlPlaneComponent: name,
}
// The selector needs to be invariant for the lifecycle of the project as it's an immutable field,
// otherwise changing would prevent an upgrade from happening.
selector = map[string]string{
"app": name,
}
)
func ReconcileDeployment(deployment *appsv1.Deployment, ownerRef config.OwnerRef, config config.DeploymentConfig, image string, featureGates []string, policy configv1.ConfigMapNameReference, availabilityProberImage string, ciphers []string, tlsVersion string, disableProfiling bool, schedulerConfig *corev1.ConfigMap, platformType hyperv1.PlatformType) error {
ownerRef.ApplyTo(deployment)
// preserve existing resource requirements for main scheduler container
mainContainer := util.FindContainer(schedulerContainerMain().Name, deployment.Spec.Template.Spec.Containers)
if mainContainer != nil {
config.SetContainerResourcesIfPresent(mainContainer)
}
maxSurge := intstr.FromInt(3)
maxUnavailable := intstr.FromInt(1)
s := deployment.Spec.Selector
if deployment.Spec.Selector == nil {
s = &metav1.LabelSelector{
MatchLabels: selector,
}
}
deployment.Spec.Selector = s
deployment.Spec.Strategy = appsv1.DeploymentStrategy{
Type: appsv1.RollingUpdateDeploymentStrategyType,
RollingUpdate: &appsv1.RollingUpdateDeployment{
MaxSurge: &maxSurge,
MaxUnavailable: &maxUnavailable,
},
}
deployment.Spec.Template = corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
Annotations: map[string]string{
schedulerConfigHashAnnotation: util.ComputeHash(schedulerConfig.Data[KubeSchedulerConfigKey]),
},
},
Spec: corev1.PodSpec{
AutomountServiceAccountToken: pointer.Bool(false),
Containers: []corev1.Container{
util.BuildContainer(schedulerContainerMain(), buildSchedulerContainerMain(image, deployment.Namespace, featureGates, policy, ciphers, tlsVersion, disableProfiling)),
},
Volumes: []corev1.Volume{
util.BuildVolume(schedulerVolumeConfig(), buildSchedulerVolumeConfig),
util.BuildVolume(schedulerVolumeCertWorkDir(), buildSchedulerVolumeCertWorkDir),
util.BuildVolume(schedulerVolumeKubeconfig(), buildSchedulerVolumeKubeconfig),
},
},
}
config.ApplyTo(deployment)
util.AvailabilityProber(kas.InClusterKASReadyURL(platformType), availabilityProberImage, &deployment.Spec.Template.Spec)
return nil
}
func schedulerContainerMain() *corev1.Container {
return &corev1.Container{
Name: "kube-scheduler",
}
}
func buildSchedulerContainerMain(image, namespace string, featureGates []string, policy configv1.ConfigMapNameReference, cipherSuites []string, tlsVersion string, disableProfiling bool) func(*corev1.Container) {
return func(c *corev1.Container) {
kubeConfigPath := path.Join(volumeMounts.Path(schedulerContainerMain().Name, schedulerVolumeKubeconfig().Name), kas.KubeconfigKey)
configPath := path.Join(volumeMounts.Path(schedulerContainerMain().Name, schedulerVolumeConfig().Name), KubeSchedulerConfigKey)
certWorkDir := volumeMounts.Path(schedulerContainerMain().Name, schedulerVolumeCertWorkDir().Name)
c.Image = image
c.Command = []string{
"hyperkube",
"kube-scheduler",
}
c.Args = []string{
fmt.Sprintf("--config=%s", configPath),
fmt.Sprintf("--cert-dir=%s", certWorkDir),
fmt.Sprintf("--secure-port=%d", schedulerSecurePort),
fmt.Sprintf("--authentication-kubeconfig=%s", kubeConfigPath),
fmt.Sprintf("--authorization-kubeconfig=%s", kubeConfigPath),
"-v=2",
}
for _, f := range featureGates {
c.Args = append(c.Args, fmt.Sprintf("--feature-gates=%s", f))
}
if len(policy.Name) > 0 {
c.Args = append(c.Args, fmt.Sprintf("--policy-config-map=%s", policy.Name))
c.Args = append(c.Args, fmt.Sprintf("--policy-config-namespace=%s", namespace))
}
if len(cipherSuites) != 0 {
c.Args = append(c.Args, fmt.Sprintf("--tls-cipher-suites=%s", strings.Join(cipherSuites, ",")))
}
if tlsVersion != "" {
c.Args = append(c.Args, fmt.Sprintf("--tls-min-version=%s", tlsVersion))
}
if disableProfiling {
c.Args = append(c.Args, "--profiling=false")
}
c.VolumeMounts = volumeMounts.ContainerMounts(c.Name)
}
}
func schedulerVolumeConfig() *corev1.Volume {
return &corev1.Volume{
Name: "scheduler-config",
}
}
func buildSchedulerVolumeConfig(v *corev1.Volume) {
v.ConfigMap = &corev1.ConfigMapVolumeSource{}
v.ConfigMap.Name = manifests.SchedulerConfig("").Name
}
func schedulerVolumeCertWorkDir() *corev1.Volume {
return &corev1.Volume{
Name: "cert-work",
}
}
func buildSchedulerVolumeCertWorkDir(v *corev1.Volume) {
v.EmptyDir = &corev1.EmptyDirVolumeSource{}
}
func schedulerVolumeKubeconfig() *corev1.Volume {
return &corev1.Volume{
Name: "kubeconfig",
}
}
func buildSchedulerVolumeKubeconfig(v *corev1.Volume) {
v.Secret = &corev1.SecretVolumeSource{
SecretName: manifests.SchedulerKubeconfigSecret("").Name,
DefaultMode: pointer.Int32(0640),
}
}