-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecret.go
87 lines (71 loc) · 2.01 KB
/
secret.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package framework
import (
"halkyon.io/api/v1beta1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"reflect"
"strings"
)
var secretGVK = v1.SchemeGroupVersion.WithKind("Secret")
var _ DependentResource = &Secret{}
type NeedsSecret interface {
GetDataMap() map[string][]byte
GetSecretName() string
Owner() SerializableResource
}
type Secret struct {
*BaseDependentResource
Delegate NeedsSecret
}
func (res Secret) Fetch() (runtime.Object, error) {
return DefaultFetcher(res)
}
func (res Secret) GetCondition(underlying runtime.Object, err error) *v1beta1.DependentCondition {
return DefaultGetConditionFor(res, err)
}
func (res Secret) Update(toUpdate runtime.Object) (bool, runtime.Object, error) {
secret := toUpdate.(*v1.Secret)
dataMap := res.Delegate.GetDataMap()
if !reflect.DeepEqual(dataMap, secret.Data) {
secret.Data = dataMap
return true, secret, nil
}
return false, secret, nil
}
func NewSecret(owner NeedsSecret, config ...DependentResourceConfig) Secret {
var c DependentResourceConfig
if len(config) != 1 {
c = NewDefaultSecretConfig()
} else {
c = config[0]
}
return Secret{BaseDependentResource: NewConfiguredBaseDependentResource(owner.Owner(), c), Delegate: owner}
}
func NewDefaultSecretConfig() DependentResourceConfig {
config := NewConfig(secretGVK)
config.Watched = true
config.Updated = true
return config
}
//buildSecret returns the secret resource
func (res Secret) Build(empty bool) (runtime.Object, error) {
secret := &v1.Secret{}
if !empty {
secret.ObjectMeta = metav1.ObjectMeta{
Name: res.Name(),
Namespace: res.owner.GetNamespace(),
}
secret.Data = res.Delegate.GetDataMap()
}
return secret, nil
}
func (res Secret) Name() string {
return res.Delegate.GetSecretName()
}
func DefaultSecretNameFor(secretOwner NeedsSecret) string {
return DefaultSecretNameFrom(secretOwner.Owner())
}
func DefaultSecretNameFrom(owner metav1.Object) string {
return strings.ToLower(owner.GetName()) + "-config"
}