Skip to content

Commit fe8ab78

Browse files
committed
check in LE init
1 parent fd8abf7 commit fe8ab78

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/LeaderElectionManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public void init(LeaderElectionConfiguration config, KubernetesClient client) {
5050
log.error(message);
5151
throw new IllegalArgumentException(message);
5252
}
53+
checkLeaseAccess();
5354
final var lock = new LeaseLock(leaseNamespace, leaseName, identity);
5455
// releaseOnCancel is not used in the underlying implementation
5556
leaderElector =
@@ -99,7 +100,6 @@ private String identity(LeaderElectionConfiguration config) {
99100

100101
public void start() {
101102
if (isLeaderElectionEnabled()) {
102-
checkLeaseAccess();
103103
leaderElectionFuture = leaderElector.start();
104104
}
105105
}

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/LeaderElectionManagerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.junit.jupiter.api.Test;
1010
import org.junit.jupiter.api.io.TempDir;
1111

12+
import io.fabric8.kubernetes.api.model.ConfigMap;
1213
import io.fabric8.kubernetes.client.KubernetesClient;
1314
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider;
1415
import io.javaoperatorsdk.operator.api.config.LeaderElectionConfiguration;
@@ -27,7 +28,7 @@ class LeaderElectionManagerTest {
2728
@BeforeEach
2829
void setUp() {
2930
ControllerManager controllerManager = mock(ControllerManager.class);
30-
kubernetesClient = mock(KubernetesClient.class);
31+
kubernetesClient = MockKubernetesClient.client(ConfigMap.class);
3132
leaderElectionManager = new LeaderElectionManager(controllerManager);
3233
}
3334

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/MockKubernetesClient.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import io.fabric8.kubernetes.api.model.HasMetadata;
88
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
9+
import io.fabric8.kubernetes.api.model.authorization.v1.*;
910
import io.fabric8.kubernetes.client.KubernetesClient;
1011
import io.fabric8.kubernetes.client.V1ApiextensionAPIGroupDSL;
1112
import io.fabric8.kubernetes.client.dsl.*;
@@ -70,6 +71,25 @@ public static <T extends HasMetadata> KubernetesClient client(Class<T> clazz,
7071
when(v1.customResourceDefinitions()).thenReturn(operation);
7172
when(operation.withName(any())).thenReturn(mock(Resource.class));
7273

74+
var mockSelfSubjectReviewHandler = mock(NamespaceableResource.class);
75+
when(mockSelfSubjectReviewHandler.create())
76+
.thenReturn(validSelfSubjectRulesReviewForLeaderElection());
77+
when(client.resource(any(SelfSubjectRulesReview.class)))
78+
.thenReturn(mockSelfSubjectReviewHandler);
79+
7380
return client;
7481
}
82+
83+
private static SelfSubjectRulesReview validSelfSubjectRulesReviewForLeaderElection() {
84+
SelfSubjectRulesReview res = new SelfSubjectRulesReview();
85+
res.setStatus(new SubjectRulesReviewStatusBuilder()
86+
.withResourceRules(new ResourceRuleBuilder()
87+
.withApiGroups("coordination.k8s.io")
88+
.withResources("leases")
89+
.withVerbs("*")
90+
.build())
91+
.build());
92+
return res;
93+
}
94+
7595
}

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/OperatorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void initOperator() {
4343

4444
@Test
4545
@DisplayName("should throw `OperationException` when Configuration is null")
46-
public void shouldThrowOperatorExceptionWhenConfigurationIsNull() {
46+
void shouldThrowOperatorExceptionWhenConfigurationIsNull() {
4747
// use a ConfigurationService that doesn't automatically create configurations
4848
ConfigurationServiceProvider.reset();
4949
ConfigurationServiceProvider.set(new AbstractConfigurationService(null));

operator-framework/src/test/java/io/javaoperatorsdk/operator/LeaderElectionPermissionIT.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,14 @@ void operatorStopsIfNoLeaderElectionPermission() {
3232
.withImpersonateUsername("leader-elector-stop-noaccess")
3333
.build()).build();
3434

35-
var operator = new Operator(client, o -> {
36-
o.withLeaderElectionConfiguration(
37-
new LeaderElectionConfiguration("lease1", "default"));
38-
o.withStopOnInformerErrorDuringStartup(false);
39-
});
40-
operator.register(new TestReconciler(), o -> o.settingNamespace("default"));
41-
4235
OperatorException exception = assertThrows(
43-
OperatorException.class,
44-
operator::start);
36+
OperatorException.class, () -> new Operator(client, o -> {
37+
o.withLeaderElectionConfiguration(
38+
new LeaderElectionConfiguration("lease1", "default"));
39+
o.withStopOnInformerErrorDuringStartup(false);
40+
}));
4541

46-
assertThat(exception.getCause().getMessage())
42+
assertThat(exception.getMessage())
4743
.contains(NO_PERMISSION_TO_LEASE_RESOURCE_MESSAGE);
4844
}
4945

0 commit comments

Comments
 (0)