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 all 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
53 changes: 3 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,6 @@ Operator SDK is under Apache 2.0 license. See the [LICENSE][license_file] file f
[of-blog]: https://coreos.com/blog/introducing-operator-framework
[operator-link]: https://coreos.com/operators/

## Enable java-operator-plugins for operator-sdk


To use java-operator-plugins for java operators we need to clone the operator-sdk repo.

### Updates in Operator-SDK go.mod

- Add the kubebuilder plugin to `go.mod`

```
github.com/operator-framework/java-operator-plugins v0.0.0-20210225171707-e42ea87455e3
```

- Replace the java-operator-plugins path in go-mod pointing to the local dir of your kube-builder repo. Example.

```
github.com/operator-framework/java-operator-plugins => /Users/sushah/go/src/github.com/sujil02/java-operator-plugins
```

### Updates in Operator-SDK `internal/cmd/operator-sdk/cli/cli.go`

- Add the java-operator-sdk import

```
quarkusv1 "github.com/operator-framework/java-operator-plugins/pkg/quarkus/v1alpha"
```

- Introduce the java bundle in `GetPluginsCLIAndRoot()` method.
```
quarkusBundle, _ := plugin.NewBundle("quarkus"+plugins.DefaultNameQualifier, plugin.Version{Number: 1},
&quarkusv1.Plugin{},
)
```

- Add the created quarkusBundle to the `cli.New`

```
cli.WithPlugins(
ansibleBundle,
gov2Bundle,
gov3Bundle,
helmBundle,
quarkusBundle,
),
```


### Build and Install the Operator-SDK
```
go mod tidy
Expand All @@ -90,15 +43,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
```
143 changes: 63 additions & 80 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

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
```

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.
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 @@ -466,7 +427,7 @@ Let's create the utility method first.
### labelsForMemcached

A simple utility method to return a map of the labels we want to attach to some
of the resources. Below the `deleteResource` method add the following
of the resources. Below the `createOrUpdateResource` method add the following
helper:

```
Expand Down Expand Up @@ -541,16 +502,42 @@ Below your `labelsForMemcached(Memcached m)` block in the
```

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.
`createMemcachedDeployment` which we have implemented above.

### deleteResource
We have now implemented the `MemcachedController.java`.

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.
## Include Dependencies

We have now implemented the `MemcachedController.java`.
Please add below dependencies in `MemcachedController.java` file.

```
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.ContainerPortBuilder;
import io.fabric8.kubernetes.api.model.LabelSelectorBuilder;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.api.model.OwnerReferenceBuilder;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodSpecBuilder;
import io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.api.model.apps.DeploymentSpecBuilder;
import org.apache.commons.collections.CollectionUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
```

Also, update `pom.xml` file and import below dependency.

```
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
```

## Run the Operator

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

2. Install the CRD

Next we will install the CRD into the `default` namespace. Using the `crd.yaml`
you created in the [Manually created crd.yaml](#manually-create-crdyaml)
section, apply it to the cluster.
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.

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

```
$ kubectl apply -f crd.yaml
$ kubectl apply -f target/kubernetes/memcacheds.cache.example.com-v1.yml
customresourcedefinition.apiextensions.k8s.io/memcacheds.cache.example.com created
```

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

2. Install the CRD

Next we will install the CRD into the `default` namespace. Using the `crd.yaml`
you created in the [Manually created crd.yaml](#manually-create-crdyaml)
section, apply it to the cluster.
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.

```
$ kubectl apply -f crd.yaml
Expand Down