Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch includes the kube_get() built-in which can query and present the k8s objects as starlark values which can be usd to use in the starlark based crashd configuration file This also includes: - Moving test code from a test file to the test suite file - Convert the ListItems from SearchResult to Starlark values - Fixes kube_get & kube_capture built-ins for kube_cfg option
- Loading branch information
Showing
13 changed files
with
1,092 additions
and
138 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 |
---|---|---|
@@ -1,35 +1,186 @@ | ||
// Copyright (c) 2020 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package k8s | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"go.starlark.net/starlark" | ||
"go.starlark.net/starlarkstruct" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/ginkgo/extensions/table" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var populateSearchResults = func() []SearchResult { | ||
content, err := ioutil.ReadFile("../testing/search_results.json") | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(len(content)).NotTo(Equal(0)) | ||
|
||
var lists []unstructured.UnstructuredList | ||
err = json.Unmarshal(content, &lists) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
var results []SearchResult | ||
for index, list := range lists { | ||
Expect(list.Items).To(HaveLen(index + 1)) | ||
results = append(results, SearchResult{ | ||
List: list.DeepCopy(), | ||
}) | ||
} | ||
return results | ||
} | ||
|
||
var _ = Describe("SearchResult", func() { | ||
|
||
Context("ToStarlarkValue", func() { | ||
|
||
Context("ListKind", func() { | ||
It("returns a dictionary of size equal to number of struct elements", func() { | ||
sr := SearchResult{ListKind: "PodList"} | ||
val := sr.ToStarlarkValue() | ||
Expect(val).To(BeAssignableToTypeOf(&starlarkstruct.Struct{})) | ||
Expect(val.AttrNames()).To(HaveLen(7)) | ||
}) | ||
|
||
It("creates value object with ListKind value", func() { | ||
_ = sr.ToStarlarkValue() | ||
sr := SearchResult{ | ||
ListKind: "NodeList", | ||
ResourceName: "nodes", | ||
ResourceKind: "Node", | ||
Namespace: "", | ||
Namespaced: false, | ||
} | ||
|
||
DescribeTable("String types", func(typeDescription, stringVal string) { | ||
structVal := sr.ToStarlarkValue() | ||
val, err := structVal.Attr(typeDescription) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
strVal, _ := val.(starlark.String) | ||
Expect(strVal.GoString()).To(Equal(stringVal)) | ||
}, | ||
Entry("", "ListKind", "NodeList"), | ||
Entry("", "ResourceName", "nodes"), | ||
Entry("", "ResourceKind", "Node"), | ||
Entry("", "Namespace", ""), | ||
) | ||
|
||
Context("For Namespaced", func() { | ||
It(fmt.Sprintf("creates a dictionary with Namespaced value"), func() { | ||
dict := sr.ToStarlarkValue() | ||
val, err := dict.Attr("Namespaced") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
boolVal, _ := val.(starlark.Bool) | ||
Expect(boolVal.Truth()).To(Equal(starlark.False)) | ||
}) | ||
}) | ||
|
||
Context("For ResourceName", func() { | ||
Context("For List", func() { | ||
|
||
}) | ||
It("returns a starlark struct", func() { | ||
sr = searchResults[0] | ||
structVal := sr.ToStarlarkValue() | ||
Expect(structVal).To(BeAssignableToTypeOf(&starlarkstruct.Struct{})) | ||
|
||
Context("For ResourceKind", func() { | ||
val, err := structVal.Attr("List") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
}) | ||
_, ok := val.(*starlarkstruct.Struct) | ||
Expect(ok).To(BeTrue()) | ||
}) | ||
|
||
Context("For Namespaced", func() { | ||
It("contains a starlark struct with the Object key", func() { | ||
sr = searchResults[0] | ||
structVal := sr.ToStarlarkValue() | ||
val, _ := structVal.Attr("List") | ||
listVal, _ := val.(*starlarkstruct.Struct) | ||
|
||
}) | ||
objVal, err := listVal.Attr("Object") | ||
Expect(err).NotTo(HaveOccurred()) | ||
objStructVal, ok := objVal.(*starlarkstruct.Struct) | ||
Expect(ok).To(BeTrue()) | ||
Expect(objStructVal).To(BeAssignableToTypeOf(&starlarkstruct.Struct{})) | ||
}) | ||
|
||
It("contains a starlark list with the Items key", func() { | ||
sr = searchResults[0] | ||
structVal := sr.ToStarlarkValue() | ||
val, _ := structVal.Attr("List") | ||
listVal, _ := val.(*starlarkstruct.Struct) | ||
|
||
itemsVal, err := listVal.Attr("Items") | ||
Expect(err).NotTo(HaveOccurred()) | ||
itemsListVal, ok := itemsVal.(*starlark.List) | ||
Expect(ok).To(BeTrue()) | ||
Expect(itemsListVal).To(BeAssignableToTypeOf(&starlark.List{})) | ||
|
||
Expect(itemsListVal.Len()).To(Equal(1)) | ||
}) | ||
|
||
Context("For each list entry", func() { | ||
|
||
Context("For Namespace", func() { | ||
var listStructVal *starlarkstruct.Struct | ||
|
||
BeforeEach(func() { | ||
sr := searchResults[0] | ||
structVal := sr.ToStarlarkValue() | ||
val, _ := structVal.Attr("List") | ||
listVal, _ := val.(*starlarkstruct.Struct) | ||
itemsVal, _ := listVal.Attr("Items") | ||
itemsListVal, _ := itemsVal.(*starlark.List) | ||
listStructVal, _ = itemsListVal.Index(0).(*starlarkstruct.Struct) | ||
}) | ||
|
||
It("returns a starlark string for a string value", func() { | ||
kindAttrVal, err := listStructVal.Attr("kind") | ||
Expect(err).NotTo(HaveOccurred()) | ||
if kind, ok := kindAttrVal.(starlark.String); !ok { | ||
Expect(kind.GoString()).To(Equal("Service")) | ||
} else { | ||
Expect(ok).To(BeTrue()) | ||
} | ||
|
||
apiVersionVal, err := listStructVal.Attr("apiVersion") | ||
Expect(err).NotTo(HaveOccurred()) | ||
if version, ok := apiVersionVal.(starlark.String); ok { | ||
Expect(version.GoString()).To(Equal("v1")) | ||
} else { | ||
Expect(ok).To(BeTrue()) | ||
} | ||
}) | ||
|
||
It("returns a starlark struct for a map value", func() { | ||
metadataAttrVal, err := listStructVal.Attr("metadata") | ||
Expect(err).NotTo(HaveOccurred()) | ||
metadata, ok := metadataAttrVal.(*starlarkstruct.Struct) | ||
Expect(ok).To(BeTrue()) | ||
|
||
labelVal, err := metadata.Attr("labels") | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(labelVal).To(BeAssignableToTypeOf(&starlarkstruct.Struct{})) | ||
}) | ||
|
||
It("returns a starlark list for an array value", func() { | ||
specAttrVal, err := listStructVal.Attr("spec") | ||
Expect(err).NotTo(HaveOccurred()) | ||
spec, ok := specAttrVal.(*starlarkstruct.Struct) | ||
Expect(ok).To(BeTrue()) | ||
|
||
portsVal, err := spec.Attr("ports") | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(portsVal).To(BeAssignableToTypeOf(&starlark.List{})) | ||
|
||
ports, ok := portsVal.(*starlark.List) | ||
Expect(ok).To(BeTrue()) | ||
Expect(ports.Len()).To(Equal(3)) | ||
Expect(ports.Index(0)).To(BeAssignableToTypeOf(&starlarkstruct.Struct{})) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.