@@ -52,93 +52,8 @@ func (r *Manager) syncNamespaceMetadata(ctx context.Context, namespace string, t
52
52
return
53
53
}
54
54
55
- capsuleLabel , _ := utils .GetTypeLabel (& capsulev1beta2.Tenant {})
56
-
57
55
res , conflictErr = controllerutil .CreateOrUpdate (ctx , r .Client , ns , func () error {
58
- annotations := make (map [string ]string )
59
- labels := map [string ]string {
60
- "kubernetes.io/metadata.name" : namespace ,
61
- capsuleLabel : tnt .GetName (),
62
- }
63
-
64
- if tnt .Spec .NamespaceOptions != nil && tnt .Spec .NamespaceOptions .AdditionalMetadata != nil {
65
- for k , v := range tnt .Spec .NamespaceOptions .AdditionalMetadata .Annotations {
66
- annotations [k ] = v
67
- }
68
- }
69
-
70
- if tnt .Spec .NamespaceOptions != nil && tnt .Spec .NamespaceOptions .AdditionalMetadata != nil {
71
- for k , v := range tnt .Spec .NamespaceOptions .AdditionalMetadata .Labels {
72
- labels [k ] = v
73
- }
74
- }
75
-
76
- if tnt .Spec .NodeSelector != nil {
77
- annotations = utils .BuildNodeSelector (tnt , annotations )
78
- }
79
-
80
- if tnt .Spec .IngressOptions .AllowedClasses != nil {
81
- if len (tnt .Spec .IngressOptions .AllowedClasses .Exact ) > 0 {
82
- annotations [AvailableIngressClassesAnnotation ] = strings .Join (tnt .Spec .IngressOptions .AllowedClasses .Exact , "," )
83
- }
84
-
85
- if len (tnt .Spec .IngressOptions .AllowedClasses .Regex ) > 0 {
86
- annotations [AvailableIngressClassesRegexpAnnotation ] = tnt .Spec .IngressOptions .AllowedClasses .Regex
87
- }
88
- }
89
-
90
- if tnt .Spec .StorageClasses != nil {
91
- if len (tnt .Spec .StorageClasses .Exact ) > 0 {
92
- annotations [AvailableStorageClassesAnnotation ] = strings .Join (tnt .Spec .StorageClasses .Exact , "," )
93
- }
94
-
95
- if len (tnt .Spec .StorageClasses .Regex ) > 0 {
96
- annotations [AvailableStorageClassesRegexpAnnotation ] = tnt .Spec .StorageClasses .Regex
97
- }
98
- }
99
-
100
- if tnt .Spec .ContainerRegistries != nil {
101
- if len (tnt .Spec .ContainerRegistries .Exact ) > 0 {
102
- annotations [AllowedRegistriesAnnotation ] = strings .Join (tnt .Spec .ContainerRegistries .Exact , "," )
103
- }
104
-
105
- if len (tnt .Spec .ContainerRegistries .Regex ) > 0 {
106
- annotations [AllowedRegistriesRegexpAnnotation ] = tnt .Spec .ContainerRegistries .Regex
107
- }
108
- }
109
-
110
- if value , ok := tnt .Annotations [api .ForbiddenNamespaceLabelsAnnotation ]; ok {
111
- annotations [api .ForbiddenNamespaceLabelsAnnotation ] = value
112
- }
113
-
114
- if value , ok := tnt .Annotations [api .ForbiddenNamespaceLabelsRegexpAnnotation ]; ok {
115
- annotations [api .ForbiddenNamespaceLabelsRegexpAnnotation ] = value
116
- }
117
-
118
- if value , ok := tnt .Annotations [api .ForbiddenNamespaceAnnotationsAnnotation ]; ok {
119
- annotations [api .ForbiddenNamespaceAnnotationsAnnotation ] = value
120
- }
121
-
122
- if value , ok := tnt .Annotations [api .ForbiddenNamespaceAnnotationsRegexpAnnotation ]; ok {
123
- annotations [api .ForbiddenNamespaceAnnotationsRegexpAnnotation ] = value
124
- }
125
-
126
- if ns .Annotations == nil {
127
- ns .SetAnnotations (annotations )
128
- } else {
129
- for k , v := range annotations {
130
- ns .Annotations [k ] = v
131
- }
132
- }
133
-
134
- if ns .Labels == nil {
135
- ns .SetLabels (labels )
136
- } else {
137
- for k , v := range labels {
138
- ns .Labels [k ] = v
139
- }
140
- }
141
-
56
+ SyncNamespaceMetadata (tnt , ns )
142
57
return nil
143
58
})
144
59
@@ -185,3 +100,92 @@ func (r *Manager) collectNamespaces(ctx context.Context, tenant *capsulev1beta2.
185
100
return
186
101
})
187
102
}
103
+
104
+ // SyncNamespaceMetadata sync namespace metadata according to tenant spec.
105
+ func SyncNamespaceMetadata (tnt * capsulev1beta2.Tenant , ns * corev1.Namespace ) {
106
+ capsuleLabel , _ := utils .GetTypeLabel (& capsulev1beta2.Tenant {})
107
+
108
+ annotations := make (map [string ]string )
109
+ labels := map [string ]string {
110
+ "kubernetes.io/metadata.name" : ns .GetName (),
111
+ capsuleLabel : tnt .GetName (),
112
+ }
113
+
114
+ if tnt .Spec .NamespaceOptions != nil && tnt .Spec .NamespaceOptions .AdditionalMetadata != nil {
115
+ for k , v := range tnt .Spec .NamespaceOptions .AdditionalMetadata .Annotations {
116
+ annotations [k ] = v
117
+ }
118
+ }
119
+
120
+ if tnt .Spec .NamespaceOptions != nil && tnt .Spec .NamespaceOptions .AdditionalMetadata != nil {
121
+ for k , v := range tnt .Spec .NamespaceOptions .AdditionalMetadata .Labels {
122
+ labels [k ] = v
123
+ }
124
+ }
125
+
126
+ if tnt .Spec .NodeSelector != nil {
127
+ annotations = utils .BuildNodeSelector (tnt , annotations )
128
+ }
129
+
130
+ if tnt .Spec .IngressOptions .AllowedClasses != nil {
131
+ if len (tnt .Spec .IngressOptions .AllowedClasses .Exact ) > 0 {
132
+ annotations [AvailableIngressClassesAnnotation ] = strings .Join (tnt .Spec .IngressOptions .AllowedClasses .Exact , "," )
133
+ }
134
+
135
+ if len (tnt .Spec .IngressOptions .AllowedClasses .Regex ) > 0 {
136
+ annotations [AvailableIngressClassesRegexpAnnotation ] = tnt .Spec .IngressOptions .AllowedClasses .Regex
137
+ }
138
+ }
139
+
140
+ if tnt .Spec .StorageClasses != nil {
141
+ if len (tnt .Spec .StorageClasses .Exact ) > 0 {
142
+ annotations [AvailableStorageClassesAnnotation ] = strings .Join (tnt .Spec .StorageClasses .Exact , "," )
143
+ }
144
+
145
+ if len (tnt .Spec .StorageClasses .Regex ) > 0 {
146
+ annotations [AvailableStorageClassesRegexpAnnotation ] = tnt .Spec .StorageClasses .Regex
147
+ }
148
+ }
149
+
150
+ if tnt .Spec .ContainerRegistries != nil {
151
+ if len (tnt .Spec .ContainerRegistries .Exact ) > 0 {
152
+ annotations [AllowedRegistriesAnnotation ] = strings .Join (tnt .Spec .ContainerRegistries .Exact , "," )
153
+ }
154
+
155
+ if len (tnt .Spec .ContainerRegistries .Regex ) > 0 {
156
+ annotations [AllowedRegistriesRegexpAnnotation ] = tnt .Spec .ContainerRegistries .Regex
157
+ }
158
+ }
159
+
160
+ if value , ok := tnt .Annotations [api .ForbiddenNamespaceLabelsAnnotation ]; ok {
161
+ annotations [api .ForbiddenNamespaceLabelsAnnotation ] = value
162
+ }
163
+
164
+ if value , ok := tnt .Annotations [api .ForbiddenNamespaceLabelsRegexpAnnotation ]; ok {
165
+ annotations [api .ForbiddenNamespaceLabelsRegexpAnnotation ] = value
166
+ }
167
+
168
+ if value , ok := tnt .Annotations [api .ForbiddenNamespaceAnnotationsAnnotation ]; ok {
169
+ annotations [api .ForbiddenNamespaceAnnotationsAnnotation ] = value
170
+ }
171
+
172
+ if value , ok := tnt .Annotations [api .ForbiddenNamespaceAnnotationsRegexpAnnotation ]; ok {
173
+ annotations [api .ForbiddenNamespaceAnnotationsRegexpAnnotation ] = value
174
+ }
175
+
176
+ if ns .Annotations == nil {
177
+ ns .SetAnnotations (annotations )
178
+ } else {
179
+ for k , v := range annotations {
180
+ ns .Annotations [k ] = v
181
+ }
182
+ }
183
+
184
+ if ns .Labels == nil {
185
+ ns .SetLabels (labels )
186
+ } else {
187
+ for k , v := range labels {
188
+ ns .Labels [k ] = v
189
+ }
190
+ }
191
+ }
0 commit comments