-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource.go
120 lines (98 loc) · 2.63 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
package configmap
import (
"reflect"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
corev1 "k8s.io/api/core/v1"
"github.com/giantswarm/app-operator/service/controller/app/controllercontext"
"github.com/giantswarm/app-operator/service/controller/app/values"
)
const (
// Name is the identifier of the resource.
Name = "configmap"
)
// Config represents the configuration used to create a new configmap resource.
type Config struct {
// Dependencies.
Logger micrologger.Logger
Values *values.Values
// Settings.
ChartNamespace string
}
// Resource implements the configmap resource.
type Resource struct {
// Dependencies.
logger micrologger.Logger
values *values.Values
// Settings.
chartNamespace string
}
// New creates a new configured configmap 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 ConfigMaps with regards to distinguishing
// fields.
func equals(a, b *corev1.ConfigMap) bool {
if a.Name != b.Name {
return false
}
if a.Namespace != b.Namespace {
return false
}
if !reflect.DeepEqual(a.Annotations, b.Annotations) {
return false
}
if !reflect.DeepEqual(a.Data, b.Data) {
return false
}
if !reflect.DeepEqual(a.Labels, b.Labels) {
return false
}
return true
}
// isEmpty checks if a ConfigMap is empty.
func isEmpty(c *corev1.ConfigMap) bool {
if c == nil {
return true
}
return equals(c, &corev1.ConfigMap{})
}
// toConfigMap converts the input into a ConfigMap.
func toConfigMap(v interface{}) (*corev1.ConfigMap, error) {
if v == nil {
return &corev1.ConfigMap{}, nil
}
configMap, ok := v.(*corev1.ConfigMap)
if !ok {
return nil, microerror.Maskf(wrongTypeError, "expected '%T', got '%T'", &corev1.ConfigMap{}, v)
}
return configMap, nil
}