Skip to content

Commit

Permalink
Create mirrored manifest if missing
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Mazzotti <andrea.mazzotti@suse.com>
  • Loading branch information
anmazzotti committed Dec 6, 2024
1 parent cde9255 commit 818568c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
18 changes: 16 additions & 2 deletions internal/sync/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/util/retry"
Expand Down Expand Up @@ -50,8 +51,21 @@ func NewDefaultSynchronizer(cl client.Client, capiProvider *turtlesv1.CAPIProvid
func (s *DefaultSynchronizer) Get(ctx context.Context) error {
log := log.FromContext(ctx)

if err := s.client.Get(ctx, client.ObjectKeyFromObject(s.Destination), s.Destination); client.IgnoreNotFound(err) != nil {
log.Error(err, "Unable to get mirrored manifest: "+client.ObjectKeyFromObject(s.Destination).String())
objKey := client.ObjectKeyFromObject(s.Destination)

err := s.client.Get(ctx, objKey, s.Destination)
if apierrors.IsNotFound(err) {
log.Info("Mirrored manifest is missing. Creating a new one.")

if err := s.client.Create(ctx, s.Destination); err != nil {
return fmt.Errorf("creating mirrored manifest: %w", err)
}

return nil
}

if err != nil {
log.Error(err, "Unable to get mirrored manifest: "+objKey.String())

return err
}
Expand Down
17 changes: 17 additions & 0 deletions internal/sync/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
turtlesv1 "github.com/rancher/turtles/api/v1alpha1"
"github.com/rancher/turtles/internal/sync"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -89,4 +91,19 @@ var _ = Describe("Core provider", func() {
Eventually(Object(secret)).Should(
HaveField("OwnerReferences", HaveLen(1)))
})

It("Should create not-found mirrored manifest", func() {
secret := &corev1.Secret{ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: ns.Name,
}}

err := testEnv.Client.Get(ctx, client.ObjectKeyFromObject(secret), secret)
Expect(apierrors.IsNotFound(err)).Should(BeTrue())

s := sync.NewDefaultSynchronizer(testEnv, capiProvider, secret)
Expect(s.Get(ctx)).To(Succeed())

Expect(testEnv.Client.Get(ctx, client.ObjectKeyFromObject(secret), secret)).Should(Succeed())
})
})

0 comments on commit 818568c

Please sign in to comment.