@@ -5,20 +5,18 @@ layout: docs
5
5
permalink : /docs/using-samples
6
6
---
7
7
8
-
9
8
# How to use sample Operators
10
9
11
- We have several sample Operators under the [ samples] ( https://github.com/java-operator-sdk/java-operator-sdk/tree/master/samples ) directory:
10
+ We have several sample Operators under
11
+ the [ samples] ( https://github.com/java-operator-sdk/java-operator-sdk/tree/master/smoke-test-samples ) directory:
12
12
13
- * * pure-java* : Minimal Operator implementation which only parses the Custom Resource and prints to
14
- stdout. Implemented with and without Spring Boot support. The two samples share the common module.
13
+ * * pure-java* : Minimal Operator implementation which only parses the Custom Resource and prints to stdout. Implemented
14
+ with and without Spring Boot support. The two samples share the common module.
15
15
* * spring-boot-plain* : Sample showing integration with Spring Boot.
16
16
17
- There are also more samples in the
18
- standalone [ samples repo] ( https://github.com/java-operator-sdk/samples ) :
17
+ There are also more samples in the standalone [ samples repo] ( https://github.com/java-operator-sdk/samples ) :
19
18
20
- * * webserver* : Simple example creating an NGINX webserver from a Custom Resource containing HTML
21
- code.
19
+ * * webserver* : Simple example creating an NGINX webserver from a Custom Resource containing HTML code.
22
20
* * mysql-schema* : Operator managing schemas in a MySQL database.
23
21
* * tomcat* : Operator with two controllers, managing Tomcat instances and Webapps for these.
24
22
@@ -27,14 +25,14 @@ Add [dependency](https://search.maven.org/search?q=a:operator-framework) to your
27
25
``` xml
28
26
29
27
<dependency >
30
- <groupId >io.javaoperatorsdk</groupId >
31
- <artifactId >operator-framework</artifactId >
32
- <version >{see https://search.maven.org/search?q=a:operator-framework for latest version}</version >
28
+ <groupId >io.javaoperatorsdk</groupId >
29
+ <artifactId >operator-framework</artifactId >
30
+ <version >{see https://search.maven.org/search?q=a:operator-framework for latest version}</version >
33
31
</dependency >
34
32
```
35
33
36
- Or alternatively with Gradle, which also requires declaring the SDK as an annotation processor to
37
- generate the mappings between controllers and custom resource classes:
34
+ Or alternatively with Gradle, which also requires declaring the SDK as an annotation processor to generate the mappings
35
+ between controllers and custom resource classes:
38
36
39
37
``` groovy
40
38
dependencies {
@@ -43,16 +41,16 @@ dependencies {
43
41
}
44
42
```
45
43
46
- Once you've added the dependency, define a main method initializing the Operator and registering a
47
- controller.
44
+ Once you've added the dependency, define a main method initializing the Operator and registering a controller.
48
45
49
46
``` java
50
47
public class Runner {
51
48
52
- public static void main (String [] args ) {
53
- Operator operator = new Operator (DefaultConfigurationService . instance());
54
- operator. register(new WebServerController ());
55
- }
49
+ public static void main (String [] args ) {
50
+ Operator operator = new Operator (DefaultConfigurationService . instance());
51
+ operator. register(new WebServerController ());
52
+ operator. start();
53
+ }
56
54
}
57
55
```
58
56
@@ -61,15 +59,15 @@ The Controller implements the business logic and describes all the classes neede
61
59
``` java
62
60
63
61
@Controller
64
- public class WebServerController implements ResourceController <WebServer > {
65
-
66
- // Return the changed resource, so it gets updated. See javadoc for details.
67
- @Override
68
- public UpdateControl<CustomService > createOrUpdateResource (CustomService resource ,
69
- Context< WebServer > context ) {
70
- // ... your logic ...
71
- return UpdateControl . updateStatusSubResource (resource);
72
- }
62
+ public class WebServerController implements Reconciler <WebServer > {
63
+
64
+ // Return the changed resource, so it gets updated. See javadoc for details.
65
+ @Override
66
+ public UpdateControl<CustomService > reconcile (CustomService resource ,
67
+ Context context ) {
68
+ // ... your logic ...
69
+ return UpdateControl . updateStatus (resource);
70
+ }
73
71
}
74
72
```
75
73
@@ -80,51 +78,49 @@ A sample custom resource POJO representation
80
78
@Group (" sample.javaoperatorsdk" )
81
79
@Version (" v1" )
82
80
public class WebServer extends CustomResource<WebServerSpec , WebServerStatus > implements
83
- Namespaced {
81
+ Namespaced {
84
82
85
83
}
86
84
87
85
public class WebServerSpec {
88
86
89
- private String html;
87
+ private String html;
90
88
91
- public String getHtml () {
92
- return html;
93
- }
89
+ public String getHtml () {
90
+ return html;
91
+ }
94
92
95
- public void setHtml (String html ) {
96
- this . html = html;
97
- }
93
+ public void setHtml (String html ) {
94
+ this . html = html;
95
+ }
98
96
}
99
97
```
100
98
101
99
### Deactivating CustomResource implementations validation
102
100
103
101
The operator will, by default, query the deployed CRDs to check that the ` CustomResource `
104
- implementations match what is known to the cluster. This requires an additional query to the cluster
105
- and, sometimes, elevated privileges for the operator to be able to read the CRDs from the cluster.
106
- This validation is mostly meant to help users new to operator development get started and avoid
107
- common mistakes. Advanced users or production deployments might want to skip this step. This is done
108
- by setting the ` CHECK_CRD_ENV_KEY ` environment variable to ` false ` .
102
+ implementations match what is known to the cluster. This requires an additional query to the cluster and, sometimes,
103
+ elevated privileges for the operator to be able to read the CRDs from the cluster. This validation is mostly meant to
104
+ help users new to operator development get started and avoid common mistakes. Advanced users or production deployments
105
+ might want to skip this step. This is done by setting the ` CHECK_CRD_ENV_KEY ` environment variable to ` false ` .
109
106
110
107
### Automatic generation of CRDs
111
108
112
- To automatically generate CRD manifests from your annotated Custom Resource classes, you only need
113
- to add the following dependencies to your project:
109
+ To automatically generate CRD manifests from your annotated Custom Resource classes, you only need to add the following
110
+ dependencies to your project:
114
111
115
112
``` xml
116
113
117
114
<dependency >
118
- <groupId >io.fabric8</groupId >
119
- <artifactId >crd-generator-apt</artifactId >
120
- <scope >provided</scope >
115
+ <groupId >io.fabric8</groupId >
116
+ <artifactId >crd-generator-apt</artifactId >
117
+ <scope >provided</scope >
121
118
</dependency >
122
119
```
123
120
124
- The CRD will be generated in ` target/classes/META-INF/fabric8 ` (or
125
- in ` target/test-classes/META-INF/fabric8 ` , if you use the ` test ` scope) with the CRD name suffixed
126
- by the generated spec version. For example, a CR using the ` java-operator-sdk.io ` group with
127
- a ` mycrs ` plural form will result in 2 files:
121
+ The CRD will be generated in ` target/classes/META-INF/fabric8 ` (or in ` target/test-classes/META-INF/fabric8 ` , if you use
122
+ the ` test ` scope) with the CRD name suffixed by the generated spec version. For example, a CR using
123
+ the ` java-operator-sdk.io ` group with a ` mycrs ` plural form will result in 2 files:
128
124
129
125
- ` mycrs.java-operator-sdk.io-v1.yml `
130
126
- ` mycrs.java-operator-sdk.io-v1beta1.yml `
@@ -134,64 +130,60 @@ a `mycrs` plural form will result in 2 files:
134
130
135
131
### Quarkus
136
132
137
- A [ Quarkus] ( https://quarkus.io ) extension is also provided to ease the development of Quarkus-based
138
- operators.
133
+ A [ Quarkus] ( https://quarkus.io ) extension is also provided to ease the development of Quarkus-based operators.
139
134
140
135
Add [ this dependency] ( https://search.maven.org/search?q=a:quarkus-operator-sdk )
141
136
to your project:
142
137
143
138
``` xml
144
139
145
140
<dependency >
146
- <groupId >io.quarkiverse.operatorsdk</groupId >
147
- <artifactId >quarkus-operator-sdk</artifactId >
148
- <version >{see https://search.maven.org/search?q=a:quarkus-operator-sdk for latest version}
149
- </version >
141
+ <groupId >io.quarkiverse.operatorsdk</groupId >
142
+ <artifactId >quarkus-operator-sdk</artifactId >
143
+ <version >{see https://search.maven.org/search?q=a:quarkus-operator-sdk for latest version}
144
+ </version >
150
145
</dependency >
151
146
```
152
147
153
148
Create an Application, Quarkus will automatically create and inject a ` KubernetesClient ` (
154
- or ` OpenShiftClient ` ), ` Operator ` , ` ConfigurationService ` and ` ResourceController ` instances that
155
- your application can use. Below, you can see the minimal code you need to write to get your operator
156
- and controllers up and running:
149
+ or ` OpenShiftClient ` ), ` Operator ` , ` ConfigurationService ` and ` ResourceController ` instances that your application can
150
+ use. Below, you can see the minimal code you need to write to get your operator and controllers up and running:
157
151
158
152
``` java
159
153
160
154
@QuarkusMain
161
155
public class QuarkusOperator implements QuarkusApplication {
162
156
163
- @Inject
164
- Operator operator;
157
+ @Inject
158
+ Operator operator;
165
159
166
- public static void main (String ... args ) {
167
- Quarkus . run(QuarkusOperator . class, args);
168
- }
160
+ public static void main (String ... args ) {
161
+ Quarkus . run(QuarkusOperator . class, args);
162
+ }
169
163
170
- @Override
171
- public int run (String ... args ) throws Exception {
172
- operator. start();
173
- Quarkus . waitForExit();
174
- return 0 ;
175
- }
164
+ @Override
165
+ public int run (String ... args ) throws Exception {
166
+ operator. start();
167
+ Quarkus . waitForExit();
168
+ return 0 ;
169
+ }
176
170
}
177
171
```
178
172
179
173
### Spring Boot
180
174
181
- You can also let Spring Boot wire your application together and automatically register the
182
- controllers.
175
+ You can also let Spring Boot wire your application together and automatically register the controllers.
183
176
184
- Add [ this dependency] ( https://search.maven.org/search?q=a:operator-framework-spring-boot-starter ) to
185
- your project:
177
+ Add [ this dependency] ( https://search.maven.org/search?q=a:operator-framework-spring-boot-starter ) to your project:
186
178
187
179
``` xml
188
180
189
181
<dependency >
190
- <groupId >io.javaoperatorsdk</groupId >
191
- <artifactId >operator-framework-spring-boot-starter</artifactId >
192
- <version >{see https://search.maven.org/search?q=a:operator-framework-spring-boot-starter for
193
- latest version}
194
- </version >
182
+ <groupId >io.javaoperatorsdk</groupId >
183
+ <artifactId >operator-framework-spring-boot-starter</artifactId >
184
+ <version >{see https://search.maven.org/search?q=a:operator-framework-spring-boot-starter for
185
+ latest version}
186
+ </version >
195
187
</dependency >
196
188
```
197
189
@@ -202,25 +194,25 @@ Create an Application
202
194
@SpringBootApplication
203
195
public class Application {
204
196
205
- public static void main (String [] args ) {
206
- SpringApplication . run(Application . class, args);
207
- }
197
+ public static void main (String [] args ) {
198
+ SpringApplication . run(Application . class, args);
199
+ }
208
200
}
209
201
```
210
202
211
203
#### Spring Boot test support
212
204
213
- Adding the following dependency would let you mock the operator for the tests where loading the
214
- spring container is necessary, but it doesn't need real access to a Kubernetes cluster.
205
+ Adding the following dependency would let you mock the operator for the tests where loading the spring container is
206
+ necessary, but it doesn't need real access to a Kubernetes cluster.
215
207
216
208
``` xml
217
209
218
210
<dependency >
219
- <groupId >io.javaoperatorsdk</groupId >
220
- <artifactId >operator-framework-spring-boot-starter-test</artifactId >
221
- <version >{see https://search.maven.org/search?q=a:operator-framework-spring-boot-starter for
222
- latest version}
223
- </version >
211
+ <groupId >io.javaoperatorsdk</groupId >
212
+ <artifactId >operator-framework-spring-boot-starter-test</artifactId >
213
+ <version >{see https://search.maven.org/search?q=a:operator-framework-spring-boot-starter for
214
+ latest version}
215
+ </version >
224
216
</dependency >
225
217
```
226
218
@@ -232,8 +224,8 @@ Mock the operator:
232
224
@EnableMockOperator
233
225
public class SpringBootStarterSampleApplicationTest {
234
226
235
- @Test
236
- void contextLoads () {
237
- }
227
+ @Test
228
+ void contextLoads () {
229
+ }
238
230
}
239
231
```
0 commit comments