Skip to content

Commit

Permalink
Don't delete CRD resources
Browse files Browse the repository at this point in the history
Just delete CRDs if deleteCRDResources is set to true in the fleet.yaml

Add the helm.sh/resource-policy annotation for CRDs if deleteCRDResources is false or not set. This will prevent helm from deleting CRDs when a Bundle is deleted

Signed-off-by: raul <raul.cabello@suse.com>
  • Loading branch information
raulcabello committed Dec 19, 2023
1 parent 7447a32 commit f94aa87
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
16 changes: 16 additions & 0 deletions charts/fleet-crd/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ spec:
enforce or lock down the deployment to a specific namespace.
nullable: true
type: string
deleteCRDResources:
description: DeleteCRDResources deletes CDRs. Warning! this
will also delete all your Custom Resources.
type: boolean
diff:
description: Diff can be used to ignore the modified state of
objects which are amended at runtime.
Expand Down Expand Up @@ -464,6 +468,10 @@ spec:
enforce or lock down the deployment to a specific namespace.
nullable: true
type: string
deleteCRDResources:
description: DeleteCRDResources deletes CDRs. Warning! this
will also delete all your Custom Resources.
type: boolean
diff:
description: Diff can be used to ignore the modified state of
objects which are amended at runtime.
Expand Down Expand Up @@ -1105,6 +1113,10 @@ spec:
or lock down the deployment to a specific namespace.
nullable: true
type: string
deleteCRDResources:
description: DeleteCRDResources deletes CDRs. Warning! this will
also delete all your Custom Resources.
type: boolean
dependsOn:
description: DependsOn refers to the bundles which must be ready
before this bundle can be deployed.
Expand Down Expand Up @@ -1873,6 +1885,10 @@ spec:
namespace.
nullable: true
type: string
deleteCRDResources:
description: DeleteCRDResources deletes CDRs. Warning! this
will also delete all your Custom Resources.
type: boolean
diff:
description: Diff can be used to ignore the modified state
of objects which are amended at runtime.
Expand Down
6 changes: 5 additions & 1 deletion internal/helmdeployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
KeepResourcesAnnotation = "fleet.cattle.io/keep-resources"
HelmUpgradeInterruptedError = "another operation (install/upgrade/rollback) is in progress"
MaxHelmHistory = 2
CRDKind = "CustomResourceDefinition"
)

var (
Expand Down Expand Up @@ -166,6 +167,10 @@ func (p *postRender) Run(renderedManifests *bytes.Buffer) (modifiedManifests *by
if err != nil {
return nil, err
}
if !p.opts.DeleteCRDResources &&
obj.GetObjectKind().GroupVersionKind().Kind == CRDKind {
annotations[kube.ResourcePolicyAnno] = kube.KeepPolicy
}
m.SetLabels(mergeMaps(m.GetLabels(), labels))
m.SetAnnotations(mergeMaps(m.GetAnnotations(), annotations))

Expand All @@ -185,7 +190,6 @@ func (p *postRender) Run(renderedManifests *bytes.Buffer) (modifiedManifests *by
m.SetNamespace(p.opts.TargetNamespace)
}
}

data, err = yaml.ToBytes(objs)
return bytes.NewBuffer(data), err
}
Expand Down
99 changes: 98 additions & 1 deletion internal/helmdeployer/deployer_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package helmdeployer

import (
"bytes"
"fmt"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
"github.com/rancher/fleet/internal/manifest"
"github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/wrangler/pkg/yaml"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/kube"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime"
)

func TestValuesFrom(t *testing.T) {
Expand Down Expand Up @@ -60,3 +70,90 @@ func TestValuesFrom(t *testing.T) {
totalValues = mergeValues(totalValues, configMapValues)
a.Equal(expected, totalValues)
}

func TestPostRenderer_Run_DeleteCRDs(t *testing.T) {
tests := map[string]struct {
crd *apiextensionsv1.CustomResourceDefinition
opts v1alpha1.BundleDeploymentOptions
expectedAnnotations map[string]string
}{
"default (no DeleteCRDResources specified)": {
crd: &apiextensionsv1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
Kind: CRDKind,
APIVersion: "apiextensions.k8s.io/v1",
},
},
opts: v1alpha1.BundleDeploymentOptions{},
expectedAnnotations: map[string]string{
kube.ResourcePolicyAnno: kube.KeepPolicy,
"objectset.rio.cattle.io/id": "-",
},
},
"DeleteCRDResources set to true": {
crd: &apiextensionsv1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
Kind: CRDKind,
APIVersion: "apiextensions.k8s.io/v1",
},
},
opts: v1alpha1.BundleDeploymentOptions{
DeleteCRDResources: true,
},
expectedAnnotations: map[string]string{
"objectset.rio.cattle.io/id": "-",
},
},
"DeleteCRDResources set to false": {
crd: &apiextensionsv1.CustomResourceDefinition{
TypeMeta: metav1.TypeMeta{
Kind: CRDKind,
APIVersion: "apiextensions.k8s.io/v1",
},
},
opts: v1alpha1.BundleDeploymentOptions{
DeleteCRDResources: false,
},
expectedAnnotations: map[string]string{
kube.ResourcePolicyAnno: kube.KeepPolicy,
"objectset.rio.cattle.io/id": "-",
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
data, err := yaml.ToBytes([]kruntime.Object{test.crd})
if err != nil {
t.Errorf("unexpected error %v", err)
}
renderedManifests := bytes.NewBuffer(data)

pr := postRender{
manifest: &manifest.Manifest{
Resources: []v1alpha1.BundleResource{},
},
chart: &chart.Chart{},
opts: test.opts,
}
postRenderedManifests, err := pr.Run(renderedManifests)
if err != nil {
t.Errorf("unexpected error %v", err)
}

data = postRenderedManifests.Bytes()
objs, err := yaml.ToObjects(bytes.NewBuffer(data))
if err != nil {
t.Errorf("unexpected error %v", err)
}

m, err := meta.Accessor(objs[0])
if err != nil {
t.Errorf("unexpected error %v", err)
}
if !cmp.Equal(m.GetAnnotations(), test.expectedAnnotations) {
t.Errorf("expected %s, got %s", test.expectedAnnotations, m.GetAnnotations())
}
})
}
}
3 changes: 3 additions & 0 deletions pkg/apis/fleet.cattle.io/v1alpha1/bundledeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type BundleDeploymentOptions struct {

// NamespaceAnnotations are annotations that will be appended to the namespace created by Fleet.
NamespaceAnnotations *map[string]string `json:"namespaceAnnotations,omitempty"`

// DeleteCRDResources deletes CDRs. Warning! this will also delete all your Custom Resources.
DeleteCRDResources bool `json:"deleteCRDResources,omitempty"`
}

type DiffOptions struct {
Expand Down

0 comments on commit f94aa87

Please sign in to comment.