1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| func newTemplateCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { var validate bool var includeCrds bool var skipTests bool client := action.NewInstall(cfg) valueOpts := &values.Options{} var kubeVersion string var extraAPIs []string var showFiles []string
cmd := &cobra.Command{ Use: "template [NAME] [CHART]", Short: "locally render templates", Long: templateDesc, Args: require.MinimumNArgs(1), ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return compInstall(args, toComplete, client) }, RunE: func(_ *cobra.Command, args []string) error { if kubeVersion != "" { parsedKubeVersion, err := chartutil.ParseKubeVersion(kubeVersion) if err != nil { return fmt.Errorf("invalid kube version '%s': %s", kubeVersion, err) } client.KubeVersion = parsedKubeVersion }
client.DryRun = true client.ReleaseName = "RELEASE-NAME" client.Replace = true client.ClientOnly = !validate client.APIVersions = chartutil.VersionSet(extraAPIs) client.IncludeCRDs = includeCrds rel, err := runInstall(args, client, valueOpts, out)
if err != nil && !settings.Debug { if rel != nil { return fmt.Errorf("%w\n\nUse --debug flag to render out invalid YAML", err) } return err }
if rel != nil { var manifests bytes.Buffer fmt.Fprintln(&manifests, strings.TrimSpace(rel.Manifest)) if !client.DisableHooks { fileWritten := make(map[string]bool) for _, m := range rel.Hooks { if skipTests && isTestHook(m) { continue } if client.OutputDir == "" { fmt.Fprintf(&manifests, "---\n# Source: %s\n%s\n", m.Path, m.Manifest) } else { newDir := client.OutputDir if client.UseReleaseName { newDir = filepath.Join(client.OutputDir, client.ReleaseName) } err = writeToFile(newDir, m.Path, m.Manifest, fileWritten[m.Path]) if err != nil { return err } fileWritten[m.Path] = true }
} }
if len(showFiles) > 0 { splitManifests := releaseutil.SplitManifests(manifests.String()) manifestsKeys := make([]string, 0, len(splitManifests)) for k := range splitManifests { manifestsKeys = append(manifestsKeys, k) } sort.Sort(releaseutil.BySplitManifestsOrder(manifestsKeys))
manifestNameRegex := regexp.MustCompile("# Source: [^/]+/(.+)") var manifestsToRender []string for _, f := range showFiles { missing := true f = filepath.ToSlash(f) for _, manifestKey := range manifestsKeys { manifest := splitManifests[manifestKey] submatch := manifestNameRegex.FindStringSubmatch(manifest) if len(submatch) == 0 { continue } manifestName := submatch[1] manifestPathSplit := strings.Split(manifestName, "/") manifestPath := strings.Join(manifestPathSplit, "/")
if matched, _ := filepath.Match(f, manifestPath); !matched { continue } manifestsToRender = append(manifestsToRender, manifest) missing = false } if missing { return fmt.Errorf("could not find template %s in chart", f) } } for _, m := range manifestsToRender { fmt.Fprintf(out, "---\n%s\n", m) } } else { fmt.Fprintf(out, "%s", manifests.String()) } }
return err }, }
f := cmd.Flags() addInstallFlags(cmd, f, client, valueOpts) f.StringArrayVarP(&showFiles, "show-only", "s", []string{}, "only show manifests rendered from the given templates") f.StringVar(&client.OutputDir, "output-dir", "", "writes the executed templates to files in output-dir instead of stdout") f.BoolVar(&validate, "validate", false, "validate your manifests against the Kubernetes cluster you are currently pointing at. This is the same validation performed on an install") f.BoolVar(&includeCrds, "include-crds", false, "include CRDs in the templated output") f.BoolVar(&skipTests, "skip-tests", false, "skip tests from templated output") f.BoolVar(&client.IsUpgrade, "is-upgrade", false, "set .Release.IsUpgrade instead of .Release.IsInstall") f.StringVar(&kubeVersion, "kube-version", "", "Kubernetes version used for Capabilities.KubeVersion") f.StringArrayVarP(&extraAPIs, "api-versions", "a", []string{}, "Kubernetes api versions used for Capabilities.APIVersions") f.BoolVar(&client.UseReleaseName, "release-name", false, "use release name in the output-dir path.") bindPostRenderFlag(cmd, &client.PostRenderer)
return cmd }
|