Skip to content

Commit 5debd0e

Browse files
Fix infra mutator (#911)
Co-authored-by: Alexander Hebel <a.hebelsun@gmail.com>
1 parent 9f4d823 commit 5debd0e

File tree

6 files changed

+53
-19
lines changed

6 files changed

+53
-19
lines changed

pkg/webhook/infrastructure/add.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ func AddToManager(mgr manager.Manager) (*extensionswebhook.Webhook, error) {
3232
{Obj: &extensionsv1alpha1.Infrastructure{}},
3333
}
3434

35+
// WithMutator can only add one mutator for the same type, so
36+
// we created a general infraMutator that just calls all Mutate funcs
3537
handler, err := extensionswebhook.NewBuilder(mgr, logger).
36-
WithMutator(NewLayoutMutator(logger, NetworkLayoutMigrationMutate), types...).
37-
WithMutator(NewFlowMutator(mgr, logger), types...).
38+
WithMutator(NewInfraMutator([]extensionswebhook.Mutator{
39+
newLayoutMutator(logger),
40+
newFlowMutator(mgr, logger),
41+
}), types...).
3842
Build()
3943
if err != nil {
4044
return nil, err

pkg/webhook/infrastructure/flow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type flowMutator struct {
2727
}
2828

2929
// NewFlowMutator returns a new Infrastructure flowMutator that uses mutateFunc to perform the mutation.
30-
func NewFlowMutator(mgr manager.Manager, logger logr.Logger) extensionswebhook.Mutator {
30+
func newFlowMutator(mgr manager.Manager, logger logr.Logger) extensionswebhook.Mutator {
3131
return &flowMutator{
3232
client: mgr.GetClient(),
3333
logger: logger,

pkg/webhook/infrastructure/flow_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var _ = Describe("Mutate", func() {
5656
)
5757

5858
BeforeEach(func() {
59-
mutator = NewFlowMutator(mgr, logger)
59+
mutator = newFlowMutator(mgr, logger)
6060
ctx = context.TODO()
6161

6262
c.EXPECT().Get(ctx, client.ObjectKey{Name: shootNamespace}, gomock.AssignableToTypeOf(&extensionsv1alpha1.Cluster{})).
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
package infrastructure
6+
7+
import (
8+
"context"
9+
10+
extensionswebhook "github.com/gardener/gardener/extensions/pkg/webhook"
11+
"sigs.k8s.io/controller-runtime/pkg/client"
12+
)
13+
14+
type infraMutator struct {
15+
mutators []extensionswebhook.Mutator
16+
}
17+
18+
// NewInfraMutator returns a new Infrastructure infraMutator that calls the
19+
// Mutate func of all passed mutators
20+
func NewInfraMutator(mutators []extensionswebhook.Mutator) extensionswebhook.Mutator {
21+
return &infraMutator{
22+
mutators: mutators,
23+
}
24+
}
25+
26+
// Mutate mutates the given object using the mutateFunc
27+
func (m *infraMutator) Mutate(ctx context.Context, new, old client.Object) error {
28+
for _, mutator := range m.mutators {
29+
err := mutator.Mutate(ctx, new, old)
30+
if err != nil {
31+
return err
32+
}
33+
}
34+
return nil
35+
}

pkg/webhook/infrastructure/layout.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,14 @@ import (
1818
azuretypes "github.com/gardener/gardener-extension-provider-azure/pkg/azure"
1919
)
2020

21-
// MutateFunc is a function that can perform a mutation on Infrastructure objects.
22-
type MutateFunc func(ctx context.Context, logger logr.Logger, new, old *extensionsv1alpha1.Infrastructure) error
23-
2421
type layoutMutator struct {
25-
logger logr.Logger
26-
mutateFunc MutateFunc
22+
logger logr.Logger
2723
}
2824

29-
// NewLayoutMutator returns a new Infrastructure layoutMutator that uses mutateFunc to perform the mutation.
30-
func NewLayoutMutator(logger logr.Logger, mutateFunc MutateFunc) extensionswebhook.Mutator {
25+
// newLayoutMutator returns a new Infrastructure layoutMutator
26+
func newLayoutMutator(logger logr.Logger) extensionswebhook.Mutator {
3127
return &layoutMutator{
32-
logger: logger,
33-
mutateFunc: mutateFunc,
28+
logger: logger,
3429
}
3530
}
3631

@@ -58,11 +53,11 @@ func (m *layoutMutator) Mutate(ctx context.Context, new, old client.Object) erro
5853
return fmt.Errorf("could not mutate: object is not of type Infrastructure")
5954
}
6055

61-
return m.mutateFunc(ctx, m.logger, newInfra, oldInfra)
56+
return mutate(ctx, m.logger, newInfra, oldInfra)
6257
}
6358

64-
// NetworkLayoutMigrationMutate annotates the infrastructure object with additonal information that are necessary during the reconciliation when migrating to a new network layout.
65-
func NetworkLayoutMigrationMutate(_ context.Context, logger logr.Logger, newInfra, oldInfra *extensionsv1alpha1.Infrastructure) error {
59+
// mutate annotates the infrastructure object with additional information that are necessary during the reconciliation when migrating to a new network layout.
60+
func mutate(_ context.Context, logger logr.Logger, newInfra, oldInfra *extensionsv1alpha1.Infrastructure) error {
6661
var (
6762
newProviderCfg, oldProviderCfg *azure.InfrastructureConfig
6863
oldProviderStatus *azure.InfrastructureStatus
@@ -78,8 +73,8 @@ func NetworkLayoutMigrationMutate(_ context.Context, logger logr.Logger, newInfr
7873
return fmt.Errorf("could not mutate object: %v", err)
7974
}
8075

81-
// if newInfra already contains the zone migration annotation, check if it is still necessary. Otherwise, remove the
82-
// the annotation.
76+
// if newInfra already contains the zone migration annotation, check if it is still necessary.
77+
// Otherwise, remove the annotation.
8378
if z, ok := newInfra.Annotations[azuretypes.NetworkLayoutZoneMigrationAnnotation]; ok {
8479
findMatchingZone := false
8580
for _, zone := range newProviderCfg.Networks.Zones {

pkg/webhook/infrastructure/layout_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var _ = Describe("Mutate", func() {
4141
var mutator extensionswebhook.Mutator
4242

4343
BeforeEach(func() {
44-
mutator = NewLayoutMutator(logger, NetworkLayoutMigrationMutate)
44+
mutator = newLayoutMutator(logger)
4545
})
4646

4747
Context("add migration annotation", func() {

0 commit comments

Comments
 (0)