Skip to content

Commit

Permalink
Merge pull request #292 from m00g3n/support-all-hs
Browse files Browse the repository at this point in the history
feat: Parametrize gardener shoot converter configuration
  • Loading branch information
kyma-bot authored Jul 16, 2024
2 parents 533e629 + e5a0aea commit c7c80fc
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 44 deletions.
2 changes: 1 addition & 1 deletion api/v1/runtime_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ type RuntimeShoot struct {
LicenceType *string `json:"licenceType,omitempty"`
SecretBindingName string `json:"secretBindingName"`
EnforceSeedLocation *bool `json:"enforceSeedLocation,omitempty"`
Kubernetes Kubernetes `json:"kubernetes"`
Kubernetes Kubernetes `json:"kubernetes,omitempty"`
Provider Provider `json:"provider"`
Networking Networking `json:"networking"`
ControlPlane gardener.ControlPlane `json:"controlPlane"`
Expand Down
31 changes: 29 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ package main
import (
"flag"
"fmt"
"io"
"os"
"time"

"github.com/gardener/gardener/pkg/apis/core/v1beta1"
gardener_apis "github.com/gardener/gardener/pkg/client/core/clientset/versioned/typed/core/v1beta1"
"github.com/go-playground/validator/v10"
infrastructuremanagerv1 "github.com/kyma-project/infrastructure-manager/api/v1"
kubeconfig_controller "github.com/kyma-project/infrastructure-manager/internal/controller/kubeconfig"
"github.com/kyma-project/infrastructure-manager/internal/controller/metrics"
runtime_controller "github.com/kyma-project/infrastructure-manager/internal/controller/runtime"
"github.com/kyma-project/infrastructure-manager/internal/controller/runtime/fsm"
"github.com/kyma-project/infrastructure-manager/internal/gardener"
"github.com/kyma-project/infrastructure-manager/internal/gardener/kubeconfig"
"github.com/kyma-project/infrastructure-manager/internal/gardener/shoot"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand Down Expand Up @@ -70,6 +73,7 @@ func main() {
var gardenerRequestTimeout time.Duration
var enableRuntimeReconciler bool
var persistShoot bool
var converterConfigFilepath string

flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
Expand All @@ -83,6 +87,7 @@ func main() {
flag.DurationVar(&gardenerRequestTimeout, "gardener-request-timeout", defaultGardenerRequestTimeout, "Timeout duration for requests to Gardener")
flag.BoolVar(&enableRuntimeReconciler, "runtime-reconciler-enabled", defaultRuntimeReconcilerEnabled, "Feature flag for all runtime reconciler functionalities")
flag.BoolVar(&persistShoot, "persist-shoot", false, "Feature flag to allow persisting created shoots")
flag.StringVar(&converterConfigFilepath, "converter-config-filepath", "hack/converter_config.json", "A file path to the gardener shoot converter configuration.")

opts := zap.Options{
Development: true,
Expand All @@ -91,7 +96,6 @@ func main() {
flag.Parse()

logger := zap.New(zap.UseFlagOptions(&opts))

ctrl.SetLogger(logger)

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Expand Down Expand Up @@ -149,16 +153,39 @@ func main() {
os.Exit(1)
}

// load converter configuration
getReader := func() (io.Reader, error) {
return os.Open(converterConfigFilepath)
}
var converterConfig shoot.ConverterConfig
if err = converterConfig.Load(getReader); err != nil {
setupLog.Error(err, "unable to load converter configuration")
os.Exit(1)
}

validate := validator.New(validator.WithRequiredStructEnabled())
if err = validate.Struct(converterConfig); err != nil {
setupLog.Error(err, "invalid converter configuration")
os.Exit(1)
}

cfg := fsm.RCCfg{
Finalizer: infrastructuremanagerv1.Finalizer,
ShootNamesapace: gardenerNamespace,
ConverterConfig: converterConfig,
}

if persistShoot {
cfg.PVCPath = "/testdata/kim"
}

if enableRuntimeReconciler {
runtimeReconciler := runtime_controller.NewRuntimeReconciler(mgr, gardenerClient, logger, cfg)
runtimeReconciler := runtime_controller.NewRuntimeReconciler(
mgr,
gardenerClient,
logger,
cfg,
)

if err = runtimeReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to setup controller with Manager", "controller", "Runtime")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,6 @@ spec:
type: string
required:
- controlPlane
- kubernetes
- name
- networking
- platformRegion
Expand Down
22 changes: 22 additions & 0 deletions config/manager/converter_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: converter-config
data:
converter_config_aws.json: |
{
"kubernetes": {
"defaultVersion": "1.29"
},
"dns": {
"secretName": "aws-route53-secret-dev",
"domainPrefix": "dev.kyma.ondemand.com",
"providerType": "aws-route53"
},
"aws": {
"enableIMDSv2": "true"
},
"gardener": {
"projectName": "kyma-dev"
}
}
1 change: 1 addition & 0 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
resources:
- manager.yaml
- converter_config.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gardener/gardener-extension-provider-gcp v1.37.0
github.com/gardener/gardener-extension-provider-openstack v1.41.0
github.com/go-logr/logr v1.4.2
github.com/go-playground/validator/v10 v10.22.0
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/pkg/errors v0.9.1
Expand All @@ -29,10 +30,13 @@ require (
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand All @@ -47,6 +51,7 @@ require (
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -59,6 +64,7 @@ require (
github.com/stretchr/objx v0.5.2 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0
github.com/evanphx/json-patch/v5 v5.9.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gardener/gardener v1.98.2 h1:gD7ZqUZet/OP8x6uiwjBzFOCvsgWDewF5IwWtaE6EfM=
github.com/gardener/gardener v1.98.2/go.mod h1:EfvKbBF53a52Wz16/Qe8hhpTZTaJ/P+CRWRr35BVVq0=
github.com/gardener/gardener-extension-provider-aws v1.56.0 h1:dESZ7IKBH8klWGYitg5hsX1aN1Cb+Oz3qLBxJ71Ltek=
Expand All @@ -39,6 +41,14 @@ github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao=
github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down Expand Up @@ -79,6 +89,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
19 changes: 19 additions & 0 deletions hack/converter_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"kubernetes": {
"defaultVersion": "1.29"
},
"dns": {
"secretName": "aws-route53-secret-dev",
"domainPrefix": "dev.kyma.ondemand.com",
"providerType": "aws-route53"
},
"aws": {
"enableIMDSv2": "true"
},
"machineImage": {
"defaultVersion": "1312.3.0"
},
"gardener": {
"projectName": "kyma-dev"
}
}
2 changes: 2 additions & 0 deletions internal/controller/runtime/fsm/runtime_fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/go-logr/logr"
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"github.com/kyma-project/infrastructure-manager/internal/gardener/shoot"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -30,6 +31,7 @@ type RCCfg struct {
Finalizer string
PVCPath string
ShootNamesapace string
shoot.ConverterConfig
}

func (f stateFn) String() string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func sFnCreateShoot(ctx context.Context, m *fsm, s *systemState) (stateFn, *ctrl.Result, error) {
m.log.Info("Create shoot")
newShoot, err := convertShoot(&s.instance)
newShoot, err := convertShoot(&s.instance, m.ConverterConfig)
if err != nil {
m.log.Error(err, "Failed to convert Runtime instance to shoot object")
return updateStatePendingWithErrorAndStop(&s.instance, imv1.ConditionTypeRuntimeProvisioned, imv1.ConditionReasonConversionError, "Runtime conversion error")
Expand Down
30 changes: 4 additions & 26 deletions internal/controller/runtime/fsm/runtime_fsm_patch_shoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

gardener "github.com/gardener/gardener/pkg/apis/core/v1beta1"
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"github.com/kyma-project/infrastructure-manager/internal/gardener/shoot"
gardener_shoot "github.com/kyma-project/infrastructure-manager/internal/gardener/shoot"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -13,7 +14,7 @@ import (
func sFnPatchExistingShoot(ctx context.Context, m *fsm, s *systemState) (stateFn, *ctrl.Result, error) {
m.log.Info("Patch shoot state")

updatedShoot, err := convertShoot(&s.instance)
updatedShoot, err := convertShoot(&s.instance, m.ConverterConfig)
if err != nil {
m.log.Error(err, "Failed to convert Runtime instance to shoot object, exiting with no retry")
return updateStatePendingWithErrorAndStop(&s.instance, imv1.ConditionTypeRuntimeProvisioned, imv1.ConditionReasonConversionError, "Runtime conversion error")
Expand Down Expand Up @@ -55,9 +56,8 @@ func sFnPatchExistingShoot(ctx context.Context, m *fsm, s *systemState) (stateFn
return updateStatusAndRequeueAfter(gardenerRequeueDuration)
}

func convertShoot(instance *imv1.Runtime) (gardener.Shoot, error) {
converterConfig := FixConverterConfig()
converter := gardener_shoot.NewConverter(converterConfig)
func convertShoot(instance *imv1.Runtime, cfg shoot.ConverterConfig) (gardener.Shoot, error) {
converter := gardener_shoot.NewConverter(cfg)
shoot, err := converter.ToShoot(*instance) // returned error is always nil BTW

if err == nil {
Expand All @@ -67,28 +67,6 @@ func convertShoot(instance *imv1.Runtime) (gardener.Shoot, error) {
return shoot, err
}

func FixConverterConfig() gardener_shoot.ConverterConfig {
return gardener_shoot.ConverterConfig{
Kubernetes: gardener_shoot.KubernetesConfig{
DefaultVersion: "1.29", //nolint:godox TODO: Should be parametrised
},

DNS: gardener_shoot.DNSConfig{
SecretName: "aws-route53-secret-dev",
DomainPrefix: "dev.kyma.ondemand.com",
ProviderType: "aws-route53",
},
Provider: gardener_shoot.ProviderConfig{
AWS: gardener_shoot.AWSConfig{
EnableIMDSv2: true, //nolint:godox TODO: Should be parametrised
},
},
Gardener: gardener_shoot.GardenerConfig{
ProjectName: "kyma-dev", //nolint:godox TODO: should be parametrised
},
}
}

// workaround
func setObjectFields(shoot *gardener.Shoot) {
shoot.Kind = "Shoot"
Expand Down
38 changes: 25 additions & 13 deletions internal/gardener/shoot/converter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package shoot

import (
"encoding/json"
"fmt"
"io"

gardener "github.com/gardener/gardener/pkg/apis/core/v1beta1"
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
Expand All @@ -17,37 +19,47 @@ type Converter struct {
}

type ProviderConfig struct {
AWS AWSConfig
AWS AWSConfig `json:"aws"`
}

type AWSConfig struct {
EnableIMDSv2 bool
EnableIMDSv2 bool `json:"enableIMDSv2"`
}

type DNSConfig struct {
SecretName string
DomainPrefix string
ProviderType string
SecretName string `json:"secretName" validate:"required"`
DomainPrefix string `json:"domainPrefix" validate:"required"`
ProviderType string `json:"providerType" validate:"required"`
}

type KubernetesConfig struct {
DefaultVersion string
DefaultVersion string `json:"defaultVersion" validate:"required"`
}

type ReaderGetter = func() (io.Reader, error)

type ConverterConfig struct {
Kubernetes KubernetesConfig
DNS DNSConfig
Provider ProviderConfig
MachineImage MachineImageConfig
Gardener GardenerConfig
Kubernetes KubernetesConfig `json:"kubernetes" validate:"required"`
DNS DNSConfig `json:"dns" validate:"required"`
Provider ProviderConfig `json:"provider"`
MachineImage MachineImageConfig `json:"machineImage" validate:"required"`
Gardener GardenerConfig `json:"gardener" validate:"required"`
}

func (c *ConverterConfig) Load(f ReaderGetter) error {
r, err := f()
if err != nil {
return err
}
return json.NewDecoder(r).Decode(c)
}

type GardenerConfig struct {
ProjectName string
ProjectName string `json:"projectName" validate:"required"`
}

type MachineImageConfig struct {
DefaultVersion string
DefaultVersion string `json:"defaultVersion" validate:"required"`
}

func NewConverter(config ConverterConfig) Converter {
Expand Down
Loading

0 comments on commit c7c80fc

Please sign in to comment.