Skip to content

[installer] Small render improvements #9947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions install/installer/cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package cmd

import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"

_ "embed"

Expand All @@ -24,6 +26,7 @@ var renderOpts struct {
Namespace string
ValidateConfigDisabled bool
UseExperimentalConfig bool
FilesDir string
}

// renderCmd represents the render command
Expand Down Expand Up @@ -58,6 +61,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)
}
Expand All @@ -66,10 +77,35 @@ 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
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))
Expand Down Expand Up @@ -187,8 +223,9 @@ 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")
renderCmd.Flags().StringVar(&renderOpts.FilesDir, "output-split-files", "", "path to output individual Kubernetes manifests to")
}