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

KIM Integration - deprovisioning does not call provisioner if Runtime CR controlled by KIM #1148

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/broker/deprovisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewDeprovisioningProcessingQueue(ctx context.Context, workersAmount int, de
step: deprovisioning.NewCheckGardenerClusterDeletedStep(db.Operations(), cli),
},
{
step: deprovisioning.NewRemoveRuntimeStep(db.Operations(), db.Instances(), provisionerClient, cfg.Provisioner.DeprovisioningTimeout, cfg.Broker.KimConfig),
step: deprovisioning.NewRemoveRuntimeStep(db.Operations(), db.Instances(), provisionerClient, cfg.Provisioner.DeprovisioningTimeout),
},
{
step: deprovisioning.NewCheckRuntimeRemovalStep(db.Operations(), db.Instances(), provisionerClient, cfg.Provisioner.DeprovisioningTimeout),
Expand Down
3 changes: 3 additions & 0 deletions internal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ type Operation struct {

LastError kebError.LastError `json:"last_error"`

// Used during KIM integration while deprovisioning - to be removed later on when provisioner not used anymore
KimDeprovisionsOnly bool `json:"kim_deprovisions_only"`

// following fields are not stored in the storage and should be added to the Merge function
InputCreator ProvisionerInputCreator `json:"-"`
}
Expand Down
20 changes: 13 additions & 7 deletions internal/process/deprovisioning/delete_runtime_resource_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
Expand Down Expand Up @@ -55,14 +54,21 @@ func (step *DeleteRuntimeResourceStep) Run(operation internal.Operation, logger
return operation, 0, nil
}

runtime := &imv1.Runtime{
ObjectMeta: v1.ObjectMeta{
Name: resourceName,
Namespace: resourceNamespace,
},
var runtime = imv1.Runtime{}
err := step.kcpClient.Get(context.Background(), client.ObjectKey{Name: resourceName, Namespace: resourceNamespace}, &runtime)
if err != nil && !errors.IsNotFound(err) {
logger.Warnf("Unable to read runtime: %s", err)
return step.operationManager.RetryOperation(operation, err.Error(), err, 5*time.Second, 1*time.Minute, logger)
}
controlledByKimOnly := !runtime.IsControlledByProvisioner()
operation, backoff, _ := step.operationManager.UpdateOperation(operation, func(operation *internal.Operation) {
operation.KimDeprovisionsOnly = controlledByKimOnly
}, logger)
if backoff > 0 {
return operation, backoff, nil
}

err := step.kcpClient.Delete(context.Background(), runtime)
err = step.kcpClient.Delete(context.Background(), &runtime)

// check the error
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package deprovisioning

import (
"context"
"strconv"
"testing"

"github.com/kyma-project/kyma-environment-broker/internal/fixture"
Expand All @@ -23,42 +24,78 @@ func TestDeleteRuntimeResourceStep_RuntimeResourceDoesNotExists(t *testing.T) {
err := imv1.AddToScheme(scheme.Scheme)
assert.NoError(t, err)
kcpClient := fake.NewClientBuilder().Build()
op := fixture.FixDeprovisioningOperationAsOperation(fixOperationID, fixInstanceID)
op := fixture.FixDeprovisioningOperation(fixOperationID, fixInstanceID)
op.RuntimeResourceName = "runtime-name"
op.KymaResourceNamespace = "kyma-ns"
memoryStorage := storage.NewMemoryStorage()
err = memoryStorage.Operations().InsertDeprovisioningOperation(op)
assert.NoError(t, err)

log := logger.NewLogDummy()

// when
step := NewDeleteRuntimeResourceStep(memoryStorage.Operations(), kcpClient)
_, backoff, err := step.Run(op, log)
postOperation, backoff, err := step.Run(op.Operation, log)

// then

assert.NoError(t, err)
assert.Zero(t, backoff)
assertRuntimeDoesNotExists(t, kcpClient, "kyma-ns", "runtime-name")

// till provisioner may be involved
assert.False(t, postOperation.KimDeprovisionsOnly)
}

func TestDeleteRuntimeResourceStep_RuntimeResourceExists(t *testing.T) {
// given
err := imv1.AddToScheme(scheme.Scheme)
assert.NoError(t, err)
op := fixture.FixDeprovisioningOperationAsOperation(fixOperationID, fixInstanceID)
op := fixture.FixDeprovisioningOperation(fixOperationID, fixInstanceID)
op.RuntimeResourceName = "runtime-name"
op.KymaResourceNamespace = "kyma-ns"
memoryStorage := storage.NewMemoryStorage()
kcpClient := fake.NewClientBuilder().WithRuntimeObjects(fixRuntimeResource("kyma-ns", "runtime-name")).Build()
err = memoryStorage.Operations().InsertDeprovisioningOperation(op)
kcpClient := fake.NewClientBuilder().WithRuntimeObjects(fixRuntimeResourceControlledByProvisioner("kyma-ns", "runtime-name", false)).Build()
log := logger.NewLogDummy()

// when
step := NewDeleteRuntimeResourceStep(memoryStorage.Operations(), kcpClient)
_, backoff, err := step.Run(op, log)
postOperation, backoff, err := step.Run(op.Operation, log)

// then
assert.NoError(t, err)
assert.Zero(t, backoff)
assertRuntimeDoesNotExists(t, kcpClient, "kyma-ns", "runtime-name")

// till provisioner may be involved
assert.True(t, postOperation.KimDeprovisionsOnly)
}

func TestDeleteRuntimeResourceStep_RuntimeResourceExistsControlledByProvisioner(t *testing.T) {
// given
err := imv1.AddToScheme(scheme.Scheme)
assert.NoError(t, err)
op := fixture.FixDeprovisioningOperation(fixOperationID, fixInstanceID)
op.RuntimeResourceName = "runtime-name"
op.KymaResourceNamespace = "kyma-ns"
memoryStorage := storage.NewMemoryStorage()
err = memoryStorage.Operations().InsertDeprovisioningOperation(op)

kcpClient := fake.NewClientBuilder().WithRuntimeObjects(fixRuntimeResourceControlledByProvisioner("kyma-ns", "runtime-name", true)).Build()
log := logger.NewLogDummy()

// when
step := NewDeleteRuntimeResourceStep(memoryStorage.Operations(), kcpClient)
postOperation, backoff, err := step.Run(op.Operation, log)

// then
assert.NoError(t, err)
assert.Zero(t, backoff)
assertRuntimeDoesNotExists(t, kcpClient, "kyma-ns", "runtime-name")

// till provisioner may be involved
assert.False(t, postOperation.KimDeprovisionsOnly)
}

func assertRuntimeDoesNotExists(t *testing.T, kcpClient client.WithWatch, namespace string, name string) {
Expand All @@ -75,3 +112,15 @@ func fixRuntimeResource(namespace string, name string) runtime.Object {
},
}
}

func fixRuntimeResourceControlledByProvisioner(namespace string, name string, controlledByProvisioner bool) runtime.Object {
return &imv1.Runtime{
ObjectMeta: v1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
imv1.LabelControlledByProvisioner: strconv.FormatBool(controlledByProvisioner),
},
},
}
}
9 changes: 4 additions & 5 deletions internal/process/deprovisioning/remove_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ type RemoveRuntimeStep struct {
instanceStorage storage.Instances
provisionerClient provisioner.Client
provisionerTimeout time.Duration
kimConfig broker.KimConfig
}

func NewRemoveRuntimeStep(os storage.Operations, is storage.Instances, cli provisioner.Client, provisionerTimeout time.Duration, kimConfig broker.KimConfig) *RemoveRuntimeStep {
func NewRemoveRuntimeStep(os storage.Operations, is storage.Instances, cli provisioner.Client, provisionerTimeout time.Duration) *RemoveRuntimeStep {
return &RemoveRuntimeStep{
operationManager: process.NewOperationManager(os),
instanceStorage: is,
provisionerClient: cli,
provisionerTimeout: provisionerTimeout,
kimConfig: kimConfig,
}
}

Expand All @@ -39,8 +37,9 @@ func (s *RemoveRuntimeStep) Name() string {
}

func (s *RemoveRuntimeStep) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
if s.kimConfig.IsDrivenByKimOnly(broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID]) {
log.Infof("Only KIM is driving the process for plan %s, skipping", broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID])

if operation.KimDeprovisionsOnly {
log.Infof("Skipping the step because the runtime %s/%s is not controlled by the provisioner", operation.GetRuntimeResourceName(), operation.GetRuntimeResourceName())
return operation, 0, nil
}

Expand Down
50 changes: 43 additions & 7 deletions internal/process/deprovisioning/remove_runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"testing"
"time"

"github.com/kyma-project/kyma-environment-broker/internal/broker"

"github.com/kyma-project/kyma-environment-broker/internal/fixture"
provisionerAutomock "github.com/kyma-project/kyma-environment-broker/internal/provisioner/automock"
"github.com/kyma-project/kyma-environment-broker/internal/storage"
Expand All @@ -14,17 +12,16 @@ import (
)

func TestRemoveRuntimeStep_Run(t *testing.T) {
t.Run("Should repeat process when deprovisioning call to provisioner succeeded", func(t *testing.T) {
t.Run("Should not repeat process when deprovisioning call to provisioner succeeded", func(t *testing.T) {
// given
log := logrus.New()
memoryStorage := storage.NewMemoryStorage()

kimConfig := broker.KimConfig{
Enabled: false,
}
operation := fixture.FixDeprovisioningOperation(fixOperationID, fixInstanceID)
operation.GlobalAccountID = fixGlobalAccountID
operation.RuntimeID = fixRuntimeID
operation.KymaResourceNamespace = "kcp-system"
operation.KimDeprovisionsOnly = false
err := memoryStorage.Operations().InsertDeprovisioningOperation(operation)
assert.NoError(t, err)

Expand All @@ -34,7 +31,7 @@ func TestRemoveRuntimeStep_Run(t *testing.T) {
provisionerClient := &provisionerAutomock.Client{}
provisionerClient.On("DeprovisionRuntime", fixGlobalAccountID, fixRuntimeID).Return(fixProvisionerOperationID, nil)

step := NewRemoveRuntimeStep(memoryStorage.Operations(), memoryStorage.Instances(), provisionerClient, time.Minute, kimConfig)
step := NewRemoveRuntimeStep(memoryStorage.Operations(), memoryStorage.Instances(), provisionerClient, time.Minute)

// when
entry := log.WithFields(logrus.Fields{"step": "TEST"})
Expand All @@ -49,5 +46,44 @@ func TestRemoveRuntimeStep_Run(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, instance.RuntimeID, fixRuntimeID)

provisionerClient.AssertNumberOfCalls(t, "DeprovisionRuntime", 1)

})

t.Run("Should not call provisioner", func(t *testing.T) {
// given
log := logrus.New()
memoryStorage := storage.NewMemoryStorage()

operation := fixture.FixDeprovisioningOperation(fixOperationID, fixInstanceID)
operation.GlobalAccountID = fixGlobalAccountID
operation.RuntimeID = fixRuntimeID
operation.KymaResourceNamespace = "kcp-system"
operation.KimDeprovisionsOnly = true
err := memoryStorage.Operations().InsertDeprovisioningOperation(operation)
assert.NoError(t, err)

err = memoryStorage.Instances().Insert(fixInstanceRuntimeStatus())
assert.NoError(t, err)

provisionerClient := &provisionerAutomock.Client{}
provisionerClient.On("DeprovisionRuntime", fixGlobalAccountID, fixRuntimeID).Return(fixProvisionerOperationID, nil)

step := NewRemoveRuntimeStep(memoryStorage.Operations(), memoryStorage.Instances(), provisionerClient, time.Minute)

// when
entry := log.WithFields(logrus.Fields{"step": "TEST"})
result, repeat, err := step.Run(operation.Operation, entry)

// then
assert.NoError(t, err)
assert.Equal(t, 0*time.Second, repeat)

instance, err := memoryStorage.Instances().GetByID(result.InstanceID)
assert.NoError(t, err)
assert.Equal(t, instance.RuntimeID, fixRuntimeID)

provisionerClient.AssertNumberOfCalls(t, "DeprovisionRuntime", 0)

})
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,8 @@ func (s *CreateRuntimeResourceStep) createLabelsForRuntime(operation internal.Op
"kyma-project.io/region": region,
"operator.kyma-project.io/kyma-name": kymaName,
}
if s.kimConfig.ViewOnly && !s.kimConfig.IsDrivenByKimOnly(broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID]) {
labels[imv1.LabelControlledByProvisioner] = "true"
} else {
labels[imv1.LabelControlledByProvisioner] = "false"
}
controlledByProvisioner := s.kimConfig.ViewOnly && !s.kimConfig.IsDrivenByKimOnly(broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID])
labels[imv1.LabelControlledByProvisioner] = strconv.FormatBool(controlledByProvisioner)
return labels
}

Expand Down
2 changes: 1 addition & 1 deletion internal/process/update/upgrade_shoot_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *UpgradeShootStep) Run(operation internal.Operation, log logrus.FieldLog
return s.operationManager.RetryOperation(operation, err.Error(), err, 5*time.Second, 1*time.Minute, log)
}
if !runtime.IsControlledByProvisioner() {
log.Infof("Skipping provisioning because the runtime is not controlled by the provisioner")
log.Infof("Skipping because the runtime is not controlled by the provisioner")
return operation, 0, nil
}

Expand Down
Loading