Skip to content

Commit 7bc1011

Browse files
authored
add labels and annotations to rollouts api (#3832)
1 parent 07e9689 commit 7bc1011

File tree

8 files changed

+201
-18
lines changed

8 files changed

+201
-18
lines changed

rollouts/api/v1alpha1/remoterootsync_types.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ type RemoteRootSyncSpec struct {
3333
}
3434

3535
type RootSyncInfo struct {
36-
Spec *RootSyncSpec `json:"spec,omitempty"`
36+
Spec *RootSyncSpec `json:"spec,omitempty"`
37+
Metadata *Metadata `json:"metadata,omitempty"`
3738
}
3839

3940
type RootSyncSpec struct {
@@ -54,6 +55,12 @@ type GitInfo struct {
5455
NoSSLVerify bool `json:"noSSLVerify,omitempty"`
5556
}
5657

58+
// Metadata specifies labels and annotations to add to the RSync object.
59+
type Metadata struct {
60+
Labels map[string]string `json:"labels,omitempty"`
61+
Annotations map[string]string `json:"annotations,omitempty"`
62+
}
63+
5764
// RemoteRootSyncStatus defines the observed state of RemoteRootSync
5865
type RemoteRootSyncStatus struct {
5966
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster

rollouts/api/v1alpha1/rollout_types.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,16 @@ type SyncTemplate struct {
128128

129129
// RootSyncTemplate represent the sync template for RootSync.
130130
type RootSyncTemplate struct {
131-
SourceFormat string `json:"sourceFormat,omitempty"`
132-
Git *GitInfo `json:"git,omitempty"`
131+
SourceFormat string `json:"sourceFormat,omitempty"`
132+
Git *GitInfo `json:"git,omitempty"`
133+
Metadata *Metadata `json:"metadata,omitempty"`
133134
}
134135

136+
// RepoSyncTemplate represent the sync template for RepoSync.
135137
type RepoSyncTemplate struct {
136-
SourceFormat string `json:"sourceFormat,omitempty"`
137-
Git *GitInfo `json:"git,omitempty"`
138+
SourceFormat string `json:"sourceFormat,omitempty"`
139+
Git *GitInfo `json:"git,omitempty"`
140+
Metadata *Metadata `json:"metadata,omitempty"`
138141
}
139142

140143
// +kubebuilder:validation:Enum=AllClusters;Custom

rollouts/api/v1alpha1/zz_generated.deepcopy.go

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

rollouts/config/crd/bases/gitops.kpt.dev_remoterootsyncs.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ spec:
6060
type: object
6161
template:
6262
properties:
63+
metadata:
64+
description: Metadata specifies labels and annotations to add
65+
to the RSync object.
66+
properties:
67+
annotations:
68+
additionalProperties:
69+
type: string
70+
type: object
71+
labels:
72+
additionalProperties:
73+
type: string
74+
type: object
75+
type: object
6376
spec:
6477
properties:
6578
git:

rollouts/config/crd/bases/gitops.kpt.dev_rollouts.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ spec:
184184
RSync object used to syncing the packages.
185185
properties:
186186
repoSync:
187+
description: RepoSyncTemplate represent the sync template for
188+
RepoSync.
187189
properties:
188190
git:
189191
properties:
@@ -217,6 +219,19 @@ spec:
217219
- auth
218220
- repo
219221
type: object
222+
metadata:
223+
description: Metadata specifies labels and annotations to
224+
add to the RSync object.
225+
properties:
226+
annotations:
227+
additionalProperties:
228+
type: string
229+
type: object
230+
labels:
231+
additionalProperties:
232+
type: string
233+
type: object
234+
type: object
220235
sourceFormat:
221236
type: string
222237
type: object
@@ -256,6 +271,19 @@ spec:
256271
- auth
257272
- repo
258273
type: object
274+
metadata:
275+
description: Metadata specifies labels and annotations to
276+
add to the RSync object.
277+
properties:
278+
annotations:
279+
additionalProperties:
280+
type: string
281+
type: object
282+
labels:
283+
additionalProperties:
284+
type: string
285+
type: object
286+
type: object
259287
sourceFormat:
260288
type: string
261289
type: object
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Adding 'configsync.gke.io/deletion-propagation-policy: Foreground' will result in
16+
# managed objects being cleaned up by Config Sync when the RootSync object created
17+
# by the rollout is deleted. As a note, this is not included in any release tag (as
18+
# of the release date of v1.14.2), and to use this, you must build Config Sync from
19+
# HEAD.
20+
21+
apiVersion: gitops.kpt.dev/v1alpha1
22+
kind: Rollout
23+
metadata:
24+
name: sample
25+
spec:
26+
description: rollout kpt samples
27+
clusters:
28+
sourceType: KCC
29+
packages:
30+
sourceType: GitHub
31+
github:
32+
selector:
33+
org: GoogleContainerTools
34+
repo: kpt-samples
35+
directory: "*"
36+
revision: main
37+
targets:
38+
selector:
39+
matchLabels:
40+
location/city: example
41+
packageToTargetMatcher:
42+
type: AllClusters
43+
strategy:
44+
type: RollingUpdate
45+
rollingUpdate:
46+
maxConcurrent: 2
47+
syncTemplate:
48+
rootSync:
49+
metadata:
50+
annotations:
51+
configsync.gke.io/deletion-propagation-policy: Foreground
52+
type: RootSync

rollouts/controllers/remoterootsync_controller.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,19 +282,22 @@ func (r *RemoteRootSyncReconciler) pruneWatches(rrsnn types.NamespacedName, clus
282282
func BuildObjectsToApply(remoterootsync *gitopsv1alpha1.RemoteRootSync) (*unstructured.Unstructured, error) {
283283
newRootSync, err := runtime.DefaultUnstructuredConverter.ToUnstructured(remoterootsync.Spec.Template)
284284
if err != nil {
285-
return nil, err
285+
return nil, fmt.Errorf("failed to convert to unstructured type: %w", err)
286286
}
287+
287288
u := unstructured.Unstructured{Object: newRootSync}
288289
u.SetGroupVersionKind(rootSyncGVK)
289290
u.SetName(remoterootsync.Name)
290291
u.SetNamespace(rootSyncNamespace)
291-
u.SetLabels(map[string]string{
292-
remoteRootSyncNameLabel: remoterootsync.Name,
293-
remoteRootSyncNamespaceLabel: remoterootsync.Namespace,
294-
})
295-
if err != nil {
296-
return nil, fmt.Errorf("failed to convert to unstructured type: %w", err)
292+
293+
labels := u.GetLabels()
294+
if labels == nil {
295+
labels = make(map[string]string)
297296
}
297+
labels[remoteRootSyncNameLabel] = remoterootsync.Name
298+
labels[remoteRootSyncNamespaceLabel] = remoterootsync.Namespace
299+
u.SetLabels(labels)
300+
298301
return &u, nil
299302
}
300303

rollouts/controllers/rollout_controller.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"flag"
2222
"fmt"
2323
"math"
24+
"reflect"
2425
"sort"
2526
"strings"
2627
"sync"
@@ -276,6 +277,14 @@ func (r *RolloutReconciler) getPackageDiscoveryClient(rolloutNamespacedName type
276277
func (r *RolloutReconciler) reconcileRollout(ctx context.Context, rollout *gitopsv1alpha1.Rollout, strategy *gitopsv1alpha1.ProgressiveRolloutStrategy, packageDiscoveryClient *packagediscovery.PackageDiscovery) error {
277278
logger := klog.FromContext(ctx)
278279

280+
if rollout != nil && rollout.Spec.SyncTemplate != nil {
281+
if rollout.Spec.SyncTemplate.Type == gitopsv1alpha1.TemplateTypeRepoSync {
282+
err := fmt.Errorf("reposync is not yet supported")
283+
logger.Error(err, "")
284+
return err
285+
}
286+
}
287+
279288
targetClusters, err := r.store.ListClusters(ctx, &rollout.Spec.Clusters, rollout.Spec.Targets.Selector)
280289
discoveredPackages, err := packageDiscoveryClient.GetPackages(ctx, rollout.Spec.Packages)
281290
if err != nil {
@@ -413,10 +422,9 @@ func (r *RolloutReconciler) computeTargets(ctx context.Context,
413422
}
414423
} else {
415424
// remoterootsync already exists
416-
if pkg.Revision != rrs.Spec.Template.Spec.Git.Revision {
417-
rrs.Spec.Template.Spec.Git.Revision = pkg.Revision
418-
// revision has been updated
419-
targets.ToBeUpdated = append(targets.ToBeUpdated, &rrs)
425+
updated, needsUpdate := pkgNeedsUpdate(rollout, rrs, pkg)
426+
if needsUpdate {
427+
targets.ToBeUpdated = append(targets.ToBeUpdated, updated)
420428
} else {
421429
targets.Unchanged = append(targets.Unchanged, &rrs)
422430
}
@@ -430,6 +438,18 @@ func (r *RolloutReconciler) computeTargets(ctx context.Context,
430438
return targets, nil
431439
}
432440

441+
func pkgNeedsUpdate(rollout *gitopsv1alpha1.Rollout, rrs gitopsv1alpha1.RemoteRootSync, pkg *packagediscovery.DiscoveredPackage) (*gitopsv1alpha1.RemoteRootSync, bool) {
442+
// TODO: We need to check other things here besides git.Revision and metadata
443+
if pkg.Revision != rrs.Spec.Template.Spec.Git.Revision ||
444+
!reflect.DeepEqual(rollout.Spec.SyncTemplate.RootSync.Metadata,
445+
rrs.Spec.Template.Metadata) {
446+
rrs.Spec.Template.Spec.Git.Revision = pkg.Revision
447+
rrs.Spec.Template.Metadata = rollout.Spec.SyncTemplate.RootSync.Metadata
448+
return &rrs, true
449+
}
450+
return nil, false
451+
}
452+
433453
func (r *RolloutReconciler) getWaveTargets(ctx context.Context, rollout *gitopsv1alpha1.Rollout, allTargets *Targets, allClusters []clusterstore.Cluster,
434454
allWaves []gitopsv1alpha1.Wave) ([]WaveTarget, error) {
435455
allWaveTargets := []WaveTarget{}
@@ -508,13 +528,20 @@ func (r *RolloutReconciler) rolloutTargets(ctx context.Context, rollout *gitopsv
508528
}
509529
}
510530

531+
var metadata *gitopsv1alpha1.Metadata
532+
if rollout != nil && rollout.Spec.SyncTemplate != nil &&
533+
rollout.Spec.SyncTemplate.RootSync != nil {
534+
metadata = rollout.Spec.SyncTemplate.RootSync.Metadata
535+
}
536+
511537
for _, target := range targets.ToBeCreated {
512538
rootSyncSpec := toRootSyncSpec(target.packageRef)
513539
rrs := newRemoteRootSync(rollout,
514540
gitopsv1alpha1.ClusterRef{Name: target.cluster.Name},
515541
rootSyncSpec,
516542
pkgID(target.packageRef),
517543
wave.Name,
544+
metadata,
518545
)
519546

520547
if maxConcurrent > concurrentUpdates {
@@ -685,7 +712,12 @@ func isRRSErrored(rss *gitopsv1alpha1.RemoteRootSync) bool {
685712
}
686713

687714
// Given a package identifier and cluster, create a RemoteRootSync object.
688-
func newRemoteRootSync(rollout *gitopsv1alpha1.Rollout, clusterRef gitopsv1alpha1.ClusterRef, rssSpec *gitopsv1alpha1.RootSyncSpec, pkgID string, waveName string) *gitopsv1alpha1.RemoteRootSync {
715+
func newRemoteRootSync(rollout *gitopsv1alpha1.Rollout,
716+
clusterRef gitopsv1alpha1.ClusterRef,
717+
rssSpec *gitopsv1alpha1.RootSyncSpec,
718+
pkgID string,
719+
waveName string,
720+
metadata *gitopsv1alpha1.Metadata) *gitopsv1alpha1.RemoteRootSync {
689721
t := true
690722
clusterName := clusterRef.Name[strings.LastIndex(clusterRef.Name, "/")+1:]
691723

@@ -709,7 +741,8 @@ func newRemoteRootSync(rollout *gitopsv1alpha1.Rollout, clusterRef gitopsv1alpha
709741
Spec: gitopsv1alpha1.RemoteRootSyncSpec{
710742
ClusterRef: clusterRef,
711743
Template: &gitopsv1alpha1.RootSyncInfo{
712-
Spec: rssSpec,
744+
Spec: rssSpec,
745+
Metadata: metadata,
713746
},
714747
},
715748
}

0 commit comments

Comments
 (0)