Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix feature gate for cluster > 1.22 #226

Merged
merged 1 commit into from
Aug 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix feature gate for cluster > 1.22
  • Loading branch information
schrodit committed Aug 9, 2022
commit 4d8223bb0d4bbb4ef8fe0959db24406b18d5b060
25 changes: 21 additions & 4 deletions pkg/webhook/controlplane/ensurer.go
Original file line number Diff line number Diff line change
@@ -62,7 +62,9 @@ func (e *ensurer) EnsureKubeAPIServerDeployment(ctx context.Context, gctx gconte
}
ps := &new.Spec.Template.Spec
if c := extensionswebhook.ContainerWithName(ps.Containers, "kube-apiserver"); c != nil {
ensureKubeAPIServerCommandLineArgs(c, cluster.Shoot.Spec.Kubernetes.Version)
if err := ensureKubeAPIServerCommandLineArgs(c, cluster.Shoot.Spec.Kubernetes.Version); err != nil {
return err
}
}

keepNodeNetworkEnvVarIfPresentInOldDeployment(new, old, "vpn-seed")
@@ -189,7 +191,7 @@ func appendUniqueFile(files *[]extensionsv1alpha1.File, file extensionsv1alpha1.
*files = append(resFiles, file)
}

func ensureKubeAPIServerCommandLineArgs(c *corev1.Container, k8sVersion string) {
func ensureKubeAPIServerCommandLineArgs(c *corev1.Container, k8sVersion string) error {
// Ensure CSI-related admission plugins
c.Command = extensionswebhook.EnsureNoStringWithPrefixContains(c.Command, "--enable-admission-plugins=",
"PersistentVolumeLabel", ",")
@@ -205,15 +207,19 @@ func ensureKubeAPIServerCommandLineArgs(c *corev1.Container, k8sVersion string)
"CSIDriverRegistry=false", ",")
c.Command = extensionswebhook.EnsureNoStringWithPrefixContains(c.Command, "--feature-gates=",
"KubeletPluginsWatcher=false", ",")
c.Command = extensionswebhook.EnsureStringWithPrefixContains(c.Command, "--feature-gates=",
"VolumeSnapshotDataSource=true", ",")

var err error
if c.Command, err = setFeatureGateInCommandIfSupported(c.Command, "VolumeSnapshotDataSource", k8sVersion, "VolumeSnapshotDataSource=true"); err != nil {
return err
}

if mustSetCSIFeatureGates(k8sVersion) {
c.Command = extensionswebhook.EnsureStringWithPrefixContains(c.Command, "--feature-gates=",
"CSINodeInfo=true", ",")
c.Command = extensionswebhook.EnsureStringWithPrefixContains(c.Command, "--feature-gates=",
"CSIDriverRegistry=true", ",")
}
return nil
}

func mustSetCSIFeatureGates(k8sVersion string) bool {
@@ -282,3 +288,14 @@ func setFeatureGateIfSupported(featureGates map[string]bool, featureGate, versio
}
return nil
}

func setFeatureGateInCommandIfSupported(command []string, featureGate, k8sVersion, value string) ([]string, error) {
isSupported, err := featuregatesvalidation.IsFeatureGateSupported(featureGate, k8sVersion)
if err != nil {
return nil, err
}
if isSupported {
return extensionswebhook.EnsureStringWithPrefixContains(command, "--feature-gates=", value, ","), nil
}
return extensionswebhook.EnsureNoStringWithPrefixContains(command, "--feature-gates=", value, ","), nil
}
60 changes: 59 additions & 1 deletion pkg/webhook/controlplane/ensurer_test.go
Original file line number Diff line number Diff line change
@@ -87,6 +87,20 @@ var _ = Describe("Ensurer", func() {
},
},
)
eContextK8s122 = gcontext.NewInternalGardenContext(
&extensionscontroller.Cluster{
Shoot: &gardencorev1beta1.Shoot{
Spec: gardencorev1beta1.ShootSpec{
Kubernetes: gardencorev1beta1.Kubernetes{
Version: "1.22.0",
},
},
Status: gardencorev1beta1.ShootStatus{
TechnicalID: namespace,
},
},
},
)

secretKey = client.ObjectKey{Namespace: namespace, Name: v1beta1constants.SecretNameCloudProvider}
secret = &corev1.Secret{
@@ -251,6 +265,44 @@ var _ = Describe("Ensurer", func() {
checkKubeAPIServerDeployment(dep, "1.21.0", annotations)
})

It("should modify existing elements of kube-apiserver deployment (k8s >= 1.22)", func() {
var (
dep = &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: v1beta1constants.DeploymentNameKubeAPIServer},
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "kube-apiserver",
Command: []string{
"--enable-admission-plugins=Priority,PersistentVolumeLabel",
"--disable-admission-plugins=",
"--feature-gates=Foo=true",
},
},
},
},
},
},
}
)

// Create mock client
client := mockclient.NewMockClient(ctrl)
client.EXPECT().Get(ctx, secretKey, &corev1.Secret{}).DoAndReturn(clientGet(secret))

// Create ensurer
ensurer := NewEnsurer(logger)
err := ensurer.(inject.Client).InjectClient(client)
Expect(err).To(Not(HaveOccurred()))

// Call EnsureKubeAPIServerDeployment method and check the result
err = ensurer.EnsureKubeAPIServerDeployment(ctx, eContextK8s122, dep, nil)
Expect(err).To(Not(HaveOccurred()))
checkKubeAPIServerDeployment(dep, "1.22.0", annotations)
})

It("should keep the NODE_NETWORK env variable in the kube-apiserver deployment if its value does not change", func() {
var (
dep = &appsv1.Deployment{
@@ -587,14 +639,20 @@ var _ = Describe("Ensurer", func() {

func checkKubeAPIServerDeployment(dep *appsv1.Deployment, k8sVersion string, annotations map[string]string) {
k8sVersionLowerThan121, _ := version.CompareVersions(k8sVersion, "<", "1.21")
k8sVersionLowerThan122, _ := version.CompareVersions(k8sVersion, "<", "1.22")

// Check that the kube-apiserver container still exists and contains all needed command line args,
// env vars, and volume mounts
c := extensionswebhook.ContainerWithName(dep.Spec.Template.Spec.Containers, "kube-apiserver")
Expect(c).To(Not(BeNil()))
Expect(c.Command).To(Not(test.ContainElementWithPrefixContaining("--enable-admission-plugins=", "PersistentVolumeLabel", ",")))
Expect(c.Command).To(test.ContainElementWithPrefixContaining("--disable-admission-plugins=", "PersistentVolumeLabel", ","))
Expect(c.Command).To(test.ContainElementWithPrefixContaining("--feature-gates=", "VolumeSnapshotDataSource=true", ","))

if k8sVersionLowerThan122 {
Expect(c.Command).To(test.ContainElementWithPrefixContaining("--feature-gates=", "VolumeSnapshotDataSource=true", ","))
} else {
Expect(c.Command).ToNot(test.ContainElementWithPrefixContaining("--feature-gates=", "VolumeSnapshotDataSource=true", ","))
}

if k8sVersionLowerThan121 {
Expect(c.Command).To(test.ContainElementWithPrefixContaining("--feature-gates=", "CSINodeInfo=true", ","))