-
Notifications
You must be signed in to change notification settings - Fork 303
/
deployment.go
146 lines (128 loc) · 4.55 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
package ocm
import (
"fmt"
"path"
hyperv1 "github.com/openshift/hypershift/api/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"
"github.com/openshift/hypershift/control-plane-operator/controllers/hostedcontrolplane/common"
"github.com/openshift/hypershift/control-plane-operator/controllers/hostedcontrolplane/manifests"
"github.com/openshift/hypershift/support/config"
"github.com/openshift/hypershift/support/util"
)
const (
configHashAnnotation = "openshift-controller-manager.hypershift.openshift.io/config-hash"
servingPort int32 = 8443
)
var (
volumeMounts = util.PodVolumeMounts{
ocmContainerMain().Name: {
ocmVolumeConfig().Name: "/etc/kubernetes/config",
ocmVolumeServingCert().Name: "/etc/kubernetes/certs",
ocmVolumeKubeconfig().Name: "/etc/kubernetes/secrets/svc-kubeconfig",
common.VolumeTotalClientCA().Name: "/etc/kubernetes/client-ca",
},
}
)
func openShiftControllerManagerLabels() map[string]string {
return map[string]string{
"app": "openshift-controller-manager",
hyperv1.ControlPlaneComponent: "openshift-controller-manager",
}
}
func ReconcileDeployment(deployment *appsv1.Deployment, ownerRef config.OwnerRef, image string, config *corev1.ConfigMap, deploymentConfig config.DeploymentConfig) error {
configBytes, ok := config.Data[configKey]
if !ok {
return fmt.Errorf("openshift apiserver configuration is not expected to be empty")
}
configHash := util.ComputeHash(configBytes)
// preserve existing resource requirements for main OCM container
mainContainer := util.FindContainer(ocmContainerMain().Name, deployment.Spec.Template.Spec.Containers)
if mainContainer != nil {
deploymentConfig.SetContainerResourcesIfPresent(mainContainer)
}
maxSurge := intstr.FromInt(1)
maxUnavailable := intstr.FromInt(0)
deployment.Spec.Strategy.Type = appsv1.RollingUpdateDeploymentStrategyType
deployment.Spec.Strategy.RollingUpdate = &appsv1.RollingUpdateDeployment{
MaxSurge: &maxSurge,
MaxUnavailable: &maxUnavailable,
}
if deployment.Spec.Selector == nil {
deployment.Spec.Selector = &metav1.LabelSelector{
MatchLabels: openShiftControllerManagerLabels(),
}
}
deployment.Spec.Template.ObjectMeta.Labels = openShiftControllerManagerLabels()
deployment.Spec.Template.ObjectMeta.Annotations = map[string]string{
configHashAnnotation: configHash,
}
deployment.Spec.Template.Spec.AutomountServiceAccountToken = pointer.BoolPtr(false)
deployment.Spec.Template.Spec.Containers = []corev1.Container{
util.BuildContainer(ocmContainerMain(), buildOCMContainerMain(image)),
}
deployment.Spec.Template.Spec.Volumes = []corev1.Volume{
util.BuildVolume(ocmVolumeConfig(), buildOCMVolumeConfig),
util.BuildVolume(ocmVolumeServingCert(), buildOCMVolumeServingCert),
util.BuildVolume(ocmVolumeKubeconfig(), buildOCMVolumeKubeconfig),
util.BuildVolume(common.VolumeTotalClientCA(), common.BuildVolumeTotalClientCA),
}
deploymentConfig.ApplyTo(deployment)
return nil
}
func ocmContainerMain() *corev1.Container {
return &corev1.Container{
Name: "openshift-controller-manager",
}
}
func buildOCMContainerMain(image string) func(*corev1.Container) {
return func(c *corev1.Container) {
c.Image = image
c.Command = []string{"openshift-controller-manager"}
c.Args = []string{
"start",
"--config",
path.Join(volumeMounts.Path(c.Name, ocmVolumeConfig().Name), configKey),
}
c.VolumeMounts = volumeMounts.ContainerMounts(c.Name)
c.Ports = []corev1.ContainerPort{
{
Name: "https",
ContainerPort: servingPort,
Protocol: corev1.ProtocolTCP,
},
}
}
}
func ocmVolumeConfig() *corev1.Volume {
return &corev1.Volume{
Name: "config",
}
}
func buildOCMVolumeConfig(v *corev1.Volume) {
v.ConfigMap = &corev1.ConfigMapVolumeSource{}
v.ConfigMap.Name = manifests.OpenShiftControllerManagerConfig("").Name
}
func ocmVolumeKubeconfig() *corev1.Volume {
return &corev1.Volume{
Name: "kubeconfig",
}
}
func buildOCMVolumeKubeconfig(v *corev1.Volume) {
v.Secret = &corev1.SecretVolumeSource{}
v.Secret.SecretName = manifests.KASServiceKubeconfigSecret("").Name
v.Secret.DefaultMode = pointer.Int32Ptr(0640)
}
func ocmVolumeServingCert() *corev1.Volume {
return &corev1.Volume{
Name: "serving-cert",
}
}
func buildOCMVolumeServingCert(v *corev1.Volume) {
v.Secret = &corev1.SecretVolumeSource{}
v.Secret.SecretName = manifests.OpenShiftControllerManagerCertSecret("").Name
v.Secret.DefaultMode = pointer.Int32Ptr(0640)
}