puppet-prod/modules/rke2/lib/facter/k8s_namespaces.rb
Ben Vincent 2d1f56779f
All checks were successful
Build / precommit (pull_request) Successful in 3m59s
fix: helm before rke2 managed manifests
- add fact to list namespaces
- require namespace before adding additional config
- renamed some files to better match what they are
2025-09-21 00:01:28 +10:00

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