Skip to content

Commit 6fede8c

Browse files
authored
Merge pull request #8026 from killianmuldoon/pr-exvars-add-test-extension-hook
🌱 Add DiscoverVariables hook implementation for test extension
2 parents 0502e00 + 717039a commit 6fede8c

File tree

5 files changed

+62
-24
lines changed

5 files changed

+62
-24
lines changed

test/e2e/data/infrastructure-docker/main/cluster-template-upgrades-runtimesdk/cluster-runtimesdk.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ spec:
2929
variables:
3030
- name: kubeadmControlPlaneMaxSurge
3131
value: "1"
32+
# imageRepository has definitions for both an inline patch and the test-patch. This value is required in the
33+
# test-patch and requires definitionFrom to reflect that.
34+
- name: imageRepository
35+
definitionFrom: test-patch
36+
value: "kindest"
37+

test/e2e/data/infrastructure-docker/main/clusterclass-quick-start-runtimesdk.yaml

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,8 @@ spec:
3333
kind: DockerMachineTemplate
3434
name: quick-start-default-worker-machinetemplate
3535
variables:
36-
- name: lbImageRepository
37-
required: false
38-
schema:
39-
openAPIV3Schema:
40-
type: string
41-
default: kindest
36+
# This variable is not used in any patch, but is here to ensure the variable discovered from the runtime hook is correctly
37+
# used.
4238
- name: imageRepository
4339
required: true
4440
schema:
@@ -47,19 +43,12 @@ spec:
4743
default: "registry.k8s.io"
4844
example: "registry.k8s.io"
4945
description: "imageRepository sets the container registry to pull images from. If empty, `registry.k8s.io` will be used by default."
50-
- name: kubeadmControlPlaneMaxSurge
51-
required: false
52-
schema:
53-
openAPIV3Schema:
54-
type: string
55-
default: ""
56-
example: "0"
57-
description: "kubeadmControlPlaneMaxSurge is the maximum number of control planes that can be scheduled above or under the desired number of control plane machines."
5846
patches:
59-
- name: lbImageRepository
47+
- name: test-patch
6048
external:
6149
generateExtension: generate-patches.k8s-upgrade-with-runtimesdk
6250
validateExtension: validate-topology.k8s-upgrade-with-runtimesdk
51+
discoverVariablesExtension: discover-variables.k8s-upgrade-with-runtimesdk
6352
---
6453
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
6554
kind: DockerClusterTemplate

test/extension/handlers/topologymutation/handler.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
intstrutil "k8s.io/apimachinery/pkg/util/intstr"
3535
ctrl "sigs.k8s.io/controller-runtime"
3636

37+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3738
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
3839
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
3940
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
@@ -119,15 +120,15 @@ func (h *ExtensionHandlers) GeneratePatches(ctx context.Context, req *runtimehoo
119120
}
120121

121122
// patchDockerClusterTemplate patches the DockerClusterTemplate.
122-
// It sets the LoadBalancer.ImageRepository if the lbImageRepository variable is provided.
123+
// It sets the LoadBalancer.ImageRepository if the imageRepository variable is provided.
123124
// NOTE: this patch is not required for any special reason, it is used for testing the patch machinery itself.
124125
func patchDockerClusterTemplate(_ context.Context, dockerClusterTemplate *infrav1.DockerClusterTemplate, templateVariables map[string]apiextensionsv1.JSON) error {
125-
lbImageRepo, found, err := topologymutation.GetStringVariable(templateVariables, "lbImageRepository")
126+
imageRepo, found, err := topologymutation.GetStringVariable(templateVariables, "imageRepository")
126127
if err != nil {
127128
return errors.Wrap(err, "could not set DockerClusterTemplate loadBalancer imageRepository")
128129
}
129130
if found {
130-
dockerClusterTemplate.Spec.Template.Spec.LoadBalancer.ImageRepository = lbImageRepo
131+
dockerClusterTemplate.Spec.Template.Spec.LoadBalancer.ImageRepository = imageRepo
131132
}
132133
return nil
133134
}
@@ -313,3 +314,36 @@ func (h *ExtensionHandlers) ValidateTopology(ctx context.Context, _ *runtimehook
313314

314315
resp.Status = runtimehooksv1.ResponseStatusSuccess
315316
}
317+
318+
// DiscoverVariables implements the HandlerFunc for the DiscoverVariables hook.
319+
func (h *ExtensionHandlers) DiscoverVariables(ctx context.Context, _ *runtimehooksv1.DiscoverVariablesRequest, resp *runtimehooksv1.DiscoverVariablesResponse) {
320+
log := ctrl.LoggerFrom(ctx)
321+
log.Info("DiscoverVariables called")
322+
323+
resp.Status = runtimehooksv1.ResponseStatusSuccess
324+
resp.Variables = []clusterv1.ClusterClassVariable{
325+
{
326+
Name: "kubeadmControlPlaneMaxSurge",
327+
Required: false,
328+
Schema: clusterv1.VariableSchema{
329+
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
330+
Type: "string",
331+
Default: &apiextensionsv1.JSON{Raw: []byte(`""`)},
332+
Example: &apiextensionsv1.JSON{Raw: []byte(`""`)},
333+
Description: "kubeadmControlPlaneMaxSurge is the maximum number of control planes that can be scheduled above or under the desired number of control plane machines.",
334+
},
335+
},
336+
},
337+
// This variable must be set in the Cluster as it has no default value and is required.
338+
{
339+
Name: "imageRepository",
340+
Required: true,
341+
Schema: clusterv1.VariableSchema{
342+
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
343+
Type: "string",
344+
Example: &apiextensionsv1.JSON{Raw: []byte(`"kindest"`)},
345+
},
346+
},
347+
},
348+
}
349+
}

test/extension/handlers/topologymutation/handler_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ func Test_patchDockerClusterTemplate(t *testing.T) {
5858
expectedErr bool
5959
}{
6060
{
61-
name: "no op if lbImageRepository is not set",
61+
name: "no op if imageRepository is not set",
6262
template: &infrav1.DockerClusterTemplate{},
6363
variables: nil,
6464
expectedTemplate: &infrav1.DockerClusterTemplate{},
6565
},
6666
{
67-
name: "set LoadBalancer.ImageRepository if lbImageRepository is set",
67+
name: "set LoadBalancer.ImageRepository if imageRepository is set",
6868
template: &infrav1.DockerClusterTemplate{},
6969
variables: map[string]apiextensionsv1.JSON{
70-
"lbImageRepository": {Raw: toJSON("testImage")},
70+
"imageRepository": {Raw: toJSON("testImage")},
7171
},
7272
expectedTemplate: &infrav1.DockerClusterTemplate{
7373
Spec: infrav1.DockerClusterTemplateSpec{
@@ -353,8 +353,8 @@ func TestHandler_GeneratePatches(t *testing.T) {
353353
}),
354354
newVariable("kubeadmControlPlaneMaxSurge", "3"),
355355
}
356-
lbImageRepositoryVar := []runtimehooksv1.Variable{
357-
newVariable("lbImageRepository", "docker.io"),
356+
imageRepositoryVar := []runtimehooksv1.Variable{
357+
newVariable("imageRepository", "docker.io"),
358358
}
359359
machineDeploymentVars123 := []runtimehooksv1.Variable{
360360
newVariable(variables.BuiltinsName, variables.Builtins{
@@ -399,7 +399,7 @@ func TestHandler_GeneratePatches(t *testing.T) {
399399
requestItem("1", kubeadmControlPlaneTemplate, controlPlaneVarsV123WithMaxSurge),
400400
requestItem("2", dockerMachineTemplate, controlPlaneVarsV123WithMaxSurge),
401401
requestItem("3", dockerMachineTemplate, machineDeploymentVars123),
402-
requestItem("4", dockerClusterTemplate, lbImageRepositoryVar),
402+
requestItem("4", dockerClusterTemplate, imageRepositoryVar),
403403
requestItem("5", kubeadmConfigTemplate, machineDeploymentVars123),
404404
},
405405
expectedResponse: &runtimehooksv1.GeneratePatchesResponse{

test/extension/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ func main() {
178178
os.Exit(1)
179179
}
180180

181+
if err := webhookServer.AddExtensionHandler(server.ExtensionHandler{
182+
Hook: runtimehooksv1.DiscoverVariables,
183+
Name: "discover-variables",
184+
HandlerFunc: topologyMutationExtensionHandlers.DiscoverVariables,
185+
}); err != nil {
186+
setupLog.Error(err, "error adding handler")
187+
os.Exit(1)
188+
}
189+
181190
// Lifecycle Hooks
182191

183192
// Gets a client to access the Kubernetes cluster where this RuntimeExtension will be deployed to;

0 commit comments

Comments
 (0)