added support for environment variables, adding value files during runtime and a global kube-context flag to pass down to helm
This commit is contained in:
parent
4674af9608
commit
373e750642
|
|
@ -15,11 +15,15 @@ const (
|
|||
|
||||
type execer struct {
|
||||
writer io.Writer
|
||||
kubeContext string
|
||||
extra []string
|
||||
}
|
||||
|
||||
func NewHelmExec(writer io.Writer) Interface {
|
||||
return &execer{writer: writer}
|
||||
func NewHelmExec(writer io.Writer, kubeContext string) Interface {
|
||||
return &execer{
|
||||
writer: writer,
|
||||
kubeContext: kubeContext,
|
||||
}
|
||||
}
|
||||
|
||||
func (helm *execer) SetExtraArgs(args ...string) {
|
||||
|
|
@ -69,6 +73,9 @@ func (helm *execer) exec(args ...string) ([]byte, error) {
|
|||
if len(helm.extra) > 0 {
|
||||
cmdargs = append(cmdargs, helm.extra...)
|
||||
}
|
||||
if helm.kubeContext != "" {
|
||||
cmdargs = append(cmdargs, "--kube-context", helm.kubeContext)
|
||||
}
|
||||
if helm.writer != nil {
|
||||
helm.writer.Write([]byte(fmt.Sprintf("exec: helm %s\n", strings.Join(cmdargs, " "))))
|
||||
}
|
||||
|
|
|
|||
25
main.go
25
main.go
|
|
@ -31,6 +31,10 @@ func main() {
|
|||
Name: "quiet, q",
|
||||
Usage: "silence output",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "kube-context",
|
||||
Usage: "Set kubectl context",
|
||||
},
|
||||
}
|
||||
|
||||
app.Commands = []cli.Command{
|
||||
|
|
@ -73,6 +77,10 @@ func main() {
|
|||
Value: "",
|
||||
Usage: "pass args to helm exec",
|
||||
},
|
||||
cli.StringSliceFlag{
|
||||
Name: "values",
|
||||
Usage: "additional value files to be merged into the command",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
state, helm, err := before(c)
|
||||
|
|
@ -85,7 +93,9 @@ func main() {
|
|||
helm.SetExtraArgs(strings.Split(args, " ")...)
|
||||
}
|
||||
|
||||
if errs := state.SyncCharts(helm); err != nil && len(errs) > 0 {
|
||||
values := c.StringSlice("values")
|
||||
|
||||
if errs := state.SyncCharts(helm, values); err != nil && len(errs) > 0 {
|
||||
for _, err := range errs {
|
||||
fmt.Printf("err: %s", err.Error())
|
||||
}
|
||||
|
|
@ -97,6 +107,12 @@ func main() {
|
|||
{
|
||||
Name: "sync",
|
||||
Usage: "sync all resources from state file (repos && charts)",
|
||||
Flags: []cli.Flag{
|
||||
cli.StringSliceFlag{
|
||||
Name: "values",
|
||||
Usage: "additional value files to be merged into the command",
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
state, helm, err := before(c)
|
||||
if err != nil {
|
||||
|
|
@ -110,7 +126,9 @@ func main() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
if errs := state.SyncCharts(helm); err != nil && len(errs) > 0 {
|
||||
values := c.StringSlice("values")
|
||||
|
||||
if errs := state.SyncCharts(helm, values); err != nil && len(errs) > 0 {
|
||||
for _, err := range errs {
|
||||
fmt.Printf("err: %s", err.Error())
|
||||
}
|
||||
|
|
@ -149,6 +167,7 @@ func main() {
|
|||
func before(c *cli.Context) (*state.HelmState, helmexec.Interface, error) {
|
||||
file := c.GlobalString("file")
|
||||
quiet := c.GlobalBool("quiet")
|
||||
kubeContext := c.GlobalString("kube-context")
|
||||
|
||||
state, err := state.ReadFromFile(file)
|
||||
if err != nil {
|
||||
|
|
@ -160,5 +179,5 @@ func before(c *cli.Context) (*state.HelmState, helmexec.Interface, error) {
|
|||
writer = os.Stdout
|
||||
}
|
||||
|
||||
return state, helmexec.NewHelmExec(writer), nil
|
||||
return state, helmexec.NewHelmExec(writer, kubeContext), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ type ChartSpec struct {
|
|||
Namespace string `yaml:"namespace"`
|
||||
Values []string `yaml:"values"`
|
||||
SetValues []SetValue `yaml:"set"`
|
||||
EnvValues []SetValue `yaml:"env"`
|
||||
}
|
||||
|
||||
type SetValue struct {
|
||||
|
|
@ -77,7 +78,7 @@ func (state *HelmState) SyncRepos(helm helmexec.Interface) []error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (state *HelmState) SyncCharts(helm helmexec.Interface) []error {
|
||||
func (state *HelmState) SyncCharts(helm helmexec.Interface, additonalValues []string) []error {
|
||||
var wg sync.WaitGroup
|
||||
errs := []error{}
|
||||
|
||||
|
|
@ -85,6 +86,14 @@ func (state *HelmState) SyncCharts(helm helmexec.Interface) []error {
|
|||
wg.Add(1)
|
||||
go func(wg *sync.WaitGroup, chart ChartSpec) {
|
||||
flags, err := flagsForChart(&chart)
|
||||
for _, value := range additonalValues {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
valfile := filepath.Join(wd, value)
|
||||
flags = append(flags, "--values", valfile)
|
||||
}
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
} else {
|
||||
|
|
@ -152,5 +161,12 @@ func flagsForChart(chart *ChartSpec) ([]string, error) {
|
|||
}
|
||||
flags = append(flags, "--set", strings.Join(val, ","))
|
||||
}
|
||||
if len(chart.EnvValues) > 0 {
|
||||
val := []string{}
|
||||
for _, set := range chart.EnvValues {
|
||||
val = append(val, fmt.Sprintf("%s=%s", set.Name, os.Getenv(set.Value)))
|
||||
}
|
||||
flags = append(flags, "--set", strings.Join(val, ","))
|
||||
}
|
||||
return flags, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue