github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/step_values_schema_template.go (about) 1 package step 2 3 import ( 4 "os" 5 "strings" 6 "text/template" 7 8 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 9 "github.com/olli-ai/jx/v2/pkg/cmd/opts/step" 10 11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 13 "github.com/pkg/errors" 14 15 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 16 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 17 "github.com/spf13/cobra" 18 ) 19 20 var ( 21 valuesSchemaTemplateLong = templates.LongDesc(` 22 Creates a JSON schema from a template 23 `) 24 25 valuesSchemaTemplateExample = templates.Examples(` 26 jx step values schema template 27 28 `) 29 ) 30 31 const ( 32 valuesSchemaJsonConfigMapNameEnvVar = "VALUES_SCHEMA_JSON_CONFIG_MAP_NAME" 33 ) 34 35 // StepValuesSchemaTemplateOptions contains the command line flags 36 type StepValuesSchemaTemplateOptions struct { 37 step.StepOptions 38 39 ConfigMapName string 40 ConfigMapKey string 41 Set []string 42 } 43 44 // NewCmdStepValuesSchemaTemplate Creates a new Command object 45 func NewCmdStepValuesSchemaTemplate(commonOpts *opts.CommonOptions) *cobra.Command { 46 options := &StepValuesSchemaTemplateOptions{ 47 StepOptions: step.StepOptions{ 48 CommonOptions: commonOpts, 49 }, 50 } 51 52 cmd := &cobra.Command{ 53 Use: "values schema template", 54 Short: "Creates a JSON schema from a template", 55 Long: valuesSchemaTemplateLong, 56 Example: valuesSchemaTemplateExample, 57 Run: func(cmd *cobra.Command, args []string) { 58 options.Cmd = cmd 59 options.Args = args 60 err := options.Run() 61 helper.CheckErr(err) 62 }, 63 } 64 65 cmd.Flags().StringVarP(&options.ConfigMapName, "config-map-name", "", "", "The name of the config map to use, "+ 66 "by default read from the VALUES_SCHEMA_JSON_CONFIG_MAP_NAME environment variable") 67 cmd.Flags().StringVarP(&options.ConfigMapKey, "config-map-key", "", "values.schema.json", 68 "The name of the key in the config map which contains values.schema.json") 69 cmd.Flags().StringArrayVarP(&options.Set, "set", "", make([]string, 0), 70 "the values to pass to the template e.g. --set foo=bar. "+ 71 "Can be specified multiple times They can be accessed as .Values.<name> e.g. .Values.foo") 72 return cmd 73 } 74 75 // Run implements this command 76 func (o *StepValuesSchemaTemplateOptions) Run() error { 77 if o.ConfigMapName == "" { 78 o.ConfigMapName = os.Getenv(valuesSchemaJsonConfigMapNameEnvVar) 79 } 80 if o.ConfigMapKey == "" { 81 o.ConfigMapKey = "values.schema.json" 82 } 83 kubeClient, ns, err := o.KubeClientAndNamespace() 84 if err != nil { 85 return errors.Wrapf(err, "getting kube client and ns") 86 } 87 cm, err := kubeClient.CoreV1().ConfigMaps(ns).Get(o.ConfigMapName, metav1.GetOptions{}) 88 if err != nil { 89 return errors.Wrapf(err, "getting config map %s", o.ConfigMapName) 90 } 91 tmplStr := cm.Data[o.ConfigMapKey] 92 values := make(map[string]interface{}) 93 for _, v := range o.Set { 94 parts := strings.Split(v, "=") 95 if len(parts) != 2 { 96 return errors.Errorf("cannot parse value %s as key=value", v) 97 } 98 values[parts[0]] = parts[1] 99 } 100 tmpl, err := template.New("values_schema_json").Parse(tmplStr) 101 if err != nil { 102 return errors.Wrapf(err, "parsing %s as go template", tmplStr) 103 } 104 var out strings.Builder 105 data := map[string]map[string]interface{}{ 106 "Values": values, 107 } 108 err = tmpl.Execute(&out, data) 109 if err != nil { 110 return errors.Wrapf(err, "executing template %s with values %v", tmplStr, data) 111 } 112 cm.Data[o.ConfigMapKey] = out.String() 113 _, err = kubeClient.CoreV1().ConfigMaps(ns).Update(cm) 114 if err != nil { 115 return errors.Wrapf(err, "writing %v to configmap %s", cm, o.ConfigMapName) 116 } 117 return nil 118 }