mirror of https://github.com/fluxcd/cli-utils.git
Order by group kind
Taking group into account ensures unrelated resources with the same kind are ordered after the important ones
This commit is contained in:
parent
c1a7c2d040
commit
82165ae2c7
|
@ -64,39 +64,41 @@ func less(i, j object.ObjMetadata) bool {
|
|||
return i.Name < j.Name
|
||||
}
|
||||
|
||||
var kind2index = computeKind2index()
|
||||
var groupKind2index = computeGroupKind2index()
|
||||
|
||||
func computeKind2index() map[string]int {
|
||||
func computeGroupKind2index() map[schema.GroupKind]int {
|
||||
// An attempt to order things to help k8s, e.g.
|
||||
// a Service should come before things that refer to it.
|
||||
// Namespace should be first.
|
||||
// In some cases order just specified to provide determinism.
|
||||
orderFirst := []string{
|
||||
"Namespace",
|
||||
"ResourceQuota",
|
||||
"StorageClass",
|
||||
"CustomResourceDefinition",
|
||||
"MutatingWebhookConfiguration",
|
||||
"ServiceAccount",
|
||||
"PodSecurityPolicy",
|
||||
"Role",
|
||||
"ClusterRole",
|
||||
"RoleBinding",
|
||||
"ClusterRoleBinding",
|
||||
"ConfigMap",
|
||||
"Secret",
|
||||
"Service",
|
||||
"LimitRange",
|
||||
"PriorityClass",
|
||||
"Deployment",
|
||||
"StatefulSet",
|
||||
"CronJob",
|
||||
"PodDisruptionBudget",
|
||||
orderFirst := []schema.GroupKind{
|
||||
{Group: "", Kind: "Namespace"},
|
||||
{Group: "", Kind: "ResourceQuota"},
|
||||
{Group: "storage.k8s.io", Kind: "StorageClass"},
|
||||
{Group: "apiextensions.k8s.io", Kind: "CustomResourceDefinition"},
|
||||
{Group: "admissionregistration.k8s.io", Kind: "MutatingWebhookConfiguration"},
|
||||
{Group: "", Kind: "ServiceAccount"},
|
||||
{Group: "extensions", Kind: "PodSecurityPolicy"}, // deprecated=1.11, removed=1.16
|
||||
{Group: "policy", Kind: "PodSecurityPolicy"}, // deprecated=1.21, removed=1.25
|
||||
{Group: "rbac.authorization.k8s.io", Kind: "Role"},
|
||||
{Group: "rbac.authorization.k8s.io", Kind: "ClusterRole"},
|
||||
{Group: "rbac.authorization.k8s.io", Kind: "RoleBinding"},
|
||||
{Group: "rbac.authorization.k8s.io", Kind: "ClusterRoleBinding"},
|
||||
{Group: "", Kind: "ConfigMap"},
|
||||
{Group: "", Kind: "Secret"},
|
||||
{Group: "", Kind: "Service"},
|
||||
{Group: "", Kind: "LimitRange"},
|
||||
{Group: "scheduling.k8s.io", Kind: "PriorityClass"},
|
||||
{Group: "extensions", Kind: "Deployment"}, // deprecated=1.8, removed=1.16
|
||||
{Group: "apps", Kind: "Deployment"},
|
||||
{Group: "apps", Kind: "StatefulSet"},
|
||||
{Group: "batch", Kind: "CronJob"},
|
||||
{Group: "policy", Kind: "PodDisruptionBudget"},
|
||||
}
|
||||
orderLast := []string{
|
||||
"ValidatingWebhookConfiguration",
|
||||
orderLast := []schema.GroupKind{
|
||||
{Group: "admissionregistration.k8s.io", Kind: "ValidatingWebhookConfiguration"},
|
||||
}
|
||||
kind2indexResult := make(map[string]int, len(orderFirst)+len(orderLast))
|
||||
kind2indexResult := make(map[schema.GroupKind]int, len(orderFirst)+len(orderLast))
|
||||
for i, n := range orderFirst {
|
||||
kind2indexResult[n] = -len(orderFirst) + i
|
||||
}
|
||||
|
@ -106,18 +108,13 @@ func computeKind2index() map[string]int {
|
|||
return kind2indexResult
|
||||
}
|
||||
|
||||
// getIndexByKind returns the index of the kind respecting the order
|
||||
func getIndexByKind(kind string) int {
|
||||
return kind2index[kind]
|
||||
}
|
||||
|
||||
func Equals(i, j schema.GroupKind) bool {
|
||||
return i.Group == j.Group && i.Kind == j.Kind
|
||||
}
|
||||
|
||||
func IsLessThan(i, j schema.GroupKind) bool {
|
||||
indexI := getIndexByKind(i.Kind)
|
||||
indexJ := getIndexByKind(j.Kind)
|
||||
indexI := groupKind2index[i]
|
||||
indexJ := groupKind2index[j]
|
||||
if indexI != indexJ {
|
||||
return indexI < indexJ
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"sort"
|
||||
"testing"
|
||||
|
||||
"gotest.tools/assert"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/cli-runtime/pkg/resource"
|
||||
|
@ -36,7 +36,7 @@ var namespaceObj = unstructured.Unstructured{
|
|||
|
||||
var deploymentObj = unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "v1",
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "testdeployment",
|
||||
|
@ -47,7 +47,7 @@ var deploymentObj = unstructured.Unstructured{
|
|||
|
||||
var deploymentObj2 = unstructured.Unstructured{
|
||||
Object: map[string]interface{}{
|
||||
"apiVersion": "v1",
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": map[string]interface{}{
|
||||
"name": "testdeployment2",
|
||||
|
@ -93,7 +93,7 @@ func TestResourceOrdering(t *testing.T) {
|
|||
|
||||
func TestGvkLessThan(t *testing.T) {
|
||||
gk1 := schema.GroupKind{
|
||||
Group: "",
|
||||
Group: "apps",
|
||||
Kind: "Deployment",
|
||||
}
|
||||
|
||||
|
@ -102,19 +102,19 @@ func TestGvkLessThan(t *testing.T) {
|
|||
Kind: "Namespace",
|
||||
}
|
||||
|
||||
assert.Equal(t, IsLessThan(gk1, gk2), false)
|
||||
assert.False(t, IsLessThan(gk1, gk2))
|
||||
}
|
||||
|
||||
func TestGvkEquals(t *testing.T) {
|
||||
gk1 := schema.GroupKind{
|
||||
Group: "",
|
||||
Group: "apps",
|
||||
Kind: "Deployment",
|
||||
}
|
||||
|
||||
gk2 := schema.GroupKind{
|
||||
Group: "",
|
||||
Group: "apps",
|
||||
Kind: "Deployment",
|
||||
}
|
||||
|
||||
assert.Equal(t, Equals(gk1, gk2), true)
|
||||
assert.True(t, Equals(gk1, gk2))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue