Skip to content

Commit f96097d

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

File tree

2 files changed

+73
-61
lines changed

2 files changed

+73
-61
lines changed

internal/helm/release/manager.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,29 @@ func equalJSONStruct(a, b interface{}) (bool, error) {
156156
return aBuf.String() == bBuf.String(), err
157157
}
158158

159+
func (m manager) getCandidateRelease(namespace, name string, chart *cpb.Chart,
160+
values map[string]interface{}) (*rpb.Release, error) {
161+
upgrade := action.NewUpgrade(m.actionConfig)
162+
upgrade.Namespace = namespace
163+
upgrade.DryRun = true
164+
return upgrade.Run(name, chart, values)
165+
}
166+
159167
func (m manager) isUpgrade(deployedRelease *rpb.Release) (bool, error) {
160168
if deployedRelease == nil {
161169
return false, nil
162170
}
163171

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)
172+
candidateRelease, err := m.getCandidateRelease(m.namespace, m.releaseName, m.chart, m.values)
169173
if err != nil {
170174
return false, err
171175
}
172-
skip = skip && ok
173176

174-
ok, err = equalJSONStruct(m.values, deployedRelease.Config)
177+
skip, err := equalJSONStruct(candidateRelease.Chart, deployedRelease.Chart)
178+
if err != nil {
179+
return false, err
180+
}
181+
ok, err := equalJSONStruct(candidateRelease.Config, deployedRelease.Config)
175182
return !(skip && ok), err
176183
}
177184

internal/helm/release/manager_test.go

+59-54
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
package release
1616

1717
import (
18-
"bytes"
19-
"encoding/json"
18+
"io/ioutil"
2019
"testing"
2120

2221
"github.com/stretchr/testify/assert"
22+
"helm.sh/helm/v3/pkg/action"
2323
cpb "helm.sh/helm/v3/pkg/chart"
2424
lpb "helm.sh/helm/v3/pkg/chart/loader"
25-
rpb "helm.sh/helm/v3/pkg/release"
25+
"helm.sh/helm/v3/pkg/chartutil"
26+
kubefake "helm.sh/helm/v3/pkg/kube/fake"
27+
"helm.sh/helm/v3/pkg/storage"
28+
"helm.sh/helm/v3/pkg/storage/driver"
2629
appsv1 "k8s.io/api/apps/v1"
2730
v1 "k8s.io/api/core/v1"
2831
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -219,49 +222,54 @@ func TestManagerGenerateStrategicMergePatch(t *testing.T) {
219222

220223
func TestManagerisUpgrade(t *testing.T) {
221224
tests := []struct {
222-
name string
223-
releaseName string
224-
releaseNs string
225-
values map[string]interface{}
226-
chart *cpb.Chart
227-
deployedRelease *rpb.Release
228-
want bool
225+
name string
226+
releaseName string
227+
releaseNs string
228+
deployValues map[string]interface{}
229+
values map[string]interface{}
230+
deployChart *cpb.Chart
231+
chart *cpb.Chart
232+
want bool
229233
}{
230234
{
231-
name: "ok",
232-
releaseName: "deployed",
233-
releaseNs: "deployed-ns",
234-
values: map[string]interface{}{"key": "value"},
235-
chart: newTestChart(t, "./testdata/simple"),
236-
deployedRelease: newTestRelease(newTestChart(t, "./testdata/simple"), map[string]interface{}{"key": "value"}, "deployed", "deployed-ns"),
237-
want: false,
235+
name: "ok",
236+
releaseName: "deployed",
237+
releaseNs: "deployed-ns",
238+
values: map[string]interface{}{"key": "value"},
239+
deployValues: map[string]interface{}{"key": "value"},
240+
chart: newTestChart(t, "./testdata/simple"),
241+
deployChart: newTestChart(t, "./testdata/simple"),
242+
want: false,
238243
},
239244
{
240-
name: "different chart",
241-
releaseName: "deployed",
242-
releaseNs: "deployed-ns",
243-
values: map[string]interface{}{"key": "value"},
244-
chart: newTestChart(t, "./testdata/simple"),
245-
deployedRelease: newTestRelease(newTestChart(t, "./testdata/simpledf"), map[string]interface{}{"key": "value"}, "deployed", "deployed-ns"),
246-
want: true,
245+
name: "different chart",
246+
releaseName: "deployed",
247+
releaseNs: "deployed-ns",
248+
values: map[string]interface{}{"key": "value"},
249+
deployValues: map[string]interface{}{"key": "value"},
250+
chart: newTestChart(t, "./testdata/simple"),
251+
deployChart: newTestChart(t, "./testdata/simpledf"),
252+
want: true,
247253
},
248254
{
249-
name: "different values",
250-
releaseName: "deployed",
251-
releaseNs: "deployed-ns",
252-
values: map[string]interface{}{"key": "1", "int": int32(1)},
253-
chart: newTestChart(t, "./testdata/simple"),
254-
deployedRelease: newTestRelease(newTestChart(t, "./testdata/simple"), map[string]interface{}{"key": "", "int": int64(1)}, "deployed", "deployed-ns"),
255-
want: true,
255+
name: "different values",
256+
releaseName: "deployed",
257+
releaseNs: "deployed-ns",
258+
deployValues: map[string]interface{}{"key": "1"},
259+
values: map[string]interface{}{"key": "1", "int": int32(1)},
260+
chart: newTestChart(t, "./testdata/simple"),
261+
deployChart: newTestChart(t, "./testdata/simple"),
262+
want: true,
256263
},
257264
{
258-
name: "nil values",
259-
releaseName: "deployed",
260-
releaseNs: "deployed-ns",
261-
values: nil,
262-
chart: newTestChart(t, "./testdata/simple"),
263-
deployedRelease: newTestRelease(newTestChart(t, "./testdata/simple"), map[string]interface{}{}, "deployed", "deployed-ns"),
264-
want: false,
265+
name: "nil values",
266+
releaseName: "deployed",
267+
releaseNs: "deployed-ns",
268+
deployValues: map[string]interface{}{},
269+
values: nil,
270+
chart: newTestChart(t, "./testdata/simple"),
271+
deployChart: newTestChart(t, "./testdata/simple"),
272+
want: false,
265273
},
266274
}
267275
for _, test := range tests {
@@ -271,10 +279,21 @@ func TestManagerisUpgrade(t *testing.T) {
271279
namespace: test.releaseNs,
272280
values: test.values,
273281
chart: test.chart,
282+
actionConfig: &action.Configuration{
283+
Releases: storage.Init(driver.NewMemory()),
284+
KubeClient: &kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: ioutil.Discard}},
285+
Capabilities: chartutil.DefaultCapabilities,
286+
Log: t.Logf,
287+
},
274288
}
275-
isUpgrade, err := m.isUpgrade(test.deployedRelease)
289+
install := action.NewInstall(m.actionConfig)
290+
install.Namespace = test.releaseNs
291+
install.ReleaseName = test.releaseName
292+
deployedRelease, err := install.Run(test.deployChart, test.deployValues)
293+
assert.Nil(t, err)
294+
isUpgrade, err := m.isUpgrade(deployedRelease)
276295
assert.Equal(t, test.want, isUpgrade)
277-
assert.Equal(t, nil, err)
296+
assert.Nil(t, err)
278297
})
279298
}
280299
}
@@ -284,17 +303,3 @@ func newTestChart(t *testing.T, path string) *cpb.Chart {
284303
assert.Nil(t, err)
285304
return chart
286305
}
287-
288-
func newTestRelease(chart *cpb.Chart, values map[string]interface{}, name, namespace string) *rpb.Release { // nolint: unparam
289-
release := rpb.Mock(&rpb.MockReleaseOptions{
290-
Name: name,
291-
Namespace: namespace,
292-
Version: 1,
293-
})
294-
295-
buffer := &bytes.Buffer{}
296-
_ = json.NewEncoder(buffer).Encode(chart)
297-
_ = json.NewDecoder(buffer).Decode(release.Chart)
298-
release.Config = values
299-
return release
300-
}

0 commit comments

Comments
 (0)