Skip to content

Commit a955392

Browse files
adam-sandorcs-marek-ruszczyk
authored andcommitted
Release, docs and samples (#46)
* organise samples and harmonize naming in pom files * add script to prepare version numbers for release * release version 0.3.2
1 parent 02383ce commit a955392

File tree

24 files changed

+190
-54
lines changed

24 files changed

+190
-54
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
language: java
22
dist: bionic
3+
cache:
4+
directories:
5+
- $HOME/.m2
36
before_install:
47
- echo $GPGKEY | base64 --decode | gpg --import
58
script:

README.md

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
# java-operator-sdk
22
[![Build Status](https://travis-ci.org/ContainerSolutions/java-operator-sdk.svg?branch=master)](https://travis-ci.org/ContainerSolutions/java-operator-sdk)
3-
> This project is in incubation phase.
43

54
SDK for building Kubernetes Operators in Java. Inspired by [operator-sdk](https://github.com/operator-framework/operator-sdk).
65
In this first iteration we aim to provide a framework which handles the reconciliation loop by dispatching events to
76
a Controller written by the user of the framework.
87

98
The Controller only contains the logic to create, update and delete the actual resources related to the CRD.
109

11-
## Implementation
12-
13-
This library relies on the amazing [kubernetes-client](https://github.com/fabric8io/kubernetes-client) from fabric8. Most of the heavy lifting is actually done by
14-
kubernetes-client.
15-
1610
## Roadmap
1711

1812
Feature we would like to implement and invite the community to help us implement in the future:
@@ -21,17 +15,20 @@ Feature we would like to implement and invite the community to help us implement
2115
* Testing support
2216
* Class generation from CRD to POJO
2317

24-
2518
## Usage
26-
> Under sample directory you can try out our sample.
19+
20+
We have several sample Operators under the samples directory:
21+
* *basic*: Minimal Operator implementation which only parses the Custom Resource and prints to stdout.
22+
Implemented with and without Spring Boot support. The two samples share the common module.
23+
* *webserver*: More realistic example creating an nginx webserver from a Custom Resource containing html code.
2724

2825
Add dependency to your project:
2926

3027
```xml
3128
<dependency>
3229
<groupId>com.github.containersolutions</groupId>
3330
<artifactId>operator-framework</artifactId>
34-
<version>0.1.1</version>
31+
<version>{see https://search.maven.org/search?q=a:operator-framework for latest version}</version>
3532
</dependency>
3633
```
3734

@@ -50,15 +47,15 @@ public class Runner {
5047
The Controller implements the business logic and describes all the classes needed to handle the CRD.
5148

5249
```java
53-
@Controller(customResourceClass = CustomService.class,
54-
kind = CustomServiceController.CRD_NAME,
55-
group = CustomServiceController.GROUP,
56-
customResourceListClass = CustomServiceList.class,
57-
customResourceDonebaleClass = CustomServiceDoneable.class)
58-
public class CustomServiceController implements ResourceController<CustomService> {
50+
@Controller(customResourceClass = WebServer.class,
51+
kind = WebServerController.KIND,
52+
group = WebServerController.GROUP,
53+
customResourceListClass = WebServerList.class,
54+
customResourceDonebaleClass = WebServerDoneable.class)
55+
public class WebServerController implements ResourceController<WebServer> {
5956

60-
public static final String CRD_NAME = "CustomService";
61-
public static final String GROUP = "sample.javaoperatorsdk";
57+
static final String KIND = "WebServer";
58+
static final String GROUP = "sample.javaoperatorsdk";
6259

6360
@Override
6461
public boolean deleteResource(CustomService resource, Context<CustomService> context) {
@@ -78,23 +75,46 @@ public class CustomServiceController implements ResourceController<CustomService
7875
Our custom resource java representation
7976

8077
```java
81-
public class CustomService extends CustomResource {
78+
public class WebServer extends CustomResource {
79+
80+
private WebServerSpec spec;
8281

83-
private ServiceSpec spec;
82+
private WebServerStatus status;
8483

85-
public ServiceSpec getSpec() {
84+
public WebServerSpec getSpec() {
8685
return spec;
8786
}
8887

89-
public void setSpec(ServiceSpec spec) {
88+
public void setSpec(WebServerSpec spec) {
9089
this.spec = spec;
9190
}
91+
92+
public WebServerStatus getStatus() {
93+
return status;
94+
}
95+
96+
public void setStatus(WebServerStatus status) {
97+
this.status = status;
98+
}
99+
}
100+
101+
public class WebServerSpec {
102+
103+
private String html;
104+
105+
public String getHtml() {
106+
return html;
107+
}
108+
109+
public void setHtml(String html) {
110+
this.html = html;
111+
}
92112
}
93113
```
94114

95115
## Spring Boot Support
96116

97-
We provide a spring boot starter to automatically handle bean registration, and registering variouse components as beans.
117+
We provide a spring boot starter to automatically handle bean registration, and registering various components as beans.
98118
To use it just include the following dependency to you project:
99119

100120
```
@@ -105,35 +125,49 @@ To use it just include the following dependency to you project:
105125
</dependency>
106126
```
107127

108-
Note that controllers needs to be registered as a bean. Thus just annotating them also with `@Component` annotation.
109-
See Spring docs for for details, also our sample with component scanning.
128+
Note that controllers needs to be registered as beans in the Spring context. For example adding the `@Component` annotation
129+
on the classes will work.
130+
See Spring docs for for details, also our spring-boot with component scanning.
110131
All controllers that are registered as a bean, gets automatically registered to operator.
111132

112133
Kubernetes client creation using properties is also supported, for complete list see: [Link for config class]
113134

114-
## Dealing with Consistency
115135

116-
### Run Single Instance
136+
## Implementation / Design details
137+
138+
This library relies on the amazing [kubernetes-client](https://github.com/fabric8io/kubernetes-client) from fabric8.
139+
Most of the heavy lifting is actually done by kubernetes-client.
140+
141+
What the framework adds on top of the bare client:
142+
* Management of events from the Kubernetes API. All events are inserted into a queue by the EventScheduler. The
143+
framework makes sure only the latest event for a certain resource is processed. This is especially important since
144+
on startup the operator can receive a whole series of obsolete events.
145+
* Retrying of failed actions. When an event handler throws an exception the event is put back in the queue.
146+
* A clean interface to the user of the framework to receive events related to the Controller's resource.
147+
148+
### Dealing with Consistency
149+
150+
#### Run Single Instance
117151

118152
There should be always just one instance of an operator running at a time (think process). If there there would be
119153
two ore more, in general it could lead to concurrency issues. Note that we are also doing optimistic locking when we update a resource.
120154
In this way the operator is not highly available. However for operators this not necessary an issue,
121155
if the operator just gets restarted after it went down.
122156

123-
### Operator Restarts
157+
#### Operator Restarts
124158

125159
When an operator is started we got events for every resource (of a type we listen to) already on the cluster. Even if the resource is not changed
126160
(We use `kubectl get ... --watch` in the background). This can be a huge amount of resources depending on your use case.
127161
So it could be a good case just have a status field on the resource which is checked, if there anything needs to be done.
128162

129-
### At Least Once
163+
#### At Least Once
130164

131165
To implement controller logic, we have to override two methods: `createOrUpdateResource` and `deleteResource`.
132166
These methods are called if a resource is create/changed or marked for deletion. In most cases these methods will be
133167
called just once, but in some rare cases can happen that are called more then once. In practice this means that the
134168
implementation needs to be **idempotent**.
135169

136-
### Deleting a Resource
170+
#### Deleting a Resource
137171

138172
During deletion process we use [Kubernetes finalizers](https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/#finalizers
139173
"Kubernetes docs") finalizers. This is required, since it can happen that the operator is not running while the delete

operator-framework/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.github.containersolutions</groupId>
88
<artifactId>java-operator-sdk</artifactId>
9-
<version>0.3.2-SNAPSHOT</version>
9+
<version>0.3.7-SNAPSHOT</version>
1010
</parent>
1111

1212
<artifactId>operator-framework</artifactId>

perform-release.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env bash
2+
BRANCH=$(git rev-parse --abbrev-ref HEAD)
3+
if [ "$BRANCH" != "master" ]; then
4+
echo "Only run releases from master branch (current is '$BRANCH')"
5+
exit 0
6+
fi
7+
8+
echo "Setting version to next release version"
9+
mvn -q build-helper:parse-version versions:set -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.incrementalVersion}
10+
find . -name 'pom.xml' | xargs git add
11+
12+
RELEASE_VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)
13+
14+
read -p "Finish release of ${RELEASE_VERSION} by pushing it to Git (y/n)?" choice
15+
if [[ $choice == 'y' ]]; then
16+
mvn -q versions:commit
17+
git commit -m "release version ${RELEASE_VERSION}"
18+
echo "git push"
19+
git push
20+
21+
echo "Have to wait until TravisCI finishes the release build, otherwise it will cancel it"
22+
read -p "Is TravisCI finished with the release build (https://travis-ci.org/ContainerSolutions/java-operator-sdk/) (y/n)?" finished
23+
if [[ $finished == 'y' ]]; then
24+
echo "Setting new SNAPSHOT version"
25+
mvn -q build-helper:parse-version versions:set -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.minorVersion}.\${parsedVersion.nextIncrementalVersion}-SNAPSHOT versions:commit
26+
NEW_VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)
27+
find . -name 'pom.xml' | xargs git add
28+
git commit -m "set SNAPSHOT version: ${NEW_VERSION}"
29+
echo "git push"
30+
git push
31+
echo "Finished release. New SNAPSHOT version is ${NEW_VERSION}"
32+
fi
33+
else
34+
echo "Reverting version"
35+
mvn -q versions:revert
36+
fi

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.github.containersolutions</groupId>
77
<artifactId>java-operator-sdk</artifactId>
8-
<version>0.3.2-SNAPSHOT</version>
8+
<version>0.3.7-SNAPSHOT</version>
99
<name>Operator SDK for Java</name>
1010
<description>Java SDK for implementing Kubernetes operators</description>
1111
<packaging>pom</packaging>
File renamed without changes.

samples/common/pom.xml renamed to samples/basic/common/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
<parent>
77
<groupId>com.github.containersolutions</groupId>
8-
<artifactId>operator-framework-samples</artifactId>
9-
<version>0.3.2-SNAPSHOT</version>
8+
<artifactId>java-operator-sdk-basic-sample</artifactId>
9+
<version>0.3.7-SNAPSHOT</version>
1010
</parent>
1111

12-
<artifactId>operator-framework-samples-common-files</artifactId>
13-
<name>Operator SDK - Samples - Common Files</name>
12+
<artifactId>operator-framework-samples-common</artifactId>
13+
<name>Operator SDK - Samples - Basic - Common Files</name>
1414
<description>Files shared between samples</description>
1515
<packaging>jar</packaging>
1616

0 commit comments

Comments
 (0)