- add fact to list namespaces - require namespace before adding additional config - renamed some files to better match what they are
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'json'
|
||||
require 'open3'
|
||||
|
||||
Facter.add(:k8s_namespaces) do
|
||||
confine do
|
||||
File.exist?('/etc/rancher/rke2/rke2.yaml') &&
|
||||
File.executable?('/usr/bin/kubectl') # Adjust this path if needed
|
||||
end
|
||||
|
||||
setcode do
|
||||
env = { 'KUBECONFIG' => '/etc/rancher/rke2/rke2.yaml' }
|
||||
cmd = ['/usr/bin/kubectl', 'get', 'namespaces', '-o', 'json']
|
||||
|
||||
stdout, stderr, status = Open3.capture3(env, *cmd)
|
||||
|
||||
if status.success?
|
||||
json = JSON.parse(stdout)
|
||||
json['items'].map { |item| item['metadata']['name'] }
|
||||
else
|
||||
Facter.debug("kubectl error: #{stderr}")
|
||||
[]
|
||||
end
|
||||
rescue StandardError => e
|
||||
Facter.debug("Exception in k8s_namespaces fact: #{e.message}")
|
||||
[]
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user