Skip to content

fix: Handle multiple meta mutators cleanly #159

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

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ issues:
- path: "api/*"
linters:
- gochecknoinits
# Idiomatic to use init functions to register APIs with scheme
- text: "hugeParam: holderRef is heavy"
linters:
- gocritic
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func main() {
}
// This metaPatchHandlers combines all other patch and variable handlers under a single handler.
// It allows to specify configuration under a single variable.
metaPatchHandlers := []mutation.GeneratePatches{
metaPatchHandlers := []mutation.MetaMutater{
httpproxy.NewMetaPatch(mgr.GetClient()),
extraapiservercertsans.NewMetaPatch(),
auditpolicy.NewPatch(),
Expand Down
25 changes: 25 additions & 0 deletions common/pkg/capi/clustertopology/handlers/cluster_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package handlers

import (
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func ClusterKeyFromReq(req *runtimehooksv1.GeneratePatchesRequest) client.ObjectKey {
for i := range req.Items {
item := req.Items[i]
if item.HolderReference.Kind == "Cluster" &&
item.HolderReference.APIVersion == capiv1.GroupVersion.String() {
return client.ObjectKey{
Namespace: item.HolderReference.Namespace,
Name: item.HolderReference.Name,
}
}
}

return client.ObjectKey{}
}
72 changes: 51 additions & 21 deletions common/pkg/capi/clustertopology/handlers/mutation/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,46 @@ package mutation

import (
"context"
"strings"

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
"sigs.k8s.io/cluster-api/exp/runtime/topologymutation"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/capi/clustertopology/handlers"
)

type MetaMutater interface {
Mutate(
ctx context.Context,
obj runtime.Object,
vars map[string]apiextensionsv1.JSON,
holderRef runtimehooksv1.HolderReference,
clusterKey client.ObjectKey,
) error
}

type metaGeneratePatches struct {
name string
wrappedHandlers []GeneratePatches
name string
decoder runtime.Decoder
mutaters []MetaMutater
}

func NewMetaGeneratePatchesHandler(name string, gp ...GeneratePatches) handlers.Named {
func NewMetaGeneratePatchesHandler(name string, m ...MetaMutater) handlers.Named {
scheme := runtime.NewScheme()
_ = bootstrapv1.AddToScheme(scheme)
_ = controlplanev1.AddToScheme(scheme)
return metaGeneratePatches{
name: name,
wrappedHandlers: gp,
name: name,
decoder: serializer.NewCodecFactory(scheme).UniversalDecoder(
controlplanev1.GroupVersion,
bootstrapv1.GroupVersion,
),
mutaters: m,
}
}

Expand All @@ -33,20 +57,26 @@ func (mgp metaGeneratePatches) GeneratePatches(
req *runtimehooksv1.GeneratePatchesRequest,
resp *runtimehooksv1.GeneratePatchesResponse,
) {
for _, h := range mgp.wrappedHandlers {
wrappedResp := &runtimehooksv1.GeneratePatchesResponse{}
h.GeneratePatches(ctx, req, wrappedResp)
resp.Items = append(resp.Items, wrappedResp.Items...)
if wrappedResp.Message != "" {
resp.Message = strings.TrimPrefix(resp.Message+"\n"+wrappedResp.Message, "\n")
}
resp.Status = wrappedResp.Status
if resp.Status == runtimehooksv1.ResponseStatusFailure {
return
}
}
clusterKey := handlers.ClusterKeyFromReq(req)

if resp.Status == "" {
resp.Status = runtimehooksv1.ResponseStatusSuccess
}
topologymutation.WalkTemplates(
ctx,
mgp.decoder,
req,
resp,
func(
ctx context.Context,
obj runtime.Object,
vars map[string]apiextensionsv1.JSON,
holderRef runtimehooksv1.HolderReference,
) error {
for _, h := range mgp.mutaters {
if err := h.Mutate(ctx, obj, vars, holderRef, clusterKey); err != nil {
return err
}
}

return nil
},
)
}
Loading