Skip to content

Commit

Permalink
Add persist shoot state function
Browse files Browse the repository at this point in the history
  • Loading branch information
m00g3n committed Jun 18, 2024
1 parent d22acfb commit 1891611
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 11 deletions.
14 changes: 11 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package main
import (
"flag"
"fmt"
"os"
"time"

kubeconfig_controller "github.com/kyma-project/infrastructure-manager/internal/controller/kubeconfig"
runtime_controller "github.com/kyma-project/infrastructure-manager/internal/controller/runtime"
"github.com/kyma-project/infrastructure-manager/internal/controller/runtime/fsm"
"os"
"time"

"github.com/gardener/gardener/pkg/apis/core/v1beta1"
gardener_apis "github.com/gardener/gardener/pkg/client/core/clientset/versioned/typed/core/v1beta1"
Expand Down Expand Up @@ -67,6 +68,7 @@ func main() {
var minimalRotationTimeRatio float64
var expirationTime time.Duration
var enableRuntimeReconciler bool
var persistShoot bool

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 @@ -78,6 +80,7 @@ func main() {
flag.Float64Var(&minimalRotationTimeRatio, "minimal-rotation-time", defaultMinimalRotationTimeRatio, "The ratio determines what is the minimal time that needs to pass to rotate certificate.")
flag.DurationVar(&expirationTime, "kubeconfig-expiration-time", defaultExpirationTime, "Dynamic kubeconfig expiration time")
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")

opts := zap.Options{
Development: true,
Expand Down Expand Up @@ -142,13 +145,18 @@ func main() {
os.Exit(1)
}

cfg := fsm.RCCfg{Finalizer: infrastructuremanagerv1.Finalizer}
if persistShoot {
cfg.PVCPath = "/testdata/kim"
}

if enableRuntimeReconciler {
if err = (&runtime_controller.RuntimeReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ShootClient: shootClient,
Log: logger,
Cfg: fsm.RCCfg{Finalizer: infrastructuremanagerv1.Finalizer},
Cfg: cfg,
EventRecorder: mgr.GetEventRecorderFor("runtime-controller"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Runtime")
Expand Down
4 changes: 3 additions & 1 deletion internal/controller/runtime/fsm/runtime_fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package fsm
import (
"context"
"fmt"
"github.com/kyma-project/infrastructure-manager/internal/gardener"
"reflect"
"runtime"
"sync"

"github.com/kyma-project/infrastructure-manager/internal/gardener"

"github.com/go-logr/logr"
imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
"k8s.io/client-go/tools/record"
Expand All @@ -23,6 +24,7 @@ type stateFn func(context.Context, *fsm, *systemState) (stateFn, *ctrl.Result, e
// runtime reconciler specific configuration
type RCCfg struct {
Finalizer string
PVCPath string
}

func (f stateFn) String() string {
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/runtime/fsm/runtime_fsm_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
)

func stopWithErrorAndRequeue(err error) (stateFn, *ctrl.Result, error) {
// TODO Przegadać
func stopWithErrorAndNoRequeue(err error) (stateFn, *ctrl.Result, error) {
return sFnUpdateStatus(nil, err), nil, nil
}

Expand Down
14 changes: 10 additions & 4 deletions internal/controller/runtime/fsm/runtime_fsm_create_shoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fsm

import (
"context"

imv1 "github.com/kyma-project/infrastructure-manager/api/v1"

gardener_shoot "github.com/kyma-project/infrastructure-manager/internal/gardener/shoot"
Expand Down Expand Up @@ -30,10 +31,10 @@ func sFnCreateShoot(ctx context.Context, m *fsm, s *systemState) (stateFn, *ctrl

m.log.Info("Shoot mapped successfully", "Name", shoot.Name, "Namespace", shoot.Namespace, "Shoot", shoot)

createdShoot, provisioningErr := m.ShootClient.Create(ctx, &shoot, v1.CreateOptions{})
s.shoot, err = m.ShootClient.Create(ctx, &shoot, v1.CreateOptions{})

if provisioningErr != nil {
m.log.Error(provisioningErr, "Failed to create new gardener Shoot")
if err != nil {
m.log.Error(err, "Failed to create new gardener Shoot")

s.instance.UpdateStateError(
imv1.ConditionTypeRuntimeProvisioning,
Expand All @@ -44,14 +45,19 @@ func sFnCreateShoot(ctx context.Context, m *fsm, s *systemState) (stateFn, *ctrl
return stopWithRequeue()
}

m.log.Info("Gardener shoot for runtime initialised successfully", "Name", createdShoot.Name, "Namespace", createdShoot.Namespace)
m.log.Info("Gardener shoot for runtime initialised successfully", "Name", s.shoot.Name, "Namespace", s.shoot.Namespace)

s.instance.UpdateStateCreating(
imv1.ConditionTypeRuntimeProvisioning,
imv1.ConditionReasonShootCreationPending,
"Shoot is pending",
)

shouldPersistShoot := m.PVCPath != ""
if shouldPersistShoot {
return switchState(sFnPersistShoot)
}

return stopWithRequeue()
}

Expand Down
3 changes: 2 additions & 1 deletion internal/controller/runtime/fsm/runtime_fsm_initialise.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fsm

import (
"context"

imv1 "github.com/kyma-project/infrastructure-manager/api/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand All @@ -21,7 +22,7 @@ func sFnInitialize(ctx context.Context, m *fsm, s *systemState) (stateFn, *ctrl.

err := m.Update(ctx, &s.instance)
if err != nil {
return stopWithErrorAndRequeue(err)
return stopWithErrorAndNoRequeue(err)
}

s.instance.UpdateStateCreating(
Expand Down
45 changes: 45 additions & 0 deletions internal/controller/runtime/fsm/runtime_fsm_persist_shoot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package fsm

import (
"context"
"fmt"
"io"
"os"

gardener "github.com/gardener/gardener/pkg/apis/core/v1beta1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/yaml"
)

var getWriter = func(filePath string) (io.Writer, error) {
file, err := os.Create(filePath)
if err != nil {
return nil, fmt.Errorf("unable to create file: %w", err)
}
return file, nil
}

func persist(path string, s *gardener.Shoot) error {
writer, err := getWriter(path)
if err != nil {
return fmt.Errorf("unable to create file: %w", err)
}

b, err := yaml.Marshal(s)
if err != nil {
return fmt.Errorf("unable to marshal shoot: %w", err)
}

if _, err = writer.Write(b); err != nil {
return fmt.Errorf("unable to write to file: %w", err)
}
return nil
}

func sFnPersistShoot(_ context.Context, m *fsm, s *systemState) (stateFn, *ctrl.Result, error) {
path := fmt.Sprintf("%s/%s-%s.yaml", m.PVCPath, s.shoot.Namespace, s.shoot.Name)
if err := persist(path, s.shoot); err != nil {
return stopWithErrorAndNoRequeue(err)
}
return stopWithNoRequeue()
}
40 changes: 40 additions & 0 deletions internal/controller/runtime/fsm/runtime_fsm_persist_shoot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package fsm

import (
"bytes"
"context"
"io"
"time"

"github.com/kyma-project/infrastructure-manager/internal/controller/runtime/fsm/testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"sigs.k8s.io/yaml"
)

type writerGetter = func(string) (io.Writer, error)

var _ = Describe("KIM sFnPersist", func() {

var b bytes.Buffer
testStringWriter := func() writerGetter {
return func(string) (io.Writer, error) {
return &b, nil
}
}

getWriter = testStringWriter()

testCtx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

expectedData, err := yaml.Marshal(&testing.ShootNoDNS)
Expect(err).ShouldNot(HaveOccurred())

It("shoutld persist shoot data", func() {
next, _, err := sFnPersistShoot(testCtx, must(newFakeFSM), &systemState{shoot: &testing.ShootNoDNS})
Expect(err).To(BeNil())
Expect(next).To(haveName("sFnUpdateStatus"))
Expect(expectedData).To(Equal(b.Bytes()))
})
})
4 changes: 3 additions & 1 deletion internal/controller/runtime/runtime_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ func NewRuntimeReconciler(mgr ctrl.Manager, shootClient gardener.ShootClient, lo
Scheme: mgr.GetScheme(),
ShootClient: shootClient,
Log: logger,
Cfg: fsm.RCCfg{Finalizer: imv1.Finalizer},
Cfg: fsm.RCCfg{
Finalizer: imv1.Finalizer,
},
}
}

Expand Down

0 comments on commit 1891611

Please sign in to comment.