-
Notifications
You must be signed in to change notification settings - Fork 181
/
namespace.go
157 lines (135 loc) · 4.84 KB
/
namespace.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package ingress
import (
"context"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"github.com/openshift/cluster-ingress-operator/pkg/manifests"
operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller"
)
// ensureRouterNamespace ensures that the router namespace exists.
func (r *reconciler) ensureRouterNamespace() (bool, *corev1.Namespace, error) {
desired := manifests.RouterNamespace()
haveNamespace, current, err := r.currentRouterNamespace()
if err != nil {
return false, nil, err
}
switch {
case !haveNamespace:
if err := r.client.Create(context.TODO(), desired); err != nil {
return false, nil, fmt.Errorf("failed to create router namespace: %v", err)
}
log.Info("created router namespace", "desired", desired)
return r.currentRouterNamespace()
case haveNamespace:
if updated, err := r.updateRouterNamespace(current, desired); err != nil {
return true, current, fmt.Errorf("failed to update router namespace: %v", err)
} else if updated {
return r.currentRouterNamespace()
}
}
return true, current, nil
}
// currentRouterNamespace gets the current router namespace resource.
func (r *reconciler) currentRouterNamespace() (bool, *corev1.Namespace, error) {
namespace := &corev1.Namespace{}
name := types.NamespacedName{
Name: operatorcontroller.DefaultOperandNamespace,
}
if err := r.client.Get(context.TODO(), name, namespace); err != nil {
if errors.IsNotFound(err) {
return false, nil, nil
}
return false, nil, err
}
return true, namespace, nil
}
// updateRouterNamespace updates the router namespace if an appropriate change
// has been detected.
func (r *reconciler) updateRouterNamespace(current, desired *corev1.Namespace) (bool, error) {
changed, updated := routerNamespaceChanged(current, desired)
if !changed {
return false, nil
}
// Diff before updating because the client may mutate the object.
diff := cmp.Diff(current, updated, cmpopts.EquateEmpty())
if err := r.client.Update(context.TODO(), updated); err != nil {
if errors.IsAlreadyExists(err) {
return false, nil
}
return false, err
}
log.Info("updated router namespace", "namespace", updated.Name, "diff", diff)
return true, nil
}
// routerNamespaceChanged returns true if current and expected differ by any
// of the annotations or the labels managed by the operator.
// Other namespace labels and annotations are not overwritten so that
// values written by other controllers are preserved.
func routerNamespaceChanged(current, expected *corev1.Namespace) (bool, *corev1.Namespace) {
knownAnnotations := []string{
"workload.openshift.io/allowed",
}
knownLabels := []string{
"openshift.io/cluster-monitoring",
"name",
"network.openshift.io/policy-group",
"policy-group.network.openshift.io/ingress",
"pod-security.kubernetes.io/enforce",
"pod-security.kubernetes.io/audit",
"pod-security.kubernetes.io/warn",
}
updated := current.DeepCopy()
changed := false
if updated.Annotations == nil {
updated.Annotations = map[string]string{}
}
if updated.Labels == nil {
updated.Labels = map[string]string{}
}
for _, annotation := range knownAnnotations {
if val, ok := current.Annotations[annotation]; !ok || val != expected.Annotations[annotation] {
updated.Annotations[annotation] = expected.Annotations[annotation]
changed = true
}
}
for _, label := range knownLabels {
if val, ok := current.Labels[label]; !ok || val != expected.Labels[label] {
updated.Labels[label] = expected.Labels[label]
changed = true
}
}
if !changed {
return false, nil
}
return true, updated
}
func (r *reconciler) ensureRouterServiceAccount() error {
sa := manifests.RouterServiceAccount()
if err := r.client.Get(context.TODO(), types.NamespacedName{Namespace: sa.Namespace, Name: sa.Name}, sa); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get router service account %s/%s: %v", sa.Namespace, sa.Name, err)
}
if err := r.client.Create(context.TODO(), sa); err != nil {
return fmt.Errorf("failed to create router service account %s/%s: %v", sa.Namespace, sa.Name, err)
}
log.Info("created router service account", "namespace", sa.Namespace, "name", sa.Name)
}
return nil
}
func (r *reconciler) ensureRouterClusterRoleBinding() error {
crb := manifests.RouterClusterRoleBinding()
if err := r.client.Get(context.TODO(), types.NamespacedName{Name: crb.Name}, crb); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get router cluster role binding %s: %v", crb.Name, err)
}
if err := r.client.Create(context.TODO(), crb); err != nil {
return fmt.Errorf("failed to create router cluster role binding %s: %v", crb.Name, err)
}
log.Info("created router cluster role binding", "name", crb.Name)
}
return nil
}