Skip to content

Commit

Permalink
[dependencies/store] Add DeleteAll func
Browse files Browse the repository at this point in the history
  • Loading branch information
davidor committed Jul 27, 2022
1 parent 71b9033 commit d3c8099
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
43 changes: 43 additions & 0 deletions controllers/datadogagent/dependencies/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type StoreClient interface {
GetOrCreate(kind kubernetes.ObjectKind, namespace, name string) (client.Object, bool)
GetVersionInfo() string
Delete(kind kubernetes.ObjectKind, namespace string, name string) bool
DeleteAll(ctx context.Context, k8sClient client.Client) []error
}

// NewStore returns a new Store instance
Expand Down Expand Up @@ -278,6 +279,48 @@ func (ds *Store) GetVersionInfo() string {
return ds.versionInfo.GitVersion
}

// DeleteAll deletes all the resources that are in the Store
func (ds *Store) DeleteAll(ctx context.Context, k8sClient client.Client) []error {
ds.mutex.RLock()
defer ds.mutex.RUnlock()

var objsToDelete []client.Object

for _, kind := range kubernetes.GetResourcesKind(ds.supportCilium) {
requirementLabel, _ := labels.NewRequirement(operatorStoreLabelKey, selection.Exists, nil)
listOptions := &client.ListOptions{
LabelSelector: labels.NewSelector().Add(*requirementLabel),
}
objList := kubernetes.ObjectListFromKind(kind)
if err := k8sClient.List(ctx, objList, listOptions); err != nil {
return []error{err}
}

items, err := apimeta.ExtractList(objList)
if err != nil {
return []error{err}
}

for _, objAPIServer := range items {
objMeta, _ := apimeta.Accessor(objAPIServer)

idObj := buildID(objMeta.GetNamespace(), objMeta.GetName())
if _, found := ds.deps[kind][idObj]; found {
partialObj := &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Name: objMeta.GetName(),
Namespace: objMeta.GetNamespace(),
},
}
partialObj.TypeMeta.SetGroupVersionKind(objAPIServer.GetObjectKind().GroupVersionKind())
objsToDelete = append(objsToDelete, partialObj)
}
}
}

return deleteObjects(ctx, k8sClient, objsToDelete)
}

func listObjectToDelete(objList client.ObjectList, cacheObjects map[string]client.Object) ([]client.Object, error) {
items, err := apimeta.ExtractList(objList)
if err != nil {
Expand Down
131 changes: 131 additions & 0 deletions controllers/datadogagent/dependencies/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"testing"

"github.com/DataDog/datadog-operator/apis/datadoghq/v2alpha1"
testutils "github.com/DataDog/datadog-operator/controllers/datadogagent/testutils"
"github.com/DataDog/datadog-operator/pkg/kubernetes"
assert "github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -585,3 +587,132 @@ func TestStore_GetOrCreate(t *testing.T) {
})
}
}

func TestStore_DeleteAll(t *testing.T) {
testConfigMap1 := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns1",
Name: "some_name",
Labels: map[string]string{
operatorStoreLabelKey: "true",
},
},
}

testConfigMap2 := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns2",
Name: "another_name",
Labels: map[string]string{
operatorStoreLabelKey: "true",
},
},
}

testStore := map[kubernetes.ObjectKind]map[string]client.Object{
kubernetes.ConfigMapKind: {
"ns1/some_name": testConfigMap1,
"ns2/another_name": testConfigMap2,
},
}

// ConfigMap not included in testStore
testConfigMap3 := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns3",
Name: "some_name",
Labels: map[string]string{
operatorStoreLabelKey: "true",
},
},
}

tests := []struct {
name string
dependenciesStore map[kubernetes.ObjectKind]map[string]client.Object
existingObjects []client.Object
objectsExpectedToBeDeleted []client.Object
objectsExpectedNotToBeDeleted []client.Object
}{
{
name: "deletes all the objects in the store",
dependenciesStore: testStore,
existingObjects: []client.Object{
testConfigMap1,
testConfigMap2,
},
objectsExpectedToBeDeleted: []client.Object{
testConfigMap1,
testConfigMap2,
},
},
{
name: "does not delete objects that are not in the store",
dependenciesStore: testStore,
existingObjects: []client.Object{
testConfigMap1,
testConfigMap2,
testConfigMap3, // Not in dependenciesStore
},
objectsExpectedToBeDeleted: []client.Object{
testConfigMap1,
testConfigMap2,
},
objectsExpectedNotToBeDeleted: []client.Object{
testConfigMap3, // Not in dependenciesStore
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
k8sClient := fake.NewClientBuilder().
WithScheme(testutils.TestScheme(true)).
WithObjects(test.existingObjects...).
Build()

store := &Store{
deps: test.dependenciesStore,
}

errs := store.DeleteAll(context.TODO(), k8sClient)
assert.Empty(t, errs)

for _, expectedToBeDeleted := range test.objectsExpectedToBeDeleted {
err := k8sClient.Get(
context.TODO(),
client.ObjectKey{
Namespace: expectedToBeDeleted.GetNamespace(),
Name: expectedToBeDeleted.GetName(),
},
&corev1.ConfigMap{}, // Adapt according to test input objects
)
assert.True(t, errors.IsNotFound(err))
}

for _, expectedToExist := range test.objectsExpectedNotToBeDeleted {
err := k8sClient.Get(
context.TODO(),
client.ObjectKey{
Namespace: expectedToExist.GetNamespace(),
Name: expectedToExist.GetName(),
},
&corev1.ConfigMap{}, // Adapt according to test input objects
)
assert.NoError(t, err)
}
})
}
}

0 comments on commit d3c8099

Please sign in to comment.