Skip to content

modified the Quarkus operator SDK version and tutorial too #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ operator-sdk init --plugins quarkus --domain xyz.com --project-name java-op
Once the operator is scaffolded check for the following files

```
.
├── Makefile
├── PROJECT
├── pom.xml
└── src
└── main
├── java
│   └── com
│   └── xyz
│   └── JavaOpOperator.java
└── resources
└── application.properties

4 directories, 4 files
```
97 changes: 26 additions & 71 deletions docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,12 @@ cd memcached-quarkus-operator
operator-sdk init --plugins quarkus --domain example.com --project-name memcached-quarkus-operator
```

**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.

#### A note on dependency management

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

### MemcachedQuarkusOperator

The quarkus plugin will scaffold out several files during the `init` phase. One
of these files is the operator's main program, `MemcachedQuarkusOperator.java`.
This file initializes and runs the operator. The operator uses java-operator-sdk,
which is similar to
[controller-runtime](https://github.com/kubernetes-sigs/controller-runtime), to
make operator development easier.

The important part of the `MemcachedQuarkusOperator.java` is the `run` method
which will start the operator and initializes the informers and watches for your
operator.

Here is an example of the `run` method that will typically be scaffolded out by
this plugin:

```
@Override
public int run(String... args) throws Exception {
operator.start();

Quarkus.waitForExit();
return 0;
}
```

## Create a new API and Controller

Expand All @@ -81,16 +58,16 @@ one shown as below.
```
$ tree
.
├── pom.xml
├── Makefile
├── PROJECT
├── pom.xml
└── src
└── main
├── java
│ └── com
│ └── example
│ ├── MemcachedController.java
│ ├── Memcached.java
│ ├── MemcachedQuarkusOperator.java
│ ├── MemcachedController.java
│ ├── MemcachedSpec.java
│ └── MemcachedStatus.java
└── resources
Expand All @@ -99,6 +76,7 @@ $ tree
6 directories, 8 files
```


#### Understanding Kubernetes APIs

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).
Expand Down Expand Up @@ -200,24 +178,35 @@ There are a couple of ways to create the CRD. You can either create the file
manually. Or let the quarkus extensions defined in `pom.xml` use the annotations
on your Spec/Status classes to create the crd files for you.

#### Manually create `crd.yaml`
#### Via Quarkus extension

Create a file with the name `crd.yaml`. A CRD enables users to add their
own/custom objects to the Kubernetes cluster. Below you will find an example
`Memcached` CRD.
Running `mvn clean install` will invoke the CRD generator extension which will analyze
the annotations on the model objects, `Memcached`, `MemcachedSpec`,
`MemcachedStatus`, and generate the CRD in `target/kubernetes`.

CRD generated in `memcacheds.cache.example.com-v1.yml`.

```
.
├── kubernetes.json
├── kubernetes.yml
└── memcacheds.cache.example.com-v1.yml

0 directories, 3 files
```

The content of the `memcacheds.cache.example.com-v1.yml` file is as shown below.

```
# Generated by Fabric8 CRDGenerator, manual edits might get overwritten!
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
name: memcacheds.cache.example.com
spec:
group: cache.example.com
names:
kind: Memcached
listKind: MemcachedList
plural: memcacheds
singular: memcached
scope: Namespaced
Expand All @@ -226,16 +215,9 @@ spec:
schema:
openAPIV3Schema:
properties:
apiVersion:
type: string
kind:
type: string
metadata:
type: object
spec:
properties:
size:
format: int32
type: integer
type: object
status:
Expand All @@ -250,27 +232,6 @@ spec:
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
```

#### Via Quarkus extension

**Note** there is currently an issue with the CRD generation that the schema
validation is not properly generated. Because of this issue, we will not cover
using this portion during this tutorial. Proceed to the
[Create sample Memcached Custom Resource](#create-sample-memcached-custom-resource) section

Running `mvn install` will invoke the CRD generator extension which will analyze
the annotations on the model objects, `Memcached`, `MemcachedSpec`,
`MemcachedStatus`, and generate the CRD in `target/kubernetes`.

```
mvn install
```

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

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

The `createOrUpdateResource` and `deleteResource` get called whenever some
The `createOrUpdateResource` get called whenever some
update/create/delete event occurs in the cluster. This will allow us to react to
changes to the Deployment.

Expand Down Expand Up @@ -544,12 +505,6 @@ Now we have a `createOrUpdateResource` method. It calls
`createMemcachedDeployment` which we have implemented above. In the next section
we will discuss the deletion of the resource.

### deleteResource

One of the benefits of the `java-operator-sdk` library is that it handles the
deletion portion for you. The scaffolded `deleteResource` is already implmented
for you.

We have now implemented the `MemcachedController.java`.

## Run the Operator
Expand Down