Skip to content

Commit

Permalink
Ensure MCM label in webhook to fix NetworkPolicies for local metal-ap…
Browse files Browse the repository at this point in the history
…i shoots
  • Loading branch information
Nuckal777 committed Nov 26, 2024
1 parent 862aeda commit e299a5a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
9 changes: 8 additions & 1 deletion pkg/controller/controlplane/valuesprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,13 @@ var _ = Describe("Valueprovider Reconcile", func() {
},
},
},
Seed: &gardencorev1beta1.Seed{},
Seed: &gardencorev1beta1.Seed{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
metal.LocalMetalAPIAnnotation: "true",
},
},
},
}

checksums := map[string]string{
Expand All @@ -187,6 +193,7 @@ var _ = Describe("Valueprovider Reconcile", func() {
},
"podLabels": map[string]any{
"maintenance.gardener.cloud/restart": "true",
metal.AllowEgressToIstioIngressLabel: "allowed",
},
"tlsCipherSuites": []string{
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
Expand Down
12 changes: 11 additions & 1 deletion pkg/webhook/controlplane/ensurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package controlplane

import (
"context"
"fmt"

"github.com/Masterminds/semver/v3"
"github.com/coreos/go-systemd/v22/unit"
Expand Down Expand Up @@ -40,15 +41,24 @@ type ensurer struct {
var ImageVector = imagevector.ImageVector()

// EnsureMachineControllerManagerDeployment ensures that the machine-controller-manager deployment conforms to the provider requirements.
func (e *ensurer) EnsureMachineControllerManagerDeployment(_ context.Context, _ extensionscontextwebhook.GardenContext, newObj, _ *appsv1.Deployment) error {
func (e *ensurer) EnsureMachineControllerManagerDeployment(ctx context.Context, gctx extensionscontextwebhook.GardenContext, newObj, _ *appsv1.Deployment) error {
image, err := ImageVector.FindImage(metal.MachineControllerManagerProviderIroncoreImageName)
if err != nil {
return err
}
cluster, err := gctx.GetCluster(ctx)
if err != nil {
return fmt.Errorf("failed to get cluster: %w", err)
}

template := &newObj.Spec.Template
ps := &template.Spec

localAPI, ok := cluster.Seed.Annotations[metal.LocalMetalAPIAnnotation]
if ok && localAPI == "true" {
template.Labels = extensionswebhook.EnsureAnnotationOrLabel(template.Labels, metal.AllowEgressToIstioIngressLabel, "allowed")
}

ps.Containers = extensionswebhook.EnsureContainerWithName(
newObj.Spec.Template.Spec.Containers,
machinecontrollermanager.ProviderSidecarContainer(newObj.Namespace, metal.ProviderName, image.String()),
Expand Down
11 changes: 10 additions & 1 deletion pkg/webhook/controlplane/ensurer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
gardenerutils "github.com/gardener/gardener/pkg/utils/gardener"
imagevectorutils "github.com/gardener/gardener/pkg/utils/imagevector"
testutils "github.com/gardener/gardener/pkg/utils/test"
"github.com/ironcore-dev/gardener-extension-provider-metal/pkg/metal"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -61,6 +62,13 @@ var _ = Describe("Ensurer", func() {
},
},
},
Seed: &gardencorev1beta1.Seed{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
metal.LocalMetalAPIAnnotation: "true",
},
},
},
},
)
)
Expand Down Expand Up @@ -250,7 +258,8 @@ var _ = Describe("Ensurer", func() {

It("should inject the sidecar container", func() {
Expect(deployment.Spec.Template.Spec.Containers).To(BeEmpty())
Expect(ensurer.EnsureMachineControllerManagerDeployment(ctx, nil, deployment, nil)).To(Succeed())
Expect(ensurer.EnsureMachineControllerManagerDeployment(ctx, eContextK8s, deployment, nil)).To(Succeed())
Expect(deployment.Spec.Template.Labels).To(HaveKeyWithValue(metal.AllowEgressToIstioIngressLabel, "allowed"))
Expect(deployment.Spec.Template.Spec.Containers).To(ConsistOf(corev1.Container{
Name: "machine-controller-manager-provider-metal",
Image: "foo:bar",
Expand Down

0 comments on commit e299a5a

Please sign in to comment.