Skip to content

Commit 92f9481

Browse files
authored
fix: take into account chart version when determine the apply strategy (#232)
Signed-off-by: Luca Burgazzoli <[email protected]>
1 parent aaf689f commit 92f9481

File tree

6 files changed

+40
-28
lines changed

6 files changed

+40
-28
lines changed

api/operator/v1alpha1/zz_generated.deepcopy.go

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

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/gorilla/mux v1.8.1
99
github.com/hashicorp/go-cleanhttp v0.5.2
1010
github.com/lburgazzoli/gomega-matchers v0.1.0
11-
github.com/lburgazzoli/k8s-manifests-renderer-helm v0.1.1
11+
github.com/lburgazzoli/k8s-manifests-renderer-helm v0.1.3
1212
github.com/onsi/gomega v1.34.2
1313
github.com/operator-framework/api v0.27.0
1414
github.com/operator-framework/operator-lifecycle-manager v0.22.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhR
245245
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
246246
github.com/lburgazzoli/gomega-matchers v0.1.0 h1:jkW45zWWGz6CP9EaUCKWcWgXiogiuHEsba28BMWv3IY=
247247
github.com/lburgazzoli/gomega-matchers v0.1.0/go.mod h1:MwRD1wEQrYBrON1pBYI/bwohSd3xeqflZa9nxXeSd9Q=
248-
github.com/lburgazzoli/k8s-manifests-renderer-helm v0.1.1 h1:vFpZ2tLT56bVNCtlzfI9z/FXAiVS1SRRUyPkogFhwlI=
249-
github.com/lburgazzoli/k8s-manifests-renderer-helm v0.1.1/go.mod h1:eqarsYWia91+fzg1Nzhol5rUO3WOEy6Z1acXr+1lCMs=
248+
github.com/lburgazzoli/k8s-manifests-renderer-helm v0.1.3 h1:P3VLDIraHPjJeD6tim0ueJu8nZslE171IB3ldRGDsQM=
249+
github.com/lburgazzoli/k8s-manifests-renderer-helm v0.1.3/go.mod h1:m3GWnxJgaLSTqc6k3EZZpjdDSIE+HqyjQicjgxn4xAo=
250250
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
251251
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
252252
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=

internal/controller/operator/instance/dapr_instance_controller_action_apply_crds.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func (a *ApplyCRDsAction) Run(ctx context.Context, rc *ReconciliationRequest) er
5151
return fmt.Errorf("cannot load CRDs: %w", err)
5252
}
5353

54+
invalidate := false
55+
force := rc.Resource.Generation != rc.Resource.Status.ObservedGeneration || !helm.IsSameChart(c, rc.Resource.Status.Chart)
56+
5457
for _, crd := range crds {
5558
resources.Labels(&crd, map[string]string{
5659
helm.ReleaseGeneration: strconv.FormatInt(rc.Resource.Generation, 10),
@@ -59,14 +62,20 @@ func (a *ApplyCRDsAction) Run(ctx context.Context, rc *ReconciliationRequest) er
5962
helm.ReleaseVersion: c.Version(),
6063
})
6164

62-
err = a.apply(ctx, rc, &crd)
65+
applied, err := a.apply(ctx, rc, &crd, force)
6366
if err != nil {
6467
return err
6568
}
69+
70+
if applied {
71+
invalidate = true
72+
}
6673
}
6774

68-
// invalidate the client so it gets aware of the new CRDs
69-
rc.Client.Invalidate()
75+
if invalidate {
76+
// invalidate the client so it gets aware of the new CRDs
77+
rc.Client.Invalidate()
78+
}
7079

7180
return nil
7281
}
@@ -75,32 +84,28 @@ func (a *ApplyCRDsAction) Cleanup(_ context.Context, _ *ReconciliationRequest) e
7584
return nil
7685
}
7786

78-
func (a *ApplyCRDsAction) apply(ctx context.Context, rc *ReconciliationRequest, crd *unstructured.Unstructured) error {
87+
func (a *ApplyCRDsAction) apply(ctx context.Context, rc *ReconciliationRequest, crd *unstructured.Unstructured, apply bool) (bool, error) {
7988
dc, err := rc.Client.Dynamic(rc.Resource.Namespace, crd)
8089
if err != nil {
81-
return fmt.Errorf("cannot create dynamic client: %w", err)
90+
return false, fmt.Errorf("cannot create dynamic client: %w", err)
8291
}
8392

84-
apply := rc.Resource.Generation != rc.Resource.Status.ObservedGeneration
85-
8693
_, err = dc.Get(ctx, crd.GetName(), metav1.GetOptions{})
8794
if err != nil && !k8serrors.IsNotFound(err) {
88-
return fmt.Errorf("cannot determine if CRD %s exists: %w", resources.Ref(crd), err)
95+
return false, fmt.Errorf("cannot determine if CRD %s exists: %w", resources.Ref(crd), err)
8996
}
9097

91-
if err != nil && k8serrors.IsNotFound(err) {
98+
if k8serrors.IsNotFound(err) {
9299
apply = true
93100
}
94101

95102
if !apply {
96103
a.l.Info("run",
97104
"apply", "false",
98105
"gen", rc.Resource.Generation,
99-
"ref", resources.Ref(crd),
100-
"generation-changed", rc.Resource.Generation != rc.Resource.Status.ObservedGeneration,
101-
"not-found", k8serrors.IsNotFound(err))
106+
"ref", resources.Ref(crd))
102107

103-
return nil
108+
return false, nil
104109
}
105110

106111
_, err = dc.Apply(ctx, crd.GetName(), crd, metav1.ApplyOptions{
@@ -109,13 +114,13 @@ func (a *ApplyCRDsAction) apply(ctx context.Context, rc *ReconciliationRequest,
109114
})
110115

111116
if err != nil {
112-
return fmt.Errorf("cannot apply CRD %s: %w", resources.Ref(crd), err)
117+
return false, fmt.Errorf("cannot apply CRD %s: %w", resources.Ref(crd), err)
113118
}
114119

115120
a.l.Info("run",
116121
"apply", "true",
117122
"gen", rc.Resource.Generation,
118123
"ref", resources.Ref(crd))
119124

120-
return nil
125+
return true, nil
121126
}

internal/controller/operator/instance/dapr_instance_controller_action_apply_resources.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,18 @@ func (a *ApplyResourcesAction) Run(ctx context.Context, rc *ReconciliationReques
6363
return istr < jstr
6464
})
6565

66-
installedVersion := ""
67-
if rc.Resource.Status.Chart != nil {
68-
installedVersion = rc.Resource.Status.Chart.Version
69-
}
70-
71-
force := rc.Resource.Generation != rc.Resource.Status.ObservedGeneration || c.Version() != installedVersion
66+
force := rc.Resource.Generation != rc.Resource.Status.ObservedGeneration || !helm.IsSameChart(c, rc.Resource.Status.Chart)
7267

7368
if force {
7469
rc.Reconciler.Event(
7570
rc.Resource,
7671
corev1.EventTypeNormal,
7772
"RenderFullHelmTemplate",
78-
fmt.Sprintf("Render full Helm template (observedGeneration: %d, generation: %d, installedChartVersion: %s, chartVersion: %s)",
73+
fmt.Sprintf("Render full Helm template (observedGeneration: %d, generation: %d, installedChart: %v, chart: %v)",
7974
rc.Resource.Status.ObservedGeneration,
8075
rc.Resource.Generation,
81-
installedVersion,
82-
c.Version()),
76+
rc.Resource.Status.Chart,
77+
c.Spec()),
8378
)
8479
}
8580

pkg/helm/helm.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package helm
22

33
import (
4+
daprApi "github.com/dapr/kubernetes-operator/api/operator/v1alpha1"
5+
helme "github.com/lburgazzoli/k8s-manifests-renderer-helm/engine"
46
"k8s.io/apimachinery/pkg/labels"
57
"k8s.io/apimachinery/pkg/selection"
68
)
@@ -36,3 +38,13 @@ func ReleaseSelector() (labels.Selector, error) {
3638

3739
return selector, nil
3840
}
41+
42+
func IsSameChart(c *helme.Chart, meta *daprApi.ChartMeta) bool {
43+
if c == nil || meta == nil {
44+
return false
45+
}
46+
47+
return c.Name() == meta.Name &&
48+
c.Version() == meta.Version &&
49+
c.Repo() == meta.Repo
50+
}

0 commit comments

Comments
 (0)