Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Commit 0cb6f40

Browse files
committed
feat: add consuming repository object to sync
1 parent 21d207c commit 0cb6f40

File tree

12 files changed

+121
-107
lines changed

12 files changed

+121
-107
lines changed

apis/delivery/v1alpha1/condition_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const (
1111
// SnapshotGetFailedReason is used when the needed snapshot does not exist.
1212
SnapshotGetFailedReason = "SnapshotGetFailed"
1313

14+
// RepositoryGetFailedReason is used when the needed repository does not exist.
15+
RepositoryGetFailedReason = "RepositoryGetFailed"
16+
1417
// CredentialsNotFoundReason is used when the needed authentication does not exist.
1518
CredentialsNotFoundReason = "CredentialsNotFound"
1619

apis/delivery/v1alpha1/sync_types.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@ package v1alpha1
77
import (
88
"time"
99

10+
v1 "k8s.io/api/core/v1"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
)
1213

13-
// Ref defines a name and namespace ref to any object.
14-
type Ref struct {
15-
Name string `json:"name"`
16-
Namespace string `json:"namespace"`
17-
}
18-
1914
// CommitTemplate defines the details of the commit to the external repository.
2015
type CommitTemplate struct {
2116
Name string `json:"name"`
@@ -25,14 +20,17 @@ type CommitTemplate struct {
2520

2621
// SyncSpec defines the desired state of Sync
2722
type SyncSpec struct {
28-
SnapshotRef Ref `json:"snapshotRef"`
29-
Interval metav1.Duration `json:"interval"`
30-
URL string `json:"url"`
31-
Branch string `json:"branch"`
32-
AuthRef Ref `json:"authRef"`
33-
CommitTemplate *CommitTemplate `json:"commitTemplate"`
34-
SubPath string `json:"subPath"`
35-
Prune bool `json:"prune,omitempty"`
23+
SnapshotRef v1.LocalObjectReference `json:"snapshotRef"`
24+
RepositoryRef v1.LocalObjectReference `json:"repositoryRef"`
25+
Interval metav1.Duration `json:"interval"`
26+
CommitTemplate *CommitTemplate `json:"commitTemplate"`
27+
SubPath string `json:"subPath"`
28+
Prune bool `json:"prune,omitempty"`
29+
30+
//+optional
31+
Branch string `json:"branch,omitempty"`
32+
//+optional
33+
AutomaticPullRequestCreation bool `json:"automaticPullRequestCreation,omitempty"`
3634
}
3735

3836
// SyncStatus defines the observed state of Sync

apis/delivery/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/mpas/v1alpha1/repository_types.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
package v1alpha1
66

77
import (
8+
"fmt"
9+
"strings"
810
"time"
911

12+
v1 "k8s.io/api/core/v1"
1013
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1114
)
1215

13-
// SecretRef is a reference to a secret in the same namespace as the referencing object.
14-
type SecretRef struct {
15-
Name string `json:"name"`
16-
}
17-
1816
// Credentials contains ways of authenticating the creation of a repository.
1917
type Credentials struct {
20-
SecretRef SecretRef `json:"secretRef"`
18+
SecretRef v1.LocalObjectReference `json:"secretRef"`
2119
}
2220

2321
// ExistingRepositoryPolicy defines what to do in case a requested repository already exists.
@@ -53,9 +51,6 @@ type RepositorySpec struct {
5351
//+optional
5452
Maintainers []string `json:"maintainers,omitempty"`
5553
//+optional
56-
//+kubebuilder:default:=true
57-
AutomaticPullRequestCreation bool `json:"automaticPullRequestCreation,omitempty"`
58-
//+optional
5954
//+kubebuilder:default:=adopt
6055
//+kubebuilder:validation:Enum=adopt;fail
6156
ExistingRepositoryPolicy ExistingRepositoryPolicy `json:"existingRepositoryPolicy,omitempty"`
@@ -89,6 +84,30 @@ func (in Repository) GetRequeueAfter() time.Duration {
8984
return in.Spec.Interval.Duration
9085
}
9186

87+
// GetRepositoryURL construct a repository URL based on either domain or the provider data.
88+
func (in Repository) GetRepositoryURL() string {
89+
if in.Spec.Domain != "" {
90+
if strings.Contains(in.Spec.Domain, "@") {
91+
return fmt.Sprintf("%s:%s/%s", in.Spec.Domain, in.Spec.Owner, in.Spec.RepositoryName)
92+
}
93+
94+
return fmt.Sprintf("%s/%s/%s", in.Spec.Domain, in.Spec.Owner, in.Spec.RepositoryName)
95+
}
96+
97+
domain := ""
98+
99+
switch in.Spec.Provider {
100+
case "github":
101+
domain = "github.com"
102+
case "gitlab":
103+
domain = "gitlab.com"
104+
case "gitea":
105+
domain = "gitea.com"
106+
}
107+
108+
return fmt.Sprintf("https://%s/%s/%s", domain, in.Spec.Owner, in.Spec.RepositoryName)
109+
}
110+
92111
//+kubebuilder:object:root=true
93112
//+kubebuilder:subresource:status
94113

apis/mpas/v1alpha1/zz_generated.deepcopy.go

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/delivery.ocm.software_syncs.yaml

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,8 @@ spec:
3535
spec:
3636
description: SyncSpec defines the desired state of Sync
3737
properties:
38-
authRef:
39-
description: Ref defines a name and namespace ref to any object.
40-
properties:
41-
name:
42-
type: string
43-
namespace:
44-
type: string
45-
required:
46-
- name
47-
- namespace
48-
type: object
38+
automaticPullRequestCreation:
39+
type: boolean
4940
branch:
5041
type: string
5142
commitTemplate:
@@ -67,29 +58,34 @@ spec:
6758
type: string
6859
prune:
6960
type: boolean
70-
snapshotRef:
71-
description: Ref defines a name and namespace ref to any object.
61+
repositoryRef:
62+
description: LocalObjectReference contains enough information to let
63+
you locate the referenced object inside the same namespace.
7264
properties:
7365
name:
66+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
67+
TODO: Add other useful fields. apiVersion, kind, uid?'
7468
type: string
75-
namespace:
69+
type: object
70+
x-kubernetes-map-type: atomic
71+
snapshotRef:
72+
description: LocalObjectReference contains enough information to let
73+
you locate the referenced object inside the same namespace.
74+
properties:
75+
name:
76+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
77+
TODO: Add other useful fields. apiVersion, kind, uid?'
7678
type: string
77-
required:
78-
- name
79-
- namespace
8079
type: object
80+
x-kubernetes-map-type: atomic
8181
subPath:
8282
type: string
83-
url:
84-
type: string
8583
required:
86-
- authRef
87-
- branch
8884
- commitTemplate
8985
- interval
86+
- repositoryRef
9087
- snapshotRef
9188
- subPath
92-
- url
9389
type: object
9490
status:
9591
description: SyncStatus defines the observed state of Sync

config/crd/bases/mpas.ocm.software_repositories.yaml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,20 @@ spec:
3535
spec:
3636
description: RepositorySpec defines the desired state of Repository
3737
properties:
38-
automaticPullRequestCreation:
39-
default: true
40-
type: boolean
4138
credentials:
4239
description: Credentials contains ways of authenticating the creation
4340
of a repository.
4441
properties:
4542
secretRef:
46-
description: SecretRef is a reference to a secret in the same
47-
namespace as the referencing object.
43+
description: LocalObjectReference contains enough information
44+
to let you locate the referenced object inside the same namespace.
4845
properties:
4946
name:
47+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
48+
TODO: Add other useful fields. apiVersion, kind, uid?'
5049
type: string
51-
required:
52-
- name
5350
type: object
51+
x-kubernetes-map-type: atomic
5452
required:
5553
- secretRef
5654
type: object

config/samples/delivery_v1alpha1_sync.yaml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,14 @@ metadata:
44
name: git-sample
55
namespace: ocm-system
66
spec:
7-
authRef:
8-
name: git-secret
9-
namespace: ocm-system
107
branch: main
118
commitTemplate:
129
email: <email>
1310
message: "Update made from git-controller"
1411
name: Gergely Brautigam
1512
interval: 10m0s
1613
subPath: ./application/podinfo
17-
url: https://github.com/Skarlso/test
18-
componentRef:
19-
name: ocm-test-component
20-
namespace: ocm-system
2114
snapshotRef:
2215
name: ocm-test-snapshot
23-
namespace: ocm-system
16+
repositoryRef:
17+
name: test-repositroy

controllers/delivery/sync_controller.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
ocmv1 "github.com/open-component-model/ocm-controller/api/v1alpha1"
2525

2626
"github.com/open-component-model/git-controller/apis/delivery/v1alpha1"
27+
mpasv1alpha1 "github.com/open-component-model/git-controller/apis/mpas/v1alpha1"
2728
providers "github.com/open-component-model/git-controller/pkg"
2829
)
2930

@@ -129,7 +130,7 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
129130

130131
snapshot := &ocmv1.Snapshot{}
131132
if err := r.Get(ctx, types.NamespacedName{
132-
Namespace: obj.Spec.SnapshotRef.Namespace,
133+
Namespace: obj.Namespace,
133134
Name: obj.Spec.SnapshotRef.Name,
134135
}, snapshot); err != nil {
135136
retErr = fmt.Errorf("failed to find snapshot: %w", err)
@@ -138,10 +139,21 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
138139
return ctrl.Result{}, retErr
139140
}
140141

142+
repository := &mpasv1alpha1.Repository{}
143+
if err := r.Get(ctx, types.NamespacedName{
144+
Namespace: obj.Namespace,
145+
Name: obj.Spec.RepositoryRef.Name,
146+
}, repository); err != nil {
147+
retErr = fmt.Errorf("failed to find repository: %w", err)
148+
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.RepositoryGetFailedReason, retErr.Error())
149+
150+
return ctrl.Result{}, retErr
151+
}
152+
141153
authSecret := &corev1.Secret{}
142154
if err := r.Get(ctx, types.NamespacedName{
143-
Namespace: obj.Spec.AuthRef.Namespace,
144-
Name: obj.Spec.AuthRef.Name,
155+
Namespace: obj.Namespace,
156+
Name: repository.Spec.Credentials.SecretRef.Name,
145157
}, authSecret); err != nil {
146158
retErr = fmt.Errorf("failed to find authentication secret: %w", err)
147159
conditions.MarkFalse(obj, meta.ReadyCondition, v1alpha1.CredentialsNotFoundReason, retErr.Error())
@@ -152,7 +164,7 @@ func (r *SyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
152164
// trim any trailing `/` and then just add.
153165
log.V(4).Info("crafting artifact URL to download from", "url", snapshot.Status.RepositoryURL)
154166
opts := &providers.PushOptions{
155-
URL: obj.Spec.URL,
167+
URL: repository.GetRepositoryURL(),
156168
Message: obj.Spec.CommitTemplate.Message,
157169
Name: obj.Spec.CommitTemplate.Name,
158170
Email: obj.Spec.CommitTemplate.Email,

0 commit comments

Comments
 (0)