Skip to content

Commit

Permalink
Request reconcile using patch instead of update
Browse files Browse the repository at this point in the history
This should prevent the generation of the object getting bumped, as
observed on a GKE K8s 1.18 cluster using the  logic before this commit.

We only want to generation to increase when there are actual changes to
the `spec` of a resource, as some controllers use the `generation`
value to make assumptions about what they should do during a
reconciliation.

Signed-off-by: Hidde Beydals <hello@hidde.co>
  • Loading branch information
hiddeco committed Jul 9, 2021
1 parent fd36482 commit fe0dda8
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd/flux/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (a alertAdapter) asClientObject() client.Object {
return a.Alert
}

func (a alertAdapter) deepCopyClientObject() client.Object {
return a.Alert.DeepCopy()
}

// notificationv1.Alert

type alertListAdapter struct {
Expand Down
4 changes: 4 additions & 0 deletions cmd/flux/alert_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (a alertProviderAdapter) asClientObject() client.Object {
return a.Provider
}

func (a alertProviderAdapter) deepCopyClientObject() client.Object {
return a.Provider.DeepCopy()
}

// notificationv1.Provider

type alertProviderListAdapter struct {
Expand Down
4 changes: 4 additions & 0 deletions cmd/flux/helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (h helmReleaseAdapter) asClientObject() client.Object {
return h.HelmRelease
}

func (h helmReleaseAdapter) deepCopyClientObject() client.Object {
return h.HelmRelease.DeepCopy()
}

// helmv2.HelmReleaseList

type helmReleaseListAdapter struct {
Expand Down
8 changes: 8 additions & 0 deletions cmd/flux/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (a imageRepositoryAdapter) asClientObject() client.Object {
return a.ImageRepository
}

func (a imageRepositoryAdapter) deepCopyClientObject() client.Object {
return a.ImageRepository.DeepCopy()
}

// imagev1.ImageRepositoryList

type imageRepositoryListAdapter struct {
Expand Down Expand Up @@ -100,6 +104,10 @@ func (a imageUpdateAutomationAdapter) asClientObject() client.Object {
return a.ImageUpdateAutomation
}

func (a imageUpdateAutomationAdapter) deepCopyClientObject() client.Object {
return a.ImageUpdateAutomation.DeepCopy()
}

// autov1.ImageUpdateAutomationList

type imageUpdateAutomationListAdapter struct {
Expand Down
4 changes: 4 additions & 0 deletions cmd/flux/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (a kustomizationAdapter) asClientObject() client.Object {
return a.Kustomization
}

func (a kustomizationAdapter) deepCopyClientObject() client.Object {
return a.Kustomization.DeepCopy()
}

// kustomizev1.KustomizationList

type kustomizationListAdapter struct {
Expand Down
7 changes: 7 additions & 0 deletions cmd/flux/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ type adapter interface {
asClientObject() client.Object
}

// copyable is an interface for a wrapper or alias from which we can
// get a deep copied client.Object, required when you e.g. want to
// calculate a patch.
type copyable interface {
deepCopyClientObject() client.Object
}

// listAdapater is the analogue to adapter, but for lists; the
// controller runtime distinguishes between methods dealing with
// objects and lists.
Expand Down
4 changes: 4 additions & 0 deletions cmd/flux/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (a receiverAdapter) asClientObject() client.Object {
return a.Receiver
}

func (a receiverAdapter) deepCopyClientObject() client.Object {
return a.Receiver.DeepCopy()
}

// notificationv1.Receiver

type receiverListAdapter struct {
Expand Down
4 changes: 3 additions & 1 deletion cmd/flux/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type reconcileCommand struct {

type reconcilable interface {
adapter // to be able to load from the cluster
copyable // to be able to calculate patches
suspendable // to tell if it's suspended

// these are implemented by anything embedding metav1.ObjectMeta
Expand Down Expand Up @@ -142,6 +143,7 @@ func requestReconciliation(ctx context.Context, kubeClient client.Client,
if err := kubeClient.Get(ctx, namespacedName, obj.asClientObject()); err != nil {
return err
}
patch := client.MergeFrom(obj.deepCopyClientObject())
if ann := obj.GetAnnotations(); ann == nil {
obj.SetAnnotations(map[string]string{
meta.ReconcileRequestAnnotation: time.Now().Format(time.RFC3339Nano),
Expand All @@ -150,7 +152,7 @@ func requestReconciliation(ctx context.Context, kubeClient client.Client,
ann[meta.ReconcileRequestAnnotation] = time.Now().Format(time.RFC3339Nano)
obj.SetAnnotations(ann)
}
return kubeClient.Update(ctx, obj.asClientObject())
return kubeClient.Patch(ctx, obj.asClientObject(), patch)
})
}

Expand Down
16 changes: 16 additions & 0 deletions cmd/flux/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func (a bucketAdapter) asClientObject() client.Object {
return a.Bucket
}

func (a bucketAdapter) deepCopyClientObject() client.Object {
return a.Bucket.DeepCopy()
}

// sourcev1.BucketList

type bucketListAdapter struct {
Expand Down Expand Up @@ -70,6 +74,10 @@ func (a helmChartAdapter) asClientObject() client.Object {
return a.HelmChart
}

func (a helmChartAdapter) deepCopyClientObject() client.Object {
return a.HelmChart.DeepCopy()
}

// sourcev1.HelmChartList

type helmChartListAdapter struct {
Expand Down Expand Up @@ -99,6 +107,10 @@ func (a gitRepositoryAdapter) asClientObject() client.Object {
return a.GitRepository
}

func (a gitRepositoryAdapter) deepCopyClientObject() client.Object {
return a.GitRepository.DeepCopy()
}

// sourcev1.GitRepositoryList

type gitRepositoryListAdapter struct {
Expand Down Expand Up @@ -128,6 +140,10 @@ func (a helmRepositoryAdapter) asClientObject() client.Object {
return a.HelmRepository
}

func (a helmRepositoryAdapter) deepCopyClientObject() client.Object {
return a.HelmRepository.DeepCopy()
}

// sourcev1.HelmRepositoryList

type helmRepositoryListAdapter struct {
Expand Down

0 comments on commit fe0dda8

Please sign in to comment.