Skip to content

Commit

Permalink
Merge pull request #948 from dheerajodha/STONEINTG-883
Browse files Browse the repository at this point in the history
feat(STONEINTG-883): Set owner reference to application for ALL Snapshots
  • Loading branch information
dheerajodha authored Dec 6, 2024
2 parents 7aa5515 + de2793a commit 392d2aa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
13 changes: 1 addition & 12 deletions internal/controller/snapshot/snapshot_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
clienterrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

applicationapiv1alpha1 "github.com/konflux-ci/application-api/api/v1alpha1"
"github.com/konflux-ci/integration-service/api/v1beta2"
Expand Down Expand Up @@ -522,18 +521,8 @@ func (a *Adapter) EnsureOverrideSnapshotValid() (controller.OperationResult, err
return controller.ContinueProcessing()
}

var err error
if !controllerutil.HasControllerReference(a.snapshot) {
a.snapshot, err = gitops.SetOwnerReference(a.context, a.client, a.snapshot, a.application)
if err != nil {
a.logger.Error(err, fmt.Sprintf("Failed to set owner reference for snapshot %s/%s", a.snapshot.Namespace, a.snapshot.Name))
return controller.RequeueWithError(err)
}
a.logger.Info("Owner reference has been set to snapshot")
}

// validate all snapshotComponents' containerImages/source in snapshot, make all errors joined
var errsForSnapshot error
var err, errsForSnapshot error
for _, snapshotComponent := range a.snapshot.Spec.Components {
snapshotComponent := snapshotComponent //G601
_, err := a.loader.GetComponent(a.context, a.client, snapshotComponent.Name, a.snapshot.Namespace)
Expand Down
1 change: 0 additions & 1 deletion internal/controller/snapshot/snapshot_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,6 @@ var _ = Describe("Snapshot Adapter", Ordered, func() {
result, err := adapter.EnsureOverrideSnapshotValid()
Expect(result.CancelRequest).To(BeFalse())
Expect(result.RequeueRequest).To(BeFalse())
Expect(controllerutil.HasControllerReference(hasInvalidOverrideSnapshot)).To(BeTrue())
Expect(buf.String()).Should(ContainSubstring("Snapshot has been marked as invalid"))
Expect(err).ToNot(HaveOccurred())

Expand Down
11 changes: 11 additions & 0 deletions internal/controller/snapshot/snapshot_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/client-go/util/retry"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

Expand Down Expand Up @@ -112,6 +113,16 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
return helpers.HandleLoaderError(logger, err, "Application", "Snapshot")
}

if !controllerutil.HasControllerReference(snapshot) {
snapshot, err = gitops.SetOwnerReference(ctx, r.Client, snapshot, application)
if err != nil {
logger.Error(err, fmt.Sprintf("Failed to set owner reference for snapshot %s/%s", snapshot.Namespace, snapshot.Name))
return ctrl.Result{}, err
}
logger.LogAuditEvent(fmt.Sprintf("Application %s has been set as a Controller OwnerReference on Snapshot %s", application.Name, snapshot.Name),
snapshot, helpers.LogActionUpdate)
}

logger = logger.WithApp(*application)

adapter := NewAdapter(ctx, snapshot, application, logger, loader, r.Client)
Expand Down
24 changes: 24 additions & 0 deletions internal/controller/snapshot/snapshot_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,30 @@ var _ = Describe("SnapshotController", func() {
Expect(err).To(BeNil())
})

It("can add the Application as a Controller OwnerReference on Snapshot", func() {
Expect(hasSnapshot.ObjectMeta.OwnerReferences).To(BeNil())

req := ctrl.Request{
NamespacedName: types.NamespacedName{
Name: hasSnapshot.Name,
Namespace: "default",
},
}
result, err := snapshotReconciler.Reconcile(ctx, req)
Expect(reflect.TypeOf(result)).To(Equal(reflect.TypeOf(reconcile.Result{})))
Expect(err).ToNot(HaveOccurred())

Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{
Namespace: hasSnapshot.Namespace,
Name: hasSnapshot.Name,
}, hasSnapshot)
return err == nil
}).Should(BeTrue())
Expect(hasSnapshot.ObjectMeta.OwnerReferences).ToNot(BeNil())
Expect(hasSnapshot.ObjectMeta.GetOwnerReferences()[0].Name).To(Equal(hasApp.Name))
})

It("Does not return an error if the component cannot be found", func() {
err := k8sClient.Delete(ctx, hasComp)
Eventually(func() bool {
Expand Down

0 comments on commit 392d2aa

Please sign in to comment.