This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
identifiers.go
63 lines (50 loc) · 1.75 KB
/
identifiers.go
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
57
58
59
60
61
62
63
package runtime
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// DefaultNamespace describes the default namespace name used for the system.
const DefaultNamespace = "default"
// Identifyable is an object which can be identified
type Identifyable interface {
// GetIdentifier can return e.g. a "namespace/name" combination, which is not guaranteed
// to be unique world-wide, or alternatively a random SHA for instance
GetIdentifier() string
}
type identifier string
func (i identifier) GetIdentifier() string { return string(i) }
type Metav1NameIdentifierFactory struct{}
func (id Metav1NameIdentifierFactory) Identify(o interface{}) (Identifyable, bool) {
switch obj := o.(type) {
case metav1.Object:
if len(obj.GetNamespace()) == 0 || len(obj.GetName()) == 0 {
return nil, false
}
return NewIdentifier(fmt.Sprintf("%s/%s", obj.GetNamespace(), obj.GetName())), true
}
return nil, false
}
type ObjectUIDIdentifierFactory struct{}
func (id ObjectUIDIdentifierFactory) Identify(o interface{}) (Identifyable, bool) {
switch obj := o.(type) {
case Object:
if len(obj.GetUID()) == 0 {
return nil, false
}
// TODO: Make sure that runtime.APIType works with this
return NewIdentifier(string(obj.GetUID())), true
}
return nil, false
}
var (
// Metav1Identifier identifies an object using its metav1.ObjectMeta Name and Namespace
Metav1NameIdentifier IdentifierFactory = Metav1NameIdentifierFactory{}
// ObjectUIDIdentifier identifies an object using its libgitops/pkg/runtime.ObjectMeta UID field
ObjectUIDIdentifier IdentifierFactory = ObjectUIDIdentifierFactory{}
)
func NewIdentifier(str string) Identifyable {
return identifier(str)
}
type IdentifierFactory interface {
Identify(o interface{}) (id Identifyable, ok bool)
}