Skip to content

Commit

Permalink
Add object selector to controlplane webhook with version constraint
Browse files Browse the repository at this point in the history
Inject gardener version to webhook switches
[make generate]
  • Loading branch information
LucaBernstein committed Jul 31, 2024
1 parent e421a05 commit cd074e7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 18 deletions.
4 changes: 3 additions & 1 deletion cmd/gardener-extension-provider-azure/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
Namespace: os.Getenv("WEBHOOK_CONFIG_NAMESPACE"),
}

gardenerVersion = new(string)
controllerSwitches = azurecmd.ControllerSwitchOptions()
webhookSwitches = azurecmd.WebhookSwitchOptions()
webhookSwitches = azurecmd.WebhookSwitchOptions(gardenerVersion)
webhookOptions = webhookcmd.NewAddToManagerOptions(
azure.Name,
genericactuator.ShootWebhooksResourceName,
Expand Down Expand Up @@ -214,6 +215,7 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
}

log.Info("Adding controllers to manager")
*gardenerVersion = generalOpts.Completed().GardenerVersion

configFileOpts.Completed().ApplyETCDStorage(&azurecontrolplaneexposure.DefaultAddOptions.ETCDStorage)
configFileOpts.Completed().ApplyHealthCheckConfig(&healthcheck.DefaultAddOptions.HealthCheckConfig)
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func ControllerSwitchOptions() *controllercmd.SwitchOptions {
}

// WebhookSwitchOptions are the webhookcmd.SwitchOptions for the provider webhooks.
func WebhookSwitchOptions() *webhookcmd.SwitchOptions {
func WebhookSwitchOptions(gardenerVersion *string) *webhookcmd.SwitchOptions {
return webhookcmd.NewSwitchOptions(
webhookcmd.Switch(extensionsnetworkwebhook.WebhookName, networkwebhook.AddToManager),
webhookcmd.Switch(acceleratednetworkwebhook.WebhookName, acceleratednetworkwebhook.AddToManager),
webhookcmd.Switch(infrastructurewebhook.WebhookName, infrastructurewebhook.AddToManager),
webhookcmd.Switch(extensionscontrolplanewebhook.WebhookName, controlplanewebhook.AddToManager),
webhookcmd.Switch(extensionscontrolplanewebhook.WebhookName, controlplanewebhook.AddToManager(gardenerVersion)),
webhookcmd.Switch(extensionscontrolplanewebhook.ExposureWebhookName, controlplaneexposurewebhook.AddToManager),
webhookcmd.Switch(extensionscloudproviderwebhook.WebhookName, cloudproviderwebhook.AddToManager),
webhookcmd.Switch(topology.WebhookName, topology.AddToManager),
Expand Down
57 changes: 42 additions & 15 deletions pkg/webhook/controlplane/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
package controlplane

import (
"fmt"

"github.com/Masterminds/semver/v3"
extensionswebhook "github.com/gardener/gardener/extensions/pkg/webhook"
"github.com/gardener/gardener/extensions/pkg/webhook/controlplane"
"github.com/gardener/gardener/extensions/pkg/webhook/controlplane/genericmutator"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/component/extensions/operatingsystemconfig/original/components/kubelet"
oscutils "github.com/gardener/gardener/pkg/component/extensions/operatingsystemconfig/utils"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
vpaautoscalingv1 "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand All @@ -20,22 +26,43 @@ import (
)

var (
logger = log.Log.WithName("azure-controlplane-webhook")
logger = log.Log.WithName("azure-controlplane-webhook")
versionConstraintGreaterEqual198 *semver.Constraints
)

func init() {
var err error
versionConstraintGreaterEqual198, err = semver.NewConstraint(">= 1.98")
utilruntime.Must(err)
}

// AddToManager creates a webhook and adds it to the manager.
func AddToManager(mgr manager.Manager) (*extensionswebhook.Webhook, error) {
logger.Info("Adding webhook to manager")
fciCodec := oscutils.NewFileContentInlineCodec()
return controlplane.New(mgr, controlplane.Args{
Kind: controlplane.KindShoot,
Provider: azure.Type,
Types: []extensionswebhook.Type{
{Obj: &appsv1.Deployment{}},
{Obj: &vpaautoscalingv1.VerticalPodAutoscaler{}},
{Obj: &extensionsv1alpha1.OperatingSystemConfig{}},
},
Mutator: genericmutator.NewMutator(mgr, NewEnsurer(mgr, logger), oscutils.NewUnitSerializer(),
kubelet.NewConfigCodec(fciCodec), fciCodec, logger),
})
func AddToManager(gardenerVersion *string) func(mgr manager.Manager) (*extensionswebhook.Webhook, error) {
return func(mgr manager.Manager) (*extensionswebhook.Webhook, error) {
var objectSelector *metav1.LabelSelector
if gardenerVersion != nil && len(*gardenerVersion) > 0 {
version, err := semver.NewVersion(*gardenerVersion)
if err != nil {
return nil, fmt.Errorf("failed to parse gardener version: %v", err)
}
if versionConstraintGreaterEqual198.Check(version) {
objectSelector = &metav1.LabelSelector{MatchLabels: map[string]string{v1beta1constants.LabelExtensionProviderMutatedByControlplaneWebhook: "true"}}
}
}

logger.Info("Adding webhook to manager")
fciCodec := oscutils.NewFileContentInlineCodec()
return controlplane.New(mgr, controlplane.Args{
Kind: controlplane.KindShoot,
Provider: azure.Type,
Types: []extensionswebhook.Type{
{Obj: &appsv1.Deployment{}},
{Obj: &vpaautoscalingv1.VerticalPodAutoscaler{}},
{Obj: &extensionsv1alpha1.OperatingSystemConfig{}},
},
ObjectSelector: objectSelector,
Mutator: genericmutator.NewMutator(mgr, NewEnsurer(mgr, logger), oscutils.NewUnitSerializer(),
kubelet.NewConfigCodec(fciCodec), fciCodec, logger),
})
}
}

0 comments on commit cd074e7

Please sign in to comment.