feat(main): Support specifying kubectl args in ResourceSets

This commit is contained in:
Martin Lehmann 2018-11-15 13:49:23 +01:00 committed by Vincent Ambo
parent 5f433c46c1
commit 54db9785d6
6 changed files with 61 additions and 2 deletions

View file

@ -28,6 +28,9 @@ type ResourceSet struct {
// Values to include when interpolating resources from this resource set.
Values map[string]interface{} `json:"values"`
// Args to pass on to kubectl for this resource set.
Args []string `json:"args"`
// Nested resource sets to include
Include []ResourceSet `json:"include"`

View file

@ -54,6 +54,42 @@ func TestLoadFlatContextFromFile(t *testing.T) {
}
}
func TestLoadContextWithArgs(t *testing.T) {
ctx, err := LoadContext("testdata/flat-with-args-test.yaml", &noExplicitVars)
if err != nil {
t.Error(err)
t.Fail()
}
expected := Context{
Name: "k8s.prod.mydomain.com",
ResourceSets: []ResourceSet{
{
Name: "some-api",
Path: "some-api",
Values: make(map[string]interface{}, 0),
Args: []string{
"--as=some-user",
"--as-group=hello:world",
"--as-banana",
"true",
},
Include: nil,
Parent: "",
},
},
BaseDir: "testdata",
ImportedVars: make(map[string]interface{}, 0),
ExplicitVars: make(map[string]interface{}, 0),
}
if !reflect.DeepEqual(*ctx, expected) {
t.Error("Loaded context and expected context did not match")
t.Fail()
}
}
func TestLoadContextWithResourceSetCollections(t *testing.T) {
ctx, err := LoadContext("testdata/collections-test.yaml", &noExplicitVars)

View file

@ -0,0 +1,9 @@
---
context: k8s.prod.mydomain.com
include:
- name: some-api
args:
- --as=some-user
- --as-group=hello:world
- --as-banana
- "true"

View file

@ -16,6 +16,7 @@ Technically a resource set is simply a folder with a few YAML and/or JSON templa
- [`name`](#name)
- [`path`](#path)
- [`values`](#values)
- [`args`](#args)
- [`include`](#include)
- [Multiple includes](#multiple-includes)
- [Nesting resource sets](#nesting-resource-sets)
@ -81,6 +82,12 @@ The `values` field specifies key/values pairs of variables that should be availa
This field is **optional**.
### `args`
The `args` field specifies a list of arguments that should be passed to `kubectl`.
This field is **optional**.
### `include`
The `include` field specifies additional resource sets that should be included and that should inherit the

View file

@ -199,7 +199,7 @@ func loadContextAndResources(file *string) (*context.Context, *[]templater.Rende
}
func runKubectlWithResources(c *context.Context, kubectlArgs *[]string, resourceSets *[]templater.RenderedResourceSet) error {
args := append(*kubectlArgs, fmt.Sprintf("--context=%s", c.Name))
argsWithContext := append(*kubectlArgs, fmt.Sprintf("--context=%s", c.Name))
for _, rs := range *resourceSets {
if len(rs.Resources) == 0 {
@ -207,7 +207,9 @@ func runKubectlWithResources(c *context.Context, kubectlArgs *[]string, resource
continue
}
kubectl := exec.Command(*kubectlBin, args...)
argsWithResourceSetArgs := append(argsWithContext, rs.Args...)
kubectl := exec.Command(*kubectlBin, argsWithResourceSetArgs...)
stdin, err := kubectl.StdinPipe()
if err != nil {

View file

@ -35,6 +35,7 @@ type RenderedResource struct {
type RenderedResourceSet struct {
Name string
Resources []RenderedResource
Args []string
}
func LoadAndApplyTemplates(include *[]string, exclude *[]string, c *context.Context) ([]RenderedResourceSet, error) {
@ -94,6 +95,7 @@ func processResourceSet(ctx *context.Context, rs *context.ResourceSet) (*Rendere
return &RenderedResourceSet{
Name: rs.Name,
Resources: resources,
Args: rs.Args,
}, nil
}