Skip to content

feat: demonstrate contract-first CRDs #1524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sample-operators/leader-election/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Leader Election E2E Test

The purpose of this module is to e2e test leader election feature.
The purpose of this module is to e2e test leader election feature and to demonstrate contract-first CRDs.

The deployment is using directly pods in order to better control some aspects in test.
In real life this would be a Deployment.
In real life this would be a Deployment.

The custom resource definition (CRD) is defined in YAML in the folder `src/main/resources/kubernetes`.
Upon build, the [java-generator-maven-plugin](https://github.com/fabric8io/kubernetes-client/blob/master/doc/java-generation-from-CRD.md)
generates the Java code under `target/generated-sources/java`.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ rules:
- apiGroups:
- "sample.javaoperatorsdk"
resources:
- leaderelectiontestcustomresources
- leaderelectiontestcustomresources/status
- leaderelections
- leaderelections/status
verbs:
- '*'
- apiGroups:
Expand Down
4 changes: 2 additions & 2 deletions sample-operators/leader-election/k8s/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ rules:
- apiGroups:
- "sample.javaoperatorsdk"
resources:
- leaderelectiontestcustomresources
- leaderelectiontestcustomresources/status
- leaderelections
- leaderelections/status
verbs:
- '*'
- apiGroups:
Expand Down
21 changes: 16 additions & 5 deletions sample-operators/leader-election/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@
<artifactId>takes</artifactId>
<version>1.21.1</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>crd-generator-apt</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down Expand Up @@ -88,6 +83,22 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.0</version>
</plugin>
<plugin>
<!-- Generate Java code from the custom resource definitions -->
<groupId>io.fabric8</groupId>
<artifactId>java-generator-maven-plugin</artifactId>
<version>${fabric8-client.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<source>src/main/resources/kubernetes</source>
</configuration>
</plugin>
</plugins>
</build>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package io.javaoperatorsdk.operator.sample;

import java.time.Duration;
import java.util.ArrayList;

import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;

import javaoperatorsdk.sample.v1.LeaderElection;
import javaoperatorsdk.sample.v1.LeaderElectionStatus;

@ControllerConfiguration()
public class LeaderElectionTestReconciler
implements Reconciler<LeaderElectionTestCustomResource> {
implements Reconciler<LeaderElection> {

private final String reconcilerName;

Expand All @@ -18,12 +22,15 @@ public LeaderElectionTestReconciler(String reconcilerName) {
}

@Override
public UpdateControl<LeaderElectionTestCustomResource> reconcile(
LeaderElectionTestCustomResource resource,
Context<LeaderElectionTestCustomResource> context) {
public UpdateControl<LeaderElection> reconcile(
LeaderElection resource,
Context<LeaderElection> context) {

if (resource.getStatus() == null) {
resource.setStatus(new LeaderElectionTestStatus());
resource.setStatus(new LeaderElectionStatus());
}
if (resource.getStatus().getReconciledBy() == null) {
resource.getStatus().setReconciledBy(new ArrayList<>());
}

resource.getStatus().getReconciledBy().add(reconcilerName);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Custom Resource Definition that will be used to generate the Java classes in target/generated-sources/java
# See https://github.com/fabric8io/kubernetes-client/blob/master/doc/java-generation-from-CRD.md
# The Java classes will then be used to recreate this CR in target/classes/META-INF/fabric8
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: leaderelections.sample.javaoperatorsdk
spec:
group: sample.javaoperatorsdk
names:
kind: LeaderElection
singular: leaderelection
plural: leaderelections
shortNames:
- le
- les
scope: Namespaced
versions:
- name: v1
schema:
openAPIV3Schema:
properties:
status:
properties:
reconciledBy:
items:
type: string
type: array
type: object
type: object
served: true
storage: true
subresources:
status: {}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;

import javaoperatorsdk.sample.v1.LeaderElection;

import static io.javaoperatorsdk.operator.junit.AbstractOperatorExtension.CRD_READY_WAIT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
Expand Down Expand Up @@ -61,7 +63,7 @@ void otherInstancesTakesOverWhenSteppingDown(String yamlFilePrefix) {
await().pollDelay(Duration.ofSeconds(MINIMAL_SECONDS_FOR_RENEWAL))
.atMost(Duration.ofSeconds(MAX_WAIT_SECONDS))
.untilAsserted(() -> {
var actualStatus = client.resources(LeaderElectionTestCustomResource.class)
var actualStatus = client.resources(LeaderElection.class)
.inNamespace(namespace).withName(TEST_RESOURCE_NAME).get().getStatus();

assertThat(actualStatus).isNotNull();
Expand All @@ -71,14 +73,14 @@ void otherInstancesTakesOverWhenSteppingDown(String yamlFilePrefix) {

client.pods().inNamespace(namespace).withName(OPERATOR_1_POD_NAME).delete();

var actualListSize = client.resources(LeaderElectionTestCustomResource.class)
var actualListSize = client.resources(LeaderElection.class)
.inNamespace(namespace).withName(TEST_RESOURCE_NAME).get().getStatus().getReconciledBy()
.size();

await().pollDelay(Duration.ofSeconds(MINIMAL_SECONDS_FOR_RENEWAL))
.atMost(Duration.ofSeconds(240))
.untilAsserted(() -> {
var actualStatus = client.resources(LeaderElectionTestCustomResource.class)
var actualStatus = client.resources(LeaderElection.class)
.inNamespace(namespace).withName(TEST_RESOURCE_NAME).get().getStatus();

assertThat(actualStatus).isNotNull();
Expand All @@ -87,7 +89,7 @@ void otherInstancesTakesOverWhenSteppingDown(String yamlFilePrefix) {
});

assertReconciliations(
client.resources(LeaderElectionTestCustomResource.class).inNamespace(namespace)
client.resources(LeaderElection.class).inNamespace(namespace)
.withName(TEST_RESOURCE_NAME).get().getStatus().getReconciledBy());
}

Expand All @@ -104,7 +106,7 @@ private void assertReconciliations(List<String> reconciledBy) {
}

private void applyCustomResource() {
var res = new LeaderElectionTestCustomResource();
var res = new LeaderElection();
res.setMetadata(new ObjectMetaBuilder()
.withName(TEST_RESOURCE_NAME)
.withNamespace(namespace)
Expand Down Expand Up @@ -150,7 +152,7 @@ private void deployOperatorsInOrder(String yamlFilePrefix) {

void applyCRD() {
String path =
"./target/classes/META-INF/fabric8/leaderelectiontestcustomresources.sample.javaoperatorsdk-v1.yml";
"./src/main/resources/kubernetes/leaderelections.sample.javaoperatorsdk-v1.yml";
try (InputStream is = new FileInputStream(path)) {
final var crd = client.load(is);
crd.createOrReplace();
Expand Down