Skip to content

Commit

Permalink
Fix setting default value updating
Browse files Browse the repository at this point in the history
Avoid updating empty value as support-bundle-image setting default value

Signed-off-by: Jian Wang <[email protected]>
(cherry picked from commit 5481e8a)
  • Loading branch information
w13915984028 authored and FrankYang0529 committed Nov 28, 2024
1 parent 97068c3 commit b6814fe
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 4 deletions.
18 changes: 18 additions & 0 deletions deploy/charts/harvester/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,21 @@ NB(thxCode): Use this value to unify the control tag and condition of KubeVirt.
{{- .Values.tags.kubevirt | toString -}}
{{- end -}}
{{- end }}

{{/*
Get Support-bundle-kit image environment for updating the default values per current release.
*/}}
{{- define "harvester.supportBundleImageEnv" -}}
{{- $result := dict -}}
{{- range $k, $v := .Values -}}
{{- if eq (toString $k) "support-bundle-kit" -}}
{{- $result = $v -}}
{{- end -}}
{{- end -}}
{{- with $result -}}
{{- with .image -}}
- name: HARVESTER_SUPPORT_BUNDLE_IMAGE_DEFAULT_VALUE
value: {{ printf "{\"repository\":\"%s\",\"tag\":\"%s\",\"imagePullPolicy\":\"%s\"}" .repository .tag .imagePullPolicy | squote }}
{{- end -}}
{{- end -}}
{{- end }}
1 change: 1 addition & 0 deletions deploy/charts/harvester/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ spec:
- name: GOCOVERDIR
value: /go-cover-dir
{{- end }}
{{ include "harvester.supportBundleImageEnv" . | indent 12 }}
- name: NAMESPACE
valueFrom:
fieldRef:
Expand Down
13 changes: 13 additions & 0 deletions pkg/controller/global/settings/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"

"github.com/sirupsen/logrus"

"github.com/rancher/steve/pkg/server"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -92,6 +94,16 @@ func (s *settingsProvider) SetAll(settingsMap map[string]settings.Setting) error
for name, setting := range settingsMap {
key := settings.GetEnvKey(name)
value := os.Getenv(key)
defaultvaluekey := settings.GetEnvDefaultValueKey(name)
defaultvalue := os.Getenv(defaultvaluekey)

// override settings from ENV first
if defaultvalue != "" && defaultvalue != setting.Default {
logrus.WithFields(logrus.Fields{
"name": name,
}).Debugf("setting default %s is replaced with %s", setting.Default, defaultvalue)
setting.Default = defaultvalue
}

obj, err := s.settings.Get(setting.Name, v1.GetOptions{})
if errors.IsNotFound(err) {
Expand All @@ -109,6 +121,7 @@ func (s *settingsProvider) SetAll(settingsMap map[string]settings.Setting) error
} else {
fallback[newSetting.Name] = newSetting.Value
}

_, err := s.settings.Create(newSetting)
if err != nil {
return err
Expand Down
34 changes: 30 additions & 4 deletions pkg/controller/master/setting/support_bundle_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ const (
)

func UpdateSupportBundleImage(settingClient v1beta1.SettingClient, settingCache v1beta1.SettingCache, app *catalogv1api.App) error {
if app.Spec.Chart == nil {
logrus.WithFields(logrus.Fields{
"namespace": app.Namespace,
"name": app.Name,
}).Warning("app has empty chart, skip to update the support-bundle-image setting")
return nil
}
// merge template and chart
values, err := chartutil.CoalesceValues(
&chart.Chart{
Expand All @@ -36,11 +43,24 @@ func UpdateSupportBundleImage(settingClient v1beta1.SettingClient, settingCache
var supportBundleYaml map[string]interface{}
v, ok := values[SupportBundleRepository]
if !ok {
logrus.Warningf("cant't find %s in apps %s/%s", SupportBundleRepository, app.Namespace, app.Name)
logrus.WithFields(logrus.Fields{
"namespace": app.Namespace,
"name": app.Name,
}).Warningf("app chart values cant't find %s, skip to update the support-bundle-image setting", SupportBundleRepository)
return nil
}
if supportBundleYaml, ok = v.(map[string]interface{}); !ok {
logrus.Warningf("unknown %s yaml struct %+v in apps %s/%s", SupportBundleRepository, v, app.Namespace, app.Name)
logrus.WithFields(logrus.Fields{
"namespace": app.Namespace,
"name": app.Name,
}).Warningf("unknown %s yaml struct %+v, skip to update the support-bundle-image setting", SupportBundleRepository, v)
return nil
}
if len(supportBundleYaml) == 0 {
logrus.WithFields(logrus.Fields{
"namespace": app.Namespace,
"name": app.Name,
}).Warning("supportBundleYaml map is empty, skip to convert to support-bundle-image setting")
return nil
}

Expand All @@ -50,16 +70,22 @@ func UpdateSupportBundleImage(settingClient v1beta1.SettingClient, settingCache
if err := mapstructure.Decode(supportBundleYaml, &supportBundle); err != nil {
return err
}

if supportBundle.Image.ImageName() == "" {
logrus.WithFields(logrus.Fields{
"namespace": app.Namespace,
"name": app.Name,
}).Warning("the converted imagename is empty, skip to update the support-bundle-image setting")
return nil
}
imageBytes, err := json.Marshal(&supportBundle.Image)
if err != nil {
return err
}

supportBundleImage, err := settingCache.Get(settings.SupportBundleImageName)
if err != nil {
return err
}

supportBundleImageCpy := supportBundleImage.DeepCopy()
supportBundleImageCpy.Default = string(imageBytes)

Expand Down
116 changes: 116 additions & 0 deletions pkg/controller/master/setting/support_bundle_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,120 @@ func Test_UpdateSupportBundleImage(t *testing.T) {

assert.Nil(t, err, "failed to get setting")
assert.Equal(t, "{\"repository\":\"test-repository\",\"tag\":\"v3.3\",\"imagePullPolicy\":\"IfNotPresent\"}", s.Default)

// image tag is empty, do not update
err = UpdateSupportBundleImage(
fakeclients.HarvesterSettingClient(clientset.HarvesterhciV1beta1().Settings),
fakeclients.HarvesterSettingCache(clientset.HarvesterhciV1beta1().Settings),
&catalogv1.App{
ObjectMeta: metav1.ObjectMeta{
Name: "test.name",
Namespace: namespace,
},
Spec: catalogv1.ReleaseSpec{
Chart: &catalogv1.Chart{
Values: map[string]interface{}{
"support-bundle-kit": map[string]interface{}{
"image": map[string]interface{}{
"repository": "",
"tag": "",
"imagePullPolicy": "IfNotPresent",
},
},
},
},
},
},
)
assert.Nil(t, err, "failed to update setting")
s, err = clientset.HarvesterhciV1beta1().Settings().Get(
context.TODO(),
settings.SupportBundleImageName,
metav1.GetOptions{})

assert.Nil(t, err, "failed to get setting")
// keeps unchanged
assert.Equal(t, "{\"repository\":\"test-repository\",\"tag\":\"v3.3\",\"imagePullPolicy\":\"IfNotPresent\"}", s.Default)

// image map is empty, do not update
err = UpdateSupportBundleImage(
fakeclients.HarvesterSettingClient(clientset.HarvesterhciV1beta1().Settings),
fakeclients.HarvesterSettingCache(clientset.HarvesterhciV1beta1().Settings),
&catalogv1.App{
ObjectMeta: metav1.ObjectMeta{
Name: "test.name",
Namespace: namespace,
},
Spec: catalogv1.ReleaseSpec{
Chart: &catalogv1.Chart{
Values: map[string]interface{}{
"support-bundle-kit": map[string]interface{}{
"image": map[string]interface{}{},
},
},
},
},
},
)
assert.Nil(t, err, "failed to update setting")
s, err = clientset.HarvesterhciV1beta1().Settings().Get(
context.TODO(),
settings.SupportBundleImageName,
metav1.GetOptions{})

assert.Nil(t, err, "failed to get setting")
// keeps unchanged
assert.Equal(t, "{\"repository\":\"test-repository\",\"tag\":\"v3.3\",\"imagePullPolicy\":\"IfNotPresent\"}", s.Default)

// invalid key from app
err = UpdateSupportBundleImage(
fakeclients.HarvesterSettingClient(clientset.HarvesterhciV1beta1().Settings),
fakeclients.HarvesterSettingCache(clientset.HarvesterhciV1beta1().Settings),
&catalogv1.App{
ObjectMeta: metav1.ObjectMeta{
Name: "test.name",
Namespace: namespace,
},
Spec: catalogv1.ReleaseSpec{
Chart: &catalogv1.Chart{
Values: map[string]interface{}{
"support-bundle-kit-error-name": map[string]interface{}{
"image": map[string]interface{}{},
},
},
},
},
},
)
assert.Nil(t, err, "failed to update setting")
s, err = clientset.HarvesterhciV1beta1().Settings().Get(
context.TODO(),
settings.SupportBundleImageName,
metav1.GetOptions{})

assert.Nil(t, err, "failed to get setting")
// keeps unchanged
assert.Equal(t, "{\"repository\":\"test-repository\",\"tag\":\"v3.3\",\"imagePullPolicy\":\"IfNotPresent\"}", s.Default)

// empty chart from app
err = UpdateSupportBundleImage(
fakeclients.HarvesterSettingClient(clientset.HarvesterhciV1beta1().Settings),
fakeclients.HarvesterSettingCache(clientset.HarvesterhciV1beta1().Settings),
&catalogv1.App{
ObjectMeta: metav1.ObjectMeta{
Name: "test.name",
Namespace: namespace,
},
Spec: catalogv1.ReleaseSpec{},
},
)
assert.Nil(t, err, "failed to update setting")
s, err = clientset.HarvesterhciV1beta1().Settings().Get(
context.TODO(),
settings.SupportBundleImageName,
metav1.GetOptions{})

assert.Nil(t, err, "failed to get setting")
// keeps unchanged
assert.Equal(t, "{\"repository\":\"test-repository\",\"tag\":\"v3.3\",\"imagePullPolicy\":\"IfNotPresent\"}", s.Default)
}
4 changes: 4 additions & 0 deletions pkg/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ func GetEnvKey(key string) string {
return "HARVESTER_" + strings.ToUpper(strings.Replace(key, "-", "_", -1))
}

func GetEnvDefaultValueKey(key string) string {
return "HARVESTER_" + strings.ToUpper(strings.Replace(key, "-", "_", -1)) + "_DEFAULT_VALUE"
}

func IsRelease() bool {
return !strings.Contains(ServerVersion.Get(), "head") && releasePattern.MatchString(ServerVersion.Get())
}
Expand Down

0 comments on commit b6814fe

Please sign in to comment.