1
1
package io .insee .dev .k8sonboarding .service ;
2
2
3
+ import java .util .ArrayList ;
3
4
import java .util .List ;
4
5
import java .util .Map ;
5
6
import java .util .regex .Matcher ;
34
35
@ Service
35
36
public class OnboardingService {
36
37
37
- private static final Logger logger = LoggerFactory .getLogger (OnboardingService .class );
38
+ private static final Logger logger = LoggerFactory .getLogger (OnboardingService .class );
38
39
39
40
public static final String ADMIN = "admin" ;
40
41
public static final String API_GROUP = "rbac.authorization.k8s.io" ;
@@ -43,14 +44,14 @@ public class OnboardingService {
43
44
public static final String LABEL_CREATED_BY = "created_by" ;
44
45
public static final String CLUSTER_ROLE = "ClusterRole" ;
45
46
46
- public static final String NO_QUOTA_VALUE = "0" ;
47
+ public static final String NO_QUOTA_VALUE = "0" ;
47
48
public static final String RESOURCE_QUOTA_REQUESTS_STORAGE = "requests.storage" ;
48
49
49
50
@ Value ("${spring.application.name:k8s-onboarding}" )
50
51
private String appName ;
51
52
52
53
@ Autowired
53
- QuotaProperties quotaProperties ;
54
+ QuotaProperties quotaProperties ;
54
55
55
56
@ Autowired
56
57
ClusterProperties clusterProperty ;
@@ -92,52 +93,50 @@ public void createNamespace(User user, String groupId) {
92
93
.addToLabels (LABEL_CREATED_BY , appName ).endMetadata ().build ();
93
94
kubernetesClient .namespaces ().resource (ns ).create ();
94
95
95
- applyQuotas (namespaceId , quotaProperties , true );
96
+ applyQuotas (namespaceId , quotaProperties , true );
96
97
}
97
98
}
98
99
99
- /**
100
- *
101
- * @param namespaceId
102
- * @param inputQuota
103
- * @param overrideExisting
104
- */
105
- private void applyQuotas (String namespaceId , QuotaProperties inputQuota , boolean overrideExisting ) {
106
- ResourceQuotaBuilder resourceQuotaBuilder = new ResourceQuotaBuilder ();
107
- resourceQuotaBuilder .withNewMetadata ()
108
- .withLabels (Map .of (LABEL_CREATED_BY , appName ))
109
- .withName (namespaceId )
110
- .withNamespace (namespaceId )
111
- .endMetadata ();
112
-
113
- Map <String , String > quotasToApply = inputQuota .asMap ();
114
-
115
- if (quotasToApply .entrySet ().stream ().filter (e -> e .getValue () != null ).count () == 0 ) {
116
- return ;
117
- }
118
-
119
- ResourceQuotaFluent .SpecNested <ResourceQuotaBuilder > resourceQuotaBuilderSpecNested = resourceQuotaBuilder
120
- .withNewSpec ();
121
- quotasToApply .entrySet ().stream ().filter (e -> e .getValue () != null ).forEach (e -> resourceQuotaBuilderSpecNested .addToHard (e .getKey (),Quantity .parse (e .getValue ())));
122
- resourceQuotaBuilderSpecNested .endSpec ();
123
-
124
- ResourceQuota quota = resourceQuotaBuilder .build ();
125
- if (overrideExisting ) {
126
- kubernetesClient .resource (quota ).inNamespace (namespaceId ).createOrReplace ();
127
- }
128
- else {
129
- try {
130
- kubernetesClient .resource (quota ).inNamespace (namespaceId ).create ();
131
- }
132
- catch (KubernetesClientException e ) {
133
- if (e .getCode () != 409 ) {
134
- // This is not a "quota already in place" error
135
- throw e ;
136
- }
137
- }
138
- }
139
- }
100
+ /**
101
+ *
102
+ * @param namespaceId
103
+ * @param inputQuota
104
+ * @param overrideExisting
105
+ */
106
+ private void applyQuotas (String namespaceId , QuotaProperties inputQuota , boolean overrideExisting ) {
107
+ ResourceQuotaBuilder resourceQuotaBuilder = new ResourceQuotaBuilder ();
108
+ resourceQuotaBuilder .withNewMetadata ()
109
+ .withLabels (Map .of (LABEL_CREATED_BY , appName ))
110
+ .withName (namespaceId )
111
+ .withNamespace (namespaceId )
112
+ .endMetadata ();
113
+
114
+ Map <String , String > quotasToApply = inputQuota .asMap ();
115
+
116
+ if (quotasToApply .entrySet ().stream ().filter (e -> e .getValue () != null ).count () == 0 ) {
117
+ return ;
118
+ }
140
119
120
+ ResourceQuotaFluent .SpecNested <ResourceQuotaBuilder > resourceQuotaBuilderSpecNested = resourceQuotaBuilder
121
+ .withNewSpec ();
122
+ quotasToApply .entrySet ().stream ().filter (e -> e .getValue () != null )
123
+ .forEach (e -> resourceQuotaBuilderSpecNested .addToHard (e .getKey (), Quantity .parse (e .getValue ())));
124
+ resourceQuotaBuilderSpecNested .endSpec ();
125
+
126
+ ResourceQuota quota = resourceQuotaBuilder .build ();
127
+ if (overrideExisting ) {
128
+ kubernetesClient .resource (quota ).inNamespace (namespaceId ).createOrReplace ();
129
+ } else {
130
+ try {
131
+ kubernetesClient .resource (quota ).inNamespace (namespaceId ).create ();
132
+ } catch (KubernetesClientException e ) {
133
+ if (e .getCode () != 409 ) {
134
+ // This is not a "quota already in place" error
135
+ throw e ;
136
+ }
137
+ }
138
+ }
139
+ }
141
140
142
141
/**
143
142
* Currently, namespaceid is ignored
@@ -170,15 +169,13 @@ public RoleBinding addPermissionsToNamespace(User user, String group) {
170
169
return null ;
171
170
}
172
171
173
-
174
-
175
- public boolean checkNamespaceExists (String namespaceId ) {
172
+ public boolean checkNamespaceExists (String namespaceId ) {
176
173
final Namespace namespace = kubernetesClient .namespaces ().withName (namespaceId ).get ();
177
174
return namespace != null ;
178
175
}
179
176
180
177
public boolean checkPermissionsExists (String namespaceId ) {
181
- final RoleBinding roleBinding = kubernetesClient .rbac ().roleBindings ().inNamespace (namespaceId )
178
+ final RoleBinding roleBinding = kubernetesClient .rbac ().roleBindings ().inNamespace (namespaceId )
182
179
.withName (clusterProperty .getNameNamespaceAdmin ()).get ();
183
180
return (roleBinding != null && !roleBinding .getSubjects ().isEmpty ());
184
181
}
@@ -206,8 +203,8 @@ public String getNamespaceId(User user, String group) {
206
203
}
207
204
208
205
private String optionallyRemoveSuffix (String rawGroup ) {
209
- if (doesRemoveSuffix ){
210
- rawGroup = StringUtils .substringBefore (rawGroup , '_' );
206
+ if (doesRemoveSuffix ) {
207
+ rawGroup = StringUtils .substringBefore (rawGroup , '_' );
211
208
return rawGroup ;
212
209
}
213
210
return rawGroup ;
@@ -240,23 +237,23 @@ public void setKubernetesClient(KubernetesClient kubernetesClient) {
240
237
}
241
238
242
239
public List <AllowedGroup > getAllowedAndFilteredGroupsForUser (User user ) {
243
- List <String > allGroups = user .getGroups ();
240
+ List <String > allGroups = user .getGroups () == null ? new ArrayList <>() : user . getGroups () ;
244
241
return allGroups
245
- .stream ()
246
- .filter (
247
- this ::checkGroupMatchesFilter
248
- ) .map (
249
- group -> new AllowedGroup (optionallyRemoveSuffix (group ),group )
250
- ) .collect (Collectors .toList ());
242
+ .stream ()
243
+ .filter (
244
+ this ::checkGroupMatchesFilter )
245
+ .map (
246
+ group -> new AllowedGroup (optionallyRemoveSuffix (group ), group ) )
247
+ .collect (Collectors .toList ());
251
248
}
252
249
253
250
@ Value ("${io.insee.dev.k8sonboarding.ui.groupFilter:.*}" )
254
251
private String groupFilter ;
252
+
255
253
private boolean checkGroupMatchesFilter (String group ) {
256
254
var groupFilterPattern = Pattern .compile (groupFilter );
257
255
Matcher m = groupFilterPattern .matcher (group );
258
256
return m .matches ();
259
257
}
260
258
261
-
262
259
}
0 commit comments