Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add labels and annotations to rollouts api #3832

Merged
merged 3 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions rollouts/api/v1alpha1/remoterootsync_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type RemoteRootSyncSpec struct {

ClusterRef ClusterRef `json:"clusterRef,omitempty"`
Template *RootSyncInfo `json:"template,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
}

type RootSyncInfo struct {
Expand All @@ -54,6 +55,12 @@ type GitInfo struct {
NoSSLVerify bool `json:"noSSLVerify,omitempty"`
}

// Metadata specifies labels and annotations to add to the RSync object.
type Metadata struct {
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
}

// RemoteRootSyncStatus defines the observed state of RemoteRootSync
type RemoteRootSyncStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Expand Down
11 changes: 7 additions & 4 deletions rollouts/api/v1alpha1/rollout_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,16 @@ type SyncTemplate struct {

// RootSyncTemplate represent the sync template for RootSync.
type RootSyncTemplate struct {
SourceFormat string `json:"sourceFormat,omitempty"`
Git *GitInfo `json:"git,omitempty"`
SourceFormat string `json:"sourceFormat,omitempty"`
Git *GitInfo `json:"git,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
}

// RepoSyncTemplate represent the sync template for RepoSync.
type RepoSyncTemplate struct {
SourceFormat string `json:"sourceFormat,omitempty"`
Git *GitInfo `json:"git,omitempty"`
SourceFormat string `json:"sourceFormat,omitempty"`
Git *GitInfo `json:"git,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
}

// +kubebuilder:validation:Enum=AllClusters;Custom
Expand Down
44 changes: 44 additions & 0 deletions rollouts/api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions rollouts/config/crd/bases/gitops.kpt.dev_remoterootsyncs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ spec:
required:
- name
type: object
metadata:
description: Metadata specifies labels and annotations to add to the
RSync object.
properties:
annotations:
additionalProperties:
type: string
type: object
labels:
additionalProperties:
type: string
type: object
type: object
template:
properties:
spec:
Expand Down
28 changes: 28 additions & 0 deletions rollouts/config/crd/bases/gitops.kpt.dev_rollouts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ spec:
RSync object used to syncing the packages.
properties:
repoSync:
description: RepoSyncTemplate represent the sync template for
RepoSync.
properties:
git:
properties:
Expand Down Expand Up @@ -217,6 +219,19 @@ spec:
- auth
- repo
type: object
metadata:
description: Metadata specifies labels and annotations to
add to the RSync object.
properties:
annotations:
additionalProperties:
type: string
type: object
labels:
additionalProperties:
type: string
type: object
type: object
sourceFormat:
type: string
type: object
Expand Down Expand Up @@ -256,6 +271,19 @@ spec:
- auth
- repo
type: object
metadata:
description: Metadata specifies labels and annotations to
add to the RSync object.
properties:
annotations:
additionalProperties:
type: string
type: object
labels:
additionalProperties:
type: string
type: object
type: object
sourceFormat:
type: string
type: object
Expand Down
18 changes: 11 additions & 7 deletions rollouts/controllers/remoterootsync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,23 @@ func (r *RemoteRootSyncReconciler) pruneWatches(rrsnn types.NamespacedName, clus
func BuildObjectsToApply(remoterootsync *gitopsv1alpha1.RemoteRootSync) (*unstructured.Unstructured, error) {
newRootSync, err := runtime.DefaultUnstructuredConverter.ToUnstructured(remoterootsync.Spec.Template)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to convert to unstructured type: %w", err)
}

u := unstructured.Unstructured{Object: newRootSync}
u.SetGroupVersionKind(rootSyncGVK)
u.SetName(remoterootsync.Name)
u.SetNamespace(rootSyncNamespace)
u.SetLabels(map[string]string{
remoteRootSyncNameLabel: remoterootsync.Name,
remoteRootSyncNamespaceLabel: remoterootsync.Namespace,
})
if err != nil {
return nil, fmt.Errorf("failed to convert to unstructured type: %w", err)

labels := make(map[string]string)
if remoterootsync.Spec.Metadata != nil {
u.SetAnnotations(remoterootsync.Spec.Metadata.Annotations)
labels = remoterootsync.Spec.Metadata.Labels
}
labels[remoteRootSyncNameLabel] = remoterootsync.Name
labels[remoteRootSyncNamespaceLabel] = remoterootsync.Namespace
u.SetLabels(labels)

return &u, nil
}

Expand Down
15 changes: 14 additions & 1 deletion rollouts/controllers/rollout_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,18 @@ func (r *RolloutReconciler) rolloutTargets(ctx context.Context, rollout *gitopsv

for _, target := range targets.ToBeCreated {
rootSyncSpec := toRootSyncSpec(target.packageRef)
var metadata *gitopsv1alpha1.Metadata

if rollout != nil && rollout.Spec.SyncTemplate != nil &&
rollout.Spec.SyncTemplate.RootSync != nil {
metadata = rollout.Spec.SyncTemplate.RootSync.Metadata
}
rrs := newRemoteRootSync(rollout,
gitopsv1alpha1.ClusterRef{Name: target.cluster.Name},
rootSyncSpec,
pkgID(target.packageRef),
wave.Name,
metadata,
)

if maxConcurrent > concurrentUpdates {
Expand Down Expand Up @@ -685,7 +692,12 @@ func isRRSErrored(rss *gitopsv1alpha1.RemoteRootSync) bool {
}

// Given a package identifier and cluster, create a RemoteRootSync object.
func newRemoteRootSync(rollout *gitopsv1alpha1.Rollout, clusterRef gitopsv1alpha1.ClusterRef, rssSpec *gitopsv1alpha1.RootSyncSpec, pkgID string, waveName string) *gitopsv1alpha1.RemoteRootSync {
func newRemoteRootSync(rollout *gitopsv1alpha1.Rollout,
clusterRef gitopsv1alpha1.ClusterRef,
rssSpec *gitopsv1alpha1.RootSyncSpec,
pkgID string,
waveName string,
metadata *gitopsv1alpha1.Metadata) *gitopsv1alpha1.RemoteRootSync {
t := true
clusterName := clusterRef.Name[strings.LastIndex(clusterRef.Name, "/")+1:]

Expand All @@ -711,6 +723,7 @@ func newRemoteRootSync(rollout *gitopsv1alpha1.Rollout, clusterRef gitopsv1alpha
Template: &gitopsv1alpha1.RootSyncInfo{
Spec: rssSpec,
},
Metadata: metadata,
},
}
}
Expand Down