2023-05-09 18:02:24 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-09 18:02:24 +00:00
|
|
|
|
|
|
|
package resource
|
|
|
|
|
|
|
|
import "github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
|
|
|
|
// EqualType compares two resource types for equality without reflection.
|
|
|
|
func EqualType(a, b *pbresource.Type) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.Group == b.Group &&
|
|
|
|
a.GroupVersion == b.GroupVersion &&
|
|
|
|
a.Kind == b.Kind
|
|
|
|
}
|
|
|
|
|
2023-08-18 00:03:05 +00:00
|
|
|
// EqualTenancy compares two resource tenancies for equality without reflection.
|
2023-05-09 18:02:24 +00:00
|
|
|
func EqualTenancy(a, b *pbresource.Tenancy) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.Partition == b.Partition &&
|
|
|
|
a.Namespace == b.Namespace
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:08:31 +00:00
|
|
|
// TODO(peering/v2) add equality method peer tenancy
|
|
|
|
|
2023-08-18 00:03:05 +00:00
|
|
|
// EqualID compares two resource IDs for equality without reflection.
|
2023-05-09 18:02:24 +00:00
|
|
|
func EqualID(a, b *pbresource.ID) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return EqualType(a.Type, b.Type) &&
|
|
|
|
EqualTenancy(a.Tenancy, b.Tenancy) &&
|
|
|
|
a.Name == b.Name &&
|
|
|
|
a.Uid == b.Uid
|
|
|
|
}
|
|
|
|
|
|
|
|
// EqualStatus compares two statuses for equality without reflection.
|
2023-05-10 09:37:54 +00:00
|
|
|
//
|
|
|
|
// Pass true for compareUpdatedAt to compare the UpdatedAt timestamps, which you
|
|
|
|
// generally *don't* want when dirty checking the status in a controller.
|
|
|
|
func EqualStatus(a, b *pbresource.Status, compareUpdatedAt bool) bool {
|
2023-05-09 18:02:24 +00:00
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.ObservedGeneration != b.ObservedGeneration {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-10 09:37:54 +00:00
|
|
|
if compareUpdatedAt && !a.UpdatedAt.AsTime().Equal(b.UpdatedAt.AsTime()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:02:24 +00:00
|
|
|
if len(a.Conditions) != len(b.Conditions) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, ac := range a.Conditions {
|
|
|
|
bc := b.Conditions[i]
|
|
|
|
|
|
|
|
if !EqualCondition(ac, bc) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// EqualCondition compares two conditions for equality without reflection.
|
|
|
|
func EqualCondition(a, b *pbresource.Condition) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.Type == b.Type &&
|
|
|
|
a.State == b.State &&
|
|
|
|
a.Reason == b.Reason &&
|
|
|
|
a.Message == b.Message &&
|
|
|
|
EqualReference(a.Resource, b.Resource)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EqualReference compares two references for equality without reflection.
|
|
|
|
func EqualReference(a, b *pbresource.Reference) bool {
|
|
|
|
if a == b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return EqualType(a.Type, b.Type) &&
|
|
|
|
EqualTenancy(a.Tenancy, b.Tenancy) &&
|
|
|
|
a.Name == b.Name &&
|
|
|
|
a.Section == b.Section
|
|
|
|
}
|
|
|
|
|
2023-08-01 18:39:15 +00:00
|
|
|
// ReferenceOrIDMatch compares two references or IDs to see if they both refer
|
|
|
|
// to the same thing.
|
|
|
|
//
|
|
|
|
// Note that this only compares fields that are common between them as
|
|
|
|
// represented by the ReferenceOrID interface and notably ignores the section
|
|
|
|
// field on references and the uid field on ids.
|
|
|
|
func ReferenceOrIDMatch(ref1, ref2 ReferenceOrID) bool {
|
|
|
|
if ref1 == nil || ref2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return EqualType(ref1.GetType(), ref2.GetType()) &&
|
|
|
|
EqualTenancy(ref1.GetTenancy(), ref2.GetTenancy()) &&
|
|
|
|
ref1.GetName() == ref2.GetName()
|
|
|
|
}
|
|
|
|
|
2023-05-09 18:02:24 +00:00
|
|
|
// EqualStatusMap compares two status maps for equality without reflection.
|
|
|
|
func EqualStatusMap(a, b map[string]*pbresource.Status) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
compared := make(map[string]struct{})
|
|
|
|
for k, av := range a {
|
|
|
|
bv, ok := b[k]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2023-05-10 09:37:54 +00:00
|
|
|
if !EqualStatus(av, bv, true) {
|
2023-05-09 18:02:24 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
compared[k] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, bv := range b {
|
|
|
|
if _, skip := compared[k]; skip {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
av, ok := a[k]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-10 09:37:54 +00:00
|
|
|
if !EqualStatus(av, bv, true) {
|
2023-05-09 18:02:24 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|