This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Make containerd restart its own patch
- Loading branch information
1 parent
f04cd61
commit 472206a
Showing
10 changed files
with
202 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 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 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 globalMirrorPatchHandler struct{} | ||
|
||
func NewPatch() *globalMirrorPatchHandler { | ||
return &globalMirrorPatchHandler{} | ||
} | ||
|
||
func (h *globalMirrorPatchHandler) 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 | ||
} |
27 changes: 27 additions & 0 deletions
27
pkg/handlers/generic/mutation/containerdrestart/restart.go
This file contains 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.
71 changes: 71 additions & 0 deletions
71
pkg/handlers/generic/mutation/containerdrestart/tests/generate_patches.go
This file contains 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,71 @@ | ||
// Copyright 2023 D2iQ, Inc. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tests | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/onsi/gomega" | ||
|
||
"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" | ||
"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/mutation/containerdrestart" | ||
) | ||
|
||
func TestGeneratePatches( | ||
t *testing.T, | ||
generatorFunc func() mutation.GeneratePatches, | ||
) { | ||
t.Helper() | ||
|
||
capitest.ValidateGeneratePatches( | ||
t, | ||
generatorFunc, | ||
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", containerdrestart.ContainerdRestartScriptOnRemote, | ||
), | ||
), | ||
}, | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands", | ||
ValueMatcher: gomega.ContainElements( | ||
containerdrestart.ContainerdRestartScriptOnRemoteCommand, | ||
), | ||
}, | ||
}, | ||
}, | ||
capitest.PatchTestDef{ | ||
Name: "restart script and command added to worker node kubeadm config template", | ||
RequestItem: request.NewKubeadmControlPlaneTemplateRequestItem(""), | ||
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{ | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/kubeadmConfigSpec/files", | ||
ValueMatcher: gomega.ContainElements( | ||
gomega.HaveKeyWithValue( | ||
"path", containerdrestart.ContainerdRestartScriptOnRemote, | ||
), | ||
), | ||
}, | ||
{ | ||
Operation: "add", | ||
Path: "/spec/template/spec/kubeadmConfigSpec/preKubeadmCommands", | ||
ValueMatcher: gomega.ContainElements( | ||
containerdrestart.ContainerdRestartScriptOnRemoteCommand, | ||
), | ||
}, | ||
}, | ||
}, | ||
) | ||
} |
This file contains 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 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 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 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