kubernetes-operator 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +25 -5
- data/lib/kubernetes-operator.rb +40 -35
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fea623164ec2a233bfca10f01311607bb48ccc75bbb5608d0c119cd551a86699
|
4
|
+
data.tar.gz: 52bb5c2f22dfd0af819f98f61a47a66a8109f4006b2b5b7efb209c73b25f08cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58b5c57faec3ad7fb0a3bf87919b33708872400f687d5216a606e3037f318a704f0d776f874d3749cb09710d87500a4c9c7fbc75f3b421bd103d0a997c01af28
|
7
|
+
data.tar.gz: 90ae44767237c5a5613e3af416d170ac29d9af338674933b1c95e5c2c5f02589af9372241fd00a131d907c892d7d0a932a571fea8bce88361991dc9bb9587847
|
data/README.md
CHANGED
@@ -1,12 +1,32 @@
|
|
1
|
-
#
|
2
|
-
|
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 and to quickly create your own operators based on ruby. It is not a framework, so the YAML CRDs still have to be created by hand.
|
3
|
+
|
4
|
+
## How it works?
|
5
|
+
- Watch changes on CR.
|
6
|
+
- Trigger Actions for Add,Update or Delete
|
7
|
+
- Handle the finalizer on create and after delete
|
8
|
+
|
9
|
+

|
10
|
+
|
11
|
+
## Installation
|
12
|
+
The gem is hosted on [rubygems.org](https://rubygems.org/gems/kubernetes-operator), so you can install it with ...
|
13
|
+
```
|
14
|
+
gem install kubernetes-operator
|
15
|
+
```
|
16
|
+
... or with bundler in your Gemfile.
|
17
|
+
|
18
|
+
## Example
|
19
|
+
|
20
|
+
```
|
21
|
+
crdGroup = "exmaple.com"
|
3
22
|
crdVersion = "v1alpha1"
|
4
|
-
crdPlural = "
|
23
|
+
crdPlural = "myres"
|
5
24
|
|
6
25
|
def my_custom_action(obj,k8sclient)
|
7
|
-
puts "
|
26
|
+
puts "Do some cool stuff for #{obj["metadata"]["crd_status"]} action of #{obj["metadata"]["name"]}"
|
8
27
|
end
|
9
28
|
|
10
29
|
opi = KubernetesOperator.new(crdGroup,crdVersion,crdPlural)
|
11
30
|
opi.setAddMethod(method(:my_custom_action))
|
12
|
-
opi.run()
|
31
|
+
opi.run()
|
32
|
+
```
|
data/lib/kubernetes-operator.rb
CHANGED
@@ -57,6 +57,10 @@ class KubernetesOperator
|
|
57
57
|
@sleepTimer = nr
|
58
58
|
end
|
59
59
|
|
60
|
+
def setScopeNamespaces(lst)
|
61
|
+
@lstOfNamespaces = lst
|
62
|
+
end
|
63
|
+
|
60
64
|
|
61
65
|
# Controller
|
62
66
|
def run
|
@@ -74,48 +78,49 @@ class KubernetesOperator
|
|
74
78
|
_uid = _i["metadata"]["uid"]
|
75
79
|
_v = _i["metadata"]["resourceVersion"]
|
76
80
|
|
77
|
-
|
81
|
+
if @lstOfNamespaces == nil || @lstOfNamespaces.contains(_i["metadata"]["namespace"])
|
82
|
+
_from_cache = @store.transaction{@store[_uid]}
|
78
83
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
+
unless _from_cache
|
85
|
+
# Add finalizer and refresh version number
|
86
|
+
_i[:metadata][:finalizers] = ["#{@crdPlural}.#{@crdVersion}.#{@crdGroup}"]
|
87
|
+
_i2 = @k8sclient.api(@crdGroup+"/"+@crdVersion).resource(@crdPlural).update_resource(_i)
|
88
|
+
_v = _i2["metadata"]["resourceVersion"]
|
84
89
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
+
# Cache last version of ressources
|
91
|
+
@store.transaction do
|
92
|
+
@store[_uid] = _v
|
93
|
+
@store.commit
|
94
|
+
end
|
90
95
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
96
|
+
# call the action method
|
97
|
+
_i["metadata"]["crd_status"] = "add"
|
98
|
+
@addMethod.call(_i,@k8sclient)
|
99
|
+
else
|
100
|
+
# only trigger action on change or delete event
|
101
|
+
unless _from_cache == _v
|
102
|
+
if _i["metadata"]["deletionTimestamp"]
|
103
|
+
# remove finalizers
|
104
|
+
_i[:metadata][:finalizers] = []
|
105
|
+
@k8sclient.api(@crdGroup+"/"+@crdVersion).resource(@crdPlural).update_resource(_i)
|
106
|
+
|
107
|
+
# call the action method
|
108
|
+
_i["metadata"]["crd_status"] = "delete"
|
109
|
+
@deleteMethod.call(_i,@k8sclient)
|
110
|
+
else
|
111
|
+
# store new version in cache
|
112
|
+
@store.transaction do
|
113
|
+
@store[_uid] = _v
|
114
|
+
@store.commit
|
115
|
+
end
|
116
|
+
|
117
|
+
# call the action method
|
118
|
+
_i["metadata"]["crd_status"] = "update"
|
119
|
+
@updateMethod.call(_i,@k8sclient)
|
110
120
|
end
|
111
|
-
|
112
|
-
# call the action method
|
113
|
-
_i["metadata"]["crd_status"] = "update"
|
114
|
-
@updateMethod.call(_i,@k8sclient)
|
115
121
|
end
|
116
122
|
end
|
117
123
|
end
|
118
|
-
|
119
124
|
end
|
120
125
|
|
121
126
|
# Done
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kubernetes-operator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tobias Kuntzsch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03-
|
11
|
+
date: 2020-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: k8s-client
|
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
50
|
- !ruby/object:Gem::Version
|
51
51
|
version: '0'
|
52
52
|
requirements: []
|
53
|
-
rubygems_version: 3.0.
|
53
|
+
rubygems_version: 3.0.3
|
54
54
|
signing_key:
|
55
55
|
specification_version: 4
|
56
56
|
summary: lib
|