forked from kyma-project/infrastructure-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
101 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
internal/controller/runtime/fsm/runtime_fsm_persist_rt_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters