-
Notifications
You must be signed in to change notification settings - Fork 218
Add Webpage Sample to V2 + fix for observed generation #693
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
Changes from 11 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
b0b7cc0
feat: webpage sample
csviri 333026e
fix: build
csviri b57111a
feat: webpage sample
csviri 1b5ee93
fix: build
csviri e50b260
Merge remote-tracking branch 'origin/v2-webpage-sample' into v2-webpa…
csviri 2c2c25a
fix: webpage refactor and fix
csviri a33973b
docs: fix renaming
csviri bb33b7d
fix: added obsered generation
csviri 72806b8
fix: sample
csviri 4d35aec
fix: observed generation IT
csviri 949ec28
fix: formatting
csviri d842c1f
refactor: API changes of Update Control (#694)
csviri f075aec
fix: crd generation
csviri 1fc7f6e
feat: webpage sample
csviri 5de45d9
fix: build
csviri 4e43736
feat: webpage sample
csviri ef74085
fix: build
csviri d68b961
fix: webpage refactor and fix
csviri ada5fae
docs: fix renaming
csviri 37e69ea
fix: added obsered generation
csviri 384cfb3
fix: sample
csviri ace6670
fix: observed generation IT
csviri b7d684c
fix: formatting
csviri 6636c3d
fix: crd generation
csviri ae1cdfa
Merge remote-tracking branch 'origin/v2-webpage-sample' into v2-webpa…
csviri 32410d8
fix: rebase on v2
csviri 3bee8ab
fix: remove unnecessary pom sections
csviri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...tor-framework/src/test/java/io/javaoperatorsdk/operator/ObservedGenerationHandlingIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService; | ||
import io.javaoperatorsdk.operator.junit.OperatorExtension; | ||
import io.javaoperatorsdk.operator.sample.observedgeneration.ObservedGenerationTestCustomResource; | ||
import io.javaoperatorsdk.operator.sample.observedgeneration.ObservedGenerationTestReconciler; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class ObservedGenerationHandlingIT { | ||
@RegisterExtension | ||
OperatorExtension operator = | ||
OperatorExtension.builder() | ||
.withConfigurationService(DefaultConfigurationService.instance()) | ||
.withReconciler(new ObservedGenerationTestReconciler()) | ||
.build(); | ||
|
||
@Test | ||
public void testReconciliationOfNonCustomResourceAndStatusUpdate() { | ||
var resource = new ObservedGenerationTestCustomResource(); | ||
resource.setMetadata(new ObjectMeta()); | ||
resource.getMetadata().setName("observed-gen1"); | ||
|
||
var createdResource = operator.create(ObservedGenerationTestCustomResource.class, resource); | ||
|
||
await().atMost(10, TimeUnit.SECONDS).untilAsserted(() -> { | ||
var d = operator.get(ObservedGenerationTestCustomResource.class, | ||
createdResource.getMetadata().getName()); | ||
assertThat(d.getStatus().getObservedGeneration()).isNotNull(); | ||
assertThat(d.getStatus().getObservedGeneration()).isEqualTo(1); | ||
}); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...aoperatorsdk/operator/sample/observedgeneration/ObservedGenerationTestCustomResource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.javaoperatorsdk.operator.sample.observedgeneration; | ||
|
||
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.Kind; | ||
import io.fabric8.kubernetes.model.annotation.ShortNames; | ||
import io.fabric8.kubernetes.model.annotation.Version; | ||
|
||
@Group("sample.javaoperatorsdk") | ||
@Version("v1") | ||
@Kind("ObservedGenerationTestCustomResource") | ||
@ShortNames("og") | ||
public class ObservedGenerationTestCustomResource | ||
extends CustomResource<Void, ObservedGenerationTestCustomResourceStatus> | ||
implements Namespaced { | ||
|
||
@Override | ||
protected ObservedGenerationTestCustomResourceStatus initStatus() { | ||
return new ObservedGenerationTestCustomResourceStatus(); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...torsdk/operator/sample/observedgeneration/ObservedGenerationTestCustomResourceStatus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package io.javaoperatorsdk.operator.sample.observedgeneration; | ||
|
||
import io.javaoperatorsdk.operator.api.ObservedGenerationAwareStatus; | ||
|
||
public class ObservedGenerationTestCustomResourceStatus extends ObservedGenerationAwareStatus { | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
.../javaoperatorsdk/operator/sample/observedgeneration/ObservedGenerationTestReconciler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.javaoperatorsdk.operator.sample.observedgeneration; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.javaoperatorsdk.operator.api.reconciler.*; | ||
|
||
import static io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration.NO_FINALIZER; | ||
|
||
@ControllerConfiguration(finalizerName = NO_FINALIZER) | ||
public class ObservedGenerationTestReconciler | ||
implements Reconciler<ObservedGenerationTestCustomResource> { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(ObservedGenerationTestReconciler.class); | ||
|
||
@Override | ||
public UpdateControl<ObservedGenerationTestCustomResource> reconcile( | ||
ObservedGenerationTestCustomResource resource, Context context) { | ||
log.info("Reconcile ObservedGenerationTestCustomResource: {}", | ||
resource.getMetadata().getName()); | ||
return UpdateControl.updateStatusSubResource(resource); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
|
||
<modules> | ||
<module>tomcat-operator</module> | ||
<module>webpage</module> | ||
</modules> | ||
|
||
<dependencies> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# WebServer Operator | ||
|
||
This is a simple example of how a Custom Resource backed by an Operator can serve as | ||
an abstraction layer. This Operator will use a webserver resource, which mainly contains a | ||
static webpage definition and creates an NGINX Deployment backed by a ConfigMap which holds | ||
the HTML. | ||
|
||
This is an example input: | ||
```yaml | ||
apiVersion: "sample.javaoperatorsdk/v1" | ||
kind: WebPage | ||
metadata: | ||
name: mynginx-hello | ||
spec: | ||
html: | | ||
<html> | ||
<head> | ||
<title>Webserver Operator</title> | ||
</head> | ||
<body> | ||
Hello World! | ||
</body> | ||
</html> | ||
``` | ||
|
||
### Try | ||
|
||
The quickest way to try the operator is to run it on your local machine, while it connects to a local or remote | ||
Kubernetes cluster. When you start it, it will use the current kubectl context on your machine to connect to the cluster. | ||
|
||
Before you run it you have to install the CRD on your cluster by running `kubectl apply -f k8s/crd.yaml` | ||
|
||
When the Operator is running you can create some Webserver Custom Resources. You can find a sample custom resource in | ||
`k8s/webpage.yaml`. You can create it by running `kubectl apply -f k8s/webpage.yaml` | ||
|
||
After the Operator has picked up the new webserver resource (see the logs) it should create the NGINX server in the | ||
same namespace where the webserver resource is created. To connect to the server using your browser you can | ||
run `kubectl get service` and view the service created by the Operator. It should have a NodePort configured. If you are | ||
running a single-node cluster (e.g. Docker for Mac or Minikube) you can connect to the VM on this port to access the | ||
page. Otherwise you can change the service to a LoadBalancer (e.g on a public cloud). | ||
|
||
You can also try to change the HTML code in `k8s/webpage.yaml` and do another `kubectl apply -f k8s/webpage.yaml`. | ||
This should update the actual NGINX deployment with the new configuration. | ||
|
||
### Build | ||
|
||
You can build the sample using `mvn jib:dockerBuild` this will produce a Docker image you can push to the registry | ||
of your choice. The JAR file is built using your local Maven and JDK and then copied into the Docker image. | ||
|
||
### Deployment | ||
|
||
1. Deploy the CRD: `kubectl apply -f k8s/crd.yaml` | ||
2. Deploy the operator: `kubectl apply -f k8s/operator.yaml` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
name: webpages.sample.javaoperatorsdk | ||
spec: | ||
group: sample.javaoperatorsdk | ||
versions: | ||
- name: v1 | ||
served: true | ||
storage: true | ||
schema: | ||
openAPIV3Schema: | ||
type: object | ||
properties: | ||
spec: | ||
type: object | ||
properties: | ||
html: | ||
type: string | ||
status: | ||
type: object | ||
properties: | ||
observedGeneration: | ||
type: integer | ||
htmlConfigMap: | ||
type: string | ||
areWeGood: | ||
type: string | ||
errorMessage: | ||
type: string | ||
subresources: | ||
status: { } | ||
scope: Namespaced | ||
names: | ||
plural: webpages | ||
singular: webpage | ||
kind: WebPage | ||
shortNames: | ||
- wp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: webserver-operator | ||
|
||
--- | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: webserver-operator | ||
namespace: webserver-operator | ||
|
||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: webserver-operator | ||
namespace: webserver-operator | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: webserver-operator | ||
replicas: 1 | ||
template: | ||
metadata: | ||
labels: | ||
app: webserver-operator | ||
spec: | ||
serviceAccountName: webserver-operator | ||
containers: | ||
- name: operator | ||
image: webserver-operator | ||
imagePullPolicy: Never | ||
ports: | ||
- containerPort: 80 | ||
readinessProbe: | ||
httpGet: | ||
path: /health | ||
port: 8080 | ||
initialDelaySeconds: 1 | ||
timeoutSeconds: 1 | ||
livenessProbe: | ||
httpGet: | ||
path: /health | ||
port: 8080 | ||
initialDelaySeconds: 30 | ||
timeoutSeconds: 1 | ||
|
||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: operator-admin | ||
subjects: | ||
- kind: ServiceAccount | ||
name: webserver-operator | ||
namespace: webserver-operator | ||
roleRef: | ||
kind: ClusterRole | ||
name: webserver-operator | ||
apiGroup: "" | ||
|
||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: webserver-operator | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- deployments | ||
- services | ||
- configmaps | ||
- pods | ||
verbs: | ||
- '*' | ||
- apiGroups: | ||
- "apps" | ||
resources: | ||
- deployments | ||
- services | ||
- configmaps | ||
verbs: | ||
- '*' | ||
- apiGroups: | ||
- "apiextensions.k8s.io" | ||
resources: | ||
- customresourcedefinitions | ||
verbs: | ||
- '*' | ||
- apiGroups: | ||
- "sample.javaoperatorsdk" | ||
resources: | ||
- webservers | ||
- webservers/status | ||
verbs: | ||
- '*' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be needed: the generator should be used for that and what we want people to use instead of manually writing CRDs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed. removed crd.yaml addd dependency.