Skip to content

Commit 646f742

Browse files
authored
Merge pull request #2097 from estroz/docs/config-gen-static-cmd-docs
🌱 config-gen: command docs no longer depend on local environment
2 parents faff148 + f7b0ae3 commit 646f742

File tree

1 file changed

+42
-24
lines changed
  • pkg/cli/alpha/config-gen

1 file changed

+42
-24
lines changed

pkg/cli/alpha/config-gen/cmd.go

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package configgen
1919
import (
2020
"fmt"
2121
"io/ioutil"
22+
"log"
2223
"os"
2324
"path/filepath"
2425
"strings"
@@ -41,8 +42,7 @@ func NewCommand() *cobra.Command {
4142
legacyPlugin := os.Getenv("KUSTOMIZE_PLUGIN_CONFIG_STRING")
4243
err := yaml.Unmarshal([]byte(legacyPlugin), kp)
4344
if err != nil {
44-
fmt.Fprintln(os.Stderr, err.Error())
45-
return nil
45+
log.Fatal(err)
4646
}
4747

4848
// Eager check to make sure pkged templates are found.
@@ -51,8 +51,7 @@ func NewCommand() *cobra.Command {
5151
})
5252
if err != nil {
5353
// this shouldn't fail if it was compiled correctly
54-
fmt.Fprintln(os.Stderr, err.Error())
55-
return nil
54+
log.Fatal(err)
5655
}
5756

5857
c := framework.TemplateCommand{
@@ -248,44 +247,64 @@ transformers:
248247
image: my-org/my-project:v0.1.0
249248
EOF
250249
251-
# generate configuration from kustomize
250+
# generate configuration from kustomize > v4.0.0
252251
kustomize build --enable-alpha-plugins .
253-
`)
254252
255-
dir, dirErr := getPluginDir()
256-
pluginFile := filepath.Join(dir, "KubebuilderConfigGen")
253+
# generate configuration from kustomize <= v4.0.0
254+
kustomize build --enable_alpha_plugins .
255+
`)
257256

258257
// command for installing the plugin
259258
install := &cobra.Command{
260259
Use: "install-as-plugin",
261260
Short: "Install config-gen as a kustomize plugin",
262-
Long: strings.TrimSpace(fmt.Sprintf(`
263-
Write a script to %s for kustomize to locate as a plugin.
264-
`, pluginFile)),
265-
Example: strings.TrimSpace(`
261+
Long: fmt.Sprintf(`Write a script to %s for kustomize to locate as a plugin.
262+
This path will be written to $XDG_CONFIG_HOME if set, otherwise $HOME.
263+
`, pluginScriptPath),
264+
Example: `
266265
kubebuilder alpha config-gen install-as-plugin
267-
`),
266+
`,
268267
RunE: func(cmd *cobra.Command, args []string) error {
269-
if dirErr != nil {
270-
return dirErr
271-
}
272-
fmt.Fprintf(cmd.OutOrStdout(), "writing kustomize plugin file at %s\n", pluginFile)
273-
err = os.MkdirAll(dir, 0700)
268+
hd, err := getPluginHomeDir()
274269
if err != nil {
270+
log.Fatal(err)
271+
}
272+
fullScriptPath := filepath.Join(hd, pluginScriptPath)
273+
274+
// Given the script perms, this command will not be able to overwrite the plugin script file.
275+
// That's ok, let the user handle removal to maintain security.
276+
if info, err := os.Stat(fullScriptPath); err == nil && !info.IsDir() {
277+
fmt.Fprintf(cmd.OutOrStdout(), "kustomize plugin configured at %s\n", fullScriptPath)
278+
return nil
279+
}
280+
281+
fmt.Fprintf(cmd.OutOrStdout(), "writing kustomize plugin file at %s\n", fullScriptPath)
282+
283+
dir, _ := filepath.Split(fullScriptPath)
284+
if err = os.MkdirAll(dir, 0700); err != nil {
275285
return err
276286
}
277287

278-
return ioutil.WriteFile(pluginFile, []byte(`#!/bin/bash
279-
KUSTOMIZE_FUNCTION=true kubebuilder alpha config-gen
280-
`), 0500)
288+
// r-x perms to prevent overwrite vulnerability since the script will be executed out-of-tree.
289+
return ioutil.WriteFile(fullScriptPath, []byte(pluginScript), 0500)
281290
},
282291
}
283292
c.AddCommand(install)
284293

285294
return c
286295
}
287296

288-
func getPluginDir() (string, error) {
297+
// Kustomize plugin execution script.
298+
const pluginScript = `#!/bin/bash
299+
KUSTOMIZE_FUNCTION=true kubebuilder alpha config-gen
300+
`
301+
302+
// Qualified directory containing the config-gen plugin script. Child of plugin home dir.
303+
var pluginScriptPath = filepath.Join("kustomize", "plugin",
304+
"kubebuilder.sigs.k8s.io", "v1alpha1", "kubebuilderconfiggen", "KubebuilderConfigGen")
305+
306+
// getPluginHomeDir returns $XDG_CONFIG_HOME if set, otherwise $HOME.
307+
func getPluginHomeDir() (string, error) {
289308
xdg := os.Getenv("XDG_CONFIG_HOME")
290309
if xdg == "" {
291310
dir, err := os.UserHomeDir()
@@ -294,6 +313,5 @@ func getPluginDir() (string, error) {
294313
}
295314
xdg = filepath.Join(dir, ".config")
296315
}
297-
dir := filepath.Join(xdg, "kustomize", "plugin", "kubebuilder.sigs.k8s.io", "v1alpha1", "kubebuilderconfiggen")
298-
return dir, nil
316+
return xdg, nil
299317
}

0 commit comments

Comments
 (0)