Skip to content

Commit

Permalink
check in LE init
Browse files Browse the repository at this point in the history
  • Loading branch information
csviri committed May 24, 2023
1 parent fd8abf7 commit fe8ab78
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void init(LeaderElectionConfiguration config, KubernetesClient client) {
log.error(message);
throw new IllegalArgumentException(message);
}
checkLeaseAccess();
final var lock = new LeaseLock(leaseNamespace, leaseName, identity);
// releaseOnCancel is not used in the underlying implementation
leaderElector =
Expand Down Expand Up @@ -99,7 +100,6 @@ private String identity(LeaderElectionConfiguration config) {

public void start() {
if (isLeaderElectionEnabled()) {
checkLeaseAccess();
leaderElectionFuture = leaderElector.start();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.javaoperatorsdk.operator.api.config.ConfigurationServiceProvider;
import io.javaoperatorsdk.operator.api.config.LeaderElectionConfiguration;
Expand All @@ -27,7 +28,7 @@ class LeaderElectionManagerTest {
@BeforeEach
void setUp() {
ControllerManager controllerManager = mock(ControllerManager.class);
kubernetesClient = mock(KubernetesClient.class);
kubernetesClient = MockKubernetesClient.client(ConfigMap.class);
leaderElectionManager = new LeaderElectionManager(controllerManager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.api.model.authorization.v1.*;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.V1ApiextensionAPIGroupDSL;
import io.fabric8.kubernetes.client.dsl.*;
Expand Down Expand Up @@ -70,6 +71,25 @@ public static <T extends HasMetadata> KubernetesClient client(Class<T> clazz,
when(v1.customResourceDefinitions()).thenReturn(operation);
when(operation.withName(any())).thenReturn(mock(Resource.class));

var mockSelfSubjectReviewHandler = mock(NamespaceableResource.class);
when(mockSelfSubjectReviewHandler.create())
.thenReturn(validSelfSubjectRulesReviewForLeaderElection());
when(client.resource(any(SelfSubjectRulesReview.class)))
.thenReturn(mockSelfSubjectReviewHandler);

return client;
}

private static SelfSubjectRulesReview validSelfSubjectRulesReviewForLeaderElection() {
SelfSubjectRulesReview res = new SelfSubjectRulesReview();
res.setStatus(new SubjectRulesReviewStatusBuilder()
.withResourceRules(new ResourceRuleBuilder()
.withApiGroups("coordination.k8s.io")
.withResources("leases")
.withVerbs("*")
.build())
.build());
return res;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void initOperator() {

@Test
@DisplayName("should throw `OperationException` when Configuration is null")
public void shouldThrowOperatorExceptionWhenConfigurationIsNull() {
void shouldThrowOperatorExceptionWhenConfigurationIsNull() {
// use a ConfigurationService that doesn't automatically create configurations
ConfigurationServiceProvider.reset();
ConfigurationServiceProvider.set(new AbstractConfigurationService(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,14 @@ void operatorStopsIfNoLeaderElectionPermission() {
.withImpersonateUsername("leader-elector-stop-noaccess")
.build()).build();

var operator = new Operator(client, o -> {
o.withLeaderElectionConfiguration(
new LeaderElectionConfiguration("lease1", "default"));
o.withStopOnInformerErrorDuringStartup(false);
});
operator.register(new TestReconciler(), o -> o.settingNamespace("default"));

OperatorException exception = assertThrows(
OperatorException.class,
operator::start);
OperatorException.class, () -> new Operator(client, o -> {
o.withLeaderElectionConfiguration(
new LeaderElectionConfiguration("lease1", "default"));
o.withStopOnInformerErrorDuringStartup(false);
}));

assertThat(exception.getCause().getMessage())
assertThat(exception.getMessage())
.contains(NO_PERMISSION_TO_LEASE_RESOURCE_MESSAGE);
}

Expand Down

0 comments on commit fe8ab78

Please sign in to comment.