forked from gardener-attic/gardener-extensions
/
ensurer.go
212 lines (186 loc) · 8.68 KB
/
ensurer.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controlplane
import (
"bytes"
"context"
"fmt"
"regexp"
"github.com/gardener/gardener-extensions/controllers/provider-gcp/pkg/gcp"
"github.com/gardener/gardener-extensions/controllers/provider-gcp/pkg/internal"
"github.com/gardener/gardener-extensions/pkg/webhook/controlplane"
"github.com/gardener/gardener-extensions/pkg/webhook/controlplane/genericmutator"
"github.com/coreos/go-systemd/unit"
gardencorev1alpha1 "github.com/gardener/gardener/pkg/apis/core/v1alpha1"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// NewEnsurer creates a new controlplane ensurer.
func NewEnsurer(logger logr.Logger) genericmutator.Ensurer {
return &ensurer{
logger: logger.WithName("gcp-controlplane-ensurer"),
}
}
type ensurer struct {
genericmutator.NoopEnsurer
client client.Client
logger logr.Logger
}
// InjectClient injects the given client into the ensurer.
func (e *ensurer) InjectClient(client client.Client) error {
e.client = client
return nil
}
// EnsureKubeAPIServerDeployment ensures that the kube-apiserver deployment conforms to the provider requirements.
func (e *ensurer) EnsureKubeAPIServerDeployment(ctx context.Context, dep *appsv1.Deployment) error {
template := &dep.Spec.Template
ps := &template.Spec
if c := controlplane.ContainerWithName(ps.Containers, "kube-apiserver"); c != nil {
ensureKubeAPIServerCommandLineArgs(c)
ensureEnvVars(c)
ensureVolumeMounts(c)
}
ensureVolumes(ps)
return e.ensureChecksumAnnotations(ctx, &dep.Spec.Template, dep.Namespace)
}
// EnsureKubeControllerManagerDeployment ensures that the kube-controller-manager deployment conforms to the provider requirements.
func (e *ensurer) EnsureKubeControllerManagerDeployment(ctx context.Context, dep *appsv1.Deployment) error {
template := &dep.Spec.Template
ps := &template.Spec
if c := controlplane.ContainerWithName(ps.Containers, "kube-controller-manager"); c != nil {
ensureKubeControllerManagerCommandLineArgs(c)
ensureEnvVars(c)
ensureVolumeMounts(c)
}
ensureKubeControllerManagerAnnotations(template)
ensureVolumes(ps)
return e.ensureChecksumAnnotations(ctx, &dep.Spec.Template, dep.Namespace)
}
func ensureKubeAPIServerCommandLineArgs(c *corev1.Container) {
c.Command = controlplane.EnsureStringWithPrefix(c.Command, "--cloud-provider=", "gce")
c.Command = controlplane.EnsureStringWithPrefix(c.Command, "--cloud-config=",
"/etc/kubernetes/cloudprovider/cloudprovider.conf")
c.Command = controlplane.EnsureStringWithPrefixContains(c.Command, "--enable-admission-plugins=",
"PersistentVolumeLabel", ",")
c.Command = controlplane.EnsureNoStringWithPrefixContains(c.Command, "--disable-admission-plugins=",
"PersistentVolumeLabel", ",")
}
func ensureKubeControllerManagerCommandLineArgs(c *corev1.Container) {
c.Command = controlplane.EnsureStringWithPrefix(c.Command, "--cloud-provider=", "external")
c.Command = controlplane.EnsureStringWithPrefix(c.Command, "--cloud-config=",
"/etc/kubernetes/cloudprovider/cloudprovider.conf")
c.Command = controlplane.EnsureStringWithPrefix(c.Command, "--external-cloud-volume-plugin=", "gce")
}
func ensureKubeControllerManagerAnnotations(t *corev1.PodTemplateSpec) {
t.Labels = controlplane.EnsureAnnotationOrLabel(t.Labels, gardencorev1alpha1.LabelNetworkPolicyToPublicNetworks, gardencorev1alpha1.LabelNetworkPolicyAllowed)
t.Labels = controlplane.EnsureAnnotationOrLabel(t.Labels, gardencorev1alpha1.LabelNetworkPolicyToPrivateNetworks, gardencorev1alpha1.LabelNetworkPolicyAllowed)
t.Labels = controlplane.EnsureAnnotationOrLabel(t.Labels, gardencorev1alpha1.LabelNetworkPolicyToBlockedCIDRs, gardencorev1alpha1.LabelNetworkPolicyAllowed)
}
var (
credentialsEnvVar = corev1.EnvVar{
Name: "GOOGLE_APPLICATION_CREDENTIALS",
Value: fmt.Sprintf("/srv/cloudprovider/%s", gcp.ServiceAccountJSONField),
}
)
func ensureEnvVars(c *corev1.Container) {
c.Env = controlplane.EnsureEnvVarWithName(c.Env, credentialsEnvVar)
}
var (
cloudProviderConfigVolumeMount = corev1.VolumeMount{
Name: internal.CloudProviderConfigName,
MountPath: "/etc/kubernetes/cloudprovider",
}
cloudProviderSecretVolumeMount = corev1.VolumeMount{
Name: gardencorev1alpha1.SecretNameCloudProvider,
MountPath: "/srv/cloudprovider",
}
cloudProviderConfigVolume = corev1.Volume{
Name: internal.CloudProviderConfigName,
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{Name: internal.CloudProviderConfigName},
},
},
}
cloudProviderSecretVolume = corev1.Volume{
Name: gardencorev1alpha1.SecretNameCloudProvider,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: gardencorev1alpha1.SecretNameCloudProvider,
},
},
}
)
func ensureVolumeMounts(c *corev1.Container) {
c.VolumeMounts = controlplane.EnsureVolumeMountWithName(c.VolumeMounts, cloudProviderConfigVolumeMount)
c.VolumeMounts = controlplane.EnsureVolumeMountWithName(c.VolumeMounts, cloudProviderSecretVolumeMount)
}
func ensureVolumes(ps *corev1.PodSpec) {
ps.Volumes = controlplane.EnsureVolumeWithName(ps.Volumes, cloudProviderConfigVolume)
ps.Volumes = controlplane.EnsureVolumeWithName(ps.Volumes, cloudProviderSecretVolume)
}
func (e *ensurer) ensureChecksumAnnotations(ctx context.Context, template *corev1.PodTemplateSpec, namespace string) error {
if err := controlplane.EnsureSecretChecksumAnnotation(ctx, template, e.client, namespace, gardencorev1alpha1.SecretNameCloudProvider); err != nil {
return err
}
return controlplane.EnsureConfigMapChecksumAnnotation(ctx, template, e.client, namespace, internal.CloudProviderConfigName)
}
// EnsureKubeletServiceUnitOptions ensures that the kubelet.service unit options conform to the provider requirements.
func (e *ensurer) EnsureKubeletServiceUnitOptions(ctx context.Context, opts []*unit.UnitOption) ([]*unit.UnitOption, error) {
if opt := controlplane.UnitOptionWithSectionAndName(opts, "Service", "ExecStart"); opt != nil {
command := controlplane.DeserializeCommandLine(opt.Value)
command = ensureKubeletCommandLineArgs(command)
opt.Value = controlplane.SerializeCommandLine(command, 1, " \\\n ")
}
opts = controlplane.EnsureUnitOption(opts, &unit.UnitOption{
Section: "Service",
Name: "ExecStartPre",
Value: `/bin/sh -c 'hostnamectl set-hostname $(cat /etc/hostname | cut -d '.' -f 1)'`,
})
return opts, nil
}
func ensureKubeletCommandLineArgs(command []string) []string {
command = controlplane.EnsureStringWithPrefix(command, "--cloud-provider=", "gce")
return command
}
// EnsureKubeletConfiguration ensures that the kubelet configuration conforms to the provider requirements.
func (e *ensurer) EnsureKubeletConfiguration(ctx context.Context, kubeletConfig *kubeletconfigv1beta1.KubeletConfiguration) error {
// Make sure CSI-related feature gates are not enabled
// TODO Leaving these enabled shouldn't do any harm, perhaps remove this code when properly tested?
delete(kubeletConfig.FeatureGates, "VolumeSnapshotDataSource")
delete(kubeletConfig.FeatureGates, "CSINodeInfo")
delete(kubeletConfig.FeatureGates, "CSIDriverRegistry")
return nil
}
var regexFindProperty = regexp.MustCompile("net.ipv4.ip_forward[[:space:]]*=[[:space:]]*([[:alnum:]]+)")
// EnsureKubernetesGeneralConfiguration ensures that the kubernetes general configuration conforms to the provider requirements.
func (e *ensurer) EnsureKubernetesGeneralConfiguration(ctx context.Context, data *string) error {
// If the needed property exists, ensure the correct value
if regexFindProperty.MatchString(*data) {
res := regexFindProperty.ReplaceAll([]byte(*data), []byte("net.ipv4.ip_forward = 1"))
*data = string(res)
return nil
}
// If the property do not exist, append it in the end of the string
buf := bytes.Buffer{}
buf.WriteString(*data)
buf.WriteString("\n")
buf.WriteString("# GCE specific settings\n")
buf.WriteString("net.ipv4.ip_forward = 1")
*data = buf.String()
return nil
}