Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 21 additions & 1 deletion internal/controller/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type KustomizationReconciler struct {
FailFast bool
GroupChangeLog bool
StrictSubstitutions bool
EnableRevisionAnnotation bool
}

func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, retErr error) {
Expand Down Expand Up @@ -812,7 +813,26 @@ func (r *KustomizationReconciler) apply(ctx context.Context,
return false, nil, err
}

if cmeta := obj.Spec.CommonMetadata; cmeta != nil {
var revisionAnnotations map[string]string
if r.EnableRevisionAnnotation {
if obj.Status.LastAppliedRevision == "" || revision != obj.Status.LastAppliedRevision {
revisionAnnotations = map[string]string{
fmt.Sprintf("%s/source-revision", eventv1.Group): revision,
}
}
if originRevision != "" && originRevision != obj.Status.LastAppliedOriginRevision {
revisionAnnotations[fmt.Sprintf("%s/source-revision", eventv1.Group)] = originRevision
}
}

cmeta := obj.Spec.CommonMetadata
if cmeta != nil {
if cmeta.Annotations == nil {
cmeta.Annotations = make(map[string]string)
}
for k, v := range revisionAnnotations {
cmeta.Annotations[k] = v
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we annotating all resources? A Kubernetes deployment will never send an event to NC. This will cause drift for potentially thousands of non-Flux resources, which negates one of the core Flux design principles, we never alter the cluster state, unless a change is made in Git; in this case, no change was made in Git, you just blindly mutate all objects.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry.. this was just a raw implementation. Of course, there is no need to add the annotation in non-FluxCD resources. In any case and as far as I understood, it wouldn't fly due to mutating the objects at applying, been a drift between the source and what's applied and therefore, as you said, altering the cluster state.

I thought that to properly populate the revision over the resources controlled by a Kustomization, it should get spread over from that, because it does have full-control about what's going get applied in the cluster in the end.

Do you have any suggestion about how to tackle this? I think it's a valid use case, especially when you rely on having most of your FluxCD resources in a source/several sources and reconciling from them.

}
ssautil.SetCommonMetadata(objects, cmeta.Labels, cmeta.Annotations)
}

Expand Down
7 changes: 7 additions & 0 deletions internal/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ const (

// ExternalArtifact controls whether the ExternalArtifact source type is enabled.
ExternalArtifact = "ExternalArtifact"

// EnableRevisionAnnotation controls whether source revision annotations
// should be enabled on applied resources.
EnableRevisionAnnotation = "EnableRevisionAnnotation"
)

var features = map[string]bool{
Expand All @@ -83,6 +87,9 @@ var features = map[string]bool{
// ExternalArtifact
// opt-in from v1.7
ExternalArtifact: false,
// EnableRevisionAnnotation
// opt-in from v1.8
EnableRevisionAnnotation: true,
}

func init() {
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ func main() {
os.Exit(1)
}

enableRevisionAnnotation, err := features.Enabled(features.EnableRevisionAnnotation)
if err != nil {
setupLog.Error(err, "unable to check feature gate "+features.EnableRevisionAnnotation)
os.Exit(1)
}

var tokenCache *pkgcache.TokenCache
if tokenCacheOptions.MaxSize > 0 {
var err error
Expand Down Expand Up @@ -330,6 +336,7 @@ func main() {
StatusManager: fmt.Sprintf("gotk-%s", controllerName),
StrictSubstitutions: strictSubstitutions,
TokenCache: tokenCache,
EnableRevisionAnnotation: enableRevisionAnnotation,
}).SetupWithManager(ctx, mgr, controller.KustomizationReconcilerOptions{
RateLimiter: runtimeCtrl.GetRateLimiter(rateLimiterOptions),
WatchConfigsPredicate: watchConfigsPredicate,
Expand Down