Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove usage of non-meta handlers #226

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions api/v1alpha1/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

const (
// CNIVariableName is the external patch variable name.
CNIVariableName = "cni"
)
45 changes: 2 additions & 43 deletions pkg/handlers/aws/mutation/ami/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,20 @@ import (

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
commonhandlers "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
capav1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
)

const (
// HandlerNamePatch is the name of the inject handler.
ControlPlaneHandlerNamePatch = "AWSAMISpecControlPlanePatch"
VariableName = "ami"
)

var (
_ commonhandlers.Named = &awsAMISpecPatchHandler{}
_ mutation.MetaMutator = &awsAMISpecPatchHandler{}
// VariableName is the external patch variable name.
VariableName = "ami"
)

type awsAMISpecPatchHandler struct {
Expand All @@ -52,10 +42,6 @@ func newAWSAMISpecPatchHandler(
}
}

func (h *awsAMISpecPatchHandler) Name() string {
return ControlPlaneHandlerNamePatch
}

func (h *awsAMISpecPatchHandler) Mutate(
ctx context.Context,
obj *unstructured.Unstructured,
Expand Down Expand Up @@ -113,30 +99,3 @@ func (h *awsAMISpecPatchHandler) Mutate(
},
)
}

func (h *awsAMISpecPatchHandler) GeneratePatches(
ctx context.Context,
req *runtimehooksv1.GeneratePatchesRequest,
resp *runtimehooksv1.GeneratePatchesResponse,
) {
topologymutation.WalkTemplates(
ctx,
unstructured.UnstructuredJSONScheme,
req,
resp,
func(
ctx context.Context,
obj runtime.Object,
vars map[string]apiextensionsv1.JSON,
holderRef runtimehooksv1.HolderReference,
) error {
return h.Mutate(
ctx,
obj.(*unstructured.Unstructured),
vars,
holderRef,
client.ObjectKey{},
)
},
)
}
26 changes: 0 additions & 26 deletions pkg/handlers/aws/mutation/ami/inject_control_plane_test.go

This file was deleted.

25 changes: 0 additions & 25 deletions pkg/handlers/aws/mutation/ami/inject_worker_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package clusterconfig
package ami

import (
"testing"
Expand All @@ -10,6 +10,7 @@ import (

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/testutils/capitest"
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
)

Expand All @@ -19,39 +20,7 @@ func TestVariableValidation(t *testing.T) {
clusterconfig.MetaVariableName,
ptr.To(v1alpha1.ClusterConfigSpec{AWS: &v1alpha1.AWSSpec{}}.VariableSchema()),
true,
NewVariable,
capitest.VariableTestDef{
Name: "specified region",
Vals: v1alpha1.ClusterConfigSpec{
AWS: &v1alpha1.AWSSpec{
Region: ptr.To(v1alpha1.Region("a-specified-region")),
},
},
},
capitest.VariableTestDef{
Name: "specified IAM instance profile",
Vals: v1alpha1.ClusterConfigSpec{
ControlPlane: &v1alpha1.NodeConfigSpec{
AWS: &v1alpha1.AWSNodeSpec{
IAMInstanceProfile: ptr.To(
v1alpha1.IAMInstanceProfile(
"control-plane.cluster-api-provider-aws.sigs.k8s.io",
),
),
},
},
},
},
capitest.VariableTestDef{
Name: "specified instance type",
Vals: v1alpha1.ClusterConfigSpec{
ControlPlane: &v1alpha1.NodeConfigSpec{
AWS: &v1alpha1.AWSNodeSpec{
InstanceType: ptr.To(v1alpha1.InstanceType("m5.small")),
},
},
},
},
awsclusterconfig.NewVariable,
capitest.VariableTestDef{
Name: "AMI specification",
Vals: v1alpha1.ClusterConfigSpec{
Expand Down
48 changes: 1 addition & 47 deletions pkg/handlers/aws/mutation/cni/calico/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@ import (
"github.com/go-logr/logr"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
commonhandlers "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches/selectors"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
capav1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/mutation/cni"
)

const (
Expand All @@ -38,21 +33,11 @@ type calicoPatchHandler struct {
variableFieldPath []string
}

var (
_ commonhandlers.Named = &calicoPatchHandler{}
_ mutation.GeneratePatches = &calicoPatchHandler{}
_ mutation.MetaMutator = &calicoPatchHandler{}
supershal marked this conversation as resolved.
Show resolved Hide resolved
)

func NewPatch() *calicoPatchHandler {
return newCalicoPatchHandler(cni.VariableName)
}

func NewMetaPatch() *calicoPatchHandler {
return newCalicoPatchHandler(
clusterconfig.MetaVariableName,
"addons",
cni.VariableName,
v1alpha1.CNIVariableName,
)
}

Expand All @@ -66,10 +51,6 @@ func newCalicoPatchHandler(
}
}

func (h *calicoPatchHandler) Name() string {
return HandlerNamePatch
}

func (h *calicoPatchHandler) Mutate(
ctx context.Context,
obj *unstructured.Unstructured,
Expand Down Expand Up @@ -119,33 +100,6 @@ func (h *calicoPatchHandler) Mutate(
)
}

func (h *calicoPatchHandler) GeneratePatches(
ctx context.Context,
req *runtimehooksv1.GeneratePatchesRequest,
resp *runtimehooksv1.GeneratePatchesResponse,
) {
topologymutation.WalkTemplates(
ctx,
unstructured.UnstructuredJSONScheme,
req,
resp,
func(
ctx context.Context,
obj runtime.Object,
vars map[string]apiextensionsv1.JSON,
holderRef runtimehooksv1.HolderReference,
) error {
return h.Mutate(
ctx,
obj.(*unstructured.Unstructured),
vars,
holderRef,
client.ObjectKey{},
)
},
)
}

func mutateAWSClusterTemplateFunc(log logr.Logger) func(obj *capav1.AWSClusterTemplate) error {
return func(obj *capav1.AWSClusterTemplate) error {
log.WithValues(
Expand Down
22 changes: 0 additions & 22 deletions pkg/handlers/aws/mutation/cni/calico/inject_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,25 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/d2iq-labs/capi-runtime-extensions/api/v1alpha1"
commonhandlers "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers/mutation"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/patches/selectors"
"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/variables"
capav1 "github.com/d2iq-labs/capi-runtime-extensions/common/pkg/external/sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers"
awsclusterconfig "github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/aws/clusterconfig"
"github.com/d2iq-labs/capi-runtime-extensions/pkg/handlers/generic/clusterconfig"
)

const (
// VariableName is the external patch variable name.
VariableName = "iamInstanceProfile"

// ControlPlaneHandlerNamePatch is the name of the inject handler.
ControlPlaneHandlerNamePatch = "AWSIAMInstanceProfileControlPlanePatch"
)

type awsIAMInstanceProfileControlPlanePatchHandler struct {
variableName string
variableFieldPath []string
}

var (
_ commonhandlers.Named = &awsIAMInstanceProfileControlPlanePatchHandler{}
_ mutation.GeneratePatches = &awsIAMInstanceProfileControlPlanePatchHandler{}
_ mutation.MetaMutator = &awsIAMInstanceProfileControlPlanePatchHandler{}
)

func NewControlPlaneMetaPatch() *awsIAMInstanceProfileControlPlanePatchHandler {
func NewControlPlanePatch() *awsIAMInstanceProfileControlPlanePatchHandler {
return newAWSIAMInstanceProfileControlPlanePatchHandler(
clusterconfig.MetaVariableName,
clusterconfig.MetaControlPlaneConfigName,
Expand All @@ -63,10 +51,6 @@ func newAWSIAMInstanceProfileControlPlanePatchHandler(
}
}

func (h *awsIAMInstanceProfileControlPlanePatchHandler) Name() string {
return ControlPlaneHandlerNamePatch
}

func (h *awsIAMInstanceProfileControlPlanePatchHandler) Mutate(
ctx context.Context,
obj *unstructured.Unstructured,
Expand Down Expand Up @@ -121,11 +105,3 @@ func (h *awsIAMInstanceProfileControlPlanePatchHandler) Mutate(
},
)
}

func (h *awsIAMInstanceProfileControlPlanePatchHandler) GeneratePatches(
ctx context.Context,
req *runtimehooksv1.GeneratePatchesRequest,
resp *runtimehooksv1.GeneratePatchesResponse,
) {
handlers.GeneratePatches(ctx, req, resp, h.Mutate)
}
Loading