Skip to content

Commit

Permalink
correct ordering of the k8s objects:
Browse files Browse the repository at this point in the history
NameSpace, CRD, ServiceAccount, Role, ClusterRole, RoleBinding,
ClusterRoleBinding
  • Loading branch information
Liujingfang1 committed Jun 21, 2018
1 parent dec5109 commit 90d16c2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
23 changes: 19 additions & 4 deletions pkg/resmap/idslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,27 @@ func (a IdSlice) Less(i, j int) bool {
return a[i].Name() < a[j].Name()
}

var typeOrders = map[string]int{
"Namespace": 0,
"CustomResourceDefinition": 1,
"ServiceAccount": 2,
"Role": 3,
"ClusterRole": 4,
"RoleBinding": 5,
"ClusterRoleBinding": 6,
}

func gvkLess(i, j schema.GroupVersionKind) bool {
if i.Kind == "Namespace" {
indexi, foundi := typeOrders[i.Kind]
indexj, foundj := typeOrders[j.Kind]
if foundi && foundj {
return indexi < indexj
}
if foundi && !foundj {
return true
} else if j.Kind == "Namespace" {
}
if !foundi && foundj {
return false
} else {
return i.String() < j.String()
}
return i.String() < j.String()
}
8 changes: 8 additions & 0 deletions pkg/resmap/idslice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ func TestLess(t *testing.T) {
resource.NewResId(schema.GroupVersionKind{Kind: "Pod"}, "pod"),
resource.NewResId(schema.GroupVersionKind{Kind: "Namespace"}, "ns1"),
resource.NewResId(schema.GroupVersionKind{Kind: "Namespace"}, "ns2"),
resource.NewResId(schema.GroupVersionKind{Kind: "Role"}, "ro"),
resource.NewResId(schema.GroupVersionKind{Kind: "RoleBinding"}, "rb"),
resource.NewResId(schema.GroupVersionKind{Kind: "CustomResourceDefinition"}, "crd"),
resource.NewResId(schema.GroupVersionKind{Kind: "ServiceAccount"}, "sa"),
}
expected := IdSlice{
resource.NewResId(schema.GroupVersionKind{Kind: "Namespace"}, "ns1"),
resource.NewResId(schema.GroupVersionKind{Kind: "Namespace"}, "ns2"),
resource.NewResId(schema.GroupVersionKind{Kind: "CustomResourceDefinition"}, "crd"),
resource.NewResId(schema.GroupVersionKind{Kind: "ServiceAccount"}, "sa"),
resource.NewResId(schema.GroupVersionKind{Kind: "Role"}, "ro"),
resource.NewResId(schema.GroupVersionKind{Kind: "RoleBinding"}, "rb"),
resource.NewResId(schema.GroupVersionKind{Kind: "ConfigMap"}, "cm"),
resource.NewResId(schema.GroupVersionKind{Kind: "Pod"}, "pod"),
}
Expand Down

0 comments on commit 90d16c2

Please sign in to comment.