@@ -5,20 +5,18 @@ layout: docs
55permalink : /docs/using-samples
66---
77
8-
98# How to use sample Operators
109
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:
1212
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.
1515* * spring-boot-plain* : Sample showing integration with Spring Boot.
1616
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 ) :
1918
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.
2220* * mysql-schema* : Operator managing schemas in a MySQL database.
2321* * tomcat* : Operator with two controllers, managing Tomcat instances and Webapps for these.
2422
@@ -27,14 +25,14 @@ Add [dependency](https://search.maven.org/search?q=a:operator-framework) to your
2725``` xml
2826
2927<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 >
3331</dependency >
3432```
3533
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:
3836
3937``` groovy
4038dependencies {
@@ -43,16 +41,16 @@ dependencies {
4341}
4442```
4543
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.
4845
4946``` java
5047public class Runner {
5148
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+ }
5654}
5755```
5856
@@ -61,15 +59,15 @@ The Controller implements the business logic and describes all the classes neede
6159``` java
6260
6361@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+ }
7371}
7472```
7573
@@ -80,51 +78,49 @@ A sample custom resource POJO representation
8078@Group (" sample.javaoperatorsdk" )
8179@Version (" v1" )
8280public class WebServer extends CustomResource<WebServerSpec , WebServerStatus > implements
83- Namespaced {
81+ Namespaced {
8482
8583}
8684
8785public class WebServerSpec {
8886
89- private String html;
87+ private String html;
9088
91- public String getHtml () {
92- return html;
93- }
89+ public String getHtml () {
90+ return html;
91+ }
9492
95- public void setHtml (String html ) {
96- this . html = html;
97- }
93+ public void setHtml (String html ) {
94+ this . html = html;
95+ }
9896}
9997```
10098
10199### Deactivating CustomResource implementations validation
102100
103101The 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 ` .
109106
110107### Automatic generation of CRDs
111108
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:
114111
115112``` xml
116113
117114<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 >
121118</dependency >
122119```
123120
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:
128124
129125- ` mycrs.java-operator-sdk.io-v1.yml `
130126- ` mycrs.java-operator-sdk.io-v1beta1.yml `
@@ -134,64 +130,60 @@ a `mycrs` plural form will result in 2 files:
134130
135131### Quarkus
136132
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.
139134
140135Add [ this dependency] ( https://search.maven.org/search?q=a:quarkus-operator-sdk )
141136to your project:
142137
143138``` xml
144139
145140<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 >
150145</dependency >
151146```
152147
153148Create 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:
157151
158152``` java
159153
160154@QuarkusMain
161155public class QuarkusOperator implements QuarkusApplication {
162156
163- @Inject
164- Operator operator;
157+ @Inject
158+ Operator operator;
165159
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+ }
169163
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+ }
176170}
177171```
178172
179173### Spring Boot
180174
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.
183176
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:
186178
187179``` xml
188180
189181<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 >
195187</dependency >
196188```
197189
@@ -202,25 +194,25 @@ Create an Application
202194@SpringBootApplication
203195public class Application {
204196
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+ }
208200}
209201```
210202
211203#### Spring Boot test support
212204
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.
215207
216208``` xml
217209
218210<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 >
224216</dependency >
225217```
226218
@@ -232,8 +224,8 @@ Mock the operator:
232224@EnableMockOperator
233225public class SpringBootStarterSampleApplicationTest {
234226
235- @Test
236- void contextLoads () {
237- }
227+ @Test
228+ void contextLoads () {
229+ }
238230}
239231```
0 commit comments