Skip to content

Commit 2512a11

Browse files
author
simone
committed
Skeleton!
1 parent d1a12af commit 2512a11

File tree

11 files changed

+120
-79
lines changed

11 files changed

+120
-79
lines changed

Readme.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
1-
# Usefull command
1+
# Website operator
22

3-
minikube start
4-
docker build -t javaoperator .
5-
minikube image load javaoperator
3+
This repository contains an example schema of an operator written in .Net. You could use it in order to start develop your first Kubernetes operator!
4+
5+
The schema contains three elements:
6+
- Controller (a.k.a. Reconciler)
7+
- Entity
8+
- Finalizer
9+
10+
The **controller** is going to manage the "reconcile" step. It will watch over the observed resources and takes care of being sure that the k8s cluster state and the desired state are the same.
11+
12+
The **entity** described the state that should be observed by the controller.
13+
14+
The **finalizer** has the responsibility of dispose unused resources.
15+
16+
## 💪🏻 Working software exercises
17+
You should start from this schema and implement the following feature:
18+
19+
- feat n.1: Create a Pod with image nginx everytime a new custom resource of type website has been created
20+
- feat n.2: When a custom resource has been deleted, remove the linked resources (Pod)
21+
- feat n.3: When a new custom resource has been created or updated, the operator should deploy a deployment manifest and take the number of replicas from the new custom resource
22+
- feat n.4: When the pod starts, update the default nginx index.html and put inside a short description from the custom resource
23+
- feat n.5: Show time: 🎉 Create a NodePort service for that deployment and retreive the public ip address.
24+
25+
## How to generate manifests
26+
27+
You could use the [KubeOps.Cli](https://buehler.github.io/dotnet-operator-sdk/src/KubeOps.Cli/README.html) tool. It allows you to generate the needed files for your operator as well as managing resources in your Kubernetes cluster.
28+
29+
dotnet new tool-manifest
30+
dotnet tool install KubeOps.Cli
31+
dotnet kubeops gen op website --out ./manifests
32+
//update the image in kustomization.yaml with the operator docker image you'll use
33+
//set the imagePullPolicy: IfNotPresent property in the deployment
34+
35+
## How to run in local env
36+
If you want to test the operator locally you could easily run it in a local minikube instance.
37+
38+
Build operator image:
39+
40+
docker build -t javaoperator .
41+
42+
Load the image in your local minikube cluster
43+
44+
minikube image load javaoperator
45+
46+
Apply it:
47+
48+
kubectl apply -k manifests
49+
50+
## Notes
51+
52+
(*) Reconciles: it stands for the act of the operator to edit
53+
(reconcile) the status of the kubernetes cluster as we want (looking at the custom resource)

kubernetes-charts/operator-role.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ rules:
2121
verbs:
2222
- '*'
2323
- apiGroups:
24-
- intre.com
24+
- intre.it
2525
resources:
26-
- sites
26+
- websites
2727
verbs:
2828
- '*'
2929
- apiGroups:
30-
- intre.com
30+
- intre.it
3131
resources:
32-
- sites/status
32+
- websites/status
3333
verbs:
3434
- get
3535
- update
36-
- patch
36+
- patch

pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@
4040
<version>5.5.0</version>
4141
</dependency>
4242

43-
<dependency>
44-
<groupId>io.fabric8</groupId>
45-
<artifactId>crd-generator-apt</artifactId>
46-
<version>6.9.2</version>
47-
<scope>provided</scope>
48-
</dependency>
49-
5043
<dependency>
5144
<groupId>org.bouncycastle</groupId>
5245
<artifactId>bcprov-jdk18on</artifactId>

src/main/java/inter/com/javaoperator/controller/WebSiteReconciler.java

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

src/main/java/inter/com/javaoperator/model/WebSiteSpec.java

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

src/main/java/inter/com/javaoperator/JavaoperatorApplication.java renamed to src/main/java/intre/it/javaoperator/JavaoperatorApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package inter.com.javaoperator;
1+
package intre.it.javaoperator;
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;

src/main/java/inter/com/javaoperator/config/Config.java renamed to src/main/java/intre/it/javaoperator/config/Config.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package inter.com.javaoperator.config;
1+
package intre.it.javaoperator.config;
22

3-
import inter.com.javaoperator.controller.WebSiteReconciler;
3+
import intre.it.javaoperator.controller.WebSiteReconciler;
44
import io.javaoperatorsdk.operator.Operator;
55
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
66
import org.springframework.context.annotation.Bean;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package intre.it.javaoperator.controller;
2+
3+
import intre.it.javaoperator.model.WebSite;
4+
import io.fabric8.kubernetes.client.KubernetesClient;
5+
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
6+
import io.javaoperatorsdk.operator.api.reconciler.*;
7+
8+
@ControllerConfiguration
9+
public class WebSiteReconciler implements Reconciler<WebSite>, Cleaner<WebSite> {
10+
private final KubernetesClient client;
11+
12+
public WebSiteReconciler() {
13+
this.client = new KubernetesClientBuilder().build();
14+
}
15+
16+
@Override
17+
public UpdateControl<WebSite> reconcile(WebSite WebSite, Context<WebSite> context) throws Exception {
18+
return UpdateControl.patchStatus(WebSite);
19+
}
20+
21+
@Override
22+
public DeleteControl cleanup(WebSite webSite, Context<WebSite> context) {
23+
return DeleteControl.defaultDelete();
24+
}
25+
}

src/main/java/inter/com/javaoperator/model/WebSite.java renamed to src/main/java/intre/it/javaoperator/model/WebSite.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package inter.com.javaoperator.model;
1+
package intre.it.javaoperator.model;
22

33
import io.fabric8.kubernetes.api.model.Namespaced;
44
import io.fabric8.kubernetes.client.CustomResource;
55
import io.fabric8.kubernetes.model.annotation.Group;
66
import io.fabric8.kubernetes.model.annotation.Version;
77

8-
@Group("com.example.javaoperator")
8+
@Group("intre.it")
99
@Version("v1")
1010
public class WebSite extends CustomResource<WebSiteSpec, WebSiteStatus> implements Namespaced {
1111

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package intre.it.javaoperator.model;
2+
3+
public class WebSiteSpec {
4+
5+
private String webSiteName;
6+
private String url;
7+
private String shortDescription;
8+
9+
public String getWebSiteName() {
10+
return webSiteName;
11+
}
12+
13+
public void setWebSiteName(String WebSite) {
14+
this.webSiteName = WebSite;
15+
}
16+
17+
public String getUrl() {
18+
return url;
19+
}
20+
21+
public void setUrl(String url) {
22+
this.url = url;
23+
}
24+
25+
public String getShortDescription() {
26+
return shortDescription;
27+
}
28+
29+
public void setShortDescription(String shortDescription) {
30+
this.shortDescription = shortDescription;
31+
}
32+
}

0 commit comments

Comments
 (0)