-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KIM Integration Check Runtime resource step (#982)
* initial version initial version format change step on conditions first test case passing tests added less is more invocation added * Step name changed * Step name changed
- Loading branch information
1 parent
6dc83c7
commit 03aeb0a
Showing
3 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package steps | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/kyma-project/kyma-environment-broker/internal/broker" | ||
"github.com/kyma-project/kyma-environment-broker/internal/kim" | ||
|
||
imv1 "github.com/kyma-project/infrastructure-manager/api/v1" | ||
|
||
"github.com/kyma-project/kyma-environment-broker/internal" | ||
"github.com/kyma-project/kyma-environment-broker/internal/process" | ||
"github.com/kyma-project/kyma-environment-broker/internal/storage" | ||
"github.com/sirupsen/logrus" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const RuntimeResourceStateReady = "Ready" | ||
|
||
func NewCheckRuntimeResourceStep(os storage.Operations, k8sClient client.Client, kimConfig kim.Config, runtimeResourceStepTimeout time.Duration) *checkRuntimeResource { | ||
return &checkRuntimeResource{ | ||
k8sClient: k8sClient, | ||
operationManager: process.NewOperationManager(os), | ||
kimConfig: kimConfig, | ||
runtimeResourceStepTimeout: runtimeResourceStepTimeout, | ||
} | ||
} | ||
|
||
type checkRuntimeResource struct { | ||
k8sClient client.Client | ||
kimConfig kim.Config | ||
operationManager *process.OperationManager | ||
runtimeResourceStepTimeout time.Duration | ||
} | ||
|
||
func (_ *checkRuntimeResource) Name() string { | ||
return "Check_RuntimeResource" | ||
} | ||
|
||
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") | ||
return operation, 0, nil | ||
} | ||
|
||
runtime, err := s.GetRuntimeResource(operation.RuntimeID, operation.KymaResourceNamespace) | ||
if err != nil { | ||
log.Errorf("unable to get Runtime resource %s/%s", operation.KymaResourceNamespace, operation.RuntimeID) | ||
return s.operationManager.RetryOperation(operation, "unable to get Runtime resource", err, time.Second, 10*time.Second, log) | ||
} | ||
|
||
// check status | ||
state := runtime.Status.State | ||
log.Infof("Runtime resource state: %s", state) | ||
if state != RuntimeResourceStateReady { | ||
if time.Since(operation.UpdatedAt) > s.runtimeResourceStepTimeout { | ||
description := fmt.Sprintf("Waiting for Runtime resource (%s/%s) ready state timeout.", operation.KymaResourceNamespace, operation.RuntimeID) | ||
log.Error(description) | ||
log.Infof("Runtime resource status: %v", runtime.Status) | ||
return s.operationManager.OperationFailed(operation, description, nil, log) | ||
} | ||
return operation, 500 * time.Millisecond, nil | ||
} | ||
return operation, 0, nil | ||
} | ||
|
||
func (s *checkRuntimeResource) GetRuntimeResource(name string, namespace string) (*imv1.Runtime, error) { | ||
runtime := imv1.Runtime{} | ||
err := s.k8sClient.Get(context.Background(), client.ObjectKey{ | ||
Namespace: namespace, | ||
Name: name, | ||
}, &runtime) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &runtime, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package steps | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
imv1 "github.com/kyma-project/infrastructure-manager/api/v1" | ||
"github.com/kyma-project/kyma-environment-broker/internal/kim" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/pivotal-cf/brokerapi/v8/domain" | ||
|
||
"github.com/kyma-project/kyma-environment-broker/internal/fixture" | ||
"github.com/kyma-project/kyma-environment-broker/internal/storage" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
func TestCheckRuntimeResource_RunWhenReady(t *testing.T) { | ||
// given | ||
err := imv1.AddToScheme(scheme.Scheme) | ||
assert.NoError(t, err) | ||
|
||
os := storage.NewMemoryStorage().Operations() | ||
existingRuntime := createRuntime("Ready") | ||
k8sClient := fake.NewClientBuilder().WithRuntimeObjects(&existingRuntime).Build() | ||
kimConfig := fixKimConfigForAzure() | ||
|
||
step := NewCheckRuntimeResourceStep(os, k8sClient, kimConfig, time.Second) | ||
operation := fixture.FixProvisioningOperation("op", "instance-id") | ||
operation.KymaResourceNamespace = "kcp-system" | ||
operation.RuntimeID = "runtime-id-000" | ||
operation.ShootName = "c-12345" | ||
err = os.InsertOperation(operation) | ||
assert.NoError(t, err) | ||
|
||
// when | ||
_, backoff, err := step.Run(operation, logrus.New()) | ||
|
||
// then | ||
assert.NoError(t, err) | ||
assert.Zero(t, backoff) | ||
} | ||
|
||
func TestCheckRuntimeResource_RunWhenNotReady_OperationFail(t *testing.T) { | ||
// given | ||
|
||
err := imv1.AddToScheme(scheme.Scheme) | ||
assert.NoError(t, err) | ||
os := storage.NewMemoryStorage().Operations() | ||
|
||
existingRuntime := createRuntime("In Progress") | ||
|
||
kimConfig := fixKimConfigForAzure() | ||
|
||
k8sClient := fake.NewClientBuilder().WithRuntimeObjects(&existingRuntime).Build() | ||
step := NewCheckRuntimeResourceStep(os, k8sClient, kimConfig, time.Second) | ||
operation := fixture.FixProvisioningOperation("op", "instance-id") | ||
operation.KymaResourceNamespace = "kcp-system" | ||
operation.RuntimeID = "runtime-id-000" | ||
operation.ShootName = "c-12345" | ||
operation.UpdatedAt = time.Now().Add(-1 * time.Hour) | ||
err = os.InsertOperation(operation) | ||
assert.NoError(t, err) | ||
|
||
// when | ||
op, backoff, err := step.Run(operation, logrus.New()) | ||
|
||
// then | ||
assert.Error(t, err) | ||
assert.Zero(t, backoff) | ||
assert.Equal(t, domain.Failed, op.State) | ||
} | ||
|
||
func TestCheckRuntimeResource_RunWhenNotReady_Retry(t *testing.T) { | ||
// given | ||
err := imv1.AddToScheme(scheme.Scheme) | ||
assert.NoError(t, err) | ||
os := storage.NewMemoryStorage().Operations() | ||
|
||
existingRuntime := createRuntime("In Progress") | ||
|
||
kimConfig := fixKimConfigForAzure() | ||
|
||
k8sClient := fake.NewClientBuilder().WithRuntimeObjects(&existingRuntime).Build() | ||
|
||
step := NewCheckRuntimeResourceStep(os, k8sClient, kimConfig, time.Second) | ||
operation := fixture.FixProvisioningOperation("op", "instance-id") | ||
operation.KymaResourceNamespace = "kcp-system" | ||
operation.RuntimeID = "runtime-id-000" | ||
operation.ShootName = "c-12345" | ||
operation.UpdatedAt = time.Now() | ||
err = os.InsertOperation(operation) | ||
assert.NoError(t, err) | ||
|
||
// when | ||
_, backoff, err := step.Run(operation, logrus.New()) | ||
|
||
// then | ||
assert.NoError(t, err) | ||
assert.NotZero(t, backoff) | ||
} | ||
|
||
func fixKimConfigForAzure() kim.Config { | ||
kimConfig := kim.Config{ | ||
Enabled: true, | ||
Plans: []string{"azure"}, | ||
ViewOnly: false, | ||
} | ||
return kimConfig | ||
} | ||
|
||
func createRuntime(state imv1.State) imv1.Runtime { | ||
existingRuntime := imv1.Runtime{} | ||
existingRuntime.ObjectMeta.Name = "runtime-id-000" | ||
existingRuntime.ObjectMeta.Namespace = "kcp-system" | ||
existingRuntime.Status.State = state | ||
condition := v1.Condition{ | ||
Message: "condition message", | ||
} | ||
existingRuntime.Status.Conditions = []v1.Condition{condition} | ||
return existingRuntime | ||
} |