Skip to content

Commit

Permalink
handle generateName in NameMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
julian7 committed Oct 20, 2022
1 parent bf7a0f9 commit 09541f2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
19 changes: 10 additions & 9 deletions kyaml/yaml/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ const (

// Field names
const (
AnnotationsField = "annotations"
APIVersionField = "apiVersion"
KindField = "kind"
MetadataField = "metadata"
DataField = "data"
BinaryDataField = "binaryData"
NameField = "name"
NamespaceField = "namespace"
LabelsField = "labels"
AnnotationsField = "annotations"
APIVersionField = "apiVersion"
KindField = "kind"
MetadataField = "metadata"
DataField = "data"
BinaryDataField = "binaryData"
NameField = "name"
GenerateNameField = "generateName"
NamespaceField = "namespace"
LabelsField = "labels"
)
6 changes: 5 additions & 1 deletion kyaml/yaml/rnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ func (rn *RNode) GetMeta() (ResourceMeta, error) {
m.Name = f.Value.YNode().Value
missingMeta = false
}
if f := meta.Field(GenerateNameField); !f.IsNilOrEmpty() {
m.GenerateName = f.Value.YNode().Value
missingMeta = false
}
if f := meta.Field(NamespaceField); !f.IsNilOrEmpty() {
m.Namespace = GetValue(f.Value)
missingMeta = false
Expand Down Expand Up @@ -1071,7 +1075,7 @@ func (rn *RNode) GetValidatedMetadata() (ResourceMeta, error) {
// A list doesn't require a name.
return m, nil
}
if m.NameMeta.Name == "" {
if m.NameMeta.Name == "" && m.NameMeta.GenerateName == "" {
return m, fmt.Errorf("missing metadata.name in object %v", m)
}
return m, nil
Expand Down
22 changes: 22 additions & 0 deletions kyaml/yaml/rnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,28 @@ func TestRNodeGetValidatedMetadata(t *testing.T) {
errMsg: "missing metadata.name",
},
},
"generateNameConfigMap": {
theMap: map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"generateName": "winnie-",
},
},
rsExp: resultExpected{
out: ResourceMeta{
TypeMeta: TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: ObjectMeta{
NameMeta: NameMeta{
GenerateName: "winnie-",
},
},
},
},
},
"configmap": {
theMap: testConfigMap,
rsExp: resultExpected{
Expand Down
2 changes: 2 additions & 0 deletions kyaml/yaml/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ type TypeMeta struct {
type NameMeta struct {
// Name is the metadata.name field of a Resource
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// GenerateName is the metadata.generateName field of a Resource if metadata.name is not set
GenerateName string `json:"generateName,omitempty" yaml:"generateName,omitempty"`
// Namespace is the metadata.namespace field of a Resource
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
}
Expand Down

0 comments on commit 09541f2

Please sign in to comment.