diff --git a/cluster/addons.go b/cluster/addons.go index a203cf76..cd22e7bf 100644 --- a/cluster/addons.go +++ b/cluster/addons.go @@ -157,16 +157,18 @@ func (c *Cluster) deployAddonsInclude(ctx context.Context) error { log.Infof(ctx, "[addons] Adding addon from url %s", addon) logrus.Debugf("URL Yaml: %s", addonYAML) + // make sure we properly separated manifests + addonYAMLStr := string(addonYAML) + + formattedAddonYAML := formatAddonYAML(addonYAMLStr) + + addonYAML = []byte(formattedAddonYAML) + logrus.Debugf("Formatted Yaml: %s", addonYAML) + if err := validateUserAddonYAML(addonYAML); err != nil { return err } - // Put 3 dashes (---) at beginning of next YAML if it is not there already so we can append to manifests - dashes := "---\n" - if !strings.HasPrefix(string(addonYAML[:]), dashes) { - addonYAML = append([]byte(dashes), addonYAML...) - } - manifests = append(manifests, addonYAML...) } else if isFilePath(addon) { addonYAML, err := ioutil.ReadFile(addon) @@ -178,9 +180,12 @@ func (c *Cluster) deployAddonsInclude(ctx context.Context) error { // make sure we properly separated manifests addonYAMLStr := string(addonYAML) - if !strings.HasPrefix(addonYAMLStr, "---") { - addonYAML = []byte(fmt.Sprintf("%s\n%s", "---", addonYAMLStr)) - } + + formattedAddonYAML := formatAddonYAML(addonYAMLStr) + + addonYAML = []byte(formattedAddonYAML) + logrus.Debugf("Formatted Yaml: %s", addonYAML) + if err := validateUserAddonYAML(addonYAML); err != nil { return err } @@ -195,6 +200,19 @@ func (c *Cluster) deployAddonsInclude(ctx context.Context) error { return c.doAddonDeploy(ctx, string(manifests), UserAddonsIncludeResourceName, false) } +func formatAddonYAML(addonYAMLStr string) string { + if !strings.HasPrefix(addonYAMLStr, "---") { + logrus.Debug("Yaml does not start with dashes") + addonYAMLStr = fmt.Sprintf("%s\n%s", "---", addonYAMLStr) + } + + if !strings.HasSuffix(addonYAMLStr, "\n") { + logrus.Debug("Yaml does not end with newline") + addonYAMLStr = fmt.Sprintf("%s\n", addonYAMLStr) + } + return addonYAMLStr +} + func validateUserAddonYAML(addon []byte) error { yamlContents := make(map[string]interface{})