Skip to content

Commit

Permalink
fix: Use generateName as last comparison when sorting (#1031)
Browse files Browse the repository at this point in the history
  • Loading branch information
zerok authored May 27, 2024
1 parent 7716fe6 commit 63cfe63
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
8 changes: 8 additions & 0 deletions pkg/kubernetes/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ func (m Metadata) Name() string {
return name.(string)
}

func (m Metadata) GenerateName() string {
generateName, ok := m["generateName"]
if !ok {
return ""
}
return generateName.(string)
}

// HasNamespace returns whether the manifest has a namespace set
func (m Metadata) HasNamespace() bool {
return m2o(m).Get("namespace").IsStr()
Expand Down
7 changes: 5 additions & 2 deletions pkg/process/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ func Sort(list manifest.List) {
return list[i].Metadata().Namespace() < list[j].Metadata().Namespace()
}

// Otherwise, order the objects by name.
return list[i].Metadata().Name() < list[j].Metadata().Name()
if list[i].Metadata().Name() != list[j].Metadata().Name() {
return list[i].Metadata().Name() < list[j].Metadata().Name()
}

return list[i].Metadata().GenerateName() < list[j].Metadata().GenerateName()
})
}
19 changes: 19 additions & 0 deletions pkg/process/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ func TestSort(t *testing.T) {
mkobj("CustomResourceDefinition", "crd2", ""),
},
},
{
// sorting by the generate name prefix if everything else is the same
raw: manifest.List{
mkGenerateObj("CustomResourceDefinition", "crd2-", ""),
mkGenerateObj("CustomResourceDefinition", "crd-", ""),
mkGenerateObj("CustomResourceDefinition", "crd1-", ""),
},
state: manifest.List{
mkGenerateObj("CustomResourceDefinition", "crd-", ""),
mkGenerateObj("CustomResourceDefinition", "crd1-", ""),
mkGenerateObj("CustomResourceDefinition", "crd2-", ""),
},
},
{
raw: manifest.List{
mkobj("Deployment", "b", "a"),
Expand Down Expand Up @@ -130,3 +143,9 @@ func mkobj(kind string, name string, ns string) map[string]interface{} {

return ret
}

func mkGenerateObj(kind string, generateName string, ns string) map[string]interface{} {
result := mkobj(kind, "", ns)
result["metadata"].(map[string]interface{})["generateName"] = generateName
return result
}

0 comments on commit 63cfe63

Please sign in to comment.