Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adds default values for kube_config
- Loading branch information
Showing
8 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package starlark | ||
|
||
import ( | ||
"go.starlark.net/starlark" | ||
"go.starlark.net/starlarkstruct" | ||
) | ||
|
||
// addDefaultKubeConf initializes a Starlark Dict with default | ||
// KUBECONFIG configuration data | ||
func addDefaultKubeConf(thread *starlark.Thread) error { | ||
args := []starlark.Tuple{ | ||
{starlark.String("path"), starlark.String(defaults.kubeconfig)}, | ||
} | ||
|
||
_, err := kubeConfigFn(thread, nil, nil, args) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// kubeConfigFn is built-in starlark function that wraps the kwargs into a dictionary value. | ||
// The result is also added to the thread for other built-in to access. | ||
func kubeConfigFn(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
var dictionary starlark.StringDict | ||
if kwargs != nil { | ||
dict, err := kwargsToStringDict(kwargs) | ||
if err != nil { | ||
return starlark.None, err | ||
} | ||
dictionary = dict | ||
} | ||
|
||
structVal := starlarkstruct.FromStringDict(starlarkstruct.Default, dictionary) | ||
|
||
// save dict to be used as default | ||
thread.SetLocal(identifiers.kubeCfg, structVal) | ||
|
||
return structVal, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package starlark | ||
|
||
import ( | ||
"strings" | ||
|
||
"go.starlark.net/starlarkstruct" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("kube_config", func() { | ||
|
||
var ( | ||
crashdScript string | ||
executor *Executor | ||
err error | ||
) | ||
|
||
execSetup := func() { | ||
executor = New() | ||
err = executor.Exec("test.kube.config", strings.NewReader(crashdScript)) | ||
Expect(err).To(BeNil()) | ||
} | ||
|
||
Context("With kube_config set in the script", func() { | ||
|
||
BeforeEach(func() { | ||
crashdScript = `kube_config(path="/foo/bar/kube/config")` | ||
execSetup() | ||
}) | ||
|
||
It("sets the kube_config in the starlark thread", func() { | ||
kubeConfigData := executor.thread.Local(identifiers.kubeCfg) | ||
Expect(kubeConfigData).NotTo(BeNil()) | ||
}) | ||
|
||
It("sets the path to the kubeconfig file", func() { | ||
kubeConfigData := executor.thread.Local(identifiers.kubeCfg) | ||
Expect(kubeConfigData).To(BeAssignableToTypeOf(&starlarkstruct.Struct{})) | ||
|
||
cfg, _ := kubeConfigData.(*starlarkstruct.Struct) | ||
Expect(cfg.AttrNames()).To(HaveLen(1)) | ||
|
||
val, err := cfg.Attr("path") | ||
Expect(err).To(BeNil()) | ||
Expect(trimQuotes(val.String())).To(Equal("/foo/bar/kube/config")) | ||
}) | ||
}) | ||
|
||
Context("With kube_config returned as a value", func() { | ||
|
||
BeforeEach(func() { | ||
crashdScript = `cfg = kube_config(path="/foo/bar/kube/config")` | ||
execSetup() | ||
}) | ||
|
||
It("returns the kube config as a result", func() { | ||
Expect(executor.result.Has("cfg")).NotTo(BeNil()) | ||
}) | ||
|
||
It("also sets the kube_config in the starlark thread", func() { | ||
kubeConfigData := executor.thread.Local(identifiers.kubeCfg) | ||
Expect(kubeConfigData).NotTo(BeNil()) | ||
|
||
cfg, _ := kubeConfigData.(*starlarkstruct.Struct) | ||
Expect(cfg.AttrNames()).To(HaveLen(1)) | ||
|
||
val, err := cfg.Attr("path") | ||
Expect(err).To(BeNil()) | ||
Expect(trimQuotes(val.String())).To(Equal("/foo/bar/kube/config")) | ||
}) | ||
}) | ||
|
||
Context("With default kube_config setup", func() { | ||
|
||
BeforeEach(func() { | ||
crashdScript = `foo = "bar"` | ||
execSetup() | ||
}) | ||
|
||
It("sets the default kube_config in the starlark thread", func() { | ||
kubeConfigData := executor.thread.Local(identifiers.kubeCfg) | ||
Expect(kubeConfigData).NotTo(BeNil()) | ||
|
||
cfg, _ := kubeConfigData.(*starlarkstruct.Struct) | ||
Expect(cfg.AttrNames()).To(HaveLen(1)) | ||
|
||
val, err := cfg.Attr("path") | ||
Expect(err).To(BeNil()) | ||
Expect(trimQuotes(val.String())).To(ContainSubstring("/.kube/config")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package starlark | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestStarlark(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Starlark Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters