Skip to content

Commit

Permalink
Extending integration tests for processing of deleting of Runtime CR …
Browse files Browse the repository at this point in the history
…and delete GardenerCluster CR and Shoot
  • Loading branch information
koala7659 committed Aug 1, 2024
1 parent aa11474 commit a67ab8d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fsm
import (
"context"
"fmt"
"k8s.io/utils/ptr"
"time"

gardener "github.com/gardener/gardener/pkg/apis/core/v1beta1"
Expand Down Expand Up @@ -57,7 +58,7 @@ var _ = Describe("KIM sFnCreateKubeconfig", func() {
},
Spec: gardener.ShootSpec{
DNS: &gardener.DNS{
Domain: ptrTo("test-domain"),
Domain: ptr.To("test-domain"),
},
},
}
Expand Down
69 changes: 57 additions & 12 deletions internal/controller/runtime/runtime_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package runtime

import (
"context"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"time"

gardener "github.com/gardener/gardener/pkg/apis/core/v1beta1"
Expand All @@ -40,16 +41,7 @@ var _ = Describe("Runtime Controller", func() {
Namespace: "default",
}

AfterEach(func() {
resource := &imv1.Runtime{}
err := k8sClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())

})

It("Should successfully create new Shoot from provided Runtime and set Ready status on CR", func() {

By("Setup the fake gardener client for Provisioning")
setupGardenerTestClientForProvisioning()

By("Create Runtime CR")
Expand Down Expand Up @@ -101,6 +93,7 @@ var _ = Describe("Runtime Controller", func() {
return false
}

// the second controller normally should do that
if gardenCluster.Status.State != imv1.ReadyState {
gardenCluster.Status.State = imv1.ReadyState
k8sClient.Status().Update(ctx, &gardenCluster)
Expand Down Expand Up @@ -173,9 +166,61 @@ var _ = Describe("Runtime Controller", func() {
Expect(customTracker.IsSequenceFullyUsed()).To(BeTrue())

// next test will be for runtime deletion
//
// By("Delete Runtime CR")
// Expect(k8sClient.Delete(ctx, &runtime)).To(Succeed())

By("Process deleting of Runtime CR and delete GardenerCluster CR and Shoot")
setupGardenerTestClientForDelete()

Expect(k8sClient.Delete(ctx, &runtime)).To(Succeed())

// should delete GardenerCluster CR and go into RuntimeStateTerminating state with condition GardenerClusterCRDeleted
Eventually(func() bool {
runtime := imv1.Runtime{}
if err := k8sClient.Get(ctx, typeNamespacedName, &runtime); err != nil {
return false
}

// check if GardenerCluster CR is deleted
gardenCluster := imv1.GardenerCluster{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: runtime.Labels[imv1.LabelKymaRuntimeID], Namespace: runtime.Namespace}, &gardenCluster)
if err == nil || !k8serrors.IsNotFound(err) {
return false
}

// check runtime state
if runtime.Status.State != imv1.RuntimeStateTerminating {
return false
}

if !runtime.IsConditionSet(imv1.ConditionTypeRuntimeDeprovisioned, imv1.ConditionReasonGardenerCRDeleted) {
return false
}

return true
}, time.Second*300, time.Second*3).Should(BeTrue())

// should delete Shoot and go into RuntimeStateTerminating state with condition GardenerClusterCRDeleted
Eventually(func() bool {
runtime := imv1.Runtime{}
if err := k8sClient.Get(ctx, typeNamespacedName, &runtime); err != nil {
return false
}

if !runtime.IsConditionSet(imv1.ConditionTypeRuntimeDeprovisioned, imv1.ConditionReasonGardenerShootDeleted) {
return false
}
return true
}, time.Second*300, time.Second*3).Should(BeTrue())

// Make sure the instance is finally deleted
Eventually(func() bool {
runtime := imv1.Runtime{}
if err := k8sClient.Get(ctx, typeNamespacedName, &runtime); err == nil {
return false
}
return true
}, time.Second*300, time.Second*3).Should(BeTrue())

Expect(customTracker.IsSequenceFullyUsed()).To(BeTrue())
})
})
})
Expand Down
51 changes: 37 additions & 14 deletions internal/controller/runtime/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package runtime

import (
"context"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"path/filepath"
"testing"
"time"

gardener_api "github.com/gardener/gardener/pkg/apis/core/v1beta1"
infrastructuremanagerv1 "github.com/kyma-project/infrastructure-manager/api/v1"
Expand Down Expand Up @@ -137,6 +139,12 @@ func setupGardenerTestClientForUpdate() {
setupShootClientWithSequence(shoots)
}

func setupGardenerTestClientForDelete() {
baseShoot := getBaseShootForTestingSequence()
shoots := fixShootsSequenceForDelete(&baseShoot)
setupShootClientWithSequence(shoots)
}

func setupShootClientWithSequence(shoots []*gardener_api.Shoot) {
clientScheme := runtime.NewScheme()
_ = gardener_api.AddToScheme(clientScheme)
Expand All @@ -159,20 +167,6 @@ func getBaseShootForTestingSequence() gardener_api.Shoot {
return convertedShoot
}

// func setupGardenerTestClientForDeleting() {
// runtimeStub := CreateRuntimeStub("test-resource")
// converterConfig := fixConverterConfigForTests()
// converter := gardener_shoot.NewConverter(converterConfig)
// convertedShoot, err := converter.ToShoot(*runtimeStub)
// if err != nil {
// panic(err)
// }
//
// shoots := fixGardenerShootsForProvisioning(&convertedShoot)
//
// objectTestTracker.SetShootListForTracker(shoots)
//}

func fixShootsSequenceForProvisioning(shoot *gardener_api.Shoot) []*gardener_api.Shoot {
var missingShoot *gardener_api.Shoot
initialisedShoot := shoot.DeepCopy()
Expand Down Expand Up @@ -232,6 +226,35 @@ func fixShootsSequenceForUpdate(shoot *gardener_api.Shoot) []*gardener_api.Shoot
return []*gardener_api.Shoot{pendingShoot, processingShoot, readyShoot, readyShoot}
}

func fixShootsSequenceForDelete(shoot *gardener_api.Shoot) []*gardener_api.Shoot {
currentShoot := shoot.DeepCopy()

currentShoot.Spec.DNS = &gardener_api.DNS{
Domain: ptr.To("test.domain"),
}

// To workaround limitation that apply patches are not supported in the fake client.
// We need to set the annotation manually. https://github.com/kubernetes/kubernetes/issues/115598
currentShoot.Annotations = map[string]string{
"confirmation.gardener.cloud/deletion": "true",
}

currentShoot.Status = gardener_api.ShootStatus{
LastOperation: &gardener_api.LastOperation{
Type: gardener_api.LastOperationTypeCreate,
State: gardener_api.LastOperationStateSucceeded,
},
}

pendingDeleteShoot := currentShoot.DeepCopy()

pendingDeleteShoot.SetDeletionTimestamp(&metav1.Time{Time: time.Now()})
pendingDeleteShoot.Status.LastOperation.Type = gardener_api.LastOperationTypeDelete
pendingDeleteShoot.Status.LastOperation.State = gardener_api.LastOperationStatePending

return []*gardener_api.Shoot{currentShoot, currentShoot, currentShoot, pendingDeleteShoot, nil}
}

func fixConverterConfigForTests() gardener_shoot.ConverterConfig {
return gardener_shoot.ConverterConfig{
Kubernetes: gardener_shoot.KubernetesConfig{
Expand Down
16 changes: 16 additions & 0 deletions internal/controller/runtime/test_client_obj_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,19 @@ func (t *CustomTracker) Get(gvr schema.GroupVersionResource, ns, name string) (r
}
return t.ObjectTracker.Get(gvr, ns, name)
}

func (t *CustomTracker) Delete(gvr schema.GroupVersionResource, ns, name string) error {
t.mu.Lock()
defer t.mu.Unlock()

if gvr.Resource == "shoots" {
for index, shoot := range t.shootSequence {
if shoot != nil && shoot.Name == name {
t.shootSequence[index] = nil
return nil
}
}
return k8serrors.NewNotFound(schema.GroupResource{}, "")
}
return t.ObjectTracker.Delete(gvr, ns, name)
}

0 comments on commit a67ab8d

Please sign in to comment.