8000 Merge results of mob programming by adam-sandor · Pull Request #6 · java-operator-sdk/samples · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jan 4, 2022. It is now read-only.

Merge results of mob programming #6

Merged
merged 19 commits into from
Aug 12, 2021
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
40 changes: 40 additions & 0 deletions .github/workflows/TomcatIntegrationTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Tomcat integration test
on:
push:
branches:
- "*"
jobs:
tomcat_integration_test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Helm
uses: azure/setup-helm@v1
with:
version: v3.4.0

- uses: actions/setup-python@v2
with:
python-version: 3.7

- name: Create kind cluster
uses: helm/kind-action@v1.2.0

- name: Apply CRDs
run: kubectl apply -f tomcat/k8s/crd.yaml

- name: Set up Java and Maven
uses: actions/setup-java@v1
with:
# java-version: ${{ matrix.java }}
java-version: 15
- uses: actions/cache@v2
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Run unit tests
run: mvn -B test --file tomcat/pom.xml
12 changes: 12 additions & 0 deletions tomcat/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@
<artifactId>takes</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.javaoperatorsdk.operator.sample;

import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;

@Group("tomcatoperator.io")
@Version("v1")
public class Webapp extends CustomResource<WebappSpec, WebappStatus> {}
public class Webapp extends CustomResource<WebappSpec, WebappStatus> implements Namespaced {}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public WebappController(KubernetesClient kubernetesClient) {

@Override
public UpdateControl<Webapp> createOrUpdateResource(Webapp webapp, Context<Webapp> context) {
if (Objects.equals(webapp.getSpec().getUrl(), webapp.getStatus().getDeployedArtifact())) {
if (webapp.getStatus() != null && Objects.equals(webapp.getSpec().getUrl(), webapp.getStatus().getDeployedArtifact())) {
return UpdateControl.noUpdate();
}

String[] command = new String[] {"wget", "-O", "/data/" + webapp.getSpec().getContextPath() + ".war", webapp.getSpec().getUrl()};

executeCommandInAllPods(kubernetesClient, webapp, command);

webapp.getStatus().setDeployedArtifact(webapp.getSpec().getUrl());
//webapp.getStatus().setDeployedArtifact(webapp.getSpec().getUrl());
return UpdateControl.updateStatusSubResource(webapp);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ spec:
- protocol: TCP
port: 80
targetPort: 8080
nodePort: 30000
type: NodePort
2 changes: 1 addition & 1 deletion tomcat/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%style{%d}{yellow} %style{%-30c{1.}}{cyan} %highlight{[%-5level] %msg%n%throwable}{INFO=black}"/>
<!-- <PatternLayout pattern="%style{%d}{yellow} %style{%-30c{1.}}{cyan} %highlight{[%-5level] %msg%n%throwable}{INFO=black}"/>-->
</Console>
</Appenders>
<Loggers>
Expand Down
71 changes: 71 additions & 0 deletions tomcat/src/test/java/sample/IntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package sample;

import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.javaoperatorsdk.operator.Operator;
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
import io.javaoperatorsdk.operator.sample.Tomcat;
import io.javaoperatorsdk.operator.sample.TomcatController;
import io.javaoperatorsdk.operator.sample.WebappController;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.*;

public class IntegrationTest {
@Test
public void test() throws InterruptedException {
Config config = new ConfigBuilder().withNamespace(null).build();
KubernetesClient client = new DefaultKubernetesClient(config);
Operator operator = new Operator(client, DefaultConfigurationService.instance());

TomcatController tomcatController = new TomcatController(client);
operator.register(tomcatController);

operator.register(new WebappController(client));

Tomcat tomcat = loadYaml(Tomcat.class, "tomcat-sample1.yaml");

tomcat.getSpec().setReplicas(3);
tomcat.getMetadata().setNamespace("tomcat-test");

MixedOperation<Tomcat, KubernetesResourceList<Tomcat>, Resource<Tomcat>> tomcatClient = client.customResources(Tomcat.class);

Namespace tt_ns = new NamespaceBuilder().withMetadata(new ObjectMetaBuilder().withName("tomcat-test").build()).build();

client.namespaces().delete(tt_ns);

await().atMost(300, SECONDS).until(() -> client.namespaces().withName("tomcat-test").get() == null);

client.namespaces().createOrReplace(tt_ns);

tomcatClient.inNamespace("tomcat-test").create(tomcat);

await().atMost(60, SECONDS).until(() -> {
Tomcat updatedTomcat = tomcatClient.inNamespace("tomcat-test").withName("test-tomcat1").get();
return updatedTomcat.getStatus() != null && (int) updatedTomcat.getStatus().getReadyReplicas() == 3;
});
}

private <T> T loadYaml(Class<T> clazz, String yaml) {
try (InputStream is = new FileInputStream("k8s/" + yaml)) {
return Serialization.unmarshal(is, clazz);
} catch (IOException ex) {
throw new IllegalStateException("Cannot find yaml on classpath: " + yaml);
}
}
}
0