diff --git a/apis/delivery/v1alpha1/sync_types.go b/apis/delivery/v1alpha1/sync_types.go index 6e26d75..7f1535f 100644 --- a/apis/delivery/v1alpha1/sync_types.go +++ b/apis/delivery/v1alpha1/sync_types.go @@ -7,6 +7,7 @@ package v1alpha1 import ( "time" + "github.com/fluxcd/pkg/apis/meta" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -33,12 +34,12 @@ type PullRequestTemplate struct { // SyncSpec defines the desired state of Sync type SyncSpec struct { - SnapshotRef v1.LocalObjectReference `json:"snapshotRef"` - RepositoryRef v1.LocalObjectReference `json:"repositoryRef"` - Interval metav1.Duration `json:"interval"` - CommitTemplate CommitTemplate `json:"commitTemplate"` - SubPath string `json:"subPath"` - Prune bool `json:"prune,omitempty"` + SnapshotRef v1.LocalObjectReference `json:"snapshotRef"` + RepositoryRef meta.NamespacedObjectReference `json:"repositoryRef"` + Interval metav1.Duration `json:"interval"` + CommitTemplate CommitTemplate `json:"commitTemplate"` + SubPath string `json:"subPath"` + Prune bool `json:"prune,omitempty"` //+optional AutomaticPullRequestCreation bool `json:"automaticPullRequestCreation,omitempty"` diff --git a/config/crd/bases/delivery.ocm.software_syncs.yaml b/config/crd/bases/delivery.ocm.software_syncs.yaml index 7ddaa0d..f6785d4 100644 --- a/config/crd/bases/delivery.ocm.software_syncs.yaml +++ b/config/crd/bases/delivery.ocm.software_syncs.yaml @@ -73,15 +73,19 @@ spec: type: string type: object repositoryRef: - description: LocalObjectReference contains enough information to let - you locate the referenced object inside the same namespace. + description: NamespacedObjectReference contains enough information + to locate the referenced Kubernetes resource object in any namespace. properties: name: - description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names - TODO: Add other useful fields. apiVersion, kind, uid?' + description: Name of the referent. type: string + namespace: + description: Namespace of the referent, when not specified it + acts as LocalObjectReference. + type: string + required: + - name type: object - x-kubernetes-map-type: atomic snapshotRef: description: LocalObjectReference contains enough information to let you locate the referenced object inside the same namespace. diff --git a/controllers/delivery/sync_controller.go b/controllers/delivery/sync_controller.go index e2d8873..39aa960 100644 --- a/controllers/delivery/sync_controller.go +++ b/controllers/delivery/sync_controller.go @@ -141,9 +141,14 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resul log.V(4).Info("found target snapshot") + namespace := obj.Spec.RepositoryRef.Namespace + if namespace == "" { + namespace = obj.Namespace + } + repository := &mpasv1alpha1.Repository{} if err = r.Get(ctx, types.NamespacedName{ - Namespace: obj.Namespace, + Namespace: namespace, Name: obj.Spec.RepositoryRef.Name, }, repository); err != nil { err = fmt.Errorf("failed to find repository: %w", err) @@ -156,7 +161,7 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (resul authSecret := &corev1.Secret{} if err = r.Get(ctx, types.NamespacedName{ - Namespace: obj.Namespace, + Namespace: repository.Namespace, Name: repository.Spec.Credentials.SecretRef.Name, }, authSecret); err != nil { err = fmt.Errorf("failed to find authentication secret: %w", err) diff --git a/controllers/delivery/sync_controller_test.go b/controllers/delivery/sync_controller_test.go index 57fc4b3..c45ada0 100644 --- a/controllers/delivery/sync_controller_test.go +++ b/controllers/delivery/sync_controller_test.go @@ -65,7 +65,7 @@ func TestSyncReconciler(t *testing.T) { SnapshotRef: v1.LocalObjectReference{ Name: snapshot.Name, }, - RepositoryRef: v1.LocalObjectReference{ + RepositoryRef: meta.NamespacedObjectReference{ Name: repository.Name, }, CommitTemplate: v1alpha1.CommitTemplate{ @@ -117,7 +117,7 @@ func TestSyncReconcilerIsSkippedIfDigestIsAlreadyPresent(t *testing.T) { SnapshotRef: v1.LocalObjectReference{ Name: "test", }, - RepositoryRef: v1.LocalObjectReference{ + RepositoryRef: meta.NamespacedObjectReference{ Name: "test", }, CommitTemplate: v1alpha1.CommitTemplate{ @@ -198,7 +198,7 @@ func TestSyncReconcilerWithAutomaticPullRequest(t *testing.T) { SnapshotRef: v1.LocalObjectReference{ Name: snapshot.Name, }, - RepositoryRef: v1.LocalObjectReference{ + RepositoryRef: meta.NamespacedObjectReference{ Name: repository.Name, }, CommitTemplate: v1alpha1.CommitTemplate{