-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconcile_leaf.rb
163 lines (123 loc) · 4.3 KB
/
reconcile_leaf.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require 'kubernetes-operator'
require 'pry'
require 'open-uri'
require 'active_record'
require './app/models/application_record'
# Bundler.require(*Rails.groups)
require 'pg'
require 'dotenv'
require './app/models/github_org'
require './app/models/repository'
require './app/models/package'
require './lib/ar_base_connection'
module Leaf
class Reconciler
require './lib/my_wasmer'
def initialize()
# init_connections
end
def init_k8s_only
@opi = @api[:opi]
@logger = @opi.getLogger
@eventHelper = @opi.getEventHelper
end
# (this is a do-nothing method, don't call it)
# In the parent process, we don't use any database connections
def init_connections
# crdVersion = "v1alpha1"
# crdPlural = "leaves"
# @api = AR::BaseConnection.
# new(version: crdVersion, plural: crdPlural, poolSize: 0)
# init_k8s_only
end
# In the forked process under each fiber, use the database
def reinit_connections
crdVersion = "v1alpha1"
crdPlural = "leaves"
@api = AR::BaseConnection.
new(version: crdVersion, plural: crdPlural, poolSize: 1)
init_k8s_only
end
def reconcile(name)
# name = obj["metadata"]["name"]
reinit_connections
@logger.info("the fiber is running leaf/#{name}")
k8s = k8s_client
obj = k8s.get_leaf(name, 'default', {})
generation = obj["metadata"]["generation"]
project = obj["spec"]["projectName"]
repo = obj["spec"]["repoName"]
image = obj["spec"]["packageName"]
patched = reconcile_async(obj: obj, name: name, project: project, repo: repo, image: image, k8s: k8s)
end
def is_finalizer_set?(obj)
metadata = obj["metadata"]
finalizers = metadata&.dig("finalizers")
fin = finalizers&.select {|f| f == "leaves.v1alpha1.example.com"}
return !fin&.first.nil?
end
def reconcile_async(obj:, name:, project:, repo:, image:, k8s:)
@logger.info("in reconcile_async leaf/#{name}")
r = get_current_stat_with_time(project, repo, image)
# @eventHelper.add(obj,"wasmer returned current download count in leaf/#{name}")
fluxcd = nil
loop do
fluxcd = ::GithubOrg.find_by(name: project)
break if fluxcd.present?
# sleep 2
end
repo_obj = ::Repository.find_or_create_by(name: repo, github_org: fluxcd)
package_obj = ::Package.find_or_create_by(name: image, repository: repo_obj)
# @eventHelper.add(obj,"saving package count in leaf/#{name}")
package_obj.download_count = r[:count]
package_obj.save!
# @eventHelper.add(obj,"saved package count in leaf/#{name}")
t = DateTime.now
repo_obj.run(k8s:, last_update: t.in_time_zone.to_time)
package_obj.run(k8s:, last_update: t.in_time_zone.to_time)
@logger.info("reconcile_async ran database activities leaf/#{name}")
name = obj["metadata"]["name"]
generation = obj["metadata"]["generation"]
# @eventHelper.add(obj,"marking finally ready with patch_entity in leaf/#{name}")
new_status = {:status => {
:count => r[:count],
:lastUpdate => r[:time].to_s,
# :lastHandledReconcileAt => r[:time].to_s,
:observedGeneration => generation,
:conditions => [ {
:lastTransitionTime => t,
:message => "OK",
:observedGeneration => generation,
:reason => "Succeeded",
:status => "True",
:type => "Ready"
} ]
} }
k8s.patch_entity('leaves', name + "/status", new_status, 'merge-patch', 'default')
end
def get_current_stat_with_time(project, repo, image)
client = Proc.new do |url|
URI.open(url)
end
t = Time.now
h = http_client_wrapped(client, project, repo, image)
c = wasmer_current_download_count(h, repo, image)
{time: t, count: c}
end
def http_client_wrapped(http_client, project, repo, image)
url = "https://github.com/#{project}/#{repo}/pkgs/container/#{image}"
http_client.call(url)
# rescue OpenURI::HTTPError => e
end
def http_client_read
http_client_wrapped.read
end
def k8s_client
@opi.instance_variable_get("@k8sclient")
end
end
end
reconcile_this = ARGV[0]
r = Leaf::Reconciler.new
r.reinit_connections
r.reconcile reconcile_this