1- // Copyright (c) 2020, 2021 , Oracle and/or its affiliates.
1+ // Copyright (c) 2020, 2022 , Oracle and/or its affiliates.
22// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33
44package oracle .kubernetes .operator ;
55
6+ import java .util .Arrays ;
67import java .util .Collection ;
78import java .util .Collections ;
89import java .util .HashSet ;
1617import javax .annotation .Nonnull ;
1718import javax .annotation .Nullable ;
1819
20+ import io .kubernetes .client .openapi .models .V1Namespace ;
21+ import io .kubernetes .client .openapi .models .V1ObjectMeta ;
1922import jakarta .validation .constraints .NotNull ;
2023import oracle .kubernetes .operator .helpers .EventHelper .EventData ;
2124import oracle .kubernetes .operator .helpers .HelmAccess ;
2730import oracle .kubernetes .operator .work .NextAction ;
2831import oracle .kubernetes .operator .work .Packet ;
2932import oracle .kubernetes .operator .work .Step ;
33+ import org .apache .commons .lang3 .ArrayUtils ;
3034
3135import static oracle .kubernetes .operator .helpers .EventHelper .EventItem .NAMESPACE_WATCHING_STOPPED ;
3236import static oracle .kubernetes .operator .helpers .EventHelper .EventItem .STOP_MANAGING_NAMESPACE ;
@@ -47,11 +51,11 @@ public class Namespaces {
4751 private static final String ALL_DOMAIN_NAMESPACES = "ALL_DOMAIN_NAMESPACES" ;
4852
4953 /**
50- * Returns true if the specified string is the name of a domain namespace .
51- * @param ns a namespace name
54+ * Returns true if the specified namespace is managed by the operator .
55+ * @param namespace namespace object to check
5256 */
53- static boolean isDomainNamespace (String ns ) {
54- return getSelectionStrategy ().isDomainNamespace (ns );
57+ static boolean isDomainNamespace (@ Nonnull V1Namespace namespace ) {
58+ return getSelectionStrategy ().isDomainNamespace (namespace );
5559 }
5660
5761 /**
@@ -68,22 +72,15 @@ static boolean isDomainNamespace(String ns) {
6872 return getSelectionStrategy ().getFoundDomainNamespaces (packet );
6973 }
7074
71- /**
72- * Returns an array of the label selectors that will determine that a namespace is being used to manage domains.
73- */
74- static String [] getLabelSelectors () {
75- return getSelectionStrategy ().getLabelSelectors ();
76- }
77-
7875 static <R > R getSelection (NamespaceStrategyVisitor <R > visitor ) {
7976 return getSelectionStrategy ().getSelection (visitor );
8077 }
8178
8279 public enum SelectionStrategy {
8380 List {
8481 @ Override
85- public boolean isDomainNamespace (@ Nonnull String namespaceName ) {
86- return getConfiguredDomainNamespaces ().contains (namespaceName );
82+ public boolean isDomainNamespace (@ Nonnull V1Namespace namespace ) {
83+ return getConfiguredDomainNamespaces ().contains (getNSName ( namespace ) );
8784 }
8885
8986 @ Override
@@ -110,26 +107,47 @@ private String getConfiguredNamespaceList() {
110107 }
111108 },
112109 LabelSelector {
113- @ Override
114- public String [] getLabelSelectors () {
115- return new String []{TuningParameters .getInstance ().get ("domainNamespaceLabelSelector" )};
116- }
117-
118110 @ Override
119111 public <V > V getSelection (NamespaceStrategyVisitor <V > visitor ) {
120112 return visitor .getLabelSelectorStrategySelection ();
121113 }
122114
123115 @ Override
124- public boolean isDomainNamespace (@ Nonnull String namespaceName ) {
125- return true ; // filtering is done by Kubernetes list call
116+ public boolean isDomainNamespace (@ Nonnull V1Namespace namespace ) {
117+ return matchSpecifiedLabelSelectors (namespace .getMetadata (), getLabelSelectors ());
118+ }
119+
120+ private String [] getLabelSelectors () {
121+ return Optional .ofNullable (TuningParameters .getInstance ().get ("domainNamespaceLabelSelector" ))
122+ .map (s -> new String []{s })
123+ .orElse (new String [0 ]);
124+ }
125+
126+ private boolean matchSpecifiedLabelSelectors (@ NotNull V1ObjectMeta nsMetadata , String [] selectors ) {
127+ return ArrayUtils .isEmpty (selectors ) || hasLabels (nsMetadata , selectors );
128+ }
129+
130+ private boolean hasLabels (@ NotNull V1ObjectMeta metadata , String [] selectors ) {
131+ return Arrays .stream (selectors ).allMatch (s -> hasLabel (metadata , s ));
132+ }
133+
134+ private boolean hasLabel (@ Nonnull V1ObjectMeta metadata , String selector ) {
135+ String [] split = selector .split ("=" );
136+ return includesLabel (metadata .getLabels (), split [0 ], split .length == 1 ? null : split [1 ]);
137+ }
138+
139+ private boolean includesLabel (Map <String , String > labels , String key , String value ) {
140+ if (labels == null || !labels .containsKey (key )) {
141+ return false ;
142+ }
143+ return value == null || value .equals (labels .get (key ));
126144 }
127145 },
128146 RegExp {
129147 @ Override
130- public boolean isDomainNamespace (@ Nonnull String namespaceName ) {
148+ public boolean isDomainNamespace (@ Nonnull V1Namespace namespace ) {
131149 try {
132- return getCompiledPattern (getRegExp ()).matcher (namespaceName ).find ();
150+ return getCompiledPattern (getRegExp ()).matcher (getNSName ( namespace ) ).find ();
133151 } catch (PatternSyntaxException e ) {
134152 LOGGER .severe (MessageKeys .EXCEPTION , e );
135153 return false ;
@@ -151,8 +169,8 @@ private Pattern getCompiledPattern(String regExp) {
151169 },
152170 Dedicated {
153171 @ Override
154- public boolean isDomainNamespace (@ Nonnull String namespaceName ) {
155- return namespaceName .equals (getOperatorNamespace ());
172+ public boolean isDomainNamespace (@ Nonnull V1Namespace namespace ) {
173+ return getNSName ( namespace ) .equals (getOperatorNamespace ());
156174 }
157175
158176 @ Override
@@ -171,13 +189,7 @@ public Collection<String> getFoundDomainNamespaces(Packet packet) {
171189 }
172190 };
173191
174- static final String [] NO_SELECTORS = new String [0 ];
175-
176- public abstract boolean isDomainNamespace (@ Nonnull String namespaceName );
177-
178- public String [] getLabelSelectors () {
179- return NO_SELECTORS ;
180- }
192+ public abstract boolean isDomainNamespace (@ Nonnull V1Namespace namespace );
181193
182194 public @ Nullable Collection <String > getConfiguredDomainNamespaces () {
183195 return null ;
@@ -187,6 +199,10 @@ public String[] getLabelSelectors() {
187199
188200 private static final Map <String , Pattern > compiledPatterns = new WeakHashMap <>();
189201
202+ String getNSName (V1Namespace namespace ) {
203+ return Optional .ofNullable (namespace ).map (V1Namespace ::getMetadata ).map (V1ObjectMeta ::getName ).orElse (null );
204+ }
205+
190206 /**
191207 * Returns a modifiable collection of found namespace names in a packet.
192208 * Callers should use this to add to the collection.
0 commit comments