Skip to content

Commit

Permalink
Adds the set_as_default directive
Browse files Browse the repository at this point in the history
This patch introduces a new starlark directive called as
`set_as_default` which can be used to set the default values of one of
the three types:
- kube_config
- ssh_config
- resources

Previously, on each call of kube_config, ssh_config or resources, the
resulting value was set in the local starlark thread and avialbale
inside the script. This patch removes the need for this default
behavior. Instead, the script developer needs to make a choice to call
the set_as_default directive with the value that needs to be stored in
the thread.
  • Loading branch information
srm09 committed Jul 21, 2020
1 parent 937fdf6 commit dc5b5cc
Show file tree
Hide file tree
Showing 22 changed files with 226 additions and 241 deletions.
7 changes: 4 additions & 3 deletions examples/capv_provider.file → examples/capv_provider.star
Expand Up @@ -22,11 +22,12 @@ capture(cmd="sudo cat /var/log/cloud-init.log", resources=nodes)

#add code to collect pod info from cluster
wc_kube_conf = kube_config(capi_provider = wc_provider)
set_as_default(kube_config = wc_kube_conf)

pod_ns=["default", "kube-system"]

kube_capture(what="logs", namespaces=pod_ns, kube_config=wc_kube_conf)
kube_capture(what="objects", kinds=["pods", "services"], namespaces=pod_ns, kube_config=wc_kube_conf)
kube_capture(what="objects", kinds=["deployments", "replicasets"], groups=["apps"], namespaces=pod_ns, kube_config=wc_kube_conf)
kube_capture(what="logs", namespaces=pod_ns)
kube_capture(what="objects", kinds=["pods", "services"], namespaces=pod_ns)
kube_capture(what="objects", kinds=["deployments", "replicasets"], groups=["apps"], namespaces=pod_ns)

archive(output_file="diagnostics.tar.gz", source_paths=[conf.workdir])
3 changes: 1 addition & 2 deletions examples/kind-api-objects.star
Expand Up @@ -10,8 +10,7 @@ nspaces=[
"cert-manager tkg-system",
]


kube_config(path=args.kubecfg)
set_as_default(kube_config = kube_config(path=args.kubecfg))

# capture Kubernetes API object and store in files (under working dir)
kube_capture(what="objects", kinds=["services", "pods"], namespaces=nspaces)
Expand Down
8 changes: 4 additions & 4 deletions examples/kind-capi-bootstrap.star
Expand Up @@ -28,12 +28,12 @@ nspaces=[
"cert-manager tkg-system",
]

kconf=kube_config(path=kind_cfg)
kconf = kube_config(path=kind_cfg)

# capture Kubernetes API object and store in files (under working dir)
kube_capture(what="objects", kinds=["services", "pods"], namespaces=nspaces, kube_conf=kconf)
kube_capture(what="objects", kinds=["deployments", "replicasets"], namespaces=nspaces, kube_conf=kconf)
kube_capture(what="objects", kinds=["clusters", "machines", "machinesets", "machinedeployments"], namespaces="tkg-system", kube_conf=kconf)
kube_capture(what="objects", kinds=["services", "pods"], namespaces=nspaces, kube_config = kconf)
kube_capture(what="objects", kinds=["deployments", "replicasets"], namespaces=nspaces, kube_config = kconf)
kube_capture(what="objects", kinds=["clusters", "machines", "machinesets", "machinedeployments"], namespaces="tkg-system", kube_config = kconf)

# bundle files stored in working dir
archive(output_file="/tmp/crashout.tar.gz", source_paths=[conf.workdir])
2 changes: 1 addition & 1 deletion examples/pod-logs.star
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

conf=crashd_config(workdir="/tmp/crashlogs")
kube_config(path="{0}/.kube/config".format(os.home))
set_as_default(kube_config = kube_config(path="{0}/.kube/config".format(os.home)))
kube_capture(what="logs", namespaces=["default", "cert-manager", "tkg-system"])

# bundle files stored in working dir
Expand Down
3 changes: 1 addition & 2 deletions examples/script-args.star
Expand Up @@ -2,8 +2,7 @@
# SPDX-License-Identifier: Apache-2.0

conf=crashd_config(workdir=args.workdir)
kube_config(path=args.kubecfg)
kube_capture(what="logs", namespaces=["default", "cert-manager", "tkg-system"])
kube_capture(what="logs", namespaces=["default", "cert-manager", "tkg-system"], kube_config = kube_config(path=args.kubecfg))

# bundle files stored in working dir
archive(output_file=args.output, source_paths=[args.workdir])
5 changes: 2 additions & 3 deletions starlark/capture_test.go
Expand Up @@ -182,8 +182,7 @@ func testCaptureFuncScriptForHostResources(t *testing.T, port string) {
{
name: "default cmd multiple machines",
script: fmt.Sprintf(`
ssh_config(username=os.username, port="%s")
resources(hosts=["127.0.0.1","localhost"])
set_as_default(resources = resources(provider = host_list_provider(hosts=["127.0.0.1","localhost"], ssh_config = ssh_config(username=os.username, port="%s"))))
result = capture("echo 'Hello World!'")`, port),
eval: func(t *testing.T, script string) {
exe := New()
Expand Down Expand Up @@ -229,7 +228,7 @@ def exec(hosts):
return result
# configuration
ssh_config(username=os.username, port="%s")
set_as_default(ssh_config = ssh_config(username=os.username, port="%s"))
hosts = resources(provider=host_list_provider(hosts=["127.0.0.1","localhost"]))
result = exec(hosts)`, port),
eval: func(t *testing.T, script string) {
Expand Down
5 changes: 2 additions & 3 deletions starlark/copy_from_test.go
Expand Up @@ -233,8 +233,7 @@ func testCopyFuncScriptForHostResources(t *testing.T, port string) {
name: "multiple machines single copyFrom",
remoteFiles: map[string]string{"foobar.c": "footext", "bar/bar.txt": "BarBar", "bar/foo.txt": "FooBar", "bar/baz.csv": "BizzBuzz"},
script: fmt.Sprintf(`
ssh_config(username=os.username, port="%s")
resources(hosts=["127.0.0.1","localhost"])
set_as_default(resources = resources(provider = host_list_provider(hosts=["127.0.0.1","localhost"], ssh_config = ssh_config(username=os.username, port="%s"))))
result = copy_from("bar/foo.txt")`, port),
eval: func(t *testing.T, script string) {
exe := New()
Expand Down Expand Up @@ -298,7 +297,7 @@ def cp(hosts):
return result
# configuration
ssh_config(username=os.username, port="%s")
set_as_default(ssh_config = ssh_config(username=os.username, port="%s"))
hosts = resources(provider=host_list_provider(hosts=["127.0.0.1","localhost"]))
result = cp(hosts)`, port),
eval: func(t *testing.T, script string) {
Expand Down
4 changes: 2 additions & 2 deletions starlark/hostlist_provider_test.go
Expand Up @@ -19,7 +19,7 @@ func TestHostListProvider(t *testing.T) {
}{
{
name: "single host",
script: `provider = host_list_provider(hosts=["foo.host"])`,
script: `provider = host_list_provider(hosts=["foo.host"], ssh_config = ssh_config(username="uname", private_key_path="path"))`,
eval: func(t *testing.T, script string) {
exe := New()
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil {
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestHostListProvider(t *testing.T) {
},
{
name: "multiple hosts",
script: `provider = host_list_provider(hosts=["foo.host.1", "foo.host.2"])`,
script: `provider = host_list_provider(hosts=["foo.host.1", "foo.host.2"], ssh_config = ssh_config(username="uname", private_key_path="path"))`,
eval: func(t *testing.T, script string) {
exe := New()
if err := exe.Exec("test.star", strings.NewReader(script)); err != nil {
Expand Down
17 changes: 8 additions & 9 deletions starlark/kube_capture_test.go
Expand Up @@ -43,7 +43,7 @@ var _ = Describe("kube_capture", func() {
It("creates a directory and files for namespaced objects", func() {
crashdScript := fmt.Sprintf(`
crashd_config(workdir="%s")
kube_config(path="%s")
set_as_default(kube_config = kube_config(path="%s"))
kube_data = kube_capture(what="objects", groups=["core"], kinds=["services"], namespaces=["default", "kube-system"])
`, workdir, k8sconfig)
execSetup(crashdScript)
Expand Down Expand Up @@ -73,8 +73,8 @@ kube_data = kube_capture(what="objects", groups=["core"], kinds=["services"], na
It("creates a directory and files for non-namespaced objects", func() {
crashdScript := fmt.Sprintf(`
crashd_config(workdir="%s")
kube_config(path="%s")
kube_data = kube_capture(what="objects", groups=["core"], kinds=["nodes"])
cfg = kube_config(path="%s")
kube_data = kube_capture(what="objects", groups=["core"], kinds=["nodes"], kube_config = cfg)
`, workdir, k8sconfig)
execSetup(crashdScript)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -100,8 +100,7 @@ kube_data = kube_capture(what="objects", groups=["core"], kinds=["nodes"])
It("creates a directory and log files for all objects in a namespace", func() {
crashdScript := fmt.Sprintf(`
crashd_config(workdir="%s")
kube_config(path="%s")
kube_data = kube_capture(what="logs", namespaces=["kube-system"])
kube_data = kube_capture(what="logs", namespaces=["kube-system"], kube_config = kube_config(path="%s"))
`, workdir, k8sconfig)
execSetup(crashdScript)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -131,8 +130,8 @@ kube_data = kube_capture(what="logs", namespaces=["kube-system"])
It("creates a log file for specific container in a namespace", func() {
crashdScript := fmt.Sprintf(`
crashd_config(workdir="%s")
kube_config(path="%s")
kube_data = kube_capture(what="logs", namespaces=["kube-system"], containers=["etcd"])
cfg = kube_config(path="%s")
kube_data = kube_capture(what="logs", namespaces=["kube-system"], containers=["etcd"], kube_config = cfg)
`, workdir, k8sconfig)
execSetup(crashdScript)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -164,8 +163,8 @@ kube_data = kube_capture(what="logs", namespaces=["kube-system"], containers=["e
Expect(err).To(HaveOccurred())
},
Entry("in global thread", fmt.Sprintf(`
kube_config(path="%s")
kube_capture(what="logs", namespaces=["kube-system"], containers=["etcd"])`, "/foo/bar")),
cfg = kube_config(path="%s")
kube_capture(what="logs", namespaces=["kube-system"], containers=["etcd"], kube_config = cfg)`, "/foo/bar")),
Entry("in function call", fmt.Sprintf(`
cfg = kube_config(path="%s")
kube_capture(what="logs", namespaces=["kube-system"], containers=["etcd"], kube_config=cfg)`, "/foo/bar")),
Expand Down
5 changes: 1 addition & 4 deletions starlark/kube_config.go
Expand Up @@ -14,7 +14,7 @@ import (
// 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.
// Starlark: kube_config(path=kubecf/path)
func KubeConfigFn(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
func KubeConfigFn(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path string
var provider *starlarkstruct.Struct

Expand Down Expand Up @@ -54,9 +54,6 @@ func KubeConfigFn(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tu
"path": starlark.String(path),
})

// save dict to be used as default
thread.SetLocal(identifiers.kubeCfg, structVal)

return structVal, nil
}

Expand Down
31 changes: 11 additions & 20 deletions starlark/kube_config_test.go
Expand Up @@ -35,17 +35,12 @@ var _ = Describe("kube_config", func() {
Context("With kube_config set in the script", func() {

BeforeEach(func() {
crashdScript = `kube_config(path="/foo/bar/kube/config")`
crashdScript = `cfg = 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)
kubeConfigData := executor.result["cfg"]
Expect(kubeConfigData).To(BeAssignableToTypeOf(&starlarkstruct.Struct{}))

cfg, _ := kubeConfigData.(*starlarkstruct.Struct)
Expand All @@ -66,10 +61,8 @@ var _ = Describe("kube_config", func() {

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)
kubeConfigData := executor.result["cfg"]
Expect(kubeConfigData).NotTo(BeNil())

cfg, _ := kubeConfigData.(*starlarkstruct.Struct)
Expand All @@ -79,26 +72,24 @@ var _ = Describe("kube_config", func() {
Expect(err).To(BeNil())
Expect(trimQuotes(val.String())).To(Equal("/foo/bar/kube/config"))
})

It("does not set the kube_config in the starlark thread", func() {
kubeConfigData := executor.thread.Local(identifiers.kubeCfg)
Expect(kubeConfigData).To(BeNil())
})
})
})

Context("With default kube_config setup", func() {
Context("For default kube_config setup", func() {

BeforeEach(func() {
crashdScript = `foo = "bar"`
execSetup()
})

It("sets the default kube_config in the starlark thread", func() {
It("does not set 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"))
Expect(kubeConfigData).To(BeNil())
})
})
})
Expand Down
11 changes: 5 additions & 6 deletions starlark/kube_get_test.go
Expand Up @@ -29,7 +29,7 @@ var _ = Describe("kube_get", func() {

It("returns a list of k8s services as starlark objects", func() {
crashdScript := fmt.Sprintf(`
kube_config(path="%s")
set_as_default(kube_config = kube_config(path="%s"))
kube_get_data = kube_get(groups=["core"], kinds=["services"], namespaces=["default", "kube-system"])
`, k8sconfig)
execSetup(crashdScript)
Expand All @@ -51,8 +51,8 @@ kube_get_data = kube_get(groups=["core"], kinds=["services"], namespaces=["defau

It("returns a list of k8s nodes as starlark objects", func() {
crashdScript := fmt.Sprintf(`
kube_config(path="%s")
kube_get_data = kube_get(groups=["core"], kinds=["nodes"])
cfg = kube_config(path="%s")
kube_get_data = kube_get(groups=["core"], kinds=["nodes"], kube_config = cfg)
`, k8sconfig)
execSetup(crashdScript)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -73,8 +73,7 @@ kube_get_data = kube_get(groups=["core"], kinds=["nodes"])

It("returns a list of etcd containers as starlark objects", func() {
crashdScript := fmt.Sprintf(`
kube_config(path="%s")
kube_get_data = kube_get(namespaces=["kube-system"], containers=["etcd"])
kube_get_data = kube_get(namespaces=["kube-system"], containers=["etcd"], kube_config = kube_config(path="%s"))
`, k8sconfig)
execSetup(crashdScript)
Expect(err).NotTo(HaveOccurred())
Expand All @@ -98,7 +97,7 @@ kube_get_data = kube_get(namespaces=["kube-system"], containers=["etcd"])
Expect(err).To(HaveOccurred())
},
Entry("in global thread", fmt.Sprintf(`
kube_config(path="%s")
set_as_default(kube_config = kube_config(path="%s"))
kube_get(namespaces=["kube-system"], containers=["etcd"])`, "/foo/bar")),
Entry("in function call", fmt.Sprintf(`
cfg = kube_config(path="%s")
Expand Down
10 changes: 4 additions & 6 deletions starlark/kube_nodes_provider_test.go
Expand Up @@ -27,9 +27,8 @@ var _ = Describe("kube_nodes_provider", func() {

It("returns a struct with the list of k8s nodes", func() {
crashdScript := fmt.Sprintf(`
kube_config(path="%s")
ssh_config(username="uname", private_key_path="path")
provider = kube_nodes_provider()`, k8sconfig)
cfg = kube_config(path="%s")
provider = kube_nodes_provider(kube_config = cfg, ssh_config = ssh_config(username="uname", private_key_path="path"))`, k8sconfig)
err = execSetup(crashdScript)
Expect(err).NotTo(HaveOccurred())

Expand All @@ -49,9 +48,8 @@ provider = kube_nodes_provider()`, k8sconfig)
It("returns a struct with ssh config", func() {
crashdScript := fmt.Sprintf(`
cfg = kube_config(path="%s")
kube_config(path="/foo/bar")
ssh_config(username="uname", private_key_path="path")
provider = kube_nodes_provider(kube_config=cfg)`, k8sconfig)
ssh_cfg = ssh_config(username="uname", private_key_path="path")
provider = kube_nodes_provider(kube_config=cfg, ssh_config = ssh_cfg)`, k8sconfig)
err = execSetup(crashdScript)
Expect(err).NotTo(HaveOccurred())

Expand Down
3 changes: 0 additions & 3 deletions starlark/resources.go
Expand Up @@ -45,9 +45,6 @@ func resourcesFunc(thread *starlark.Thread, b *starlark.Builtin, args starlark.T
return starlark.None, err
}

// save resources for future use
thread.SetLocal(identifiers.resources, resources)

return resources, nil
}

Expand Down

0 comments on commit dc5b5cc

Please sign in to comment.