Skip to content

Commit 8267dce

Browse files
committed
e2e test
1 parent 4f25906 commit 8267dce

File tree

18 files changed

+464
-54
lines changed

18 files changed

+464
-54
lines changed

.github/workflows/e2e-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- "sample-operators/mysql-schema"
2222
- "sample-operators/tomcat-operator"
2323
- "sample-operators/webpage"
24+
- "sample-operators/leader-election"
2425
runs-on: ubuntu-latest
2526
steps:
2627
- name: Checkout

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ private void stopLeading() {
6464
}
6565

6666
private String identity(LeaderElectionConfiguration config) {
67-
String identity = config.getIdentity().orElse(System.getenv("HOSTNAME"));
68-
if (identity == null || identity.isBlank()) {
69-
identity = UUID.randomUUID().toString();
67+
String id = config.getIdentity().orElse(System.getenv("HOSTNAME"));
68+
if (id == null || id.isBlank()) {
69+
id = UUID.randomUUID().toString();
7070
}
71-
return identity;
71+
return id;
7272
}
7373

7474
public void start() {

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/ConfigurationServiceOverrider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public ConfigurationServiceOverrider withObjectMapper(ObjectMapper objectMapper)
8484

8585
public LeaderElectionConfiguration withLeaderElectionConfiguration(
8686
LeaderElectionConfiguration leaderElectionConfiguration) {
87+
this.leaderElectionConfiguration = leaderElectionConfiguration;
8788
return leaderElectionConfiguration;
8889
}
8990

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/LeaderElectionConfigurationBuilder.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ public final class LeaderElectionConfigurationBuilder {
1111
private Duration leaseDuration = LEASE_DURATION_DEFAULT_VALUE;
1212
private Duration renewDeadline = RENEW_DEADLINE_DEFAULT_VALUE;
1313
private Duration retryPeriod = RETRY_PERIOD_DEFAULT_VALUE;
14+
private String identity;
1415

15-
private LeaderElectionConfigurationBuilder() {}
16+
public LeaderElectionConfigurationBuilder() {}
1617

1718
public static LeaderElectionConfigurationBuilder aLeaderElectionConfiguration() {
1819
return new LeaderElectionConfigurationBuilder();
@@ -43,8 +44,13 @@ public LeaderElectionConfigurationBuilder withRetryPeriod(Duration retryPeriod)
4344
return this;
4445
}
4546

47+
public LeaderElectionConfigurationBuilder withIdentity(String identity) {
48+
this.identity = identity;
49+
return this;
50+
}
51+
4652
public LeaderElectionConfiguration build() {
4753
return new LeaderElectionConfiguration(leaseName, leaseNamespace, leaseDuration, renewDeadline,
48-
retryPeriod);
54+
retryPeriod, identity);
4955
}
5056
}

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

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

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/leaderelection/LeaderElectionTestReconciler.java

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Leader Election E2E Test
2+
3+
The purpose of this module is to e2e test leader election feature.
4+
5+
The deployment is using directly pods in order to better control some aspects in test.
6+
In real life this would be a Deployment.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: leader-election-operator-2
5+
spec:
6+
serviceAccountName: leader-election-operator
7+
containers:
8+
- name: operator
9+
image: leader-election-operator
10+
imagePullPolicy: Never
11+
env:
12+
- name: POD_NAMESPACE
13+
valueFrom:
14+
fieldRef:
15+
fieldPath: metadata.namespace
16+
- name: POD_NAME
17+
valueFrom:
18+
fieldRef:
19+
fieldPath: metadata.name
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
apiVersion: v1
2+
kind: ServiceAccount
3+
metadata:
4+
name: leader-election-operator
5+
6+
---
7+
apiVersion: v1
8+
kind: Pod
9+
metadata:
10+
name: leader-election-operator-1
11+
spec:
12+
serviceAccountName: leader-election-operator
13+
containers:
14+
- name: operator
15+
image: leader-election-operator
16+
imagePullPolicy: Never
17+
env:
18+
- name: POD_NAMESPACE
19+
valueFrom:
20+
fieldRef:
21+
fieldPath: metadata.namespace
22+
- name: POD_NAME
23+
valueFrom:
24+
fieldRef:
25+
fieldPath: metadata.name
26+
27+
---
28+
apiVersion: rbac.authorization.k8s.io/v1
29+
kind: ClusterRoleBinding
30+
metadata:
31+
name: operator-admin
32+
subjects:
33+
- kind: ServiceAccount
34+
name: leader-election-operator
35+
roleRef:
36+
kind: ClusterRole
37+
name: leader-election-operator
38+
apiGroup: ""
39+
40+
---
41+
apiVersion: rbac.authorization.k8s.io/v1
42+
kind: ClusterRole
43+
metadata:
44+
name: leader-election-operator
45+
rules:
46+
- apiGroups:
47+
- "apiextensions.k8s.io"
48+
resources:
49+
- customresourcedefinitions
50+
verbs:
51+
- '*'
52+
- apiGroups:
53+
- "sample.javaoperatorsdk"
54+
resources:
55+
- leaderelectiontests
56+
- leaderelectiontests/status
57+
verbs:
58+
- '*'
59+
- apiGroups:
60+
- "coordination.k8s.io"
61+
resources:
62+
- "leases"
63+
verbs:
64+
- '*'
65+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>io.javaoperatorsdk</groupId>
9+
<artifactId>sample-operators</artifactId>
10+
<version>3.1.1-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>sample-leader-election</artifactId>
14+
<name>Operator SDK - Samples - Leader Election</name>
15+
<description>An E2E test for leader election</description>
16+
<packaging>jar</packaging>
17+
18+
<properties>
19+
<maven.compiler.source>11</maven.compiler.source>
20+
<maven.compiler.target>11</maven.compiler.target>
21+
<jib-maven-plugin.version>3.2.1</jib-maven-plugin.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>io.javaoperatorsdk</groupId>
27+
<artifactId>operator-framework</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.apache.logging.log4j</groupId>
31+
<artifactId>log4j-slf4j-impl</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.takes</groupId>
35+
<artifactId>takes</artifactId>
36+
<version>1.21.1</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>io.fabric8</groupId>
40+
<artifactId>crd-generator-apt</artifactId>
41+
<scope>provided</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>io.fabric8</groupId>
45+
<artifactId>crd-generator-api</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.awaitility</groupId>
49+
<artifactId>awaitility</artifactId>
50+
<scope>compile</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>io.javaoperatorsdk</groupId>
54+
<artifactId>operator-framework-junit-5</artifactId>
55+
<version>${project.version}</version>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>com.google.cloud.tools</groupId>
63+
<artifactId>jib-maven-plugin</artifactId>
64+
<version>${jib-maven-plugin.version}</version>
65+
<configuration>
66+
<from>
67+
<image>gcr.io/distroless/java:11</image>
68+
</from>
69+
<to>
70+
<image>leader-election-operator</image>
71+
</to>
72+
</configuration>
73+
</plugin>
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-compiler-plugin</artifactId>
77+
<version>3.10.0</version>
78+
</plugin>
79+
</plugins>
80+
</build>
81+
82+
</project>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.javaoperatorsdk.operator.sample.leaderelection;
1+
package io.javaoperatorsdk.operator.sample;
22

33
import io.fabric8.kubernetes.api.model.Namespaced;
44
import io.fabric8.kubernetes.client.CustomResource;
@@ -9,7 +9,7 @@
99
@Group("sample.javaoperatorsdk")
1010
@Version("v1")
1111
@ShortNames("le")
12-
public class LeaderElectionTestCustomResource
13-
extends CustomResource<Void, LeaderElectionTestCustomResourceStatus>
12+
public class LeaderElectionTest
13+
extends CustomResource<Void, LeaderElectionTestStatus>
1414
implements Namespaced {
1515
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.javaoperatorsdk.operator.sample;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import io.fabric8.kubernetes.client.ConfigBuilder;
7+
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
8+
import io.javaoperatorsdk.operator.Operator;
9+
import io.javaoperatorsdk.operator.api.config.LeaderElectionConfigurationBuilder;
10+
11+
public class LeaderElectionTestOperator {
12+
13+
private static final Logger log = LoggerFactory.getLogger(LeaderElectionTestOperator.class);
14+
15+
public static void main(String[] args) {
16+
System.out.println("Starting...");
17+
String identity = System.getenv("POD_NAME");
18+
String namespace = System.getenv("POD_NAMESPACE");
19+
20+
log.info("Starting operator with identity: {}", identity);
21+
22+
var client = new KubernetesClientBuilder().withConfig(new ConfigBuilder()
23+
.withNamespace(namespace)
24+
.build()).build();
25+
26+
Operator operator = new Operator(client,
27+
c -> c.withLeaderElectionConfiguration(new LeaderElectionConfigurationBuilder()
28+
.withLeaseName("leader-election-test")
29+
.withLeaseNamespace(namespace)
30+
.withIdentity(identity)
31+
.build()));
32+
operator.register(new LeaderElectionTestReconciler(identity));
33+
operator.installShutdownHook();
34+
operator.start();
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.javaoperatorsdk.operator.sample;
2+
3+
import java.time.Duration;
4+
5+
import io.javaoperatorsdk.operator.api.reconciler.Context;
6+
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
7+
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
8+
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
9+
10+
@ControllerConfiguration()
11+
public class LeaderElectionTestReconciler
12+
implements Reconciler<LeaderElectionTest> {
13+
14+
15+
private final String reconcilerName;
16+
17+
public LeaderElectionTestReconciler(String reconcilerName) {
18+
this.reconcilerName = reconcilerName;
19+
}
20+
21+
@Override
22+
public UpdateControl<LeaderElectionTest> reconcile(
23+
LeaderElectionTest resource,
24+
Context<LeaderElectionTest> context) {
25+
26+
if (resource.getStatus() == null) {
27+
resource.setStatus(new LeaderElectionTestStatus());
28+
}
29+
30+
resource.getStatus().getReconciledBy().add(reconcilerName);
31+
32+
return UpdateControl.patchStatus(resource).rescheduleAfter(Duration.ofSeconds(1));
33+
}
34+
35+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package io.javaoperatorsdk.operator.sample.leaderelection;
1+
package io.javaoperatorsdk.operator.sample;
22

33
import java.util.ArrayList;
44
import java.util.List;
55

6-
public class LeaderElectionTestCustomResourceStatus {
6+
public class LeaderElectionTestStatus {
77

88
private List<String> reconciledBy;
99

@@ -14,7 +14,7 @@ public List<String> getReconciledBy() {
1414
return reconciledBy;
1515
}
1616

17-
public LeaderElectionTestCustomResourceStatus setReconciledBy(List<String> reconciledBy) {
17+
public LeaderElectionTestStatus setReconciledBy(List<String> reconciledBy) {
1818
this.reconciledBy = reconciledBy;
1919
return this;
2020
}

0 commit comments

Comments
 (0)