-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource.go
138 lines (112 loc) · 2.87 KB
/
resource.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package secret
import (
"reflect"
"github.com/ghodss/yaml"
"github.com/giantswarm/app/v5/pkg/values"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
corev1 "k8s.io/api/core/v1"
"github.com/giantswarm/app-operator/v5/service/controller/app/controllercontext"
)
const (
// Name is the identifier of the resource.
Name = "secret"
)
// Config represents the configuration used to create a new secret resource.
type Config struct {
// Dependencies.
Logger micrologger.Logger
Values *values.Values
// Settings.
ChartNamespace string
}
// Resource implements the secret resource.
type Resource struct {
// Dependencies.
logger micrologger.Logger
values *values.Values
// Settings.
chartNamespace string
}
// New creates a new configured secret resource.
func New(config Config) (*Resource, error) {
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
if config.Values == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Values must not be empty", config)
}
if config.ChartNamespace == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.ChartNamespace must not be empty", config)
}
r := &Resource{
logger: config.Logger,
values: config.Values,
chartNamespace: config.ChartNamespace,
}
return r, nil
}
func (r *Resource) Name() string {
return Name
}
// addStatusToContext adds the status to the controller context. It will be
// used to set the CR status in the status resource.
func addStatusToContext(cc *controllercontext.Context, reason, status string) {
cc.Status = controllercontext.Status{
ChartStatus: controllercontext.ChartStatus{
Reason: reason,
Status: status,
},
}
}
// equals asseses the equality of Secrets with regards to distinguishing
// fields.
func equals(a, b *corev1.Secret) bool {
if a.Name != b.Name {
return false
}
if a.Namespace != b.Namespace {
return false
}
if !reflect.DeepEqual(a.Annotations, b.Annotations) {
return false
}
var source, dest map[string]interface{}
{
source = make(map[string]interface{})
dest = make(map[string]interface{})
err := yaml.Unmarshal(a.Data["values"], &source)
if err != nil {
return false
}
err = yaml.Unmarshal(b.Data["values"], &dest)
if err != nil {
return false
}
}
if !reflect.DeepEqual(source, dest) {
return false
}
if !reflect.DeepEqual(a.Labels, b.Labels) {
return false
}
return true
}
// isEmpty checks if a Secret is empty.
func isEmpty(s *corev1.Secret) bool {
if s == nil {
return true
}
return equals(s, &corev1.Secret{})
}
// toSecret converts the input into a Secret.
func toSecret(v interface{}) (*corev1.Secret, error) {
if v == nil {
return &corev1.Secret{}, nil
}
secret, ok := v.(*corev1.Secret)
if !ok {
return nil, microerror.Maskf(wrongTypeError, "expected '%T', got '%T'", &corev1.Secret{}, v)
}
return secret, nil
}