Skip to content

Commit

Permalink
Do not process raw yaml files as templates unless in templates/ folder
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Oct 21, 2020
1 parent e9ad8c4 commit 83bb5e7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/bundle"
"github.com/rancher/fleet/pkg/manifest"
"github.com/rancher/fleet/pkg/rawyaml"
"github.com/rancher/wrangler/pkg/kv"
"helm.sh/helm/v3/pkg/chart"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -59,7 +60,11 @@ outer:
continue outer
}
}
resource.Name = "chart/templates/" + resource.Name
if strings.HasPrefix(resource.Name, "templates/") {
resource.Name = "chart/" + resource.Name
} else {
resource.Name = rawyaml.YAMLPrefix + resource.Name
}
result = append(result, resource)
}

Expand Down
12 changes: 10 additions & 2 deletions pkg/helmdeployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import (
"strings"
"time"

"helm.sh/helm/v3/pkg/storage/driver"

"github.com/rancher/fleet/modules/agent/pkg/deployer"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/kustomize"
"github.com/rancher/fleet/pkg/manifest"
"github.com/rancher/fleet/pkg/rawyaml"
"github.com/rancher/fleet/pkg/render"
"github.com/rancher/wrangler/pkg/apply"
corecontrollers "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
Expand All @@ -26,6 +25,7 @@ import (
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/storage/driver"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
Expand Down Expand Up @@ -82,6 +82,7 @@ type postRender struct {
labelPrefix string
bundleID string
manifest *manifest.Manifest
chart *chart.Chart
mapper meta.RESTMapper
opts fleet.BundleDeploymentOptions
}
Expand All @@ -106,6 +107,12 @@ func (p *postRender) Run(renderedManifests *bytes.Buffer) (modifiedManifests *by
objs = newObjs
}

yamlObjs, err := rawyaml.ToObjects(p.chart)
if err != nil {
return nil, err
}
objs = append(objs, yamlObjs...)

labels, annotations, err := apply.GetLabelsAndAnnotations(name.SafeConcatName(p.labelPrefix, p.bundleID), nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -293,6 +300,7 @@ func (h *helm) install(bundleID string, manifest *manifest.Manifest, chart *char
bundleID: bundleID,
manifest: manifest,
opts: options,
chart: chart,
}

if !h.useGlobalCfg {
Expand Down
36 changes: 36 additions & 0 deletions pkg/rawyaml/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package rawyaml

import (
"bytes"
"strings"

"github.com/rancher/wrangler/pkg/yaml"
"helm.sh/helm/v3/pkg/chart"
"k8s.io/apimachinery/pkg/runtime"
)

const (
YAMLPrefix = "chart/raw-yaml/"
inChartPrefix = "raw-yaml/"
)

func ToObjects(c *chart.Chart) (result []runtime.Object, _ error) {
for _, resource := range c.Files {
if !strings.HasPrefix(resource.Name, inChartPrefix) {
continue
}
objs, err := yaml.ToObjects(bytes.NewBuffer(resource.Data))
if err != nil {
return nil, err
}
for _, obj := range objs {
apiVersion, kind := obj.GetObjectKind().GroupVersionKind().ToAPIVersionAndKind()
if apiVersion == "" || kind == "" {
continue
}
result = append(result, obj)
}
}

return result, nil
}

0 comments on commit 83bb5e7

Please sign in to comment.