Skip to content

Commit

Permalink
Fixing issues called out in PR review by fleet team (#5)
Browse files Browse the repository at this point in the history
Restoring global.fleet.clusterLabels in helm data
Adding DisablePreProcess tests

Signed-off-by: Greg Sidelinger <gate@ilive4code.net>

Co-authored-by: Raj Perera <rajiteh@gmail.com>
  • Loading branch information
gregsidelinger and rajiteh committed Mar 29, 2022
1 parent 8af675f commit 25168d5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/gitrepo-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ helm:
key: values.yaml
# Override immutable resources. This could be dangerous.
force: false
# Disable go template pre-prosessing on the fleet values
# Disable go template pre-processing on the fleet values
disablePreProcess: false

# A paused bundle will not update downstream clusters but instead mark the bundle
Expand Down
20 changes: 15 additions & 5 deletions pkg/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/rancher/fleet/pkg/manifest"
"github.com/rancher/fleet/pkg/options"
"github.com/rancher/fleet/pkg/summary"
"github.com/rancher/wrangler/pkg/data"
corecontrollers "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/pkg/name"
"github.com/rancher/wrangler/pkg/yaml"
Expand Down Expand Up @@ -286,19 +287,27 @@ func addClusterLabels(opts *fleet.BundleDeploymentOptions, cluster *fleet.Cluste
clusterLabels[k] = v
}
}
if len(clusterLabels) == 0 {
return
}

newValues := map[string]interface{}{
"global": map[string]interface{}{
"fleet": map[string]interface{}{
"clusterLabels": clusterLabels,
},
},
}
if opts.Helm == nil {
opts.Helm = &fleet.HelmOptions{}
opts.Helm = &fleet.HelmOptions{
Values: &fleet.GenericMap{
Data: newValues,
},
}
return nil
}

opts.Helm = opts.Helm.DeepCopy()
if opts.Helm.Values == nil || opts.Helm.Values.Data == nil {
opts.Helm.Values = &fleet.GenericMap{
Data: map[string]interface{}{},
Data: newValues,
}
return nil
}
Expand All @@ -307,6 +316,7 @@ func addClusterLabels(opts *fleet.BundleDeploymentOptions, cluster *fleet.Cluste
return err
}

opts.Helm.Values.Data = data.MergeMaps(opts.Helm.Values.Data, newValues)
if opts.Helm.DisablePreProcess == false {
opts.Helm.Values.Data, err = processTemplateValues(opts.Helm.Values.Data, values)
if err != nil {
Expand Down
42 changes: 42 additions & 0 deletions pkg/target/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/rancher/wrangler/pkg/yaml"

"github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
)

const bundleYaml = `namespace: default
Expand Down Expand Up @@ -295,3 +296,44 @@ func TestProcessTemplateValues(t *testing.T) {
}

}

func TestAddClusterLabels(t *testing.T) {
templateContext := map[string]interface{}{}

cluster := &fleet.Cluster{}
cluster.ObjectMeta.Name = "local"
cluster.ObjectMeta.Labels = map[string]string{"name": "local"}

const bundleYaml = `namespace: default
helm:
releaseName: disablePreProcessTests
values:
clusterName: "{{ .ClusterLabels.name }}"
`
opts := &fleet.BundleDeploymentOptions{}
err := yaml.Unmarshal([]byte(bundleYaml), opts)
if err != nil {
t.Fatalf("error during yaml parsing %v", err)
}

clusterOpts := opts.DeepCopy()
clusterOpts.Helm.DisablePreProcess = false
err = addClusterLabels(clusterOpts, cluster, templateContext)
if err != nil {
t.Fatalf("error during addClusterLabels %v", err)
}
if clusterOpts.Helm.Values.Data["clusterName"] != "local" {
t.Fatal("template replacement not performed for clusterName")
}

clusterOpts = opts.DeepCopy()
clusterOpts.Helm.DisablePreProcess = true
err = addClusterLabels(opts, cluster, templateContext)
if err != nil {
t.Fatalf("error during addClusterLabels %v", err)
}
if clusterOpts.Helm.Values.Data["clusterName"] != "{{ .ClusterLabels.name }}" {
t.Fatalf("template replacement was performed for clusterName")
}

}

0 comments on commit 25168d5

Please sign in to comment.