Skip to content

Commit 24ea443

Browse files
committed
refactor: how handlers are added to server
When adding new handlers I kept forgetting to add it to the meta hander. Although this commit doesn't really solve it does make it easier to visually see a difference. It also simplifies adding handlers by not asking for a package variable when calling <patchpackage>.NewPatch.
1 parent c16c492 commit 24ea443

File tree

17 files changed

+105
-59
lines changed

17 files changed

+105
-59
lines changed

cmd/main.go

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"sigs.k8s.io/controller-runtime/pkg/manager"
2525
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
2626

27+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
2728
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
2829
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/server"
2930
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/auditpolicy"
@@ -111,39 +112,50 @@ func main() {
111112
os.Exit(1)
112113
}
113114

114-
runtimeWebhookServer := server.NewServer(
115-
runtimeWebhookServerOpts,
116-
115+
// Handlers for lifecycle hooks.
116+
lifeCycleHandlers := []handlers.Named{
117117
servicelbgc.New(mgr.GetClient()),
118-
119-
calico.New(mgr.GetClient(), calicoCNIConfig, clusterconfig.VariableName, "cni"),
120-
118+
}
119+
// Handlers that apply patches to the Cluster object and its objects.
120+
// Used by CAPI's GeneratePatches hook.
121+
patchHandlers := []handlers.Named{
122+
httpproxy.NewPatch(mgr.GetClient()),
123+
extraapiservercertsans.NewPatch(),
124+
auditpolicy.NewPatch(),
125+
kubernetesimageregistry.NewPatch(),
126+
}
127+
// Handlers used by CAPI's DiscoverVariables hook.
128+
// It's ok that this does not match patchHandlers.
129+
// Some of those handlers may always get applied and not have a corresponding variable.
130+
variableHandlers := []handlers.Named{
121131
httpproxy.NewVariable(),
122-
httpproxy.NewPatch(mgr.GetClient(), httpproxy.VariableName),
123-
124132
extraapiservercertsans.NewVariable(),
125-
extraapiservercertsans.NewPatch(extraapiservercertsans.VariableName),
126-
133+
kubernetesimageregistry.NewVariable(),
134+
}
135+
// This metaPatchHandlers combines all other patch and variable handlers under a single handler.
136+
// It allows to specify configuration under a single variable.
137+
metaPatchHandlers := []mutation.GeneratePatches{
138+
httpproxy.NewMetaPatch(mgr.GetClient()),
139+
extraapiservercertsans.NewMetaPatch(),
127140
auditpolicy.NewPatch(),
141+
kubernetesimageregistry.NewMetaPatch(),
142+
}
143+
metaHandlers := []handlers.Named{
144+
// This Calico handler relies on a variable but does not generate a patch.
145+
// Instead it creates other resources in the API.
146+
calico.NewMetaHandler(mgr.GetClient(), calicoCNIConfig),
147+
clusterconfig.NewVariable(),
148+
mutation.NewMetaGeneratePatchesHandler("clusterConfigPatch", metaPatchHandlers...),
149+
}
128150

129-
kubernetesimageregistry.NewVariable(),
130-
kubernetesimageregistry.NewPatch(kubernetesimageregistry.VariableName),
151+
var allHandlers []handlers.Named
152+
allHandlers = append(allHandlers, lifeCycleHandlers...)
153+
allHandlers = append(allHandlers, patchHandlers...)
154+
allHandlers = append(allHandlers, variableHandlers...)
155+
allHandlers = append(allHandlers, metaHandlers...)
156+
157+
runtimeWebhookServer := server.NewServer(runtimeWebhookServerOpts, allHandlers...)
131158

132-
clusterconfig.NewVariable(),
133-
mutation.NewMetaGeneratePatchesHandler(
134-
"clusterConfigPatch",
135-
httpproxy.NewPatch(mgr.GetClient(), clusterconfig.VariableName, httpproxy.VariableName),
136-
extraapiservercertsans.NewPatch(
137-
clusterconfig.VariableName,
138-
extraapiservercertsans.VariableName,
139-
),
140-
auditpolicy.NewPatch(),
141-
kubernetesimageregistry.NewPatch(
142-
clusterconfig.VariableName,
143-
kubernetesimageregistry.VariableName,
144-
),
145-
),
146-
)
147159
if err := mgr.Add(runtimeWebhookServer); err != nil {
148160
setupLog.Error(err, "unable to add runtime webhook server runnable to controller manager")
149161
os.Exit(1)

common/pkg/capi/clustertopology/handlers/mutation/meta.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import (
1212
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
1313
)
1414

15+
const (
16+
// MetaVariableName is the meta cluster config patch variable name.
17+
MetaVariableName = "clusterConfig"
18+
)
19+
1520
type metaGeneratePatches struct {
1621
name string
1722
wrappedHandlers []GeneratePatches

pkg/handlers/clusterconfig/variables.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ var (
2020
)
2121

2222
const (
23-
// VariableName is http proxy external patch variable name.
24-
VariableName = "clusterConfig"
25-
2623
// HandlerNameVariable is the name of the variable handler.
2724
HandlerNameVariable = "ClusterConfigVars"
2825
)
@@ -43,7 +40,7 @@ func (h *clusterConfigVariableHandler) DiscoverVariables(
4340
resp *runtimehooksv1.DiscoverVariablesResponse,
4441
) {
4542
resp.Variables = append(resp.Variables, clusterv1.ClusterClassVariable{
46-
Name: VariableName,
43+
Name: mutation.MetaVariableName,
4744
Required: false,
4845
Schema: v1alpha1.ClusterConfigSpec{}.VariableSchema(),
4946
})

pkg/handlers/clusterconfig/variables_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
"k8s.io/utils/ptr"
1010

1111
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
12+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
1213
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest"
1314
)
1415

1516
func TestVariableValidation(t *testing.T) {
1617
capitest.ValidateDiscoverVariables(
1718
t,
18-
VariableName,
19+
mutation.MetaVariableName,
1920
ptr.To(v1alpha1.ClusterConfigSpec{}.VariableSchema()),
2021
NewVariable,
2122
capitest.VariableTestDef{

pkg/handlers/cni/calico/handler.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ import (
2626
"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
2727
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
2828
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/lifecycle"
29+
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
2930
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
3031
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/k8s/client"
3132
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/k8s/parser"
3233
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/cni"
3334
)
3435

36+
const (
37+
variableName = "cni"
38+
)
39+
3540
type CalicoCNIConfig struct {
3641
defaultsNamespace string
3742

@@ -78,17 +83,15 @@ var (
7883
calicoInstallationGK = schema.GroupKind{Group: "operator.tigera.io", Kind: "Installation"}
7984
)
8085

81-
func New(
86+
func NewMetaHandler(
8287
c ctrlclient.Client,
8388
cfg *CalicoCNIConfig,
84-
variableName string,
85-
variablePath ...string,
8689
) *CalicoCNI {
8790
return &CalicoCNI{
8891
client: c,
8992
config: cfg,
90-
variableName: variableName,
91-
variablePath: variablePath,
93+
variableName: mutation.MetaVariableName,
94+
variablePath: []string{variableName},
9295
}
9396
}
9497

pkg/handlers/extraapiservercertsans/inject.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ var (
4141
_ mutation.GeneratePatches = &extraAPIServerCertSANsPatchHandler{}
4242
)
4343

44-
func NewPatch(
44+
func NewPatch() *extraAPIServerCertSANsPatchHandler {
45+
return newExtraAPIServerCertSANsPatchHandler(variableName)
46+
}
47+
48+
func NewMetaPatch() *extraAPIServerCertSANsPatchHandler {
49+
return newExtraAPIServerCertSANsPatchHandler(mutation.MetaVariableName, variableName)
50+
}
51+
52+
func newExtraAPIServerCertSANsPatchHandler(
4553
variableName string,
4654
variableFieldPath ...string,
4755
) *extraAPIServerCertSANsPatchHandler {

pkg/handlers/extraapiservercertsans/inject_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ import (
1717
func TestGeneratePatches(t *testing.T) {
1818
capitest.ValidateGeneratePatches(
1919
t,
20-
func() mutation.GeneratePatches { return NewPatch(VariableName) },
20+
func() mutation.GeneratePatches { return NewPatch() },
2121
capitest.PatchTestDef{
2222
Name: "unset variable",
2323
},
2424
capitest.PatchTestDef{
2525
Name: "extra API server cert SANs set",
2626
Vars: []runtimehooksv1.Variable{
2727
capitest.VariableWithValue(
28-
VariableName,
28+
variableName,
2929
v1alpha1.ExtraAPIServerCertSANs{"a.b.c.example.com", "d.e.f.example.com"},
3030
),
3131
},

pkg/handlers/extraapiservercertsans/variables.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var (
2020
)
2121

2222
const (
23-
// VariableName is http proxy external patch variable name.
24-
VariableName = "extraAPIServerCertSANs"
23+
// variableName is http proxy external patch variable name.
24+
variableName = "extraAPIServerCertSANs"
2525

2626
// HandlerNameVariable is the name of the variable handler.
2727
HandlerNameVariable = "ExtraAPIServerCertSANsVars"
@@ -43,7 +43,7 @@ func (h *extraAPIServerCertSANsVariableHandler) DiscoverVariables(
4343
resp *runtimehooksv1.DiscoverVariablesResponse,
4444
) {
4545
resp.Variables = append(resp.Variables, clusterv1.ClusterClassVariable{
46-
Name: VariableName,
46+
Name: variableName,
4747
Required: false,
4848
Schema: v1alpha1.ExtraAPIServerCertSANs{}.VariableSchema(),
4949
})

pkg/handlers/extraapiservercertsans/variables_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func TestVariableValidation(t *testing.T) {
1616
capitest.ValidateDiscoverVariables(
1717
t,
18-
VariableName,
18+
variableName,
1919
ptr.To(v1alpha1.ExtraAPIServerCertSANs{}.VariableSchema()),
2020
NewVariable,
2121
capitest.VariableTestDef{

pkg/handlers/httpproxy/inject.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ var (
5353

5454
func NewPatch(
5555
cl ctrlclient.Reader,
56+
) *httpProxyPatchHandler {
57+
return newHTTPProxyPatchHandler(cl, variableName)
58+
}
59+
60+
func NewMetaPatch(
61+
cl ctrlclient.Reader,
62+
) *httpProxyPatchHandler {
63+
return newHTTPProxyPatchHandler(cl, mutation.MetaVariableName, variableName)
64+
}
65+
66+
func newHTTPProxyPatchHandler(
67+
cl ctrlclient.Reader,
5668
variableName string,
5769
variableFieldPath ...string,
5870
) *httpProxyPatchHandler {

pkg/handlers/httpproxy/inject_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestGeneratePatches(t *testing.T) {
2222
t,
2323
func() *httpProxyPatchHandler {
2424
fakeClient := fake.NewClientBuilder().Build()
25-
return NewPatch(fakeClient, VariableName)
25+
return NewPatch(fakeClient)
2626
},
2727
capitest.PatchTestDef{
2828
Name: "unset variable",
@@ -31,7 +31,7 @@ func TestGeneratePatches(t *testing.T) {
3131
Name: "http proxy set for KubeadmConfigTemplate default-worker",
3232
Vars: []runtimehooksv1.Variable{
3333
capitest.VariableWithValue(
34-
VariableName,
34+
variableName,
3535
v1alpha1.HTTPProxy{
3636
HTTP: "http://example.com",
3737
HTTPS: "https://example.com",
@@ -58,7 +58,7 @@ func TestGeneratePatches(t *testing.T) {
5858
Name: "http proxy set for KubeadmConfigTemplate generic worker",
5959
Vars: []runtimehooksv1.Variable{
6060
capitest.VariableWithValue(
61-
VariableName,
61+
variableName,
6262
v1alpha1.HTTPProxy{
6363
HTTP: "http://example.com",
6464
HTTPS: "https://example.com",
@@ -85,7 +85,7 @@ func TestGeneratePatches(t *testing.T) {
8585
Name: "http proxy set for KubeadmControlPlaneTemplate",
8686
Vars: []runtimehooksv1.Variable{
8787
capitest.VariableWithValue(
88-
VariableName,
88+
variableName,
8989
v1alpha1.HTTPProxy{
9090
HTTP: "http://example.com",
9191
HTTPS: "https://example.com",

pkg/handlers/httpproxy/variables.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var (
2020
)
2121

2222
const (
23-
// VariableName is http proxy external patch variable name.
24-
VariableName = "proxy"
23+
// variableName is http proxy external patch variable name.
24+
variableName = "proxy"
2525

2626
// HandlerNameVariable is the name of the variable handler.
2727
HandlerNameVariable = "HTTPProxyVars"
@@ -43,7 +43,7 @@ func (h *httpProxyVariableHandler) DiscoverVariables(
4343
resp *runtimehooksv1.DiscoverVariablesResponse,
4444
) {
4545
resp.Variables = append(resp.Variables, clusterv1.ClusterClassVariable{
46-
Name: VariableName,
46+
Name: variableName,
4747
Required: false,
4848
Schema: v1alpha1.HTTPProxy{}.VariableSchema(),
4949
})

pkg/handlers/httpproxy/variables_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func TestVariableValidation(t *testing.T) {
1616
capitest.ValidateDiscoverVariables(
1717
t,
18-
VariableName,
18+
variableName,
1919
ptr.To(v1alpha1.HTTPProxy{}.VariableSchema()),
2020
NewVariable,
2121
capitest.VariableTestDef{

pkg/handlers/kubernetesimageregistry/inject.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ var (
4141
_ mutation.GeneratePatches = &imageRegistryPatchHandler{}
4242
)
4343

44-
func NewPatch(
44+
func NewPatch() *imageRegistryPatchHandler {
45+
return newImageRegistryPatchHandler(variableName)
46+
}
47+
48+
func NewMetaPatch() *imageRegistryPatchHandler {
49+
return newImageRegistryPatchHandler(mutation.MetaVariableName, variableName)
50+
}
51+
52+
func newImageRegistryPatchHandler(
4553
variableName string,
4654
variableFieldPath ...string,
4755
) *imageRegistryPatchHandler {

pkg/handlers/kubernetesimageregistry/inject_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ import (
1717
func TestGeneratePatches(t *testing.T) {
1818
capitest.ValidateGeneratePatches(
1919
t,
20-
func() mutation.GeneratePatches { return NewPatch(VariableName) },
20+
func() mutation.GeneratePatches { return NewPatch() },
2121
capitest.PatchTestDef{
2222
Name: "unset variable",
2323
},
2424
capitest.PatchTestDef{
2525
Name: "kubernetesImageRegistry set",
2626
Vars: []runtimehooksv1.Variable{
2727
capitest.VariableWithValue(
28-
VariableName,
28+
variableName,
2929
v1alpha1.KubernetesImageRegistry("my-registry.io/my-org/my-repo"),
3030
),
3131
},

pkg/handlers/kubernetesimageregistry/variables.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var (
2020
)
2121

2222
const (
23-
// VariableName is http proxy external patch variable name.
24-
VariableName = "kubernetesImageRegistry"
23+
// variableName is http proxy external patch variable name.
24+
variableName = "kubernetesImageRegistry"
2525

2626
// HandlerNameVariable is the name of the variable handler.
2727
HandlerNameVariable = "ImageRegistryVars"
@@ -43,7 +43,7 @@ func (h *imageRegistryVariableHandler) DiscoverVariables(
4343
resp *runtimehooksv1.DiscoverVariablesResponse,
4444
) {
4545
resp.Variables = append(resp.Variables, clusterv1.ClusterClassVariable{
46-
Name: VariableName,
46+
Name: variableName,
4747
Required: false,
4848
Schema: v1alpha1.KubernetesImageRegistry("").VariableSchema(),
4949
})

pkg/handlers/kubernetesimageregistry/variables_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func TestVariableValidation(t *testing.T) {
1616
capitest.ValidateDiscoverVariables(
1717
t,
18-
VariableName,
18+
variableName,
1919
ptr.To(v1alpha1.KubernetesImageRegistry("").VariableSchema()),
2020
NewVariable,
2121
capitest.VariableTestDef{

0 commit comments

Comments
 (0)