Skip to content

Commit

Permalink
Labels for Runtime CR - stage 1 of KIM integration (#925)
Browse files Browse the repository at this point in the history
* labels initialize

yaml library changed

more labels set properly

TODOs added

commas corrected

TODOs removed

go imports

* decoding template

* removing comment

* small refactor, extracting functions

* more stubs

* imports

* workaround for no KymaTemplate

* administrators passed
  • Loading branch information
jaroslaw-pieszka authored Jul 11, 2024
1 parent cd80bb0 commit 509e6aa
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmd/broker/provisioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func TestProvisioningWithKIM(t *testing.T) {
},
"parameters": {
"name": "testing-cluster",
"region": "eu-central-1"
"region": "eu-central-1",
"administrators":["newAdmin1@kyma.cx", "newAdmin2@kyma.cx"]
}
}`)

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/dlmiddlecote/sqlstats v1.0.2
github.com/docker/docker v27.0.3+incompatible
github.com/docker/go-connections v0.5.0
github.com/gardener/gardener v1.98.0
github.com/go-co-op/gocron v1.37.0
github.com/gocraft/dbr v0.0.0-20190714181702-8114670a83bd
github.com/google/uuid v1.6.0
Expand Down Expand Up @@ -42,6 +43,7 @@ require (
k8s.io/apimachinery v0.30.2
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
sigs.k8s.io/controller-runtime v0.18.4
sigs.k8s.io/yaml v1.4.0
)

require (
Expand All @@ -65,7 +67,6 @@ require (
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/gardener/gardener v1.98.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
Expand Down Expand Up @@ -133,7 +134,6 @@ require (
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace (
Expand Down
74 changes: 67 additions & 7 deletions internal/process/provisioning/create_runtime_resource_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import (
"fmt"
"time"

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

"sigs.k8s.io/yaml"

gardener "github.com/gardener/gardener/pkg/apis/core/v1beta1"
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"
"gopkg.in/yaml.v3"

"github.com/kyma-project/kyma-environment-broker/internal"
"github.com/kyma-project/kyma-environment-broker/internal/process"
Expand Down Expand Up @@ -46,17 +51,19 @@ func (s *CreateRuntimeResourceStep) Run(operation internal.Operation, log logrus
return operation, 0, nil
}

runtimeCR, err := s.createRuntimeResourceObject(operation)
kymaResourceName, kymaResourceNamespace := getKymaNames(operation)

runtimeCR, err := s.createRuntimeResourceObject(operation, kymaResourceName, kymaResourceNamespace)
if err != nil {
return s.operationManager.OperationFailed(operation, fmt.Sprintf("while creating Runtime CR object: %s", err), err, log)
}

if s.kimConfig.DryRun {
yaml, err := RuntimeToYaml(runtimeCR)
if err != nil {
log.Errorf("failed to encode Runtime CR to yaml: %s", err)
log.Errorf("failed to encode Runtime CR as yaml: %s", err)
} else {
log.Infof("Runtime CR yaml:%s", yaml)
fmt.Println(yaml)
}
} else {
err := s.CreateResource(runtimeCR)
Expand All @@ -68,18 +75,71 @@ func (s *CreateRuntimeResourceStep) Run(operation internal.Operation, log logrus
return operation, 0, nil
}

func getKymaNames(operation internal.Operation) (string, string) {
template, err := steps.DecodeKymaTemplate(operation.KymaTemplate)
if err != nil {
//TODO remove fallback
return "", ""
}
return template.GetName(), template.GetNamespace()
}

func (s *CreateRuntimeResourceStep) CreateResource(cr *imv1.Runtime) error {
logrus.Info("Creating Runtime CR - TO BE IMPLEMENTED")
return nil
}

func (s *CreateRuntimeResourceStep) createRuntimeResourceObject(operation internal.Operation) (*imv1.Runtime, error) {
runtime := imv1.Runtime{}
runtime.Spec.Shoot.Name = "shoot-name"
func (s *CreateRuntimeResourceStep) createRuntimeResourceObject(operation internal.Operation, kymaName, kymaNamespace string) (*imv1.Runtime, error) {

runtime := imv1.Runtime{}
runtime.ObjectMeta.Name = operation.RuntimeID
runtime.ObjectMeta.Namespace = kymaNamespace
runtime.ObjectMeta.Labels = s.createLabelsForRuntime(operation, kymaName)
runtime.Spec.Shoot.Provider = s.createShootProvider(operation)
runtime.Spec.Shoot.Provider.Workers = []gardener.Worker{}
runtime.Spec.Shoot.Provider.Type = string(operation.ProvisioningParameters.PlatformProvider)
runtime.Spec.Security = s.createSecurityConfiguration(operation)
return &runtime, nil
}

func (s *CreateRuntimeResourceStep) createShootProvider(operation internal.Operation) imv1.Provider {
provider := imv1.Provider{}
logrus.Info("Creating Shoot Provider - TO BE IMPLEMENTED")
return provider
}

func (s *CreateRuntimeResourceStep) createLabelsForRuntime(operation internal.Operation, kymaName string) map[string]string {
labels := map[string]string{
"kyma-project.io/instance-id": operation.InstanceID,
"kyma-project.io/runtime-id": operation.RuntimeID,
"kyma-project.io/broker-plan-id": operation.ProvisioningParameters.PlanID,
"kyma-project.io/broker-plan-name": broker.PlanNamesMapping[operation.ProvisioningParameters.PlanID],
"kyma-project.io/global-account-id": operation.ProvisioningParameters.ErsContext.GlobalAccountID,
"kyma-project.io/subaccount-id": operation.ProvisioningParameters.ErsContext.SubAccountID,
"kyma-project.io/shoot-name": operation.ShootName,
"kyma-project.io/region": *operation.ProvisioningParameters.Parameters.Region,
"operator.kyma-project.io/kyma-name": kymaName,
}
return labels
}

func (s *CreateRuntimeResourceStep) createSecurityConfiguration(operation internal.Operation) imv1.Security {
security := imv1.Security{}
security.Administrators = operation.ProvisioningParameters.Parameters.RuntimeAdministrators
//TODO: Networking
//networking:
//filter:
// # spec.security.networking.filter.egress.enabled is required
//egress:
//enabled: false
// # spec.security.networking.filter.ingress.enabled is optional (default=false), not implemented in the first KIM release
// ingress:
// enabled: true

logrus.Info("Creating Security Configuration - UNDER CONSTRUCTION")
return security
}

func RuntimeToYaml(runtime *imv1.Runtime) (string, error) {
result, err := yaml.Marshal(runtime)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ func TestCreateRuntimeResourceStep_HappyPath_YamlOnly(t *testing.T) {
_, err = memoryStorage.Instances().GetByID(preOperation.InstanceID)
assert.NoError(t, err)

//assert.YAMLEq(t, expectedKymaTemplate, op.KymaTemplate)
}

0 comments on commit 509e6aa

Please sign in to comment.