diff --git a/internal/controller/runtime/fsm/runtime_fsm.go b/internal/controller/runtime/fsm/runtime_fsm.go index 7fa76793..ca4839a8 100644 --- a/internal/controller/runtime/fsm/runtime_fsm.go +++ b/internal/controller/runtime/fsm/runtime_fsm.go @@ -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" @@ -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 { diff --git a/internal/controller/runtime/fsm/runtime_fsm_common.go b/internal/controller/runtime/fsm/runtime_fsm_common.go index e22bfd3d..7f55cfaa 100644 --- a/internal/controller/runtime/fsm/runtime_fsm_common.go +++ b/internal/controller/runtime/fsm/runtime_fsm_common.go @@ -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 } diff --git a/internal/controller/runtime/fsm/runtime_fsm_create_shoot.go b/internal/controller/runtime/fsm/runtime_fsm_create_shoot.go index 14631e10..6726c4ae 100644 --- a/internal/controller/runtime/fsm/runtime_fsm_create_shoot.go +++ b/internal/controller/runtime/fsm/runtime_fsm_create_shoot.go @@ -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" @@ -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, @@ -44,7 +45,7 @@ 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, diff --git a/internal/controller/runtime/fsm/runtime_fsm_initialise.go b/internal/controller/runtime/fsm/runtime_fsm_initialise.go index 550b1915..a234d7ed 100644 --- a/internal/controller/runtime/fsm/runtime_fsm_initialise.go +++ b/internal/controller/runtime/fsm/runtime_fsm_initialise.go @@ -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" @@ -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( diff --git a/internal/controller/runtime/fsm/runtime_fsm_persist_rt.go b/internal/controller/runtime/fsm/runtime_fsm_persist_rt.go new file mode 100644 index 00000000..8886496e --- /dev/null +++ b/internal/controller/runtime/fsm/runtime_fsm_persist_rt.go @@ -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 sFnPersistRt(_ 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() +} diff --git a/internal/controller/runtime/fsm/runtime_fsm_persist_rt_test.go b/internal/controller/runtime/fsm/runtime_fsm_persist_rt_test.go new file mode 100644 index 00000000..99aa3ec4 --- /dev/null +++ b/internal/controller/runtime/fsm/runtime_fsm_persist_rt_test.go @@ -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 := sFnPersistRt(testCtx, must(newFakeFSM), &systemState{shoot: &testing.ShootNoDNS}) + Expect(err).To(BeNil()) + Expect(next).To(haveName("sFnUpdateStatus")) + Expect(expectedData).To(Equal(b.Bytes())) + }) +}) diff --git a/internal/controller/runtime/runtime_controller.go b/internal/controller/runtime/runtime_controller.go index 67690c52..9ec89d3b 100644 --- a/internal/controller/runtime/runtime_controller.go +++ b/internal/controller/runtime/runtime_controller.go @@ -81,7 +81,10 @@ 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, + PVCPath: "/testdata/kim", + }, } }