kubernetes-operator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +12 -0
- data/lib/kubernetes-operator.rb +129 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c172c29b434156c033e913d2a75f2ca3d4e0167aca5a97d8beedc3b54624d790
|
4
|
+
data.tar.gz: 8365a0305165569330e73566c307243c2184817a0343b0db38f3d9fe92a937b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bd3ef139436194830920678f1f5a3d8c0b06580b45c3dd6af92542d438c524743335bc4c3fb856e2a3bd0b37a80637a00ea278fe6ef102c6394dd549533c1032
|
7
|
+
data.tar.gz: 8b75159dae0de65a1a6d687f864036f569b0aeb57281061881f56e58e894986ed46b55ffea665a958a0fb3afb8d5bfc75a1c92d61c9a185cc77f17ecba6d9ea7
|
data/README.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# example usage
|
2
|
+
crdGroup = "familykuntzsch.de"
|
3
|
+
crdVersion = "v1alpha1"
|
4
|
+
crdPlural = "fuconfigs"
|
5
|
+
|
6
|
+
def my_custom_action(obj,k8sclient)
|
7
|
+
puts "==> Im cool as fu (#{obj["metadata"]["crd_status"]} of #{obj["metadata"]["name"]})"
|
8
|
+
end
|
9
|
+
|
10
|
+
opi = KubernetesOperator.new(crdGroup,crdVersion,crdPlural)
|
11
|
+
opi.setAddMethod(method(:my_custom_action))
|
12
|
+
opi.run()
|
@@ -0,0 +1,129 @@
|
|
1
|
+
#!/usr/local/bin/ruby
|
2
|
+
|
3
|
+
require 'k8s-client'
|
4
|
+
require 'yaml'
|
5
|
+
require 'yaml/store'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
|
9
|
+
class KubernetesOperator
|
10
|
+
|
11
|
+
def initialize(crdGroup, crdVersion, crdPlural)
|
12
|
+
# parameter
|
13
|
+
@crdGroup = crdGroup
|
14
|
+
@crdVersion = crdVersion
|
15
|
+
@crdPlural = crdPlural
|
16
|
+
|
17
|
+
# default config
|
18
|
+
@sleepTimer = 10
|
19
|
+
|
20
|
+
# create cache
|
21
|
+
@store = YAML::Store.new("#{@crdGroup}_#{@crdVersion}_#{@crdPlural}.yaml")
|
22
|
+
|
23
|
+
# Kubeconfig
|
24
|
+
puts '{leve: "info", message: "validate kube config"}'
|
25
|
+
if File.exist?("#{Dir.home}/.kube/config")
|
26
|
+
puts '{leve: "info", message: "found kubeconfig in home directory"}'
|
27
|
+
@k8sclient = K8s::Client.config(
|
28
|
+
K8s::Config.load_file(
|
29
|
+
File.expand_path '~/.kube/config'
|
30
|
+
)
|
31
|
+
)
|
32
|
+
else
|
33
|
+
puts '{leve: "info", message: "use in cluster configuration"}'
|
34
|
+
@k8sclient = K8s::Client.in_cluster_config
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Action Methods
|
39
|
+
def setAddMethod(callback)
|
40
|
+
@addMethod = callback
|
41
|
+
end
|
42
|
+
|
43
|
+
def setUpdateMethod(callback)
|
44
|
+
@updateMethod = callback
|
45
|
+
end
|
46
|
+
|
47
|
+
def setDeleteMethod(callback)
|
48
|
+
@deleteMethod = callback
|
49
|
+
end
|
50
|
+
|
51
|
+
def defaultActionMethod(obj,k8sclient)
|
52
|
+
puts "{leve: \"info\", action: \"#{obj["metadata"]["crd_status"]}\", ressource: \"#{obj["metadata"]["namespace"]}/#{obj["metadata"]["name"]}\"}"
|
53
|
+
end
|
54
|
+
|
55
|
+
# Config Methods
|
56
|
+
def setSleepTimer(nr)
|
57
|
+
@sleepTimer = nr
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
# Controller
|
62
|
+
def run
|
63
|
+
|
64
|
+
@addMethod = method(:defaultActionMethod) unless @addMethod
|
65
|
+
@updateMethod = method(:defaultActionMethod) unless @updateMethod
|
66
|
+
@deleteMethod = method(:defaultActionMethod) unless @deleteMethod
|
67
|
+
|
68
|
+
while true
|
69
|
+
|
70
|
+
# Search for ressources
|
71
|
+
_ressources = @k8sclient.api(@crdGroup+"/"+@crdVersion).resource(@crdPlural).list()
|
72
|
+
|
73
|
+
_ressources.each do |_i|
|
74
|
+
_uid = _i["metadata"]["uid"]
|
75
|
+
_v = _i["metadata"]["resourceVersion"]
|
76
|
+
|
77
|
+
_from_cache = @store.transaction{@store[_uid]}
|
78
|
+
|
79
|
+
unless _from_cache
|
80
|
+
# Add finalizer and refresh version number
|
81
|
+
_i[:metadata][:finalizers] = ["#{@crdPlural}.#{@crdVersion}.#{@crdGroup}"]
|
82
|
+
_i2 = @k8sclient.api(@crdGroup+"/"+@crdVersion).resource(@crdPlural).update_resource(_i)
|
83
|
+
_v = _i2["metadata"]["resourceVersion"]
|
84
|
+
|
85
|
+
# Cache last version of ressources
|
86
|
+
@store.transaction do
|
87
|
+
@store[_uid] = _v
|
88
|
+
@store.commit
|
89
|
+
end
|
90
|
+
|
91
|
+
# call the action method
|
92
|
+
_i["metadata"]["crd_status"] = "add"
|
93
|
+
@addMethod.call(_i,@k8sclient)
|
94
|
+
else
|
95
|
+
# only trigger action on change or delete event
|
96
|
+
unless _from_cache == _v
|
97
|
+
if _i["metadata"]["deletionTimestamp"]
|
98
|
+
# remove finalizers
|
99
|
+
_i[:metadata][:finalizers] = []
|
100
|
+
@k8sclient.api(@crdGroup+"/"+@crdVersion).resource(@crdPlural).update_resource(_i)
|
101
|
+
|
102
|
+
# call the action method
|
103
|
+
_i["metadata"]["crd_status"] = "delete"
|
104
|
+
@deleteMethod.call(_i,@k8sclient)
|
105
|
+
else
|
106
|
+
# store new version in cache
|
107
|
+
@store.transaction do
|
108
|
+
@store[_uid] = _v
|
109
|
+
@store.commit
|
110
|
+
end
|
111
|
+
|
112
|
+
# call the action method
|
113
|
+
_i["metadata"]["crd_status"] = "update"
|
114
|
+
@updateMethod.call(_i,@k8sclient)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
# Done
|
122
|
+
sleep @sleepTimer
|
123
|
+
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kubernetes-operator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tobias Kuntzsch
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: k8s-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Libary to create an kubernetes operator with ruby
|
28
|
+
email: mail@tobiaskuntzsch.de
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- README.md
|
34
|
+
- lib/kubernetes-operator.rb
|
35
|
+
homepage: https://gitlab.com/tobiaskuntzsch/kubernetes-operator
|
36
|
+
licenses: []
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubygems_version: 3.0.8
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: lib
|
57
|
+
test_files: []
|