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 bb80baa
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 8 deletions.
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
9 changes: 5 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,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,
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_rt.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 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()
}
40 changes: 40 additions & 0 deletions internal/controller/runtime/fsm/runtime_fsm_persist_rt_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 := sFnPersistRt(testCtx, must(newFakeFSM), &systemState{shoot: &testing.ShootNoDNS})
Expect(err).To(BeNil())
Expect(next).To(haveName("sFnUpdateStatus"))
Expect(expectedData).To(Equal(b.Bytes()))
})
})
5 changes: 4 additions & 1 deletion internal/controller/runtime/runtime_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
}

Expand Down

0 comments on commit bb80baa

Please sign in to comment.