Skip to content

Commit

Permalink
add mutex for namespace creation
Browse files Browse the repository at this point in the history
  • Loading branch information
wr0ngway committed Jan 20, 2022
1 parent 217639a commit 87645e7
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/kubetruth/kubeapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def initialize(namespace: nil, token: nil, api_url: nil)

@api_url = api_url || 'https://kubernetes.default.svc'
@api_clients = {}
@namespace_mutex = Mutex.new
end

def api_url(api)
Expand Down Expand Up @@ -79,14 +80,16 @@ def apiVersion_client(apiVersion)
end

def ensure_namespace(ns = namespace)
begin
client.get_namespace(ns)
rescue Kubeclient::ResourceNotFoundError
newns = Kubeclient::Resource.new
newns.metadata = {}
newns.metadata.name = ns
set_managed(newns)
client.create_namespace(newns)
@namespace_mutex.synchronize do
begin
client.get_namespace(ns)
rescue Kubeclient::ResourceNotFoundError
newns = Kubeclient::Resource.new
newns.metadata = {}
newns.metadata.name = ns
set_managed(newns)
client.create_namespace(newns)
end
end
end

Expand Down

0 comments on commit 87645e7

Please sign in to comment.