From 26b7efc98d4e746366dcef31e0223ca75ee48c60 Mon Sep 17 00:00:00 2001 From: Christian Weichel Date: Wed, 11 May 2022 20:27:54 +0000 Subject: [PATCH 1/2] [installer] Support rendering to individual files --- install/installer/cmd/render.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/install/installer/cmd/render.go b/install/installer/cmd/render.go index ec3fde8e019bd4..662c4921e55638 100644 --- a/install/installer/cmd/render.go +++ b/install/installer/cmd/render.go @@ -8,6 +8,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" _ "embed" @@ -24,6 +25,7 @@ var renderOpts struct { Namespace string ValidateConfigDisabled bool UseExperimentalConfig bool + FilesDir string } // renderCmd represents the render command @@ -58,6 +60,14 @@ A config file is required which can be generated with the init command.`, return err } + if renderOpts.FilesDir != "" { + err := saveYamlToFiles(renderOpts.FilesDir, yaml) + if err != nil { + return err + } + return nil + } + for _, item := range yaml { fmt.Println(item) } @@ -66,6 +76,22 @@ A config file is required which can be generated with the init command.`, }, } +func saveYamlToFiles(dir string, yaml []string) error { + for i, mf := range yaml { + objs, err := common.YamlToRuntimeObject([]string{mf}) + if err != nil { + return err + } + obj := objs[0] + fn := filepath.Join(dir, fmt.Sprintf("%03d_%s_%s.yaml", i, obj.Kind, obj.Metadata.Name)) + err = ioutil.WriteFile(fn, []byte(mf), 0644) + if err != nil { + return err + } + } + return nil +} + func loadConfig(cfgFN string) (rawCfg interface{}, cfgVersion string, cfg *configv1.Config, err error) { var overrideConfig string // Update overrideConfig if cfgFN is not empty @@ -191,4 +217,5 @@ func init() { renderCmd.PersistentFlags().StringVarP(&renderOpts.Namespace, "namespace", "n", "default", "namespace to deploy to") renderCmd.Flags().BoolVar(&renderOpts.ValidateConfigDisabled, "no-validation", false, "if set, the config will not be validated before running") renderCmd.Flags().BoolVar(&renderOpts.UseExperimentalConfig, "use-experimental-config", false, "enable the use of experimental config that is prone to be changed") + renderCmd.Flags().StringVar(&renderOpts.FilesDir, "output-split-files", "", "path to output individual Kubernetes manifests to") } From 3c421c0810ace2fc9d2ec48a421ac1b281073dca Mon Sep 17 00:00:00 2001 From: Christian Weichel Date: Wed, 11 May 2022 20:31:08 +0000 Subject: [PATCH 2/2] [installer] Support reading config file from stdin this makes rendering using the Docker image much easier because you can just pipe the config to the installer. --- install/installer/cmd/render.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/install/installer/cmd/render.go b/install/installer/cmd/render.go index 662c4921e55638..bd32b7c20cd3b8 100644 --- a/install/installer/cmd/render.go +++ b/install/installer/cmd/render.go @@ -6,6 +6,7 @@ package cmd import ( "fmt" + "io" "io/ioutil" "os" "path/filepath" @@ -95,7 +96,16 @@ func saveYamlToFiles(dir string, yaml []string) error { func loadConfig(cfgFN string) (rawCfg interface{}, cfgVersion string, cfg *configv1.Config, err error) { var overrideConfig string // Update overrideConfig if cfgFN is not empty - if cfgFN != "" { + switch cfgFN { + case "-": + b, err := io.ReadAll(os.Stdin) + if err != nil { + return nil, "", nil, err + } + overrideConfig = string(b) + case "": + return nil, "", nil, fmt.Errorf("missing config file") + default: cfgBytes, err := ioutil.ReadFile(cfgFN) if err != nil { panic(fmt.Sprintf("couldn't read file %s, %s", cfgFN, err)) @@ -213,7 +223,7 @@ func renderKubernetesObjects(cfgVersion string, cfg *configv1.Config) ([]string, func init() { rootCmd.AddCommand(renderCmd) - renderCmd.PersistentFlags().StringVarP(&renderOpts.ConfigFN, "config", "c", os.Getenv("GITPOD_INSTALLER_CONFIG"), "path to the config file") + renderCmd.PersistentFlags().StringVarP(&renderOpts.ConfigFN, "config", "c", os.Getenv("GITPOD_INSTALLER_CONFIG"), "path to the config file, use - for stdin") renderCmd.PersistentFlags().StringVarP(&renderOpts.Namespace, "namespace", "n", "default", "namespace to deploy to") renderCmd.Flags().BoolVar(&renderOpts.ValidateConfigDisabled, "no-validation", false, "if set, the config will not be validated before running") renderCmd.Flags().BoolVar(&renderOpts.UseExperimentalConfig, "use-experimental-config", false, "enable the use of experimental config that is prone to be changed")