-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the cleanup command to manage finalizers
This commit is to ensures helm is capable to delete custom resources and its definition when uninstalling the CRDS chart. Signed-off-by: David Cassany <[email protected]>
- Loading branch information
1 parent
961b0c5
commit 771d048
Showing
9 changed files
with
194 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
image: | ||
empty: rancher/pause:3.1 | ||
repository: "rancher/elemental-operator" | ||
tag: "%VERSION%" | ||
imagePullPolicy: IfNotPresent | ||
|
||
global: | ||
cattle: | ||
systemDefaultRegistry: "" | ||
|
||
# used only if systemDefaultRegistry is empty | ||
registryUrl: "%%IMG_REPO%%" |
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,17 @@ | ||
{{- define "system_default_registry" -}} | ||
{{- if .Values.global.cattle.systemDefaultRegistry -}} | ||
{{- printf "%s/" .Values.global.cattle.systemDefaultRegistry -}} | ||
{{- else -}} | ||
{{- "" -}} | ||
{{- end -}} | ||
{{- end -}} | ||
|
||
{{- define "registry_url" -}} | ||
{{- if .Values.global.cattle.systemDefaultRegistry -}} | ||
{{ include "system_default_registry" . }} | ||
{{- else if .Values.registryUrl -}} | ||
{{- printf "%s/" .Values.registryUrl -}} | ||
{{- else -}} | ||
{{- "" -}} | ||
{{- end -}} | ||
{{- end -}} |
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,15 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
creationTimestamp: null | ||
name: '{{ .Release.Name }}' | ||
rules: | ||
- apiGroups: | ||
- elemental.cattle.io | ||
resources: | ||
- machineinventories | ||
verbs: | ||
- get | ||
- list | ||
- patch | ||
- update |
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,13 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: {{ .Release.Name }} | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: {{ .Release.Name }} | ||
subjects: | ||
- kind: ServiceAccount | ||
name: {{ .Release.Name }} | ||
namespace: {{.Release.Namespace}} | ||
|
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,18 @@ | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: {{ .Release.Name }} | ||
annotations: | ||
"helm.sh/hook": pre-delete | ||
"helm.sh/hook-delete-policy": hook-succeeded | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: {{ .Release.Name }} | ||
imagePullPolicy: "{{ .Values.image.imagePullPolicy }}" | ||
image: {{ template "registry_url" . }}{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }} | ||
args: | ||
- cleanup | ||
serviceAccountName: {{ .Release.Name }} | ||
restartPolicy: Never |
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,4 @@ | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: {{ .Release.Name }} |
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,12 @@ | ||
image: | ||
empty: rancher/pause:3.1 | ||
repository: "rancher/elemental-operator" | ||
tag: latest | ||
imagePullPolicy: IfNotPresent | ||
|
||
global: | ||
cattle: | ||
systemDefaultRegistry: "" | ||
|
||
# used only if systemDefaultRegistry is empty | ||
registryUrl: "" |
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,101 @@ | ||
/* | ||
Copyright © 2022 - 2023 SUSE LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cleanup | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
client "sigs.k8s.io/controller-runtime/pkg/client" | ||
runtimeconfig "sigs.k8s.io/controller-runtime/pkg/client/config" | ||
|
||
elementalv1 "github.com/rancher/elemental-operator/api/v1beta1" | ||
) | ||
|
||
// +kubebuilder:rbac:groups=elemental.cattle.io,resources=machineinventories,verbs=get;list;update;patch | ||
|
||
var ( | ||
scheme = runtime.NewScheme() | ||
cleanupLog = ctrl.Log.WithName("cleanup") | ||
) | ||
|
||
func NewCleanupCommand() *cobra.Command { | ||
|
||
cmd := &cobra.Command{ | ||
Use: "cleanup", | ||
Short: "Removes existing finalizers so CRDs deletion will not leave resources pending on finalizers management", | ||
Args: cobra.NoArgs, | ||
Run: func(_ *cobra.Command, _ []string) { | ||
cleanupRun() | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func cleanupRun() { | ||
cleanupLog.Info("starting registration") | ||
|
||
ctx := ctrl.SetupSignalHandler() | ||
|
||
restConfig, err := runtimeconfig.GetConfig() | ||
if err != nil { | ||
cleanupLog.Error(err, "failed to find kubeconfig") | ||
os.Exit(1) | ||
} | ||
|
||
err = elementalv1.AddToScheme(scheme) | ||
if err != nil { | ||
cleanupLog.Error(err, "failed setting the scheme") | ||
os.Exit(1) | ||
} | ||
|
||
cl, err := client.New(restConfig, client.Options{ | ||
Scheme: scheme, | ||
}) | ||
if err != nil { | ||
cleanupLog.Error(err, "error building restconfig") | ||
os.Exit(1) | ||
} | ||
|
||
inventoryList := &elementalv1.MachineInventoryList{} | ||
|
||
err = cl.List(ctx, inventoryList, &client.ListOptions{}) | ||
if err != nil { | ||
cleanupLog.Error(err, "failed getting MachineInventoryList") | ||
os.Exit(1) | ||
} | ||
|
||
var errs []error | ||
cleanupLog.Info("Cleaning MachineInventory resources") | ||
for _, i := range inventoryList.Items { | ||
cleanupLog.Info("Removing finalizers", "name", i.Name) | ||
patchBase := client.MergeFrom(i.DeepCopy()) | ||
i.Finalizers = []string{} | ||
err = cl.Patch(ctx, &i, patchBase) | ||
if err != nil { | ||
cleanupLog.Error(err, "failed patching MachineInventory", "name", i.Name) | ||
errs = append(errs, err) | ||
} | ||
} | ||
if errs != nil { | ||
cleanupLog.Error(err, "failed patching some resources") | ||
os.Exit(1) | ||
} | ||
} |
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