-
Notifications
You must be signed in to change notification settings - Fork 370
/
grafanaServiceAccount.go
76 lines (65 loc) · 2.44 KB
/
grafanaServiceAccount.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
package model
import (
"github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
"github.com/grafana-operator/grafana-operator/v4/controllers/constants"
v1 "k8s.io/api/core/v1"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const OpenShiftOAuthRedirect = "serviceaccounts.openshift.io/oauth-redirectreference.primary"
func getServiceAccountLabels(cr *v1alpha1.Grafana) map[string]string {
if cr.Spec.ServiceAccount == nil {
return nil
}
return cr.Spec.ServiceAccount.Labels
}
func getServiceAccountAnnotations(cr *v1alpha1.Grafana, existing map[string]string) map[string]string {
if cr.Spec.ServiceAccount == nil {
return existing
}
return MergeAnnotations(cr.Spec.ServiceAccount.Annotations, existing)
}
func mergeImagePullSecrets(requested []v1.LocalObjectReference, existing []v1.LocalObjectReference) []v1.LocalObjectReference {
appendIfAbsent := func(secrets []v1.LocalObjectReference, secret v1.LocalObjectReference) []v1.LocalObjectReference {
for _, s := range secrets {
if s.Name == secret.Name {
return secrets
}
}
return append(secrets, secret)
}
for _, s := range requested {
existing = appendIfAbsent(existing, s)
}
return existing
}
func getServiceAccountImagePullSecrets(cr *v1alpha1.Grafana, existing []v1.LocalObjectReference) []v1.LocalObjectReference {
if cr.Spec.ServiceAccount == nil {
return existing
}
return mergeImagePullSecrets(cr.Spec.ServiceAccount.ImagePullSecrets, existing)
}
func GrafanaServiceAccount(cr *v1alpha1.Grafana) *v1.ServiceAccount {
return &v1.ServiceAccount{
ObjectMeta: v12.ObjectMeta{
Name: constants.GrafanaServiceAccountName,
Namespace: cr.Namespace,
Labels: getServiceAccountLabels(cr),
Annotations: getServiceAccountAnnotations(cr, nil),
},
ImagePullSecrets: getServiceAccountImagePullSecrets(cr, nil),
}
}
func GrafanaServiceAccountSelector(cr *v1alpha1.Grafana) client.ObjectKey {
return client.ObjectKey{
Namespace: cr.Namespace,
Name: constants.GrafanaServiceAccountName,
}
}
func GrafanaServiceAccountReconciled(cr *v1alpha1.Grafana, currentState *v1.ServiceAccount) *v1.ServiceAccount {
reconciled := currentState.DeepCopy()
reconciled.Labels = getServiceAccountLabels(cr)
reconciled.Annotations = getServiceAccountAnnotations(cr, currentState.Annotations)
reconciled.ImagePullSecrets = getServiceAccountImagePullSecrets(cr, currentState.ImagePullSecrets)
return reconciled
}