Introduce gvk package to isolate apimachinery schema.

This commit is contained in:
Jeffrey Regan
2018-09-25 16:57:31 -07:00
parent 8aa0cc145c
commit fb355eb320
38 changed files with 570 additions and 477 deletions

View File

@@ -20,15 +20,15 @@ import (
"reflect"
"testing"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/pkg/configmapandsecret"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/types"
)
var cmap = schema.GroupVersionKind{Version: "v1", Kind: "ConfigMap"}
var cmap = gvk.Gvk{Version: "v1", Kind: "ConfigMap"}
func TestNewFromConfigMaps(t *testing.T) {
type testCase struct {

View File

@@ -19,7 +19,6 @@ package resmap
import (
"sort"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/pkg/resource"
)
@@ -31,33 +30,8 @@ var _ sort.Interface = IdSlice{}
func (a IdSlice) Len() int { return len(a) }
func (a IdSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a IdSlice) Less(i, j int) bool {
if a[i].Gvk().String() != a[j].Gvk().String() {
return gvkLess(a[i].Gvk(), a[j].Gvk())
if !a[i].Gvk().Equals(a[j].Gvk()) {
return a[i].Gvk().IsLessThan(a[j].Gvk())
}
return a[i].String() < a[j].String()
}
var order = []string{"Namespace", "CustomResourceDefinition", "ServiceAccount",
"Role", "ClusterRole", "RoleBinding", "ClusterRoleBinding"}
var typeOrders = func() map[string]int {
m := map[string]int{}
for i, n := range order {
m[n] = i
}
return m
}()
func gvkLess(i, j schema.GroupVersionKind) bool {
indexi, foundi := typeOrders[i.Kind]
indexj, foundj := typeOrders[j.Kind]
if foundi && foundj {
return indexi < indexj
}
if foundi && !foundj {
return true
}
if !foundi && foundj {
return false
}
return i.String() < j.String()
}

View File

@@ -21,30 +21,29 @@ import (
"sort"
"testing"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/pkg/resource"
)
func TestLess(t *testing.T) {
ids := IdSlice{
resource.NewResId(schema.GroupVersionKind{Kind: "ConfigMap"}, "cm"),
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"),
resource.NewResIdKindOnly("ConfigMap", "cm"),
resource.NewResIdKindOnly("Pod", "pod"),
resource.NewResIdKindOnly("Namespace", "ns1"),
resource.NewResIdKindOnly("Namespace", "ns2"),
resource.NewResIdKindOnly("Role", "ro"),
resource.NewResIdKindOnly("RoleBinding", "rb"),
resource.NewResIdKindOnly("CustomResourceDefinition", "crd"),
resource.NewResIdKindOnly("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"),
resource.NewResIdKindOnly("Namespace", "ns1"),
resource.NewResIdKindOnly("Namespace", "ns2"),
resource.NewResIdKindOnly("CustomResourceDefinition", "crd"),
resource.NewResIdKindOnly("ServiceAccount", "sa"),
resource.NewResIdKindOnly("Role", "ro"),
resource.NewResIdKindOnly("RoleBinding", "rb"),
resource.NewResIdKindOnly("ConfigMap", "cm"),
resource.NewResIdKindOnly("Pod", "pod"),
}
sort.Sort(ids)
if !reflect.DeepEqual(ids, expected) {

View File

@@ -30,6 +30,7 @@ import (
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/kustomize/pkg/gvk"
internal "sigs.k8s.io/kustomize/pkg/internal/error"
"sigs.k8s.io/kustomize/pkg/loader"
"sigs.k8s.io/kustomize/pkg/patch"
@@ -129,11 +130,12 @@ func (m ResMap) DeepCopy() ResMap {
func (m ResMap) insert(newName string, obj *unstructured.Unstructured) error {
oldName := obj.GetName()
gvk := obj.GroupVersionKind()
id := resource.NewResId(gvk, oldName)
gvKind := gvk.FromSchemaGvk(obj.GroupVersionKind())
id := resource.NewResId(gvKind, oldName)
if _, found := m[id]; found {
return fmt.Errorf("the <name: %q, GroupVersionKind: %v> already exists in the map", oldName, gvk)
return fmt.Errorf(
"the <name: %q, GroupVersionKind: %v> already exists in the map", oldName, gvKind)
}
obj.SetName(newName)
m[id] = resource.NewResourceFromUnstruct(*obj)

View File

@@ -21,13 +21,13 @@ import (
"reflect"
"testing"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/internal/loadertest"
"sigs.k8s.io/kustomize/pkg/resource"
)
var deploy = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "Deployment"}
var statefulset = schema.GroupVersionKind{Group: "apps", Version: "v1", Kind: "StatefulSet"}
var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}
var statefulset = gvk.Gvk{Group: "apps", Version: "v1", Kind: "StatefulSet"}
func TestEncodeAsYaml(t *testing.T) {
encoded := []byte(`apiVersion: v1

View File

@@ -22,14 +22,14 @@ import (
"testing"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/pkg/configmapandsecret"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/resource"
"sigs.k8s.io/kustomize/pkg/types"
)
var secret = schema.GroupVersionKind{Version: "v1", Kind: "Secret"}
var secret = gvk.Gvk{Version: "v1", Kind: "Secret"}
func TestNewResMapFromSecretArgs(t *testing.T) {
secrets := []types.SecretArgs{