Skip to content

Commit e8922a3

Browse files
committed
Merge branch 'master' into remove_group_and_kind
2 parents 2658ebd + 0360db4 commit e8922a3

File tree

16 files changed

+516
-0
lines changed

16 files changed

+516
-0
lines changed

samples/mysql-schema/Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM openjdk:12-alpine
2+
3+
ENTRYPOINT ["java", "-jar", "/usr/share/operator/operator.jar"]
4+
5+
ARG JAR_FILE
6+
ADD target/${JAR_FILE} /usr/share/operator/operator.jar
7+

samples/mysql-schema/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# WebServer Operator
2+
3+
This is a more complex example of how a Custom Resource backed by an Operator can serve as
4+
an abstraction layer. This Operator will use an webserver resource, which mainly contains a
5+
static webpage definition and creates a nginx Deployment backed by a ConfigMap which holds
6+
the html.
7+
8+
This is an example input:
9+
```yaml
10+
apiVersion: "sample.javaoperatorsdk/v1"
11+
kind: WebServer
12+
metadata:
13+
name: mynginx-hello
14+
spec:
15+
html: |
16+
<html>
17+
<head>
18+
<title>Webserver Operator</title>
19+
</head>
20+
<body>
21+
Hello World!!
22+
</body>
23+
</html>
24+
```
25+
26+
### Try
27+
28+
The quickest way to try the operator is to run it on your local machine, while it connects to a local or remote
29+
Kubernetes cluster. When you start it it will use the current kubectl context on your machine to connect to the cluster.
30+
31+
Before you run it you have to install the CRD on your cluster by running `kubectl apply -f crd/crd.yaml`
32+
33+
When the Operator is running you can create some Webserver Custom Resources. You can find a sample custom resource in
34+
`crd/webserver.yaml`. You can create it by running `kubectl apply -f webserver.yaml`
35+
36+
After the Operator has picked up the new webserver resource (see the logs) it should create the nginx server in the
37+
same namespace where the webserver resource is created. To connect to the server using your browser you can
38+
run `kubectl get service` and view the service created by the Operator. It should have a NodePort configured. If you are
39+
running a single-node cluster (e.g. Docker for Mac or Minikube) you can connect to the VM on this port to access the
40+
page.
41+
42+
You can also try to change the html code in `crd/webserver.yaml` and do another `kubectl apply -f crd/webserver.yaml`.
43+
This should update the actual nginx deployment with new configuration.
44+
45+
### Build
46+
47+
You can build the sample using `mvn dockerfile:build` this will produce a Docker image you can push to the registry
48+
of your choice. The jar file is built using your local Maven and JDK and then copied into the Docker image.
49+
50+
### Deployment
51+
52+
1. Deploy the CRD: kubectl apply -f crd/crd.yaml
53+
2. Deploy the operator: kubectl apply -f k8s/deployment.yaml

samples/mysql-schema/crd/crd.yaml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: schemas.mysql.sample.javaoperatorsdk
5+
spec:
6+
group: mysql.sample.javaoperatorsdk
7+
version: v1
8+
scope: Namespaced
9+
names:
10+
plural: schemas
11+
singular: schema
12+
kind: MySQLSchema
13+
validation:
14+
openAPIV3Schema:
15+
type: object
16+
properties:
17+
spec:
18+
type: object
19+
properties:
20+
encoding:
21+
type: string

samples/mysql-schema/crd/example.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: "mysql.sample.javaoperatorsdk/v1"
2+
kind: MySQLSchema
3+
metadata:
4+
name: mydb
5+
spec:
6+
encoding: utf8
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: mysql-schema-operator
5+
---
6+
apiVersion: apps/v1
7+
kind: Deployment
8+
metadata:
9+
name: mysql-schema-operator
10+
namespace: mysql-schema-operator
11+
spec:
12+
selector:
13+
matchLabels:
14+
app: mysql-schema-operator
15+
replicas: 1
16+
template:
17+
metadata:
18+
labels:
19+
app: mysql-schema-operator
20+
spec:
21+
serviceAccount: mysql-schema-operator
22+
containers:
23+
- name: operator
24+
image: mysql-schema-operator
25+
imagePullPolicy: Never
26+
ports:
27+
- containerPort: 80
28+
env:
29+
- name: MYSQL_HOST
30+
value: mysql.mysql
31+
- name: MYSQL_USER
32+
value: root
33+
- name: MYSQL_PASSWORD
34+
value: password
35+
readinessProbe:
36+
httpGet:
37+
path: /health
38+
port: 8080
39+
initialDelaySeconds: 1
40+
timeoutSeconds: 1
41+
livenessProbe:
42+
httpGet:
43+
path: /health
44+
port: 8080
45+
initialDelaySeconds: 30
46+
timeoutSeconds: 1
47+
---
48+
apiVersion: v1
49+
kind: ServiceAccount
50+
metadata:
51+
name: mysql-schema-operator
52+
namespace: mysql-schema-operator
53+
---
54+
apiVersion: rbac.authorization.k8s.io/v1
55+
kind: ClusterRoleBinding
56+
metadata:
57+
name: operator-admin
58+
subjects:
59+
- kind: ServiceAccount
60+
name: mysql-schema-operator
61+
namespace: mysql-schema-operator
62+
roleRef:
63+
kind: ClusterRole
64+
name: cluster-admin
65+
apiGroup: ""

samples/mysql-schema/k8s/mysql.yaml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: mysql
5+
---
6+
apiVersion: v1
7+
kind: Service
8+
metadata:
9+
name: mysql
10+
namespace: mysql
11+
spec:
12+
ports:
13+
- port: 3306
14+
selector:
15+
app: mysql
16+
type: NodePort
17+
---
18+
apiVersion: apps/v1
19+
kind: Deployment
20+
metadata:
21+
name: mysql
22+
namespace: mysql
23+
spec:
24+
selector:
25+
matchLabels:
26+
app: mysql
27+
strategy:
28+
type: Recreate
29+
template:
30+
metadata:
31+
labels:
32+
app: mysql
33+
spec:
34+
containers:
35+
- image: mysql:5.6
36+
name: mysql
37+
env:
38+
# Use secret in real usage
39+
- name: MYSQL_ROOT_PASSWORD
40+
value: password
41+
ports:
42+
- containerPort: 3306
43+
name: mysql

samples/mysql-schema/pom.xml

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>com.github.containersolutions</groupId>
8+
<artifactId>java-operator-sdk-samples</artifactId>
9+
<version>0.3.10-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>mysql-schema-sample</artifactId>
13+
<name>Operator SDK - Samples - MySQL Schema</name>
14+
<description>Provisions new Schemas in a MySQL database</description>
15+
<packaging>jar</packaging>
16+
17+
<properties>
18+
<java.version>8</java.version>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
<maven.compiler.target>1.8</maven.compiler.target>
21+
</properties>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>com.github.containersolutions</groupId>
26+
<artifactId>operator-framework</artifactId>
27+
<version>${project.version}</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter-engine</artifactId>
32+
<version>5.4.2</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.apache.logging.log4j</groupId>
36+
<artifactId>log4j-slf4j-impl</artifactId>
37+
<version>2.11.2</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.takes</groupId>
41+
<artifactId>takes</artifactId>
42+
<version>1.17</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>mysql</groupId>
46+
<artifactId>mysql-connector-java</artifactId>
47+
<version>8.0.18</version>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-compiler-plugin</artifactId>
56+
<version>3.8.1</version>
57+
</plugin>
58+
<plugin>
59+
<groupId>com.spotify</groupId>
60+
<artifactId>dockerfile-maven-plugin</artifactId>
61+
<version>1.4.12</version>
62+
<configuration>
63+
<repository>mysql-schema-operator</repository>
64+
<tag>latest</tag>
65+
<buildArgs>
66+
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
67+
</buildArgs>
68+
</configuration>
69+
</plugin>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-shade-plugin</artifactId>
73+
<version>2.4.3</version>
74+
<executions>
75+
<execution>
76+
<phase>package</phase>
77+
<goals>
78+
<goal>shade</goal>
79+
</goals>
80+
<configuration>
81+
<createDependencyReducedPom>false</createDependencyReducedPom>
82+
<transformers>
83+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
84+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
85+
<manifestEntries>
86+
<Main-Class>com.github.containersolutions.operator.sample.MySQLSchemaOperator</Main-Class>
87+
<Build-Number>1.0</Build-Number>
88+
<Multi-Release>true</Multi-Release>
89+
</manifestEntries>
90+
</transformer>
91+
</transformers>
92+
<filters>
93+
<filter>
94+
<artifact>io.fabric8:openshift-client</artifact>
95+
<excludes>
96+
<exclude>io/fabric8/kubernetes/client/Config*</exclude>
97+
</excludes>
98+
</filter>
99+
</filters>
100+
</configuration>
101+
</execution>
102+
</executions>
103+
</plugin>
104+
</plugins>
105+
</build>
106+
107+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.containersolutions.operator.sample;
2+
3+
import com.github.containersolutions.operator.Operator;
4+
import io.fabric8.kubernetes.client.Config;
5+
import io.fabric8.kubernetes.client.ConfigBuilder;
6+
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.takes.facets.fork.FkRegex;
10+
import org.takes.facets.fork.TkFork;
11+
import org.takes.http.Exit;
12+
import org.takes.http.FtBasic;
13+
14+
import java.io.IOException;
15+
16+
public class MySQLSchemaOperator {
17+
18+
private static final Logger log = LoggerFactory.getLogger(MySQLSchemaOperator.class);
19+
20+
public static void main(String[] args) throws IOException {
21+
log.info("MySQL Schema Operator starting");
22+
23+
Config config = new ConfigBuilder().withNamespace(null).build();
24+
Operator operator = new Operator(new DefaultKubernetesClient(config));
25+
operator.registerControllerForAllNamespaces(new SchemaController());
26+
27+
new FtBasic(
28+
new TkFork(new FkRegex("/health", "ALL GOOD!")), 8080
29+
).start(Exit.NEVER);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.containersolutions.operator.sample;
2+
3+
import io.fabric8.kubernetes.client.CustomResource;
4+
5+
public class Schema extends CustomResource {
6+
7+
private SchemaSpec spec;
8+
9+
private SchemaStatus status;
10+
11+
public SchemaSpec getSpec() {
12+
return spec;
13+
}
14+
15+
public void setSpec(SchemaSpec spec) {
16+
this.spec = spec;
17+
}
18+
19+
public SchemaStatus getStatus() {
20+
return status;
21+
}
22+
23+
public void setStatus(SchemaStatus status) {
24+
this.status = status;
25+
}
26+
}

0 commit comments

Comments
 (0)