This repository was archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Make containerd restart its own patch #18
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ca85110
feat: Make containerd restart its own patch
dlipovetsky db01c53
fix: unit tests for kubeadmconfigtemplate with containerdrestart patch
supershal cbea9a0
fix: add comment for always add containerd patch
supershal ec630e2
test: move unit test to their own package
supershal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package containerdrestart | ||
|
||
import ( | ||
"context" | ||
|
||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
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" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors" | ||
) | ||
|
||
type containerdRestartPatchHandler struct{} | ||
|
||
func NewPatch() *containerdRestartPatchHandler { | ||
return &containerdRestartPatchHandler{} | ||
} | ||
|
||
func (h *containerdRestartPatchHandler) Mutate( | ||
ctx context.Context, | ||
obj *unstructured.Unstructured, | ||
vars map[string]apiextensionsv1.JSON, | ||
holderRef runtimehooksv1.HolderReference, | ||
clusterKey ctrlclient.ObjectKey, | ||
) error { | ||
log := ctrl.LoggerFrom(ctx).WithValues( | ||
"holderRef", holderRef, | ||
) | ||
|
||
file, command := generateContainerdRestartScript() | ||
|
||
if err := patches.MutateIfApplicable( | ||
obj, vars, &holderRef, selectors.ControlPlane(), log, | ||
func(obj *controlplanev1.KubeadmControlPlaneTemplate) error { | ||
log.WithValues( | ||
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(), | ||
"patchedObjectName", ctrlclient.ObjectKeyFromObject(obj), | ||
).Info("adding containerd restart script to control plane kubeadm config spec") | ||
obj.Spec.Template.Spec.KubeadmConfigSpec.Files = append( | ||
obj.Spec.Template.Spec.KubeadmConfigSpec.Files, | ||
file, | ||
) | ||
|
||
log.WithValues( | ||
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(), | ||
"patchedObjectName", ctrlclient.ObjectKeyFromObject(obj), | ||
).Info("adding containerd restart command to control plane kubeadm config spec") | ||
obj.Spec.Template.Spec.KubeadmConfigSpec.PreKubeadmCommands = append( | ||
obj.Spec.Template.Spec.KubeadmConfigSpec.PreKubeadmCommands, | ||
command, | ||
) | ||
|
||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
if err := patches.MutateIfApplicable( | ||
obj, vars, &holderRef, selectors.WorkersKubeadmConfigTemplateSelector(), log, | ||
func(obj *bootstrapv1.KubeadmConfigTemplate) error { | ||
log.WithValues( | ||
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(), | ||
"patchedObjectName", ctrlclient.ObjectKeyFromObject(obj), | ||
).Info("adding containerd restart script to worker node kubeadm config template") | ||
obj.Spec.Template.Spec.Files = append(obj.Spec.Template.Spec.Files, file) | ||
|
||
log.WithValues( | ||
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(), | ||
"patchedObjectName", ctrlclient.ObjectKeyFromObject(obj), | ||
).Info("adding containerd restart command to worker node kubeadm config template") | ||
obj.Spec.Template.Spec.PreKubeadmCommands = append(obj.Spec.Template.Spec.PreKubeadmCommands, command) | ||
|
||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
97 changes: 97 additions & 0 deletions
97
pkg/handlers/generic/mutation/containerdrestart/inject_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package containerdrestart | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" | ||
|
||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest/request" | ||
) | ||
|
||
func TestContainerdRestartPatch(t *testing.T) { | ||
gomega.RegisterFailHandler(Fail) | ||
RunSpecs(t, "Containerd restart mutator suite") | ||
} | ||
|
||
var _ = Describe("Generate Containerd restart patches", func() { | ||
// only add aws region patch | ||
patchGenerator := func() mutation.GeneratePatches { | ||
return mutation.NewMetaGeneratePatchesHandler("", NewPatch()).(mutation.GeneratePatches) | ||
} | ||
|
||
testDefs := []capitest.PatchTestDef{ | ||
{ | ||
Name: "restart script and command added to control plane kubeadm config spec", | ||
RequestItem: request.NewKubeadmControlPlaneTemplateRequestItem(""), | ||
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{ | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/kubeadmConfigSpec/files", | ||
ValueMatcher: gomega.ContainElements( | ||
gomega.HaveKeyWithValue( | ||
"path", ContainerdRestartScriptOnRemote, | ||
), | ||
), | ||
}, | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands", | ||
ValueMatcher: gomega.ContainElements( | ||
ContainerdRestartScriptOnRemoteCommand, | ||
), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Name: "restart script and command added to worker node kubeadm config template", | ||
Vars: []runtimehooksv1.Variable{ | ||
capitest.VariableWithValue( | ||
"builtin", | ||
map[string]any{ | ||
"machineDeployment": map[string]any{ | ||
"class": "*", | ||
}, | ||
}, | ||
), | ||
}, | ||
RequestItem: request.NewKubeadmConfigTemplateRequestItem(""), | ||
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{ | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/files", | ||
ValueMatcher: gomega.ContainElements( | ||
gomega.HaveKeyWithValue( | ||
"path", ContainerdRestartScriptOnRemote, | ||
), | ||
), | ||
}, | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/preKubeadmCommands", | ||
ValueMatcher: gomega.ContainElements( | ||
ContainerdRestartScriptOnRemoteCommand, | ||
), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// create test node for each case | ||
for testIdx := range testDefs { | ||
tt := testDefs[testIdx] | ||
It(tt.Name, func() { | ||
capitest.AssertGeneratePatches( | ||
GinkgoT(), | ||
patchGenerator, | ||
&tt, | ||
) | ||
}) | ||
} | ||
}) |
27 changes: 27 additions & 0 deletions
27
pkg/handlers/generic/mutation/containerdrestart/restart.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package containerdrestart | ||
|
||
import ( | ||
_ "embed" | ||
|
||
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" | ||
) | ||
|
||
const ( | ||
ContainerdRestartScriptOnRemote = "/etc/containerd/restart.sh" | ||
ContainerdRestartScriptOnRemoteCommand = "/bin/bash " + ContainerdRestartScriptOnRemote | ||
) | ||
|
||
//go:embed templates/containerd-restart.sh | ||
var containerdRestartScript []byte | ||
|
||
//nolint:gocritic // no need for named return values | ||
func generateContainerdRestartScript() (bootstrapv1.File, string) { | ||
return bootstrapv1.File{ | ||
Path: ContainerdRestartScriptOnRemote, | ||
Content: string(containerdRestartScript), | ||
Permissions: "0700", | ||
}, | ||
ContainerdRestartScriptOnRemoteCommand | ||
} |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.