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

@@ -19,13 +19,13 @@ package resource
import (
"strings"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/pkg/gvk"
)
// ResId conflates GroupVersionKind with a textual name to uniquely identify a kubernetes resource (object).
type ResId struct {
// GroupVersionKind of the resource.
gvk schema.GroupVersionKind
// Gvk of the resource.
gvKind gvk.Gvk
// original name of the resource before transformation.
name string
// namePrefix of the resource
@@ -39,45 +39,46 @@ type ResId struct {
}
// NewResIdWithPrefixNamespace creates new resource identifier with a prefix and a namespace
func NewResIdWithPrefixNamespace(g schema.GroupVersionKind, n, p, ns string) ResId {
return ResId{gvk: g, name: n, prefix: p, namespace: ns}
func NewResIdWithPrefixNamespace(k gvk.Gvk, n, p, ns string) ResId {
return ResId{gvKind: k, name: n, prefix: p, namespace: ns}
}
// NewResIdWithPrefix creates new resource identifier with a prefix
func NewResIdWithPrefix(g schema.GroupVersionKind, n, p string) ResId {
return ResId{gvk: g, name: n, prefix: p}
func NewResIdWithPrefix(k gvk.Gvk, n, p string) ResId {
return ResId{gvKind: k, name: n, prefix: p}
}
// NewResId creates new resource identifier
func NewResId(g schema.GroupVersionKind, n string) ResId {
return NewResIdWithPrefix(g, n, "")
func NewResId(k gvk.Gvk, n string) ResId {
return ResId{gvKind: k, name: n}
}
// NewResIdKindOnly creates new resource identifier
func NewResIdKindOnly(k string, n string) ResId {
return ResId{gvKind: gvk.FromKind(k), name: n}
}
// String of ResId based on GVK, name and prefix
func (n ResId) String() string {
fields := []string{n.gvk.Group, n.gvk.Version, n.gvk.Kind, n.namespace, n.prefix, n.name}
fields := []string{n.gvKind.Group, n.gvKind.Version, n.gvKind.Kind,
n.namespace, n.prefix, n.name}
return strings.Join(fields, "_") + ".yaml"
}
// GvknString of ResId based on GVK and name
func (n ResId) GvknString() string {
if n.gvk.Group == "" {
return strings.Join([]string{n.gvk.Version, n.gvk.Kind, n.name}, "_") + ".yaml"
}
return strings.Join([]string{n.gvk.Group, n.gvk.Version, n.gvk.Kind, n.name}, "_") + ".yaml"
return n.gvKind.String() + "_" + n.name + ".yaml"
}
// GvknEquals return if two ResId have the same Group/Version/Kind and name
// The comparison excludes prefix
func (n ResId) GvknEquals(id ResId) bool {
return n.gvk.Group == id.gvk.Group && n.gvk.Version == id.gvk.Version &&
n.gvk.Kind == id.gvk.Kind && n.name == id.name
return n.gvKind.Equals(id.gvKind) && n.name == id.name
}
// Gvk returns Group/Version/Kind of the resource.
func (n ResId) Gvk() schema.GroupVersionKind {
return n.gvk
func (n ResId) Gvk() gvk.Gvk {
return n.gvKind
}
// Name returns resource name.
@@ -97,12 +98,12 @@ func (n ResId) Namespace() string {
// CopyWithNewPrefix make a new copy from current ResId and append a new prefix
func (n ResId) CopyWithNewPrefix(p string) ResId {
return ResId{gvk: n.gvk, name: n.name, prefix: n.concatPrefix(p), namespace: n.namespace}
return ResId{gvKind: n.gvKind, name: n.name, prefix: n.concatPrefix(p), namespace: n.namespace}
}
// CopyWithNewNamespace make a new copy from current ResId and set a new namespace
func (n ResId) CopyWithNewNamespace(ns string) ResId {
return ResId{gvk: n.gvk, name: n.name, prefix: n.prefix, namespace: ns}
return ResId{gvKind: n.gvKind, name: n.name, prefix: n.prefix, namespace: ns}
}
// HasSameLeftmostPrefix check if two ResIds have the same

View File

@@ -3,26 +3,26 @@ package resource
import (
"testing"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/pkg/gvk"
)
var stringTests = []struct {
x ResId
s string
}{
{ResId{gvk: schema.GroupVersionKind{Group: "g", Version: "v", Kind: "k"},
{ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
name: "nm", prefix: "p", namespace: "ns"}, "g_v_k_ns_p_nm.yaml"},
{ResId{gvk: schema.GroupVersionKind{Version: "v", Kind: "k"},
{ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"},
name: "nm", prefix: "p", namespace: "ns"}, "_v_k_ns_p_nm.yaml"},
{ResId{gvk: schema.GroupVersionKind{Kind: "k"},
{ResId{gvKind: gvk.Gvk{Kind: "k"},
name: "nm", prefix: "p", namespace: "ns"}, "__k_ns_p_nm.yaml"},
{ResId{gvk: schema.GroupVersionKind{},
{ResId{gvKind: gvk.Gvk{},
name: "nm", prefix: "p", namespace: "ns"}, "___ns_p_nm.yaml"},
{ResId{gvk: schema.GroupVersionKind{},
{ResId{gvKind: gvk.Gvk{},
name: "nm", prefix: "p"}, "____p_nm.yaml"},
{ResId{gvk: schema.GroupVersionKind{},
{ResId{gvKind: gvk.Gvk{},
name: "nm"}, "_____nm.yaml"},
{ResId{gvk: schema.GroupVersionKind{}}, "_____.yaml"},
{ResId{gvKind: gvk.Gvk{}}, "_____.yaml"},
{ResId{}, "_____.yaml"},
}
@@ -38,19 +38,19 @@ var gvknStringTests = []struct {
x ResId
s string
}{
{ResId{gvk: schema.GroupVersionKind{Group: "g", Version: "v", Kind: "k"},
{ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
name: "nm", prefix: "p", namespace: "ns"}, "g_v_k_nm.yaml"},
{ResId{gvk: schema.GroupVersionKind{Version: "v", Kind: "k"},
{ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"},
name: "nm", prefix: "p", namespace: "ns"}, "v_k_nm.yaml"},
{ResId{gvk: schema.GroupVersionKind{Kind: "k"},
{ResId{gvKind: gvk.Gvk{Kind: "k"},
name: "nm", prefix: "p", namespace: "ns"}, "_k_nm.yaml"},
{ResId{gvk: schema.GroupVersionKind{},
{ResId{gvKind: gvk.Gvk{},
name: "nm", prefix: "p", namespace: "ns"}, "__nm.yaml"},
{ResId{gvk: schema.GroupVersionKind{},
{ResId{gvKind: gvk.Gvk{},
name: "nm", prefix: "p"}, "__nm.yaml"},
{ResId{gvk: schema.GroupVersionKind{},
{ResId{gvKind: gvk.Gvk{},
name: "nm"}, "__nm.yaml"},
{ResId{gvk: schema.GroupVersionKind{}}, "__.yaml"},
{ResId{gvKind: gvk.Gvk{}}, "__.yaml"},
{ResId{}, "__.yaml"},
}
@@ -66,17 +66,17 @@ var GvknEqualsTest = []struct {
x1 ResId
x2 ResId
}{
{ResId{gvk: schema.GroupVersionKind{Group: "g", Version: "v", Kind: "k"},
{ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
name: "nm", prefix: "AA", namespace: "X"},
ResId{gvk: schema.GroupVersionKind{Group: "g", Version: "v", Kind: "k"},
ResId{gvKind: gvk.Gvk{Group: "g", Version: "v", Kind: "k"},
name: "nm", prefix: "BB", namespace: "Z"}},
{ResId{gvk: schema.GroupVersionKind{Version: "v", Kind: "k"},
{ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"},
name: "nm", prefix: "AA", namespace: "X"},
ResId{gvk: schema.GroupVersionKind{Version: "v", Kind: "k"},
ResId{gvKind: gvk.Gvk{Version: "v", Kind: "k"},
name: "nm", prefix: "BB", namespace: "Z"}},
{ResId{gvk: schema.GroupVersionKind{Kind: "k"},
{ResId{gvKind: gvk.Gvk{Kind: "k"},
name: "nm", prefix: "AA", namespace: "X"},
ResId{gvk: schema.GroupVersionKind{Kind: "k"},
ResId{gvKind: gvk.Gvk{Kind: "k"},
name: "nm", prefix: "BB", namespace: "Z"}},
{ResId{name: "nm", prefix: "AA", namespace: "X"},
ResId{name: "nm", prefix: "BB", namespace: "Z"}},

View File

@@ -20,12 +20,12 @@ package resource
import (
"encoding/json"
"fmt"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/kustomize/pkg/gvk"
)
// Resource is an "Unstructured" (json/map form) Kubernetes API resource object
@@ -77,7 +77,7 @@ func (r *Resource) IsGenerated() bool {
// Id returns the ResId for the resource.
func (r *Resource) Id() ResId {
return NewResId(r.GroupVersionKind(), r.GetName())
return NewResId(gvk.FromSchemaGvk(r.GroupVersionKind()), r.GetName())
}
// Merge performs merge with other resource.