kubernetes-operator 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +84 -8
- data/lib/kubernetes-operator.rb +4 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 648d3409d77416f22cad190066b561a44dcff635d420f53fcc5fd96ab3d5d648
|
4
|
+
data.tar.gz: e7a978d1e2bf3ec613dcc0a23f338427143fff11d7baff8187ec40c9c52a82db
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50e28406518c4ed1c94f70b27ffc488fff793f790935ca569645da9b6c2b49ee6d53e24fbc2d09ff80dfbb3c805a07accbccc6af58825990acf5960a268b85df
|
7
|
+
data.tar.gz: 3c5fbb879e6bbd1989853fb44cab7fdb5b732f538ec801a1d5e11566114abca0a7a21b25cdb78264c7649bac4f5e05cc26208747541f3d43d37b23d61b63b179
|
data/README.md
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
# Ruby Kubernetes Operator
|
2
|
-
If you do not want to create your operators for kubernetes in golang, it is difficult to find good frameworks. This gem is used to implement a few basic functions
|
2
|
+
If you do not want to create your operators for kubernetes in golang, it is difficult to find good frameworks. This gem is used to implement a few basic functions andand helps eucht with a quick entry into the world of operators. Unlike the operator-sdk in golang, the gem does not offer yaml generators. You would have to create the crds.yml yourself.<br>
|
3
3
|
|
4
|
-
##
|
5
|
-
|
6
|
-
-
|
7
|
-
-
|
4
|
+
## Current features
|
5
|
+
|
6
|
+
- Basic: Watch changes of custom resources and trigger upsert (add, update) or delete methods.
|
7
|
+
- Cache resource version in yaml store to avoid double events.
|
8
|
+
- Handle the finalizer on create and after delete events.
|
9
|
+
- Update status after successfuly passed upsert methods.
|
10
|
+
- Provides an event handler, so you can easy create events forcustom resources. On deletion it also will clean up all lost events.
|
8
11
|
|
9
12
|
![KubernetesOperator.png](KubernetesOperator.png)
|
10
13
|
|
@@ -14,19 +17,92 @@ The gem is hosted on [rubygems.org](https://rubygems.org/gems/kubernetes-operato
|
|
14
17
|
gem install kubernetes-operator
|
15
18
|
```
|
16
19
|
... or with bundler in your Gemfile.
|
20
|
+
```
|
21
|
+
source 'https://rubygems.org'
|
22
|
+
gem 'kubernetes-operator'
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
An full example you will found in [./example](./example), also you can check our [yard](https://tobiaskuntzsch.gitlab.io/kubernetes-operator/) documentation.
|
17
27
|
|
18
|
-
|
28
|
+
### Watch events
|
29
|
+
The simplest start is an operator who only listens to the upsert events and writes the status in a logfile.
|
19
30
|
|
20
31
|
```
|
21
32
|
crdGroup = "exmaple.com"
|
22
33
|
crdVersion = "v1alpha1"
|
23
34
|
crdPlural = "myres"
|
24
35
|
|
25
|
-
def my_custom_action(obj
|
36
|
+
def my_custom_action(obj)
|
26
37
|
puts "Do some cool stuff for #{obj["metadata"]["crd_status"]} action of #{obj["metadata"]["name"]}"
|
27
38
|
end
|
28
39
|
|
29
40
|
opi = KubernetesOperator.new(crdGroup,crdVersion,crdPlural)
|
30
|
-
opi.
|
41
|
+
opi.setUpsertMethod(method(:my_custom_action))
|
42
|
+
opi.run()
|
43
|
+
```
|
44
|
+
|
45
|
+
You can also add an delete methode.
|
46
|
+
|
47
|
+
```
|
48
|
+
...
|
49
|
+
|
50
|
+
def my_custom_action_fordelete(obj)
|
51
|
+
puts "Delete #{obj["metadata"]["crd_status"]} action of #{obj["metadata"]["name"]}"
|
52
|
+
end
|
53
|
+
|
54
|
+
...
|
55
|
+
|
56
|
+
opi.setDeleteMethod(method(:my_custom_action_fordelete))
|
31
57
|
opi.run()
|
58
|
+
```
|
59
|
+
### Return status subressources
|
60
|
+
If you have activated the subresource status in your crd, you can return a status `[:status]` object.<br><br>
|
61
|
+
Example resource definition:
|
62
|
+
```
|
63
|
+
spec:
|
64
|
+
...
|
65
|
+
subresources:
|
66
|
+
status: {}
|
67
|
+
...
|
68
|
+
validation:
|
69
|
+
openAPIV3Schema:
|
70
|
+
properties:
|
71
|
+
...
|
72
|
+
status:
|
73
|
+
type: object
|
74
|
+
properties:
|
75
|
+
message:
|
76
|
+
type: "string"
|
77
|
+
minimum: 1
|
78
|
+
```
|
79
|
+
Example upsert method:
|
80
|
+
```
|
81
|
+
def upsert(obj)
|
82
|
+
{:status => {:message => "upsert works fine"}}
|
83
|
+
end
|
84
|
+
```
|
85
|
+
Result:
|
86
|
+
```
|
87
|
+
Status:
|
88
|
+
Message: upsert works fine
|
89
|
+
```
|
90
|
+
### Logging
|
91
|
+
The operator framework generates an Log4r::Logger, you can also use this to send logs.
|
92
|
+
```
|
93
|
+
@logger = opi.getLogger()
|
94
|
+
@logger.info("create new fancy sample with the name #{obj["spec"]["sampleName"]}")
|
95
|
+
```
|
96
|
+
### Events
|
97
|
+
You can add events to your custom resource by using the EventHelper.
|
98
|
+
```
|
99
|
+
@eventHelper = opi.getEventHelper()
|
100
|
+
@eventHelper.add(obj,"fancy sample event")
|
101
|
+
```
|
102
|
+
Result:
|
103
|
+
```
|
104
|
+
Events:
|
105
|
+
Type Reason Age From Message
|
106
|
+
---- ------ ---- ---- -------
|
107
|
+
Normal Upsert 36s KubernetesOperator fancy sample event
|
32
108
|
```
|
data/lib/kubernetes-operator.rb
CHANGED
@@ -242,7 +242,7 @@ class KubernetesOperator
|
|
242
242
|
unless isCached
|
243
243
|
# trigger action
|
244
244
|
@logger.info("trigger add action for #{notice[:object][:metadata][:name]} (#{notice[:object][:metadata][:uid]})")
|
245
|
-
resp = @addMethod.call(notice[:object]
|
245
|
+
resp = @addMethod.call(notice[:object])
|
246
246
|
# update status
|
247
247
|
if resp[:status]
|
248
248
|
@k8sclient.patch_entity(@crdPlural,notice[:object][:metadata][:name]+"/status", {status: resp[:status]},'merge-patch',@options[:namespace])
|
@@ -266,7 +266,7 @@ class KubernetesOperator
|
|
266
266
|
unless notice[:object][:metadata][:deletionTimestamp]
|
267
267
|
# trigger action
|
268
268
|
@logger.info("trigger update action for #{notice[:object][:metadata][:name]} (#{notice[:object][:metadata][:uid]})")
|
269
|
-
resp = @updateMethod.call(notice[:object]
|
269
|
+
resp = @updateMethod.call(notice[:object])
|
270
270
|
# update status
|
271
271
|
if resp[:status]
|
272
272
|
@k8sclient.patch_entity(@crdPlural,notice[:object][:metadata][:name]+"/status", {status: resp[:status]},'merge-patch',@options[:namespace])
|
@@ -279,7 +279,7 @@ class KubernetesOperator
|
|
279
279
|
else
|
280
280
|
# trigger action
|
281
281
|
@logger.info("trigger delete action for #{notice[:object][:metadata][:name]} (#{notice[:object][:metadata][:uid]})")
|
282
|
-
@
|
282
|
+
@deleteMethod.call(notice[:object])
|
283
283
|
# remove finalizer
|
284
284
|
@logger.info("remove finalizer to #{notice[:object][:metadata][:name]} (#{notice[:object][:metadata][:uid]})")
|
285
285
|
patched = @k8sclient.patch_entity(@crdPlural,notice[:object][:metadata][:name], {metadata: {finalizers: nil}},'merge-patch',@options[:namespace])
|
@@ -313,7 +313,7 @@ class KubernetesOperator
|
|
313
313
|
|
314
314
|
# default methode, only an esay print command
|
315
315
|
private
|
316
|
-
def defaultActionMethod(obj
|
316
|
+
def defaultActionMethod(obj)
|
317
317
|
puts "{leve: \"info\", action: \"#{obj["metadata"]["crd_status"]}\", ressource: \"#{obj["metadata"]["namespace"]}/#{obj["metadata"]["name"]}\"}"
|
318
318
|
end
|
319
319
|
end
|