Skip to content

Commit 7b8aa82

Browse files
modified the Quarkus operator SDK version and tutorial too (operator-framework#40)
* modified the version of quarkus operator sdk and tutorial too * updated quarkus version * updated tutorial and readme * modified content with new changes * removed note as .placeholder file will be there * updated wih spec and status * updated doc with note * updated readme * updated turotial * updated turotial * updated turotial * updated turotial * updated turotial * updated turotial
1 parent f088b2b commit 7b8aa82

File tree

2 files changed

+66
-130
lines changed

2 files changed

+66
-130
lines changed

Diff for: README.md

+3-50
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,6 @@ Operator SDK is under Apache 2.0 license. See the [LICENSE][license_file] file f
2525
[of-blog]: https://coreos.com/blog/introducing-operator-framework
2626
[operator-link]: https://coreos.com/operators/
2727

28-
## Enable java-operator-plugins for operator-sdk
29-
30-
31-
To use java-operator-plugins for java operators we need to clone the operator-sdk repo.
32-
33-
### Updates in Operator-SDK go.mod
34-
35-
- Add the kubebuilder plugin to `go.mod`
36-
37-
```
38-
github.com/operator-framework/java-operator-plugins v0.0.0-20210225171707-e42ea87455e3
39-
```
40-
41-
- Replace the java-operator-plugins path in go-mod pointing to the local dir of your kube-builder repo. Example.
42-
43-
```
44-
github.com/operator-framework/java-operator-plugins => /Users/sushah/go/src/github.com/sujil02/java-operator-plugins
45-
```
46-
47-
### Updates in Operator-SDK `internal/cmd/operator-sdk/cli/cli.go`
48-
49-
- Add the java-operator-sdk import
50-
51-
```
52-
quarkusv1 "github.com/operator-framework/java-operator-plugins/pkg/quarkus/v1alpha"
53-
```
54-
55-
- Introduce the java bundle in `GetPluginsCLIAndRoot()` method.
56-
```
57-
quarkusBundle, _ := plugin.NewBundle("quarkus"+plugins.DefaultNameQualifier, plugin.Version{Number: 1},
58-
&quarkusv1.Plugin{},
59-
)
60-
```
61-
62-
- Add the created quarkusBundle to the `cli.New`
63-
64-
```
65-
cli.WithPlugins(
66-
ansibleBundle,
67-
gov2Bundle,
68-
gov3Bundle,
69-
helmBundle,
70-
quarkusBundle,
71-
),
72-
```
73-
74-
7528
### Build and Install the Operator-SDK
7629
```
7730
go mod tidy
@@ -90,15 +43,15 @@ operator-sdk init --plugins quarkus --domain xyz.com --project-name java-op
9043
Once the operator is scaffolded check for the following files
9144

9245
```
46+
.
47+
├── Makefile
9348
├── PROJECT
9449
├── pom.xml
9550
└── src
9651
└── main
9752
├── java
98-
│   └── com
99-
│   └── xyz
100-
│   └── JavaOpOperator.java
10153
└── resources
10254
└── application.properties
10355
56+
4 directories, 4 files
10457
```

Diff for: docs/tutorial.md

+63-80
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,12 @@ cd memcached-quarkus-operator
3030
operator-sdk init --plugins quarkus --domain example.com --project-name memcached-quarkus-operator
3131
```
3232

33+
**Note** Please do not commit this file structure to the `GitHub` immediately after the `init` command. The directory structure does not contain any file, and GitHub will not create an empty directory.
34+
3335
#### A note on dependency management
3436

3537
`operator-sdk init` generates `pom.xml` file. This file contains all the dependencies required to run the operator.
3638

37-
### MemcachedQuarkusOperator
38-
39-
The quarkus plugin will scaffold out several files during the `init` phase. One
40-
of these files is the operator's main program, `MemcachedQuarkusOperator.java`.
41-
This file initializes and runs the operator. The operator uses java-operator-sdk,
42-
which is similar to
43-
[controller-runtime](https://github.com/kubernetes-sigs/controller-runtime), to
44-
make operator development easier.
45-
46-
The important part of the `MemcachedQuarkusOperator.java` is the `run` method
47-
which will start the operator and initializes the informers and watches for your
48-
operator.
49-
50-
Here is an example of the `run` method that will typically be scaffolded out by
51-
this plugin:
52-
53-
```
54-
@Override
55-
public int run(String... args) throws Exception {
56-
operator.start();
57-
58-
Quarkus.waitForExit();
59-
return 0;
60-
}
61-
```
6239

6340
## Create a new API and Controller
6441

@@ -81,16 +58,16 @@ one shown as below.
8158
```
8259
$ tree
8360
.
84-
├── pom.xml
61+
├── Makefile
8562
├── PROJECT
63+
├── pom.xml
8664
└── src
8765
└── main
8866
├── java
8967
│ └── com
9068
│ └── example
91-
│ ├── MemcachedController.java
9269
│ ├── Memcached.java
93-
│ ├── MemcachedQuarkusOperator.java
70+
│ ├── MemcachedController.java
9471
│ ├── MemcachedSpec.java
9572
│ └── MemcachedStatus.java
9673
└── resources
@@ -99,6 +76,7 @@ $ tree
9976
6 directories, 8 files
10077
```
10178

79+
10280
#### Understanding Kubernetes APIs
10381

10482
For an in-depth explanation of Kubernetes APIs and the group-version-kind model, check out these [kubebuilder docs](https://book.kubebuilder.io/cronjob-tutorial/gvks.html).
@@ -200,24 +178,35 @@ There are a couple of ways to create the CRD. You can either create the file
200178
manually. Or let the quarkus extensions defined in `pom.xml` use the annotations
201179
on your Spec/Status classes to create the crd files for you.
202180

203-
#### Manually create `crd.yaml`
181+
#### Via Quarkus extension
182+
183+
Running `mvn clean install` will invoke the CRD generator extension which will analyze
184+
the annotations on the model objects, `Memcached`, `MemcachedSpec`,
185+
`MemcachedStatus`, and generate the CRD in `target/kubernetes`.
186+
187+
CRD generated in `memcacheds.cache.example.com-v1.yml`.
188+
189+
```
190+
.
191+
├── kubernetes.json
192+
├── kubernetes.yml
193+
└── memcacheds.cache.example.com-v1.yml
194+
195+
0 directories, 3 files
196+
```
204197

205-
Create a file with the name `crd.yaml`. A CRD enables users to add their
206-
own/custom objects to the Kubernetes cluster. Below you will find an example
207-
`Memcached` CRD.
198+
The content of the `memcacheds.cache.example.com-v1.yml` file is as shown below.
208199

209200
```
201+
# Generated by Fabric8 CRDGenerator, manual edits might get overwritten!
210202
apiVersion: apiextensions.k8s.io/v1
211203
kind: CustomResourceDefinition
212204
metadata:
213-
annotations:
214-
controller-gen.kubebuilder.io/version: v0.4.1
215205
name: memcacheds.cache.example.com
216206
spec:
217207
group: cache.example.com
218208
names:
219209
kind: Memcached
220-
listKind: MemcachedList
221210
plural: memcacheds
222211
singular: memcached
223212
scope: Namespaced
@@ -226,16 +215,9 @@ spec:
226215
schema:
227216
openAPIV3Schema:
228217
properties:
229-
apiVersion:
230-
type: string
231-
kind:
232-
type: string
233-
metadata:
234-
type: object
235218
spec:
236219
properties:
237220
size:
238-
format: int32
239221
type: integer
240222
type: object
241223
status:
@@ -250,27 +232,6 @@ spec:
250232
storage: true
251233
subresources:
252234
status: {}
253-
status:
254-
acceptedNames:
255-
kind: ""
256-
plural: ""
257-
conditions: []
258-
storedVersions: []
259-
```
260-
261-
#### Via Quarkus extension
262-
263-
**Note** there is currently an issue with the CRD generation that the schema
264-
validation is not properly generated. Because of this issue, we will not cover
265-
using this portion during this tutorial. Proceed to the
266-
[Create sample Memcached Custom Resource](#create-sample-memcached-custom-resource) section
267-
268-
Running `mvn install` will invoke the CRD generator extension which will analyze
269-
the annotations on the model objects, `Memcached`, `MemcachedSpec`,
270-
`MemcachedStatus`, and generate the CRD in `target/kubernetes`.
271-
272-
```
273-
mvn install
274235
```
275236

276237
### Create sample Memcached Custom Resource
@@ -298,12 +259,12 @@ This controller implements the `ResourceController` interface from the
298259
`java-operator-sdk`. This interface has some important and useful methods.
299260

300261
Initially the `MemcachedController.java` will contain the empty stubs for
301-
`createOrUpdateResource` and `deleteResource`. In this section we will fill in
262+
`createOrUpdateResource`. In this section we will fill in
302263
the controller logic in these methods. We will also add a
303264
`createMemcachedDeployment` method that will create the Deployment for our
304265
operator and a `labelsForMemcached` method that returns the labels.
305266

306-
The `createOrUpdateResource` and `deleteResource` get called whenever some
267+
The `createOrUpdateResource` get called whenever some
307268
update/create/delete event occurs in the cluster. This will allow us to react to
308269
changes to the Deployment.
309270

@@ -466,7 +427,7 @@ Let's create the utility method first.
466427
### labelsForMemcached
467428

468429
A simple utility method to return a map of the labels we want to attach to some
469-
of the resources. Below the `deleteResource` method add the following
430+
of the resources. Below the `createOrUpdateResource` method add the following
470431
helper:
471432

472433
```
@@ -541,16 +502,42 @@ Below your `labelsForMemcached(Memcached m)` block in the
541502
```
542503

543504
Now we have a `createOrUpdateResource` method. It calls
544-
`createMemcachedDeployment` which we have implemented above. In the next section
545-
we will discuss the deletion of the resource.
505+
`createMemcachedDeployment` which we have implemented above.
546506

547-
### deleteResource
507+
We have now implemented the `MemcachedController.java`.
548508

549-
One of the benefits of the `java-operator-sdk` library is that it handles the
550-
deletion portion for you. The scaffolded `deleteResource` is already implmented
551-
for you.
509+
## Include Dependencies
552510

553-
We have now implemented the `MemcachedController.java`.
511+
Please add below dependencies in `MemcachedController.java` file.
512+
513+
```
514+
import io.fabric8.kubernetes.api.model.ContainerBuilder;
515+
import io.fabric8.kubernetes.api.model.ContainerPortBuilder;
516+
import io.fabric8.kubernetes.api.model.LabelSelectorBuilder;
517+
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
518+
import io.fabric8.kubernetes.api.model.OwnerReferenceBuilder;
519+
import io.fabric8.kubernetes.api.model.Pod;
520+
import io.fabric8.kubernetes.api.model.PodSpecBuilder;
521+
import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
522+
import io.fabric8.kubernetes.api.model.apps.Deployment;
523+
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
524+
import io.fabric8.kubernetes.api.model.apps.DeploymentSpecBuilder;
525+
import org.apache.commons.collections.CollectionUtils;
526+
import java.util.HashMap;
527+
import java.util.List;
528+
import java.util.Map;
529+
import java.util.stream.Collectors;
530+
```
531+
532+
Also, update `pom.xml` file and import below dependency.
533+
534+
```
535+
<dependency>
536+
<groupId>commons-collections</groupId>
537+
<artifactId>commons-collections</artifactId>
538+
<version>3.2.2</version>
539+
</dependency>
540+
```
554541

555542
## Run the Operator
556543

@@ -597,9 +584,7 @@ quay.io/YOURUSER/memcached-quarkus-operator 0.0.1
597584

598585
2. Install the CRD
599586

600-
Next we will install the CRD into the `default` namespace. Using the `crd.yaml`
601-
you created in the [Manually created crd.yaml](#manually-create-crdyaml)
602-
section, apply it to the cluster.
587+
Next we will install the CRD into the `default` namespace. Using the `target/kubernetes/memcacheds.cache.example.com-v1.yml` , apply it to the cluster.
603588

604589
<!--
605590
TODO: Uncomment this when the crd generator works properly.
@@ -610,7 +595,7 @@ customresourcedefinition.apiextensions.k8s.io/memcacheds.cache.example.com creat
610595
-->
611596

612597
```
613-
$ kubectl apply -f crd.yaml
598+
$ kubectl apply -f target/kubernetes/memcacheds.cache.example.com-v1.yml
614599
customresourcedefinition.apiextensions.k8s.io/memcacheds.cache.example.com created
615600
```
616601

@@ -722,9 +707,7 @@ You should see a nice `BUILD SUCCESS` method like the one below:
722707

723708
2. Install the CRD
724709

725-
Next we will install the CRD into the `default` namespace. Using the `crd.yaml`
726-
you created in the [Manually created crd.yaml](#manually-create-crdyaml)
727-
section, apply it to the cluster.
710+
Next we will install the CRD into the `default` namespace. Using the `target/kubernetes/memcacheds.cache.example.com-v1.yml` , apply it to the cluster.
728711

729712
```
730713
$ kubectl apply -f crd.yaml

0 commit comments

Comments
 (0)