Skip to content

Commit

Permalink
Merge pull request #2132 from seans3/inventory-set
Browse files Browse the repository at this point in the history
Adds new helper function retrieveGroupingLabel()
  • Loading branch information
k8s-ci-robot authored Jan 21, 2020
2 parents 5353db3 + 3623d92 commit 758d428
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
30 changes: 23 additions & 7 deletions cmd/kubectl/kubectlcobra/grouping.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,36 @@ const (
GroupingHash = "kustomize.config.k8s.io/inventory-hash"
)

// retrieveGroupingLabel returns the string value of the GroupingLabel
// for the passed object. Returns error if the passed object is nil or
// is not a grouping object.
func retrieveGroupingLabel(obj runtime.Object) (string, error) {
var groupingLabel string
if obj == nil {
return "", fmt.Errorf("Grouping object is nil.\n")
}
accessor, err := meta.Accessor(obj)
if err != nil {
return "", err
}
labels := accessor.GetLabels()
groupingLabel, exists := labels[GroupingLabel]
if !exists {
return "", fmt.Errorf("Grouping label does not exist for grouping object: %s\n", GroupingLabel)
}
return strings.TrimSpace(groupingLabel), nil
}

// isGroupingObject returns true if the passed object has the
// grouping label.
// TODO(seans3): Check type is ConfigMap.
func isGroupingObject(obj runtime.Object) bool {
if obj == nil {
return false
}
accessor, err := meta.Accessor(obj)
if err == nil {
labels := accessor.GetLabels()
_, exists := labels[GroupingLabel]
if exists {
return true
}
groupingLabel, err := retrieveGroupingLabel(obj)
if err == nil && len(groupingLabel) > 0 {
return true
}
return false
}
Expand Down
65 changes: 64 additions & 1 deletion cmd/kubectl/kubectlcobra/grouping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var pod1Name = "pod-1"
var pod2Name = "pod-2"
var pod3Name = "pod-3"

var testGroupingLabel = "test-app-label"

var groupingObj = unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
Expand All @@ -30,7 +32,7 @@ var groupingObj = unstructured.Unstructured{
"name": groupingObjName,
"namespace": testNamespace,
"labels": map[string]interface{}{
GroupingLabel: "true",
GroupingLabel: testGroupingLabel,
},
},
},
Expand Down Expand Up @@ -109,6 +111,67 @@ var nilInfo = &resource.Info{
Object: nil,
}

var groupingObjLabelWithSpace = unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": groupingObjName,
"namespace": testNamespace,
"labels": map[string]interface{}{
GroupingLabel: "\tgrouping-label ",
},
},
},
}

func TestRetrieveGroupingLabel(t *testing.T) {
tests := []struct {
obj runtime.Object
groupingLabel string
isError bool
}{
// Nil grouping object throws error.
{
obj: nil,
groupingLabel: "",
isError: true,
},
// Pod is not a grouping object.
{
obj: &pod2,
groupingLabel: "",
isError: true,
},
// Retrieves label without preceding/trailing whitespace.
{
obj: &groupingObjLabelWithSpace,
groupingLabel: "grouping-label",
isError: false,
},
{
obj: &groupingObj,
groupingLabel: testGroupingLabel,
isError: false,
},
}

for _, test := range tests {
actual, err := retrieveGroupingLabel(test.obj)
if test.isError && err == nil {
t.Errorf("Did not receive expected error.\n")
}
if !test.isError {
if err != nil {
t.Fatalf("Received unexpected error: %s\n", err)
}
if test.groupingLabel != actual {
t.Errorf("Expected grouping label (%s), got (%s)\n", test.groupingLabel, actual)
}
}
}
}

func TestIsGroupingObject(t *testing.T) {
tests := []struct {
obj runtime.Object
Expand Down

0 comments on commit 758d428

Please sign in to comment.