Skip to content

Commit

Permalink
on Universal get .Name if display-name label is not set
Browse files Browse the repository at this point in the history
Signed-off-by: Ilya Lobkov <[email protected]>
  • Loading branch information
lobkovilya committed Jan 7, 2024
1 parent c47e371 commit 024badc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
10 changes: 10 additions & 0 deletions pkg/core/resources/model/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,16 @@ func IsLocallyOriginated(mode config_core.CpMode, r Resource) bool {
}
}

func GetDisplayName(r Resource) string {
// prefer display name as it's more predictable, because
// * Kubernetes expects sorting to be by just a name. Considering suffix with namespace breaks this
// * When policies are synced to Zone, hash suffix also breaks sorting
if labels := r.GetMeta().GetLabels(); labels != nil && labels[mesh_proto.DisplayName] != "" {
return labels[mesh_proto.DisplayName]
}
return r.GetMeta().GetName()
}

func equalNames(mesh, n1, n2 string) bool {
// instead of dragging the info if Zone is federated or not we can simply check 3 possible combinations
return n1 == n2 || hash.HashedName(mesh, n1) == n2 || hash.HashedName(mesh, n2) == n1
Expand Down
2 changes: 1 addition & 1 deletion pkg/kds/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func HashSuffixMapper(checkKDSFeature bool, labelsToUse ...string) reconcile.Res
return r, nil
}

name := r.GetMeta().GetLabels()[mesh_proto.DisplayName]
name := core_model.GetDisplayName(r)
values := make([]string, 0, len(labelsToUse))
for _, lbl := range labelsToUse {
values = append(values, r.GetMeta().GetLabels()[lbl])
Expand Down
12 changes: 1 addition & 11 deletions pkg/plugins/policies/core/matchers/dataplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,7 @@ func (b ByTargetRef) Less(i, j int) bool {
}
}

return nameToCompare(b[i].GetMeta()) > nameToCompare(b[j].GetMeta())
return core_model.GetDisplayName(b[i]) > core_model.GetDisplayName(b[j])
}

func (b ByTargetRef) Swap(i, j int) { b[i], b[j] = b[j], b[i] }

func nameToCompare(meta core_model.ResourceMeta) string {
// prefer display name as it's more predictable, because
// * Kubernetes expects sorting to be by just a name. Considering suffix with namespace breaks this
// * When policies are synced to Zone, hash suffix also breaks sorting
if labels := meta.GetLabels(); labels != nil && labels[mesh_proto.DisplayName] != "" {
return labels[mesh_proto.DisplayName]
}
return meta.GetName()
}

0 comments on commit 024badc

Please sign in to comment.