Skip to content

Commit b2ce8a1

Browse files
author
ocorriga
committed
Address comments in Operator
Signed-off-by: ocorriga <ocorriga@redhat.com>
1 parent 5d36651 commit b2ce8a1

File tree

4 files changed

+57
-49
lines changed

4 files changed

+57
-49
lines changed

operator/src/main/java/io/strimzi/kafka/access/KafkaAccessOperator.java

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,49 @@
66

77
import io.javaoperatorsdk.operator.Operator;
88
import io.strimzi.kafka.access.server.HealthServlet;
9-
import java.util.Arrays;
9+
import java.util.Collections;
10+
import java.util.HashSet;
1011
import java.util.Set;
11-
import java.util.stream.Collectors;
12+
import org.apache.kafka.common.errors.InvalidConfigurationException;
1213
import org.eclipse.jetty.server.Server;
1314
import org.eclipse.jetty.servlet.ServletHandler;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
1617

18+
import static java.util.Arrays.asList;
19+
1720
/**
1821
* The main operator class for Strimzi Access Operator
1922
*/
2023
public class KafkaAccessOperator {
2124

2225
private static final Logger LOGGER = LoggerFactory.getLogger(KafkaAccessOperator.class);
2326
private static final int HEALTH_CHECK_PORT = 8080;
27+
private final static String ANY_NAMESPACE = "*";
28+
29+
/**
30+
* Parses namespace configuration from a string
31+
*
32+
* @param namespacesList Comma-separated list of namespaces or "*" for all namespaces
33+
* @return Set of namespace names
34+
*/
35+
private static Set<String> parseNamespaces(String namespacesList) {
36+
Set<String> namespaces;
37+
if (namespacesList.equals(ANY_NAMESPACE)) {
38+
namespaces = Collections.singleton(ANY_NAMESPACE);
39+
} else {
40+
if (namespacesList.trim().equals(ANY_NAMESPACE)) {
41+
namespaces = Collections.singleton(ANY_NAMESPACE);
42+
} else if (namespacesList.matches("(\\s*[a-z0-9.-]+\\s*,)*\\s*[a-z0-9.-]+\\s*")) {
43+
namespaces = new HashSet<>(asList(namespacesList.trim().split("\\s*,+\\s*")));
44+
} else {
45+
throw new InvalidConfigurationException("Not a valid list of namespaces nor the 'any namespace' wildcard "
46+
+ ANY_NAMESPACE);
47+
}
48+
}
49+
50+
return namespaces;
51+
}
2452

2553
/**
2654
* Initializes the operator and runs a servlet for health checking
@@ -32,22 +60,23 @@ public static void main(final String[] args) {
3260
final Operator operator = new Operator(overrider -> overrider
3361
.withUseSSAToPatchPrimaryResource(false));
3462

35-
String strimziNamespace = System.getenv("STRIMZI_NAMESPACE");
36-
if (strimziNamespace != null && strimziNamespace.matches("\\*")) {
37-
LOGGER.info("Watching all namespaces");
38-
operator.register(new KafkaAccessReconciler(operator.getKubernetesClient()),
39-
o -> o.watchingAllNamespaces());
40-
} else {
41-
Set<String> namespaces =
42-
strimziNamespace == null ? Set.of() :
43-
Arrays.stream(strimziNamespace.split(","))
44-
.map(String::trim)
45-
.collect(Collectors.toSet());
46-
LOGGER.info("Watching specific namespaces: {}", namespaces);
47-
operator.register(new KafkaAccessReconciler(operator.getKubernetesClient()),
48-
o -> o.settingNamespaces(namespaces));
63+
String strimziNamespace = System.getenv().getOrDefault("STRIMZI_NAMESPACE", "*");
64+
Set<String> namespaces = parseNamespaces(strimziNamespace);
65+
66+
operator.register(new KafkaAccessReconciler(operator.getKubernetesClient()),
67+
configuration -> configuration.settingNamespaces(namespaces));
68+
69+
try {
70+
operator.start();
71+
LOGGER.info("Kafka Access operator started successfully for namespaces: {}", namespaces);
72+
} catch (Exception e) {
73+
LOGGER.error("Failed to start Kafka Access operator. This may be due to missing RoleBindings for one or more namespaces: {}. " +
74+
"Please ensure that appropriate RoleBindings exist for all configured namespaces. Error: {}",
75+
namespaces, e.getMessage(), e);
76+
// Continue running to keep pod alive and allow health checks to work
77+
LOGGER.warn("Operator will continue running but may not be able to reconcile resources in namespaces without proper permissions");
4978
}
50-
operator.start();
79+
5180
Server server = new Server(HEALTH_CHECK_PORT);
5281
ServletHandler handler = new ServletHandler();
5382
server.setHandler(handler);

operator/src/test/java/io/strimzi/kafka/access/KafkaAccessReconcilerTest.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -560,23 +560,16 @@ void testReconcileWithUserProvidedSecretAndTestDeleteSecretWithNameChange() {
560560
assertThat(oldSecretBeforeRename).isNotNull();
561561
assertThat(oldSecretBeforeRename.getType()).isEqualTo("servicebinding.io/kafka");
562562

563-
// Retry update to handle race condition with reconciler status updates
564-
boolean updated = false;
565-
for (int i = 0; i < 5 && !updated; i++) {
566-
try {
567-
KafkaAccess currentKafkaAccess = client.resources(KafkaAccess.class).inNamespace(NAMESPACE).withName(NAME).get();
568-
assertThat(currentKafkaAccess).isNotNull();
569-
currentKafkaAccess.getSpec().setSecretName(NEW_USER_PROVIDED_SECRET_NAME);
570-
client.resources(KafkaAccess.class).resource(currentKafkaAccess).update();
571-
updated = true;
572-
} catch (io.fabric8.kubernetes.client.KubernetesClientException e) {
573-
if (e.getCode() != 409) {
574-
throw e;
575-
}
576-
// Retry on 409 conflict
577-
}
578-
}
579-
assertThat(updated).as("Failed to update KafkaAccess after retries").isTrue();
563+
KafkaAccess currentKafkaAccess = client.resources(KafkaAccess.class).inNamespace(NAMESPACE).withName(NAME).get();
564+
assertThat(currentKafkaAccess).isNotNull();
565+
566+
client.resources(KafkaAccess.class)
567+
.inNamespace(NAMESPACE)
568+
.withName(NAME)
569+
.edit(ka -> {
570+
ka.getSpec().setSecretName(NEW_USER_PROVIDED_SECRET_NAME);
571+
return ka;
572+
});
580573

581574
client.resources(KafkaAccess.class).inNamespace(NAMESPACE).withName(NAME).waitUntilCondition(updatedKafkaAccess -> {
582575
final Optional<String> bindingName = Optional.ofNullable(updatedKafkaAccess)

packaging/install/030-ClusterRoleBinding.yaml

Lines changed: 0 additions & 14 deletions
This file was deleted.

packaging/install/030-RoleBinding.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ metadata:
88
subjects:
99
- kind: ServiceAccount
1010
name: strimzi-access-operator
11-
namespace: strimzi-access-operator
11+
namespace: myproject
1212
roleRef:
1313
kind: ClusterRole
1414
name: strimzi-access-operator

0 commit comments

Comments
 (0)