Skip to content

WebPage E2E Improvement #1226

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 7 commits into from
May 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

// this annotation only activates when using managed dependents and is not otherwise needed
@KubernetesDependent(labelSelector = SELECTOR)
class ConfigMapDependentResource extends CRUKubernetesDependentResource<ConfigMap, WebPage> {
public class ConfigMapDependentResource extends CRUKubernetesDependentResource<ConfigMap, WebPage> {

private static final Logger log = LoggerFactory.getLogger(ConfigMapDependentResource.class);

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

// this annotation only activates when using managed dependents and is not otherwise needed
@KubernetesDependent(labelSelector = WebPageManagedDependentsReconciler.SELECTOR)
class DeploymentDependentResource extends CRUKubernetesDependentResource<Deployment, WebPage> {
public class DeploymentDependentResource
extends CRUKubernetesDependentResource<Deployment, WebPage> {

public DeploymentDependentResource() {
super(Deployment.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

// this annotation only activates when using managed dependents and is not otherwise needed
@KubernetesDependent(labelSelector = WebPageManagedDependentsReconciler.SELECTOR)
class ServiceDependentResource extends CRUKubernetesDependentResource<Service, WebPage> {
public class ServiceDependentResource extends CRUKubernetesDependentResource<Service, WebPage> {

public ServiceDependentResource() {
super(Service.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
public class WebPage extends CustomResource<WebPageSpec, WebPageStatus>
implements Namespaced {

@Override
protected WebPageStatus initStatus() {
return new WebPageStatus();
}

@Override
public String toString() {
return "WebPage{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
import static io.javaoperatorsdk.operator.sample.Utils.createStatus;
import static io.javaoperatorsdk.operator.sample.Utils.handleError;
import static io.javaoperatorsdk.operator.sample.Utils.simulateErrorIfRequested;
import static io.javaoperatorsdk.operator.sample.WebPageManagedDependentsReconciler.SELECTOR;

/**
* Shows how to implement a reconciler with managed dependent resources.
*/
@ControllerConfiguration(
labelSelector = SELECTOR,
dependents = {
@Dependent(type = ConfigMapDependentResource.class),
@Dependent(type = DeploymentDependentResource.class),
Expand All @@ -27,7 +25,7 @@
public class WebPageManagedDependentsReconciler
implements Reconciler<WebPage>, ErrorStatusHandler<WebPage> {

static final String SELECTOR = "managed";
public static final String SELECTOR = "managed";

@Override
public ErrorStatusUpdateControl<WebPage> updateErrorStatus(WebPage resource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

public class WebPageOperator {
public static final String WEBPAGE_RECONCILER_ENV = "WEBPAGE_RECONCILER";
public static final String WEBPAGE_RECONCILER_ENV_VALUE = "classic";
public static final String WEBPAGE_CLASSIC_RECONCILER_ENV_VALUE = "classic";
public static final String WEBPAGE_MANAGED_DEPENDENT_RESOURCE_ENV_VALUE = "managed";
private static final Logger log = LoggerFactory.getLogger(WebPageOperator.class);


Expand All @@ -27,8 +28,12 @@ public static void main(String[] args) throws IOException {
Config config = new ConfigBuilder().withNamespace(null).build();
KubernetesClient client = new DefaultKubernetesClient(config);
Operator operator = new Operator(client);
if (WEBPAGE_RECONCILER_ENV_VALUE.equals(System.getenv(WEBPAGE_RECONCILER_ENV))) {
String reconcilerEnvVar = System.getenv(WEBPAGE_RECONCILER_ENV);
if (WEBPAGE_CLASSIC_RECONCILER_ENV_VALUE.equals(reconcilerEnvVar)) {
operator.register(new WebPageReconciler(client));
} else if (WEBPAGE_MANAGED_DEPENDENT_RESOURCE_ENV_VALUE
.equals(reconcilerEnvVar)) {
operator.register(new WebPageManagedDependentsReconciler());
} else {
operator.register(new WebPageStandaloneDependentsReconciler(client));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Objects;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
Expand All @@ -27,10 +26,15 @@

public abstract class WebPageOperatorAbstractTest {

static final Logger log = LoggerFactory.getLogger(WebPageOperatorDependentResourcesE2E.class);
static final Logger log =
LoggerFactory.getLogger(WebPageOperatorStandaloneDependentResourcesE2E.class);

static final KubernetesClient client = new DefaultKubernetesClient();
public static final String TEST_PAGE = "test-page";
public static final String TITLE1 = "Hello Operator World";
public static final String TITLE2 = "Hello Operator World Title 2";
public static final int WAIT_SECONDS = 20;
public static final Duration POLL_INTERVAL = Duration.ofSeconds(1);

boolean isLocal() {
String deployment = System.getProperty("test.deployment");
Expand All @@ -42,24 +46,42 @@ boolean isLocal() {
@Test
void testAddingWebPage() {

var webPage = createWebPage();
var webPage = createWebPage(TITLE1);
operator().create(WebPage.class, webPage);

await()
.atMost(Duration.ofSeconds(20))
.pollInterval(Duration.ofSeconds(1))
.until(
.atMost(Duration.ofSeconds(WAIT_SECONDS))
.pollInterval(POLL_INTERVAL)
.untilAsserted(
() -> {
var actual = operator().get(WebPage.class, TEST_PAGE);
var deployment = operator().get(Deployment.class, deploymentName(webPage));

return Boolean.TRUE.equals(actual.getStatus().getAreWeGood())
&& Objects.equals(deployment.getSpec().getReplicas(),
deployment.getStatus().getReadyReplicas());
assertThat(actual.getStatus().getAreWeGood()).isTrue();
assertThat(deployment.getSpec().getReplicas())
.isEqualTo(deployment.getStatus().getReadyReplicas());
});

String response = httpGetForWebPage(webPage);
assertThat(response).contains("<title>Hello Operator World</title>");
assertThat(httpGetForWebPage(webPage)).contains(TITLE1);

// update part: changing title
operator().replace(WebPage.class, createWebPage(TITLE2));

await().atMost(Duration.ofSeconds(WAIT_SECONDS))
.pollInterval(POLL_INTERVAL)
.untilAsserted(() -> {
String page = httpGetForWebPage(webPage);
assertThat(page).isNotNull().contains(TITLE2);
});

// delete part: deleting webpage
operator().delete(WebPage.class, createWebPage(TITLE2));

await().atMost(Duration.ofSeconds(WAIT_SECONDS))
.pollInterval(POLL_INTERVAL)
.untilAsserted(() -> {
Deployment deployment = operator().get(Deployment.class, deploymentName(webPage));
assertThat(deployment).isNull();
});
}

String httpGetForWebPage(WebPage webPage) {
Expand All @@ -75,7 +97,7 @@ String httpGetForWebPage(WebPage webPage) {
.uri(new URI("http://localhost:" + portForward.getLocalPort())).build();
return httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body();
} catch (URISyntaxException | IOException | InterruptedException e) {
throw new IllegalStateException(e);
return null;
} finally {
if (portForward != null) {
try {
Expand All @@ -87,7 +109,7 @@ String httpGetForWebPage(WebPage webPage) {
}
}

WebPage createWebPage() {
WebPage createWebPage(String title) {
WebPage webPage = new WebPage();
webPage.setMetadata(new ObjectMeta());
webPage.getMetadata().setName(TEST_PAGE);
Expand All @@ -98,7 +120,7 @@ WebPage createWebPage() {
.setHtml(
"<html>\n"
+ " <head>\n"
+ " <title>Hello Operator World</title>\n"
+ " <title>" + title + "</title>\n"
+ " </head>\n"
+ " <body>\n"
+ " Hello World! \n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import io.javaoperatorsdk.operator.junit.ClusterOperatorExtension;
import io.javaoperatorsdk.operator.junit.LocalOperatorExtension;

import static io.javaoperatorsdk.operator.sample.WebPageOperator.WEBPAGE_CLASSIC_RECONCILER_ENV_VALUE;
import static io.javaoperatorsdk.operator.sample.WebPageOperator.WEBPAGE_RECONCILER_ENV;
import static io.javaoperatorsdk.operator.sample.WebPageOperator.WEBPAGE_RECONCILER_ENV_VALUE;
import static io.javaoperatorsdk.operator.sample.WebPageReconciler.lowLevelLabel;

class WebPageOperatorE2E extends WebPageOperatorAbstractTest {
Expand All @@ -40,7 +40,8 @@ public WebPageOperatorE2E() throws FileNotFoundException {}
container.setEnv(new ArrayList<>());
}
container.getEnv().add(
new EnvVar(WEBPAGE_RECONCILER_ENV, WEBPAGE_RECONCILER_ENV_VALUE, null));
new EnvVar(WEBPAGE_RECONCILER_ENV, WEBPAGE_CLASSIC_RECONCILER_ENV_VALUE,
null));
})
.build();

Expand All @@ -51,8 +52,8 @@ AbstractOperatorExtension operator() {
}

@Override
WebPage createWebPage() {
WebPage page = super.createWebPage();
WebPage createWebPage(String title) {
WebPage page = super.createWebPage(title);
page.getMetadata().setLabels(lowLevelLabel());
return page;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.javaoperatorsdk.operator.sample;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import org.junit.jupiter.api.extension.RegisterExtension;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.javaoperatorsdk.operator.junit.AbstractOperatorExtension;
import io.javaoperatorsdk.operator.junit.ClusterOperatorExtension;
import io.javaoperatorsdk.operator.junit.LocalOperatorExtension;

import static io.javaoperatorsdk.operator.sample.WebPageOperator.*;

class WebPageOperatorManagedDependentResourcesE2E extends WebPageOperatorAbstractTest {

public WebPageOperatorManagedDependentResourcesE2E() throws FileNotFoundException {}

@RegisterExtension
AbstractOperatorExtension operator =
isLocal()
? LocalOperatorExtension.builder()
.waitForNamespaceDeletion(false)
.withReconciler(new WebPageManagedDependentsReconciler())
.build()
: ClusterOperatorExtension.builder()
.waitForNamespaceDeletion(false)
.withOperatorDeployment(client.load(new FileInputStream("k8s/operator.yaml")).get(),
resources -> {
Deployment deployment = (Deployment) resources.stream()
.filter(r -> r instanceof Deployment).findFirst().orElseThrow();
Container container =
deployment.getSpec().getTemplate().getSpec().getContainers().get(0);
if (container.getEnv() == null) {
container.setEnv(new ArrayList<>());
}
container.getEnv().add(
new EnvVar(WEBPAGE_RECONCILER_ENV,
WEBPAGE_MANAGED_DEPENDENT_RESOURCE_ENV_VALUE, null));
})
.build();

@Override
AbstractOperatorExtension operator() {
return operator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import io.javaoperatorsdk.operator.junit.ClusterOperatorExtension;
import io.javaoperatorsdk.operator.junit.LocalOperatorExtension;

class WebPageOperatorDependentResourcesE2E extends WebPageOperatorAbstractTest {
class WebPageOperatorStandaloneDependentResourcesE2E extends WebPageOperatorAbstractTest {

public WebPageOperatorDependentResourcesE2E() throws FileNotFoundException {}
public WebPageOperatorStandaloneDependentResourcesE2E() throws FileNotFoundException {}

@RegisterExtension
AbstractOperatorExtension operator =
Expand Down