Skip to content
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

Cleanup Dynakube's config and enrichment secrets when deleting it #4047

Merged
merged 8 commits into from
Nov 14, 2024
4 changes: 2 additions & 2 deletions pkg/controllers/dynakube/injection/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ const (

testHost = "test-host"

testDynakube = "test-name"
testDynakube2 = "test-name2"
testDynakube = "test-dynakube"
testDynakube2 = "test-dynakube2"
testNamespace = "test-namespace"
testNamespace2 = "test-namespace2"

Expand Down
22 changes: 22 additions & 0 deletions pkg/injection/namespace/mapper/dynakube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (

"github.com/Dynatrace/dynatrace-operator/pkg/api/scheme/fake"
"github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta3/dynakube"
"github.com/Dynatrace/dynatrace-operator/pkg/consts"
dtwebhook "github.com/Dynatrace/dynatrace-operator/pkg/webhook"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
Expand Down Expand Up @@ -177,4 +179,24 @@ func TestUnmapFromDynaKube(t *testing.T) {
assert.Empty(t, ns.Labels)
assert.Len(t, ns.Annotations, 1)
})
t.Run("Remove "+consts.AgentInitSecretName+" and "+consts.EnrichmentEndpointSecretName+" secrets", func(t *testing.T) {
clt := fake.NewClient(namespace, namespace2)
ctx := context.Background()

namespaces, err := GetNamespacesForDynakube(ctx, clt, dk.Name)
require.NoError(t, err)

clt.Create(ctx, &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: consts.AgentInitSecretName, Namespace: namespace.Name}})
clt.Create(ctx, &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: consts.EnrichmentEndpointSecretName, Namespace: namespace.Name}})

dm := NewDynakubeMapper(ctx, clt, clt, "dynatrace", dk)
err = dm.UnmapFromDynaKube(namespaces)
require.NoError(t, err)

var secret corev1.Secret
err = clt.Get(ctx, types.NamespacedName{Name: consts.AgentInitSecretName}, &secret)
assert.True(t, k8serrors.IsNotFound(err))
err = clt.Get(ctx, types.NamespacedName{Name: consts.EnrichmentEndpointSecretName}, &secret)
assert.True(t, k8serrors.IsNotFound(err))
})
}
12 changes: 12 additions & 0 deletions pkg/injection/namespace/mapper/dynakubes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"

"github.com/Dynatrace/dynatrace-operator/pkg/api/v1beta3/dynakube"
"github.com/Dynatrace/dynatrace-operator/pkg/consts"
k8ssecret "github.com/Dynatrace/dynatrace-operator/pkg/util/kubeobjects/secret"
dtwebhook "github.com/Dynatrace/dynatrace-operator/pkg/webhook"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -57,6 +59,16 @@ func (dm DynakubeMapper) UnmapFromDynaKube(namespaces []corev1.Namespace) error
if err := dm.client.Update(dm.ctx, &namespaces[i]); err != nil {
return errors.WithMessagef(err, "failed to remove label %s from namespace %s", dtwebhook.InjectionInstanceLabel, ns.Name)
}

err := k8ssecret.Query(dm.client, dm.apiReader, log).DeleteForNamespace(dm.ctx, consts.AgentInitSecretName, ns.Name)
if err != nil {
return err
}

err = k8ssecret.Query(dm.client, dm.apiReader, log).DeleteForNamespace(dm.ctx, consts.EnrichmentEndpointSecretName, ns.Name)
if err != nil {
return err
}
}

return nil
Expand Down
14 changes: 10 additions & 4 deletions pkg/util/kubeobjects/internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,22 @@ func (c Generic[T, L]) createOrUpdateForNamespaces(ctx context.Context, object T
return goerrors.Join(errs...)
}

func (c Generic[T, L]) DeleteForNamespace(ctx context.Context, objectName string, namespace string) error {
c.Log.Info("deleting object from namespace", "name", objectName, "namespace", namespace)

c.Target.SetName(objectName)
c.Target.SetNamespace(namespace)

return c.Delete(ctx, c.Target)
}
albertogdd marked this conversation as resolved.
Show resolved Hide resolved

func (c Generic[T, L]) DeleteForNamespaces(ctx context.Context, objectName string, namespaces []string) error {
c.Log.Info("deleting objects from multiple namespaces", "name", objectName, "len(namespaces)", len(namespaces))

errs := make([]error, 0, len(namespaces))

for _, namespace := range namespaces {
c.Target.SetName(objectName)
c.Target.SetNamespace(namespace)

err := c.Delete(ctx, c.Target)
err := c.DeleteForNamespace(ctx, objectName, namespace)
if err != nil {
errs = append(errs, err)
}
Expand Down
Loading