Skip to content

Commit

Permalink
Update runtime resource with additional worker node pools
Browse files Browse the repository at this point in the history
  • Loading branch information
KsaweryZietara committed Jan 2, 2025
1 parent 393fdc1 commit 7000bd9
Show file tree
Hide file tree
Showing 13 changed files with 580 additions and 54 deletions.
15 changes: 15 additions & 0 deletions cmd/broker/broker_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"testing"
"time"

"github.com/gardener/gardener/pkg/apis/core/v1beta1"

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

"github.com/kyma-project/kyma-environment-broker/internal/storage/dberr"
Expand Down Expand Up @@ -1276,3 +1278,16 @@ func createResource(t *testing.T, gvk schema.GroupVersionKind, k8sClient client.
err := k8sClient.Create(context.TODO(), object)
assert.NoError(t, err)
}

func (s *BrokerSuiteTest) assertAdditionalWorkerIsCreated(t *testing.T, provider imv1.Provider, name, machineType string, autoScalerMin, autoScalerMax int) {
var worker *v1beta1.Worker
//for _, additionalWorker := range provider.AdditionalWorkers {
// if additionalWorker.Name == name {
// worker = &additionalWorker
// }
//}
require.NotNil(t, worker)
assert.Equal(t, machineType, worker.Machine.Type)
assert.Equal(t, int32(autoScalerMin), worker.Minimum)
assert.Equal(t, int32(autoScalerMax), worker.Maximum)
}
52 changes: 52 additions & 0 deletions cmd/broker/provisioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1877,3 +1877,55 @@ func TestProvisioning_Modules(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
})
}

func TestProvisioningWithAdditionalWorkerNodePools(t *testing.T) {
//given
cfg := fixConfig()
cfg.Broker.KimConfig.Enabled = true
cfg.Broker.KimConfig.Plans = []string{"preview"}
cfg.Broker.KimConfig.KimOnlyPlans = []string{"preview"}

suite := NewBrokerSuiteTestWithConfig(t, cfg)
defer suite.TearDown()
iid := uuid.New().String()

// when
resp := suite.CallAPI("PUT", fmt.Sprintf("oauth/cf-eu21/v2/service_instances/%s?accepts_incomplete=true", iid),
`{
"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
"plan_id": "5cb3d976-b85c-42ea-a636-79cadda109a9",
"context": {
"globalaccount_id": "g-account-id",
"subaccount_id": "sub-id",
"user_id": "john.smith@email.com"
},
"parameters": {
"name": "testing-cluster",
"region": "eu-central-1",
"additionalWorkerNodePools": [
{
"name": "name-1",
"machineType": "m6i.large",
"autoScalerMin": 1,
"autoScalerMax": 10
},
{
"name": "name-2",
"machineType": "m5.large",
"autoScalerMin": 0,
"autoScalerMax": 0
}
]
}
}`)

opID := suite.DecodeOperationID(resp)
suite.processKIMOnlyProvisioningByOperationID(opID)

// then
suite.WaitForOperationState(opID, domain.Succeeded)
runtime := suite.GetRuntimeResourceByInstanceID(iid)
//assert.Len(t, runtime.Spec.Shoot.Provider.AdditionalWorkers, 2)
suite.assertAdditionalWorkerIsCreated(t, runtime.Spec.Shoot.Provider, "name-1", "m6i.large", 1, 10)
suite.assertAdditionalWorkerIsCreated(t, runtime.Spec.Shoot.Provider, "name-2", "m5.large", 0, 0)
}
11 changes: 8 additions & 3 deletions cmd/broker/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"log/slog"

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

"github.com/kyma-project/kyma-environment-broker/internal/event"
"github.com/kyma-project/kyma-environment-broker/internal/process"
"github.com/kyma-project/kyma-environment-broker/internal/process/input"
"github.com/kyma-project/kyma-environment-broker/internal/process/steps"
"github.com/kyma-project/kyma-environment-broker/internal/process/update"
"github.com/kyma-project/kyma-environment-broker/internal/provider"
"github.com/kyma-project/kyma-environment-broker/internal/provisioner"
"github.com/kyma-project/kyma-environment-broker/internal/storage"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -19,6 +19,11 @@ func NewUpdateProcessingQueue(ctx context.Context, manager *process.StagedManage
provisionerClient provisioner.Client, publisher event.Publisher,
cfg Config, k8sClientProvider K8sClientProvider, cli client.Client, logs *slog.Logger) *process.Queue {

trialRegionsMapping, err := provider.ReadPlatformRegionMappingFromFile(cfg.TrialRegionMappingFilePath)
if err != nil {
fatalOnError(err, logs)
}

manager.DefineStages([]string{"cluster", "btp-operator", "btp-operator-check", "check", "runtime_resource", "check_runtime_resource"})
updateSteps := []struct {
disabled bool
Expand All @@ -42,7 +47,7 @@ func NewUpdateProcessingQueue(ctx context.Context, manager *process.StagedManage
},
{
stage: "runtime_resource",
step: update.NewUpdateRuntimeStep(db.Operations(), cli, cfg.UpdateRuntimeResourceDelay),
step: update.NewUpdateRuntimeStep(db.Operations(), cli, cfg.UpdateRuntimeResourceDelay, cfg.Provisioner, cfg.Broker.UseSmallerMachineTypes, trialRegionsMapping),
condition: update.SkipForOwnClusterPlan,
},
{
Expand Down
259 changes: 259 additions & 0 deletions cmd/broker/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2526,3 +2526,262 @@ func TestUpdateErsContextAndParamsForExpiredInstance(t *testing.T) {
}`)
assert.Equal(t, http.StatusBadRequest, response.StatusCode)
}

func TestUpdateAdditionalWorkerNodePools(t *testing.T) {
t.Run("should add additional worker node pools", func(t *testing.T) {
// given
suite := NewBrokerSuiteTest(t)
defer suite.TearDown()
iid := uuid.New().String()

resp := suite.CallAPI("PUT", fmt.Sprintf("oauth/cf-eu10/v2/service_instances/%s?accepts_incomplete=true&plan_id=7d55d31d-35ae-4438-bf13-6ffdfa107d9f&service_id=47c9dcbf-ff30-448e-ab36-d3bad66ba281", iid),
`{
"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
"plan_id": "5cb3d976-b85c-42ea-a636-79cadda109a9",
"context": {
"globalaccount_id": "g-account-id",
"subaccount_id": "sub-id",
"user_id": "john.smith@email.com"
},
"parameters": {
"name": "testing-cluster",
"region": "eu-central-1"
}
}`)
opID := suite.DecodeOperationID(resp)
suite.waitForRuntimeAndMakeItReady(opID)
suite.WaitForOperationState(opID, domain.Succeeded)

// when
// OSB update:
resp = suite.CallAPI("PATCH", fmt.Sprintf("oauth/cf-eu10/v2/service_instances/%s?accepts_incomplete=true", iid),
`{
"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
"plan_id": "5cb3d976-b85c-42ea-a636-79cadda109a9",
"context": {
"globalaccount_id": "g-account-id",
"user_id": "john.smith@email.com"
},
"parameters": {
"additionalWorkerNodePools": {
"list": [
{
"name": "name-1",
"machineType": "m6i.large",
"autoScalerMin": 1,
"autoScalerMax": 10
},
{
"name": "name-2",
"machineType": "m5.large",
"autoScalerMin": 0,
"autoScalerMax": 0
}
]
}
}
}`)
assert.Equal(t, http.StatusAccepted, resp.StatusCode)
upgradeOperationID := suite.DecodeOperationID(resp)

// then
suite.WaitForOperationState(upgradeOperationID, domain.Succeeded)
runtime := suite.GetRuntimeResourceByInstanceID(iid)
//assert.Len(t, runtime.Spec.Shoot.Provider.AdditionalWorkers, 2)
suite.assertAdditionalWorkerIsCreated(t, runtime.Spec.Shoot.Provider, "name-1", "m6i.large", 1, 10)
suite.assertAdditionalWorkerIsCreated(t, runtime.Spec.Shoot.Provider, "name-2", "m5.large", 0, 0)
})

t.Run("should replace additional worker node pools", func(t *testing.T) {
// given
suite := NewBrokerSuiteTest(t)
defer suite.TearDown()
iid := uuid.New().String()

resp := suite.CallAPI("PUT", fmt.Sprintf("oauth/cf-eu10/v2/service_instances/%s?accepts_incomplete=true&plan_id=7d55d31d-35ae-4438-bf13-6ffdfa107d9f&service_id=47c9dcbf-ff30-448e-ab36-d3bad66ba281", iid),
`{
"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
"plan_id": "5cb3d976-b85c-42ea-a636-79cadda109a9",
"context": {
"globalaccount_id": "g-account-id",
"subaccount_id": "sub-id",
"user_id": "john.smith@email.com"
},
"parameters": {
"name": "testing-cluster",
"region": "eu-central-1",
"additionalWorkerNodePools": [
{
"name": "name-1",
"machineType": "m6i.large",
"autoScalerMin": 1,
"autoScalerMax": 10
}
]
}
}`)
opID := suite.DecodeOperationID(resp)
suite.waitForRuntimeAndMakeItReady(opID)
suite.WaitForOperationState(opID, domain.Succeeded)

// when
// OSB update:
resp = suite.CallAPI("PATCH", fmt.Sprintf("oauth/cf-eu10/v2/service_instances/%s?accepts_incomplete=true", iid),
`{
"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
"plan_id": "5cb3d976-b85c-42ea-a636-79cadda109a9",
"context": {
"globalaccount_id": "g-account-id",
"user_id": "john.smith@email.com"
},
"parameters": {
"additionalWorkerNodePools": {
"list": [
{
"name": "name-2",
"machineType": "m5.large",
"autoScalerMin": 0,
"autoScalerMax": 0
}
]
}
}
}`)
assert.Equal(t, http.StatusAccepted, resp.StatusCode)
upgradeOperationID := suite.DecodeOperationID(resp)

// then
suite.WaitForOperationState(upgradeOperationID, domain.Succeeded)
runtime := suite.GetRuntimeResourceByInstanceID(iid)
//assert.Len(t, runtime.Spec.Shoot.Provider.AdditionalWorkers, 1)
suite.assertAdditionalWorkerIsCreated(t, runtime.Spec.Shoot.Provider, "name-2", "m5.large", 0, 0)
})

t.Run("should not change additional worker node pools when list is empty", func(t *testing.T) {
// given
suite := NewBrokerSuiteTest(t)
defer suite.TearDown()
iid := uuid.New().String()

resp := suite.CallAPI("PUT", fmt.Sprintf("oauth/cf-eu10/v2/service_instances/%s?accepts_incomplete=true&plan_id=7d55d31d-35ae-4438-bf13-6ffdfa107d9f&service_id=47c9dcbf-ff30-448e-ab36-d3bad66ba281", iid),
`{
"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
"plan_id": "5cb3d976-b85c-42ea-a636-79cadda109a9",
"context": {
"globalaccount_id": "g-account-id",
"subaccount_id": "sub-id",
"user_id": "john.smith@email.com"
},
"parameters": {
"name": "testing-cluster",
"region": "eu-central-1",
"additionalWorkerNodePools": [
{
"name": "name-1",
"machineType": "m6i.large",
"autoScalerMin": 1,
"autoScalerMax": 10
},
{
"name": "name-2",
"machineType": "m5.large",
"autoScalerMin": 0,
"autoScalerMax": 0
}
]
}
}`)
opID := suite.DecodeOperationID(resp)
suite.waitForRuntimeAndMakeItReady(opID)
suite.WaitForOperationState(opID, domain.Succeeded)

// when
// OSB update:
resp = suite.CallAPI("PATCH", fmt.Sprintf("oauth/cf-eu10/v2/service_instances/%s?accepts_incomplete=true", iid),
`{
"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
"plan_id": "5cb3d976-b85c-42ea-a636-79cadda109a9",
"context": {
"globalaccount_id": "g-account-id",
"user_id": "john.smith@email.com"
},
"parameters": {
"additionalWorkerNodePools": {
"list": []
}
}
}`)
assert.Equal(t, http.StatusAccepted, resp.StatusCode)
upgradeOperationID := suite.DecodeOperationID(resp)

// then
suite.WaitForOperationState(upgradeOperationID, domain.Succeeded)
runtime := suite.GetRuntimeResourceByInstanceID(iid)
//assert.Len(t, runtime.Spec.Shoot.Provider.AdditionalWorkers, 2)
suite.assertAdditionalWorkerIsCreated(t, runtime.Spec.Shoot.Provider, "name-1", "m6i.large", 1, 10)
suite.assertAdditionalWorkerIsCreated(t, runtime.Spec.Shoot.Provider, "name-2", "m5.large", 0, 0)
})

t.Run("should remove additional worker node pools", func(t *testing.T) {
// given
suite := NewBrokerSuiteTest(t)
defer suite.TearDown()
iid := uuid.New().String()

resp := suite.CallAPI("PUT", fmt.Sprintf("oauth/cf-eu10/v2/service_instances/%s?accepts_incomplete=true&plan_id=7d55d31d-35ae-4438-bf13-6ffdfa107d9f&service_id=47c9dcbf-ff30-448e-ab36-d3bad66ba281", iid),
`{
"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
"plan_id": "5cb3d976-b85c-42ea-a636-79cadda109a9",
"context": {
"globalaccount_id": "g-account-id",
"subaccount_id": "sub-id",
"user_id": "john.smith@email.com"
},
"parameters": {
"name": "testing-cluster",
"region": "eu-central-1",
"additionalWorkerNodePools": [
{
"name": "name-1",
"machineType": "m6i.large",
"autoScalerMin": 1,
"autoScalerMax": 10
},
{
"name": "name-2",
"machineType": "m5.large",
"autoScalerMin": 0,
"autoScalerMax": 0
}
]
}
}`)
opID := suite.DecodeOperationID(resp)
suite.waitForRuntimeAndMakeItReady(opID)
suite.WaitForOperationState(opID, domain.Succeeded)

// when
// OSB update:
resp = suite.CallAPI("PATCH", fmt.Sprintf("oauth/cf-eu10/v2/service_instances/%s?accepts_incomplete=true", iid),
`{
"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
"plan_id": "5cb3d976-b85c-42ea-a636-79cadda109a9",
"context": {
"globalaccount_id": "g-account-id",
"user_id": "john.smith@email.com"
},
"parameters": {
"additionalWorkerNodePools": {
"remove": true
}
}
}`)
assert.Equal(t, http.StatusAccepted, resp.StatusCode)
upgradeOperationID := suite.DecodeOperationID(resp)

// then
suite.WaitForOperationState(upgradeOperationID, domain.Succeeded)
//runtime := suite.GetRuntimeResourceByInstanceID(iid)
//assert.Len(t, runtime.Spec.Shoot.Provider.AdditionalWorkers, 0)
})
}
Loading

0 comments on commit 7000bd9

Please sign in to comment.