All checks were successful
Build / precommit (pull_request) Successful in 5m15s
- add fact to list namespaces - require namespace before adding additional config - renamed some files to better match what they are
30 lines
732 B
Ruby
30 lines
732 B
Ruby
# 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
|