Skip to content

Commit

Permalink
todos added
Browse files Browse the repository at this point in the history
pass necessary data to handler

barebones

kim config moved to broker package

change package reference

change package reference again

redo

reredo

removing unused functions

fixes to fixes

initial stubs for handler tests

simplified tests setup

handling errors from provisioner (in a nasty way)

dto extended and set if RuntimeResource fetched

fake client extended, remarks and todos added

before test implementation

suite corrected

no scheme used and make fix run

let focus on one test case

failing new test cases

first passing test case (out of new ones)

test cases added

error handling for nested fields

fix for signature

setting workers in the fixture

additional assertion

review remark and conflict resolved

log level depending on k8s client error
  • Loading branch information
jaroslaw-pieszka committed Aug 30, 2024
1 parent e71b5b7 commit 8d52691
Show file tree
Hide file tree
Showing 23 changed files with 621 additions and 127 deletions.
4 changes: 3 additions & 1 deletion cmd/broker/broker_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ func NewBrokerSuiteTestWithConfig(t *testing.T, cfg *Config, version ...string)
expirationHandler := expiration.NewHandler(db.Instances(), db.Operations(), deprovisioningQueue, logs)
expirationHandler.AttachRoutes(ts.router)

runtimeHandler := kebRuntime.NewHandler(db.Instances(), db.Operations(), db.RuntimeStates(), db.InstancesArchived(), cfg.MaxPaginationPage, cfg.DefaultRequestRegion, provisionerClient, logs)
runtimeHandler := kebRuntime.NewHandler(db.Instances(), db.Operations(), db.RuntimeStates(), db.InstancesArchived(), cfg.MaxPaginationPage, cfg.DefaultRequestRegion, provisionerClient, cli, broker.KimConfig{
Enabled: false,
}, logs)
runtimeHandler.AttachRoutes(ts.router)

ts.httpServer = httptest.NewServer(ts.router)
Expand Down
5 changes: 4 additions & 1 deletion cmd/broker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,10 @@ func main() {
// create list runtimes endpoint
runtimeHandler := runtime.NewHandler(db.Instances(), db.Operations(),
db.RuntimeStates(), db.InstancesArchived(), cfg.MaxPaginationPage,
cfg.DefaultRequestRegion, provisionerClient, logs)
cfg.DefaultRequestRegion, provisionerClient,
cli,
cfg.Broker.KimConfig,
logs)
runtimeHandler.AttachRoutes(router)

// create expiration endpoint
Expand Down
4 changes: 4 additions & 0 deletions common/runtime/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type RuntimeDTO struct {
AVSInternalEvaluationID int64 `json:"avsInternalEvaluationID"`
KymaConfig *gqlschema.KymaConfigInput `json:"kymaConfig,omitempty"`
ClusterConfig *gqlschema.GardenerConfigInput `json:"clusterConfig,omitempty"`
RuntimeConfig *map[string]interface{} `json:"runtimeConfig,omitempty"`
}

type RuntimeStatus struct {
Expand Down Expand Up @@ -119,6 +120,7 @@ const (
ClusterConfigParam = "cluster_config"
ExpiredParam = "expired"
GardenerConfigParam = "gardener_config"
RuntimeConfigParam = "runtime_config"
)

type OperationDetail string
Expand All @@ -139,6 +141,8 @@ type ListParameters struct {
KymaConfig bool
// ClusterConfig specifies whether Gardener cluster configuration details should be included in the response for each runtime
ClusterConfig bool
// RuntimeResourceConfig specifies whether current Runtime Custom Resource details should be included in the response for each runtime
RuntimeResourceConfig bool
// GardenerConfig specifies whether current Gardener cluster configuration details from provisioner should be included in the response for each runtime
GardenerConfig bool
// GlobalAccountIDs parameter filters runtimes by specified global account IDs
Expand Down
3 changes: 1 addition & 2 deletions internal/broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
"github.com/kyma-project/kyma-environment-broker/internal"
kim "github.com/kyma-project/kyma-environment-broker/internal/kim"
)

const (
Expand Down Expand Up @@ -50,7 +49,7 @@ type Config struct {
EnableShootAndSeedSameRegion bool `envconfig:"default=false"`

Binding BindingConfig
KimConfig kim.Config
KimConfig KimConfig
UseSmallerMachineTypes bool `envconfig:"default=false"`
}

Expand Down
25 changes: 20 additions & 5 deletions internal/kim/config.go → internal/broker/kim_config.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package kim
package broker

type Config struct {
type KimConfig struct {
Enabled bool `envconfig:"default=false"` // if true, KIM will be used
DryRun bool `envconfig:"default=true"` // if true, only yamls are generated, no resources are created
ViewOnly bool `envconfig:"default=true"` // if true, provisioner will control the process
Plans []string `envconfig:"default=preview"`
KimOnlyPlans []string `envconfig:"default=,"`
}

func (c *Config) IsEnabledForPlan(planName string) bool {
func (c *KimConfig) IsEnabledForPlan(planName string) bool {
if c.Enabled == false {
return false
}
Expand All @@ -20,7 +20,7 @@ func (c *Config) IsEnabledForPlan(planName string) bool {
return false
}

func (c *Config) IsDrivenByKimOnly(planName string) bool {
func (c *KimConfig) IsDrivenByKimOnly(planName string) bool {
if !c.IsEnabledForPlan(planName) {
return false
}
Expand All @@ -32,6 +32,21 @@ func (c *Config) IsDrivenByKimOnly(planName string) bool {
return false
}

func (c *Config) IsDrivenByKim(planName string) bool {
func (c *KimConfig) IsPlanIdDrivenByKimOnly(planID string) bool {
planName := PlanIDsMapping[planID]
return c.IsDrivenByKimOnly(planName)
}

func (c *KimConfig) IsPlanIdDrivenByKim(planID string) bool {
planName := PlanIDsMapping[planID]
return c.IsDrivenByKim(planName)
}

func (c *KimConfig) IsDrivenByKim(planName string) bool {
return (c.IsEnabledForPlan(planName) && !c.ViewOnly && !c.DryRun) || c.IsDrivenByKimOnly(planName)
}

func (c *KimConfig) IsEnabledForPlanID(planID string) bool {
planName := PlanIDsMapping[planID]
return c.IsEnabledForPlan(planName)
}
22 changes: 14 additions & 8 deletions internal/kim/config_test.go → internal/broker/kim_config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kim
package broker

import (
"testing"
Expand All @@ -7,7 +7,7 @@ import (
)

func TestIsEnabled_KimDisabled(t *testing.T) {
config := &Config{
config := &KimConfig{
Enabled: false,
Plans: []string{"gcp", "preview"},
ViewOnly: false,
Expand All @@ -22,7 +22,7 @@ func TestIsEnabled_KimDisabled(t *testing.T) {
}

func TestIsEnabled_KimEnabledForPreview(t *testing.T) {
config := &Config{
config := &KimConfig{
Enabled: true,
Plans: []string{"preview"},
ViewOnly: false,
Expand All @@ -38,7 +38,7 @@ func TestIsEnabled_KimEnabledForPreview(t *testing.T) {
}

func TestIsEnabled_KimEnabledForPreview_DryRun(t *testing.T) {
config := &Config{
config := &KimConfig{
Enabled: true,
Plans: []string{"preview"},
ViewOnly: false,
Expand All @@ -54,7 +54,7 @@ func TestIsEnabled_KimEnabledForPreview_DryRun(t *testing.T) {
}

func TestDrivenByKimOnly_KimDisabled(t *testing.T) {
config := &Config{
config := &KimConfig{
Enabled: false,
Plans: []string{"gcp", "preview"},
KimOnlyPlans: []string{"preview"},
Expand All @@ -67,10 +67,16 @@ func TestDrivenByKimOnly_KimDisabled(t *testing.T) {
assert.False(t, config.IsDrivenByKim("preview"))
assert.False(t, config.IsDrivenByKimOnly("gcp"))
assert.False(t, config.IsDrivenByKimOnly("preview"))
assert.False(t, config.IsPlanIdDrivenByKimOnly("ca6e5357-707f-4565-bbbd-b3ab732597c6"))
assert.False(t, config.IsPlanIdDrivenByKimOnly("5cb3d976-b85c-42ea-a636-79cadda109a9"))
assert.False(t, config.IsPlanIdDrivenByKim("ca6e5357-707f-4565-bbbd-b3ab732597c6"))
assert.False(t, config.IsPlanIdDrivenByKim("5cb3d976-b85c-42ea-a636-79cadda109a9"))
assert.False(t, config.IsPlanIdDrivenByKimOnly("ca6e5357-707f-4565-bbbd-b3ab732597c6"))
assert.False(t, config.IsPlanIdDrivenByKimOnly("5cb3d976-b85c-42ea-a636-79cadda109a9"))
}

func TestDrivenByKimOnly_PreviewByKimOnly(t *testing.T) {
config := &Config{
config := &KimConfig{
Enabled: true,
Plans: []string{"preview"},
KimOnlyPlans: []string{"preview"},
Expand All @@ -86,7 +92,7 @@ func TestDrivenByKimOnly_PreviewByKimOnly(t *testing.T) {
}

func TestDrivenByKimOnly_PreviewByKimOnlyButNotEnabled(t *testing.T) {
config := &Config{
config := &KimConfig{
Enabled: true,
KimOnlyPlans: []string{"preview"},
ViewOnly: false,
Expand All @@ -101,7 +107,7 @@ func TestDrivenByKimOnly_PreviewByKimOnlyButNotEnabled(t *testing.T) {
}

func TestDrivenByKim_ButNotByKimOnly(t *testing.T) {
config := &Config{
config := &KimConfig{
Enabled: true,
KimOnlyPlans: []string{"no-plan"},
Plans: []string{"preview"},
Expand Down
8 changes: 3 additions & 5 deletions internal/process/deprovisioning/remove_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"time"

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

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

"github.com/kyma-project/kyma-environment-broker/internal/broker"
Expand All @@ -23,10 +21,10 @@ type RemoveRuntimeStep struct {
instanceStorage storage.Instances
provisionerClient provisioner.Client
provisionerTimeout time.Duration
kimConfig kim.Config
kimConfig broker.KimConfig
}

func NewRemoveRuntimeStep(os storage.Operations, is storage.Instances, cli provisioner.Client, provisionerTimeout time.Duration, kimConfig kim.Config) *RemoveRuntimeStep {
func NewRemoveRuntimeStep(os storage.Operations, is storage.Instances, cli provisioner.Client, provisionerTimeout time.Duration, kimConfig broker.KimConfig) *RemoveRuntimeStep {
return &RemoveRuntimeStep{
operationManager: process.NewOperationManager(os),
instanceStorage: is,
Expand All @@ -41,7 +39,7 @@ 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]) {
if !s.kimConfig.IsDrivenByKim(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
}
Expand Down
5 changes: 2 additions & 3 deletions internal/process/provisioning/check_runtime_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

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

"github.com/sirupsen/logrus"

Expand All @@ -21,13 +20,13 @@ type CheckRuntimeStep struct {
provisionerClient provisioner.Client
operationManager *process.OperationManager
provisioningTimeout time.Duration
kimConfig kim.Config
kimConfig broker.KimConfig
}

func NewCheckRuntimeStep(os storage.Operations,
provisionerClient provisioner.Client,
provisioningTimeout time.Duration,
kimConfig kim.Config) *CheckRuntimeStep {
kimConfig broker.KimConfig) *CheckRuntimeStep {
return &CheckRuntimeStep{
provisionerClient: provisionerClient,
operationManager: process.NewOperationManager(os),
Expand Down
6 changes: 2 additions & 4 deletions internal/process/provisioning/check_runtime_step_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/kim"

"github.com/kyma-project/control-plane/components/provisioner/pkg/gqlschema"
"github.com/kyma-project/kyma-environment-broker/internal"
"github.com/kyma-project/kyma-environment-broker/internal/broker"
Expand Down Expand Up @@ -45,7 +43,7 @@ func TestCheckRuntimeStep_RunProvisioningSucceeded(t *testing.T) {
RuntimeID: ptr.String(statusRuntimeID),
})

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

Expand Down Expand Up @@ -92,7 +90,7 @@ func TestCheckRuntimeStep_RunProvisioningSucceeded_WithKimOnly(t *testing.T) {
RuntimeID: ptr.String(statusRuntimeID),
})

kimConfig := kim.Config{
kimConfig := broker.KimConfig{
Enabled: true,
Plans: []string{"gcp"},
KimOnlyPlans: []string{"gcp"},
Expand Down
6 changes: 2 additions & 4 deletions internal/process/provisioning/create_runtime_resource_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"github.com/kyma-project/kyma-environment-broker/internal/broker"

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

"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"
Expand All @@ -39,14 +37,14 @@ type CreateRuntimeResourceStep struct {
instanceStorage storage.Instances
runtimeStateStorage storage.RuntimeStates
k8sClient client.Client
kimConfig kim.Config
kimConfig broker.KimConfig
config input.Config
trialPlatformRegionMapping map[string]string
useSmallerMachineTypes bool
oidcDefaultValues internal.OIDCConfigDTO
}

func NewCreateRuntimeResourceStep(os storage.Operations, is storage.Instances, k8sClient client.Client, kimConfig kim.Config, cfg input.Config,
func NewCreateRuntimeResourceStep(os storage.Operations, is storage.Instances, k8sClient client.Client, kimConfig broker.KimConfig, cfg input.Config,
trialPlatformRegionMapping map[string]string, useSmallerMachines bool, oidcDefaultValues internal.OIDCConfigDTO) *CreateRuntimeResourceStep {
return &CreateRuntimeResourceStep{
operationManager: process.NewOperationManager(os),
Expand Down
38 changes: 4 additions & 34 deletions internal/process/provisioning/create_runtime_resource_step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"

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

"github.com/kyma-project/kyma-environment-broker/internal/storage"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -816,33 +814,24 @@ func getClientForTests(t *testing.T) client.Client {
return cli
}

func fixKimConfig(planName string, dryRun bool) kim.Config {
return kim.Config{
func fixKimConfig(planName string, dryRun bool) broker.KimConfig {
return broker.KimConfig{
Enabled: true,
Plans: []string{planName},
ViewOnly: false,
DryRun: dryRun,
}
}

func fixKimConfigWithAllPlans(dryRun bool) kim.Config {
return kim.Config{
func fixKimConfigWithAllPlans(dryRun bool) broker.KimConfig {
return broker.KimConfig{
Enabled: true,
Plans: []string{"azure", "gcp", "azure_lite", "trial", "aws", "free", "preview", "sap-converged-cloud"},
ViewOnly: false,
DryRun: dryRun,
}
}

func fixKimConfigProvisionerDriven(planName string, dryRun bool) kim.Config {
return kim.Config{
Enabled: true,
Plans: []string{planName},
ViewOnly: true,
DryRun: dryRun,
}
}

func fixInstanceAndOperation(planID, region, platformRegion string) (internal.Instance, internal.Operation) {
instance := fixInstance()
operation := fixOperationForCreateRuntimeResourceStep(OperationID, instance.InstanceID, planID, region, platformRegion)
Expand Down Expand Up @@ -884,22 +873,3 @@ modules: []
`
return operation
}

func fixProvisionerParameters(cloudProvider internal.CloudProvider, region string) internal.ProvisioningParametersDTO {
return internal.ProvisioningParametersDTO{
Name: "cluster-test",
VolumeSizeGb: ptr.Integer(50),
MachineType: ptr.String("Standard_D8_v3"),
Region: ptr.String(region),
Purpose: ptr.String("Purpose"),
LicenceType: ptr.String("LicenceType"),
Zones: []string{"1"},
AutoScalerParameters: internal.AutoScalerParameters{
AutoScalerMin: ptr.Integer(3),
AutoScalerMax: ptr.Integer(10),
MaxSurge: ptr.Integer(4),
MaxUnavailable: ptr.Integer(1),
},
Provider: &cloudProvider,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +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"
"github.com/kyma-project/kyma-environment-broker/internal/broker"
kebError "github.com/kyma-project/kyma-environment-broker/internal/error"
"github.com/kyma-project/kyma-environment-broker/internal/process"
"github.com/kyma-project/kyma-environment-broker/internal/provisioner"
Expand All @@ -30,10 +28,10 @@ type CreateRuntimeWithoutKymaStep struct {
instanceStorage storage.Instances
runtimeStateStorage storage.RuntimeStates
provisionerClient provisioner.Client
kimConfig kim.Config
kimConfig broker.KimConfig
}

func NewCreateRuntimeWithoutKymaStep(os storage.Operations, runtimeStorage storage.RuntimeStates, is storage.Instances, cli provisioner.Client, kimConfig kim.Config) *CreateRuntimeWithoutKymaStep {
func NewCreateRuntimeWithoutKymaStep(os storage.Operations, runtimeStorage storage.RuntimeStates, is storage.Instances, cli provisioner.Client, kimConfig broker.KimConfig) *CreateRuntimeWithoutKymaStep {
return &CreateRuntimeWithoutKymaStep{
operationManager: process.NewOperationManager(os),
instanceStorage: is,
Expand Down
Loading

0 comments on commit 8d52691

Please sign in to comment.