-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreference.go
More file actions
56 lines (47 loc) · 1.81 KB
/
Copy pathreference.go
File metadata and controls
56 lines (47 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-License-Identifier: Apache-2.0
package types
import "fmt"
// ResourceReference references a Kubernetes resource by name and namespace.
// Provides a clean, reusable type for referencing GitDestinations and other resources.
//
// UID, when set, identifies the specific object generation. It is deliberately
// excluded from String/Key/Equal so in-memory bookkeeping stays keyed by
// namespace/name; it scopes durable Redis keys (e.g. watch cursors) so a recreated
// GitTarget never inherits a deleted predecessor's state.
type ResourceReference struct {
Name string
Namespace string
UID string
}
// NewResourceReference creates a new resource reference.
func NewResourceReference(name, namespace string) ResourceReference {
return ResourceReference{
Name: name,
Namespace: namespace,
}
}
// WithUID returns a copy of the reference carrying the given object UID.
func (r ResourceReference) WithUID(uid string) ResourceReference {
r.UID = uid
return r
}
// String returns "namespace/name" format.
func (r ResourceReference) String() string {
return fmt.Sprintf("%s/%s", r.Namespace, r.Name)
}
// Key returns a string key suitable for map lookups: "namespace/name".
//
// Not to be confused with [ResourceIdentifier.Key], which is the fully-qualified
// "{group}/{version}/{resource}/{namespace}/{name}" identity of a watched object. This one
// names a GitTarget-like object by reference and carries no group, version or resource.
func (r ResourceReference) Key() string {
return r.String()
}
// Equal checks if two references are equal.
func (r ResourceReference) Equal(other ResourceReference) bool {
return r.Name == other.Name && r.Namespace == other.Namespace
}
// IsZero returns true if this is an empty reference.
func (r ResourceReference) IsZero() bool {
return r.Name == "" && r.Namespace == ""
}