Skip to content

Commit 3ad3639

Browse files
committed
fix: issue-5041 (operator-framework#5042)
Signed-off-by: cndoit18 <[email protected]>
1 parent 35313e8 commit 3ad3639

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

Diff for: internal/helm/release/manager.go

+11-25
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"encoding/json"
2121
"errors"
2222
"fmt"
23-
"reflect"
2423
"strings"
2524

2625
jsonpatch "gomodules.xyz/jsonpatch/v3"
@@ -138,41 +137,28 @@ func notFoundErr(err error) bool {
138137
return err != nil && strings.Contains(err.Error(), "not found")
139138
}
140139

141-
// This is caused by the different logic of loading from local and loading from secret
142-
// For example, the Raw field, which has the tag `json:"-"`, causes the Unmarshal to be lost when it into Release
143-
// We need to make them follow the JSON tag
144-
// see: https://github.com/helm/helm/blob/cf0c6fed519d48101cd69ce01a355125215ee46f/pkg/storage/driver/util.go#L81
145-
func equalJSONStruct(a, b interface{}) (bool, error) {
146-
if reflect.ValueOf(a).IsNil() || reflect.ValueOf(b).IsNil() {
147-
return apiequality.Semantic.DeepEqual(a, b), nil
148-
}
149-
150-
aBuf, bBuf := &bytes.Buffer{}, &bytes.Buffer{}
151-
err := json.NewEncoder(aBuf).Encode(a)
152-
if err != nil {
153-
return false, err
154-
}
155-
err = json.NewEncoder(bBuf).Encode(b)
156-
return aBuf.String() == bBuf.String(), err
140+
func (m manager) getCandidateRelease(namespace, name string, chart *cpb.Chart,
141+
values map[string]interface{}) (*rpb.Release, error) {
142+
upgrade := action.NewUpgrade(m.actionConfig)
143+
upgrade.Namespace = namespace
144+
upgrade.DryRun = true
145+
return upgrade.Run(name, chart, values)
157146
}
158147

159148
func (m manager) isUpgrade(deployedRelease *rpb.Release) (bool, error) {
160149
if deployedRelease == nil {
161150
return false, nil
162151
}
163152

164-
// Judging whether to skip updates
165-
skip := m.namespace == deployedRelease.Namespace
166-
skip = skip && m.releaseName == deployedRelease.Name
167-
168-
ok, err := equalJSONStruct(m.chart, deployedRelease.Chart)
153+
candidateRelease, err := m.getCandidateRelease(m.namespace, m.releaseName, m.chart, m.values)
169154
if err != nil {
170155
return false, err
171156
}
172-
skip = skip && ok
173157

174-
ok, err = equalJSONStruct(m.values, deployedRelease.Config)
175-
return !(skip && ok), err
158+
skip := apiequality.Semantic.DeepEqual(candidateRelease.Chart, deployedRelease.Chart)
159+
skip = skip && apiequality.Semantic.DeepEqual(candidateRelease.Config, deployedRelease.Config)
160+
161+
return !skip, nil
176162
}
177163

178164
func (m manager) getDeployedRelease() (*rpb.Release, error) {

Diff for: internal/helm/release/manager_test.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ package release
1717
import (
1818
"bytes"
1919
"encoding/json"
20+
"io/ioutil"
2021
"testing"
2122

2223
"github.com/stretchr/testify/assert"
24+
"helm.sh/helm/v3/pkg/action"
2325
cpb "helm.sh/helm/v3/pkg/chart"
2426
lpb "helm.sh/helm/v3/pkg/chart/loader"
27+
"helm.sh/helm/v3/pkg/chartutil"
28+
kubefake "helm.sh/helm/v3/pkg/kube/fake"
2529
rpb "helm.sh/helm/v3/pkg/release"
30+
"helm.sh/helm/v3/pkg/storage"
31+
"helm.sh/helm/v3/pkg/storage/driver"
2632
appsv1 "k8s.io/api/apps/v1"
2733
v1 "k8s.io/api/core/v1"
2834
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -271,7 +277,14 @@ func TestManagerisUpgrade(t *testing.T) {
271277
namespace: test.releaseNs,
272278
values: test.values,
273279
chart: test.chart,
280+
actionConfig: &action.Configuration{
281+
Releases: storage.Init(driver.NewMemory()),
282+
KubeClient: &kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: ioutil.Discard}},
283+
Capabilities: chartutil.DefaultCapabilities,
284+
Log: t.Logf,
285+
},
274286
}
287+
assert.Equal(t, nil, m.actionConfig.Releases.Create(test.deployedRelease))
275288
isUpgrade, err := m.isUpgrade(test.deployedRelease)
276289
assert.Equal(t, test.want, isUpgrade)
277290
assert.Equal(t, nil, err)
@@ -285,16 +298,20 @@ func newTestChart(t *testing.T, path string) *cpb.Chart {
285298
return chart
286299
}
287300

301+
// This is caused by the different logic of loading from local and loading from secret
302+
// For example, the Raw field, which has the tag `json:"-"`, causes the Unmarshal to be lost when it into Release
303+
// We need to make them follow the JSON tag
304+
// see: https://github.com/helm/helm/blob/cf0c6fed519d48101cd69ce01a355125215ee46f/pkg/storage/driver/util.go#L81
288305
func newTestRelease(chart *cpb.Chart, values map[string]interface{}, name, namespace string) *rpb.Release { // nolint: unparam
289306
release := rpb.Mock(&rpb.MockReleaseOptions{
290307
Name: name,
291308
Namespace: namespace,
292309
Version: 1,
310+
Chart: chart,
293311
})
294-
295-
buffer := &bytes.Buffer{}
296-
_ = json.NewEncoder(buffer).Encode(chart)
297-
_ = json.NewDecoder(buffer).Decode(release.Chart)
298312
release.Config = values
313+
buffer := &bytes.Buffer{}
314+
_ = json.NewEncoder(buffer).Encode(release)
315+
_ = json.NewDecoder(buffer).Decode(release)
299316
return release
300317
}

0 commit comments

Comments
 (0)