Merge pull request #2854 from monopole/inlineTypeMeta

Inline repeated types.
This commit is contained in:
Kubernetes Prow Robot
2020-08-18 10:39:11 -07:00
committed by GitHub
4 changed files with 80 additions and 45 deletions

View File

@@ -275,40 +275,67 @@ func TestCreatePathAnnotationValue(t *testing.T) {
}{
{
`dir`,
yaml.ResourceMeta{Kind: "foo",
APIVersion: "apps/v1",
ObjectMeta: yaml.ObjectMeta{Name: "bar", Namespace: "baz"},
yaml.ResourceMeta{
TypeMeta: yaml.TypeMeta{
APIVersion: "apps/v1",
Kind: "foo",
},
ObjectMeta: yaml.ObjectMeta{
NameMeta: yaml.NameMeta{
Name: "bar", Namespace: "baz",
},
},
},
`dir/baz/foo_bar.yaml`, `with namespace`,
},
{
``,
yaml.ResourceMeta{Kind: "foo",
APIVersion: "apps/v1",
ObjectMeta: yaml.ObjectMeta{Name: "bar", Namespace: "baz"},
yaml.ResourceMeta{
TypeMeta: yaml.TypeMeta{
APIVersion: "apps/v1",
Kind: "foo",
},
ObjectMeta: yaml.ObjectMeta{
NameMeta: yaml.NameMeta{
Name: "bar", Namespace: "baz",
},
},
},
`baz/foo_bar.yaml`, `without dir`,
},
{
`dir`,
yaml.ResourceMeta{Kind: "foo",
APIVersion: "apps/v1",
ObjectMeta: yaml.ObjectMeta{Name: "bar"},
yaml.ResourceMeta{
TypeMeta: yaml.TypeMeta{
APIVersion: "apps/v1",
Kind: "foo",
},
ObjectMeta: yaml.ObjectMeta{
NameMeta: yaml.NameMeta{Name: "bar"},
},
},
`dir/foo_bar.yaml`, `without namespace`,
},
{
``,
yaml.ResourceMeta{Kind: "foo",
APIVersion: "apps/v1",
ObjectMeta: yaml.ObjectMeta{Name: "bar"},
yaml.ResourceMeta{
TypeMeta: yaml.TypeMeta{
APIVersion: "apps/v1",
Kind: "foo",
},
ObjectMeta: yaml.ObjectMeta{
NameMeta: yaml.NameMeta{Name: "bar"},
},
},
`foo_bar.yaml`, `without namespace or dir`,
},
{
``,
yaml.ResourceMeta{Kind: "foo",
APIVersion: "apps/v1",
yaml.ResourceMeta{
TypeMeta: yaml.TypeMeta{
APIVersion: "apps/v1",
Kind: "foo",
},
ObjectMeta: yaml.ObjectMeta{},
},
`foo_.yaml`, `without namespace, dir or name`,
@@ -316,7 +343,9 @@ func TestCreatePathAnnotationValue(t *testing.T) {
{
``,
yaml.ResourceMeta{
APIVersion: "apps/v1",
TypeMeta: yaml.TypeMeta{
APIVersion: "apps/v1",
},
ObjectMeta: yaml.ObjectMeta{},
},
`_.yaml`, `without any`,

View File

@@ -698,11 +698,15 @@ metadata:
return
}
assert.Equal(t, ResourceMeta{
Kind: "Deployment",
APIVersion: "v1/apps",
TypeMeta: TypeMeta{
Kind: "Deployment",
APIVersion: "v1/apps",
},
ObjectMeta: ObjectMeta{
Name: "foo",
Namespace: "bar",
NameMeta: NameMeta{
Name: "foo",
Namespace: "bar",
},
Annotations: map[string]string{
"ka": "va",
},

View File

@@ -36,11 +36,15 @@ spec:
}
expected := ResourceMeta{
APIVersion: "rbac.istio.io/v1alpha1",
Kind: "ServiceRole",
TypeMeta: TypeMeta{
APIVersion: "rbac.istio.io/v1alpha1",
Kind: "ServiceRole",
},
ObjectMeta: ObjectMeta{
Name: "wildcard",
Namespace: "default",
NameMeta: NameMeta{
Name: "wildcard",
Namespace: "default",
},
Annotations: map[string]string{"foo": "bar"},
},
}

View File

@@ -88,27 +88,33 @@ func (f FilterFunc) Filter(object *RNode) (*RNode, error) {
return f(object)
}
// TypeMeta partially copies apimachinery/pkg/apis/meta/v1.TypeMeta
// No need for a direct dependence; the fields are stable.
type TypeMeta struct {
Kind string
APIVersion string
// APIVersion is the apiVersion field of a Resource
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
// Kind is the kind field of a Resource
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
}
// NameMeta contains name information.
type NameMeta struct {
// Name is the metadata.name field of a Resource
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// Namespace is the metadata.namespace field of a Resource
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
}
// ResourceMeta contains the metadata for a both Resource Type and Resource.
type ResourceMeta struct {
// APIVersion is the apiVersion field of a Resource
APIVersion string `yaml:"apiVersion,omitempty"`
// Kind is the kind field of a Resource
Kind string `yaml:"kind,omitempty"`
TypeMeta `json:",inline" yaml:",inline"`
// ObjectMeta is the metadata field of a Resource
ObjectMeta `yaml:"metadata,omitempty"`
}
// ObjectMeta contains metadata about a Resource
type ObjectMeta struct {
// Name is the metadata.name field of a Resource
Name string `yaml:"name,omitempty"`
// Namespace is the metadata.namespace field of a Resource
Namespace string `yaml:"namespace,omitempty"`
NameMeta `json:",inline" yaml:",inline"`
// Labels is the metadata.labels field of a Resource
Labels map[string]string `yaml:"labels,omitempty"`
// Annotations is the metadata.annotations field of a Resource.
@@ -119,24 +125,16 @@ type ObjectMeta struct {
// the information needed to uniquely identify a resource in a cluster.
func (m *ResourceMeta) GetIdentifier() ResourceIdentifier {
return ResourceIdentifier{
Name: m.Name,
Namespace: m.Namespace,
APIVersion: m.APIVersion,
Kind: m.Kind,
TypeMeta: m.TypeMeta,
NameMeta: m.NameMeta,
}
}
// ResourceIdentifier contains the information needed to uniquely
// identify a resource in a cluster.
type ResourceIdentifier struct {
// Name is the name of the resource as set in metadata.name
Name string `yaml:"name,omitempty"`
// Namespace is the namespace of the resource as set in metadata.namespace
Namespace string `yaml:"namespace,omitempty"`
// ApiVersion is the apiVersion of the resource
APIVersion string `yaml:"apiVersion,omitempty"`
// Kind is the kind of the resource
Kind string `yaml:"kind,omitempty"`
TypeMeta `json:",inline" yaml:",inline"`
NameMeta `json:",inline" yaml:",inline"`
}
// Comments struct is comment yaml comment types