Skip to content

Commit f7ea208

Browse files
committed
refactor: provider an entrypoint to the infra provider meta handlers
This aligns the structure to how the lifecycle handlers are created. More importantly it enables us to pass globalOptions to those infra handlers that need it.
1 parent 33b29dc commit f7ea208

File tree

7 files changed

+144
-44
lines changed

7 files changed

+144
-44
lines changed

cmd/main.go

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,10 @@ import (
2727
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
2828
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers"
2929
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/server"
30-
awsclusterconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/clusterconfig"
31-
awsmutation "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation"
32-
awsworkerconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/workerconfig"
33-
dockerclusterconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker/clusterconfig"
34-
dockermutation "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker/mutation"
35-
dockerworkerconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker/workerconfig"
30+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws"
31+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker"
3632
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle"
37-
nutanixclusterconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/clusterconfig"
38-
nutanixmutation "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/mutation"
39-
nutanixworkerconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/workerconfig"
33+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix"
4034
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/options"
4135
)
4236

@@ -84,12 +78,27 @@ func main() {
8478

8579
genericLifecycleHandlers := lifecycle.New(globalOptions)
8680

81+
// awsMetaHandlers combines all AWS patch and variable handlers under a single handler.
82+
// It allows to specify configuration under a single variable.
83+
awsMetaHandlers := aws.New(globalOptions)
84+
85+
// dockerMetaHandlers combines all Docker patch and variable handlers under a single handler.
86+
// It allows to specify configuration under a single variable.
87+
dockerMetaHandlers := docker.New(globalOptions)
88+
89+
// nutanixMetaHandlers combines all Nutanix patch and variable handlers under a single handler.
90+
// It allows to specify configuration under a single variable.
91+
nutanixMetaHandlers := nutanix.New(globalOptions)
92+
8793
// Initialize and parse command line flags.
8894
logs.AddFlags(pflag.CommandLine, logs.SkipLoggingConfigurationFlags())
8995
logsv1.AddFlags(logOptions, pflag.CommandLine)
9096
globalOptions.AddFlags(pflag.CommandLine)
9197
runtimeWebhookServerOpts.AddFlags(pflag.CommandLine)
9298
genericLifecycleHandlers.AddFlags(pflag.CommandLine)
99+
awsMetaHandlers.AddFlags(pflag.CommandLine)
100+
dockerMetaHandlers.AddFlags(pflag.CommandLine)
101+
nutanixMetaHandlers.AddFlags(pflag.CommandLine)
93102
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
94103
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
95104
pflag.Parse()
@@ -113,39 +122,11 @@ func main() {
113122
os.Exit(1)
114123
}
115124

116-
// This genericMetaPatchHandlers combines all other patch and variable handlers under a single handler.
117-
// It allows to specify configuration under a single variable.
118-
// awsMetaHandlers combines all AWS patch and variable handlers under a single handler.
119-
awsMetaHandlers := []handlers.Named{
120-
awsclusterconfig.NewVariable(),
121-
awsworkerconfig.NewVariable(),
122-
awsmutation.MetaPatchHandler(mgr),
123-
awsmutation.MetaWorkerPatchHandler(mgr),
124-
}
125-
126-
// dockerMetaHandlers combines all Docker patch and variable handlers under a single handler.
127-
// It allows to specify configuration under a single variable.
128-
dockerMetaHandlers := []handlers.Named{
129-
dockerclusterconfig.NewVariable(),
130-
dockerworkerconfig.NewVariable(),
131-
dockermutation.MetaPatchHandler(mgr),
132-
dockermutation.MetaWorkerPatchHandler(mgr),
133-
}
134-
135-
// nutanixMetaHandlers combines all Nutanix patch and variable handlers under a single handler.
136-
// It allows to specify configuration under a single variable.
137-
nutanixMetaHandlers := []handlers.Named{
138-
nutanixclusterconfig.NewVariable(),
139-
nutanixworkerconfig.NewVariable(),
140-
nutanixmutation.MetaPatchHandler(mgr),
141-
nutanixmutation.MetaWorkerPatchHandler(mgr),
142-
}
143-
144125
var allHandlers []handlers.Named
145126
allHandlers = append(allHandlers, genericLifecycleHandlers.AllHandlers(mgr)...)
146-
allHandlers = append(allHandlers, awsMetaHandlers...)
147-
allHandlers = append(allHandlers, dockerMetaHandlers...)
148-
allHandlers = append(allHandlers, nutanixMetaHandlers...)
127+
allHandlers = append(allHandlers, awsMetaHandlers.AllHandlers(mgr)...)
128+
allHandlers = append(allHandlers, dockerMetaHandlers.AllHandlers(mgr)...)
129+
allHandlers = append(allHandlers, nutanixMetaHandlers.AllHandlers(mgr)...)
149130

150131
runtimeWebhookServer := server.NewServer(runtimeWebhookServerOpts, allHandlers...)
151132

pkg/handlers/aws/handlers.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package aws
5+
6+
import (
7+
"github.com/spf13/pflag"
8+
"sigs.k8s.io/controller-runtime/pkg/manager"
9+
10+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers"
11+
awsclusterconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/clusterconfig"
12+
awsmutation "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/mutation"
13+
awsworkerconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/aws/workerconfig"
14+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/options"
15+
)
16+
17+
type Handlers struct{}
18+
19+
func New(
20+
_ *options.GlobalOptions,
21+
) *Handlers {
22+
return &Handlers{}
23+
}
24+
25+
func (h *Handlers) AllHandlers(mgr manager.Manager) []handlers.Named {
26+
return []handlers.Named{
27+
awsclusterconfig.NewVariable(),
28+
awsworkerconfig.NewVariable(),
29+
awsmutation.MetaPatchHandler(mgr),
30+
awsmutation.MetaWorkerPatchHandler(mgr),
31+
}
32+
}
33+
34+
func (h *Handlers) AddFlags(_ *pflag.FlagSet) {}

pkg/handlers/docker/handlers.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package docker
5+
6+
import (
7+
"github.com/spf13/pflag"
8+
"sigs.k8s.io/controller-runtime/pkg/manager"
9+
10+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers"
11+
dockerclusterconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker/clusterconfig"
12+
dockermutation "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker/mutation"
13+
dockerworkerconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/docker/workerconfig"
14+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/options"
15+
)
16+
17+
type Handlers struct{}
18+
19+
func New(
20+
_ *options.GlobalOptions,
21+
) *Handlers {
22+
return &Handlers{}
23+
}
24+
25+
func (h *Handlers) AllHandlers(mgr manager.Manager) []handlers.Named {
26+
return []handlers.Named{
27+
dockerclusterconfig.NewVariable(),
28+
dockerworkerconfig.NewVariable(),
29+
dockermutation.MetaPatchHandler(mgr),
30+
dockermutation.MetaWorkerPatchHandler(mgr),
31+
}
32+
}
33+
34+
func (h *Handlers) AddFlags(_ *pflag.FlagSet) {}

pkg/handlers/nutanix/handlers.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2023 D2iQ, Inc. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package nutanix
5+
6+
import (
7+
"github.com/spf13/pflag"
8+
"sigs.k8s.io/controller-runtime/pkg/manager"
9+
10+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers"
11+
nutanixclusterconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/clusterconfig"
12+
nutanixmutation "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/mutation"
13+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/mutation/controlplaneendpoint"
14+
nutanixworkerconfig "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/nutanix/workerconfig"
15+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/options"
16+
)
17+
18+
type Handlers struct {
19+
nutanixControlPlaneEndpointConfig *controlplaneendpoint.Config
20+
}
21+
22+
func New(
23+
globalOptions *options.GlobalOptions,
24+
) *Handlers {
25+
return &Handlers{
26+
nutanixControlPlaneEndpointConfig: &controlplaneendpoint.Config{
27+
GlobalOptions: globalOptions,
28+
},
29+
}
30+
}
31+
32+
func (h *Handlers) AllHandlers(mgr manager.Manager) []handlers.Named {
33+
return []handlers.Named{
34+
nutanixclusterconfig.NewVariable(),
35+
nutanixworkerconfig.NewVariable(),
36+
nutanixmutation.MetaPatchHandler(mgr, h.nutanixControlPlaneEndpointConfig),
37+
nutanixmutation.MetaWorkerPatchHandler(mgr),
38+
}
39+
}
40+
41+
func (h *Handlers) AddFlags(_ *pflag.FlagSet) {}

pkg/handlers/nutanix/mutation/controlplaneendpoint/inject.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,41 @@ import (
2222
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors"
2323
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables"
2424
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/clusterconfig"
25+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/options"
2526
)
2627

2728
const (
2829
// VariableName is the external patch variable name.
2930
VariableName = "controlPlaneEndpoint"
3031
)
3132

33+
type Config struct {
34+
*options.GlobalOptions
35+
}
36+
3237
type nutanixControlPlaneEndpoint struct {
38+
config *Config
39+
3340
variableName string
3441
variableFieldPath []string
3542
}
3643

37-
func NewPatch() *nutanixControlPlaneEndpoint {
44+
func NewPatch(cfg *Config) *nutanixControlPlaneEndpoint {
3845
return newNutanixControlPlaneEndpoint(
46+
cfg,
3947
clusterconfig.MetaVariableName,
4048
v1alpha1.NutanixVariableName,
4149
VariableName,
4250
)
4351
}
4452

4553
func newNutanixControlPlaneEndpoint(
54+
cfg *Config,
4655
variableName string,
4756
variableFieldPath ...string,
4857
) *nutanixControlPlaneEndpoint {
4958
return &nutanixControlPlaneEndpoint{
59+
config: cfg,
5060
variableName: variableName,
5161
variableFieldPath: variableFieldPath,
5262
}

pkg/handlers/nutanix/mutation/controlplaneendpoint/inject_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestControlPlaneEndpointPatch(t *testing.T) {
2626

2727
var _ = Describe("Generate Nutanix ControlPlane endpoint patches", func() {
2828
patchGenerator := func() mutation.GeneratePatches {
29-
return mutation.NewMetaGeneratePatchesHandler("", helpers.TestEnv.Client, NewPatch()).(mutation.GeneratePatches)
29+
return mutation.NewMetaGeneratePatchesHandler("", helpers.TestEnv.Client, NewPatch(nil)).(mutation.GeneratePatches)
3030
}
3131

3232
testDefs := []capitest.PatchTestDef{

pkg/handlers/nutanix/mutation/metapatch_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515
)
1616

1717
// MetaPatchHandler returns a meta patch handler for mutating CAPX clusters.
18-
func MetaPatchHandler(mgr manager.Manager) handlers.Named {
18+
func MetaPatchHandler(mgr manager.Manager, cfg *controlplaneendpoint.Config) handlers.Named {
1919
patchHandlers := append(
2020
[]mutation.MetaMutator{
21-
controlplaneendpoint.NewPatch(),
21+
controlplaneendpoint.NewPatch(cfg),
2222
prismcentralendpoint.NewPatch(),
2323
machinedetails.NewControlPlanePatch(),
2424
},

0 commit comments

Comments
 (0)