/
resource.go
87 lines (69 loc) · 1.9 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
package appresource
import (
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/giantswarm/apiextensions-application/api/v1alpha1"
)
type Config struct {
G8sClient client.Client
Logger micrologger.Logger
StateGetter StateGetter
AllowedAnnotations []string
Name string
}
type Resource struct {
g8sClient client.Client
logger micrologger.Logger
stateGetter StateGetter
allowedAnnotations map[string]bool
name string
}
func New(config Config) (*Resource, error) {
if config.G8sClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.G8sClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
if config.StateGetter == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.StateGetter must not be empty", config)
}
if config.Name == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.Name must not be empty", config)
}
r := &Resource{
g8sClient: config.G8sClient,
logger: config.Logger,
stateGetter: config.StateGetter,
name: config.Name,
}
if config.AllowedAnnotations != nil {
allowedAnnotation := map[string]bool{}
{
for _, annotation := range config.AllowedAnnotations {
allowedAnnotation[annotation] = true
}
}
r.allowedAnnotations = allowedAnnotation
}
return r, nil
}
func (r *Resource) Name() string {
return r.name
}
func containsAppCR(appCRs []*v1alpha1.App, appCR *v1alpha1.App) bool {
for _, a := range appCRs {
if appCR.Name == a.Name && appCR.Namespace == a.Namespace {
return true
}
}
return false
}
func toAppCRs(v interface{}) ([]*v1alpha1.App, error) {
x, ok := v.([]*v1alpha1.App)
if !ok {
return nil, microerror.Maskf(wrongTypeError, "expected '%T', got '%T'", x, v)
}
return x, nil
}