Skip to content

Commit

Permalink
KIM Integration - correctly skipped steps for KIM only driven plans (#…
Browse files Browse the repository at this point in the history
…1033)

* skipping corrected

* tests corrected

* make fix

* make fix

* test case added

* simplifying

* simplifying still

* missing return
  • Loading branch information
jaroslaw-pieszka authored Aug 13, 2024
1 parent e949993 commit ba96707
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/broker/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewProvisioningProcessingQueue(ctx context.Context, provisionManager *proce
{
condition: provisioning.SkipForOwnClusterPlan,
stage: createRuntimeStageName,
step: provisioning.NewCreateRuntimeWithoutKymaStep(db.Operations(), db.RuntimeStates(), db.Instances(), provisionerClient),
step: provisioning.NewCreateRuntimeWithoutKymaStep(db.Operations(), db.RuntimeStates(), db.Instances(), provisionerClient, cfg.Broker.KimConfig),
},
{
stage: createRuntimeStageName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"time"

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

"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
"github.com/kyma-project/kyma-environment-broker/internal"
kebError "github.com/kyma-project/kyma-environment-broker/internal/error"
Expand All @@ -27,14 +30,16 @@ type CreateRuntimeWithoutKymaStep struct {
instanceStorage storage.Instances
runtimeStateStorage storage.RuntimeStates
provisionerClient provisioner.Client
kimConfig kim.Config
}

func NewCreateRuntimeWithoutKymaStep(os storage.Operations, runtimeStorage storage.RuntimeStates, is storage.Instances, cli provisioner.Client) *CreateRuntimeWithoutKymaStep {
func NewCreateRuntimeWithoutKymaStep(os storage.Operations, runtimeStorage storage.RuntimeStates, is storage.Instances, cli provisioner.Client, kimConfig kim.Config) *CreateRuntimeWithoutKymaStep {
return &CreateRuntimeWithoutKymaStep{
operationManager: process.NewOperationManager(os),
instanceStorage: is,
provisionerClient: cli,
runtimeStateStorage: runtimeStorage,
kimConfig: kimConfig,
}
}

Expand All @@ -43,6 +48,11 @@ func (s *CreateRuntimeWithoutKymaStep) Name() string {
}

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

if operation.RuntimeID != "" {
log.Infof("RuntimeID already set %s, skipping", operation.RuntimeID)
return operation, 0, nil
Expand Down
68 changes: 65 additions & 3 deletions internal/process/provisioning/create_runtime_without_kyma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"reflect"
"testing"

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

"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
"github.com/kyma-project/kyma-environment-broker/internal/broker"
"github.com/kyma-project/kyma-environment-broker/internal/provider"
Expand All @@ -30,6 +32,10 @@ func TestCreateRuntimeWithoutKyma_Run(t *testing.T) {
err = memoryStorage.Instances().Insert(fixInstance())
assert.NoError(t, err)

kimConfig := kim.Config{
Enabled: false,
}

disabled := false
provisionerInput := fixProvisionerInput(disabled, false)

Expand All @@ -47,7 +53,7 @@ func TestCreateRuntimeWithoutKyma_Run(t *testing.T) {
RuntimeID: ptr.String(runtimeID),
}, nil)

step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient)
step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient, kimConfig)

// when
entry := log.WithFields(logrus.Fields{"step": "TEST"})
Expand All @@ -63,6 +69,54 @@ func TestCreateRuntimeWithoutKyma_Run(t *testing.T) {
assert.Equal(t, instance.RuntimeID, runtimeID)
}

func TestCreateRuntimeWithoutKyma_SkipForKIM(t *testing.T) {
// given
log := logrus.New()
memoryStorage := storage.NewMemoryStorage()

operation := fixOperationCreateRuntime(t, broker.GCPPlanID, "europe-west3")
operation.ShootDomain = "kyma.org"
err := memoryStorage.Operations().InsertOperation(operation)
assert.NoError(t, err)

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

kimConfig := kim.Config{
Enabled: true,
Plans: []string{"gcp"},
KimOnlyPlans: []string{"gcp"},
}

disabled := false
provisionerInput := fixProvisionerInput(disabled, false)

provisionerClient := &provisionerAutomock.Client{}
provisionerClient.On("ProvisionRuntime", globalAccountID, subAccountID, mock.MatchedBy(
func(input gqlschema.ProvisionRuntimeInput) bool {
return reflect.DeepEqual(input.RuntimeInput.Labels, provisionerInput.RuntimeInput.Labels) &&
input.KymaConfig == nil && reflect.DeepEqual(input.ClusterConfig, provisionerInput.ClusterConfig)
},
)).Return(gqlschema.OperationStatus{
ID: ptr.String(provisionerOperationID),
Operation: "",
State: "",
Message: nil,
RuntimeID: ptr.String(runtimeID),
}, nil)

step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient, kimConfig)

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

// then
assert.NoError(t, err)
assert.Zero(t, repeat)
assert.Empty(t, operation.ProvisionerOperationID)
}

func TestCreateRuntimeWithoutKyma_RunWithEuAccess(t *testing.T) {
// given
log := logrus.New()
Expand All @@ -76,6 +130,10 @@ func TestCreateRuntimeWithoutKyma_RunWithEuAccess(t *testing.T) {
err = memoryStorage.Instances().Insert(fixInstance())
assert.NoError(t, err)

kimConfig := kim.Config{
Enabled: false,
}

disabled := false
provisionerInput := fixProvisionerInput(disabled, true)

Expand All @@ -93,7 +151,7 @@ func TestCreateRuntimeWithoutKyma_RunWithEuAccess(t *testing.T) {
RuntimeID: ptr.String(runtimeID),
}, nil)

step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient)
step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient, kimConfig)

// when
entry := log.WithFields(logrus.Fields{"step": "TEST"})
Expand Down Expand Up @@ -184,13 +242,17 @@ func TestCreateRuntimeWithoutKymaStep_RunWithBadRequestError(t *testing.T) {
err := memoryStorage.Operations().InsertOperation(operation)
assert.NoError(t, err)

kimConfig := kim.Config{
Enabled: false,
}

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

provisionerClient := &provisionerAutomock.Client{}
provisionerClient.On("ProvisionRuntime", globalAccountID, subAccountID, mock.Anything).Return(gqlschema.OperationStatus{}, fmt.Errorf("some permanent error"))

step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient)
step := NewCreateRuntimeWithoutKymaStep(memoryStorage.Operations(), memoryStorage.RuntimeStates(), memoryStorage.Instances(), provisionerClient, kimConfig)

// when
entry := log.WithFields(logrus.Fields{"step": "TEST"})
Expand Down
18 changes: 2 additions & 16 deletions internal/process/steps/runtime_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,8 @@ func (_ *checkRuntimeResource) Name() string {
}

func (s *checkRuntimeResource) Run(operation internal.Operation, log logrus.FieldLogger) (internal.Operation, time.Duration, error) {
if !s.kimConfig.IsEnabledForPlan(broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID]) {
if !s.kimConfig.Enabled {
log.Infof("KIM is not enabled, skipping")
return operation, 0, nil
}
log.Infof("KIM is not enabled for plan %s, skipping", broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID])
return operation, 0, nil
}

if s.kimConfig.ViewOnly {
log.Infof("Provisioner is controlling provisioning process, skipping")
return operation, 0, nil
}

if s.kimConfig.DryRun {
log.Infof("KIM integration in dry-run mode, skipping")
if !s.kimConfig.IsDrivenByKim(broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID]) {
log.Infof("Only provisioner is controlling provisioning process, skipping")
return operation, 0, nil
}

Expand Down
3 changes: 1 addition & 2 deletions internal/process/steps/runtime_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,11 @@ func TestCheckRuntimeResource_RunWhenNotReady_Retry(t *testing.T) {
}

func fixKimConfigForAzure() kim.Config {
kimConfig := kim.Config{
return kim.Config{
Enabled: true,
Plans: []string{"azure"},
ViewOnly: false,
}
return kimConfig
}

func createRuntime(state imv1.State) imv1.Runtime {
Expand Down

0 comments on commit ba96707

Please sign in to comment.