Module: K8y::Kubeconfig

Defined in:
lib/k8y/kubeconfig.rb,
lib/k8y/kubeconfig/user.rb,
lib/k8y/kubeconfig/config.rb,
lib/k8y/kubeconfig/cluster.rb,
lib/k8y/kubeconfig/context.rb,
lib/k8y/kubeconfig/auth_info.rb,
lib/k8y/kubeconfig/auth_provider.rb

Defined Under Namespace

Classes: AuthInfo, AuthProvider, Cluster, Config, Context, User

Constant Summary collapse

Error =
Class.new(Error)
NotInClusterError =
Class.new(Error)
IN_CLUSTER_NAME =
"in-cluster"
TOKEN_FILE =
"/var/run/secrets/kubernetes.io/serviceaccount/token"
ROOT_CA_FILE =
"/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"

Class Method Summary collapse

Class Method Details

.from_file(file = File.open(ENV["KUBECONFIG"])) ⇒ Object



15
16
17
18
# File 'lib/k8y/kubeconfig.rb', line 15

def self.from_file(file = File.open(ENV["KUBECONFIG"]))
  hash = YAML.safe_load(file.read, permitted_classes: [Date, Time])
  Config.from_hash(hash)
end

.in_cluster_configObject

Raises:



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
# File 'lib/k8y/kubeconfig.rb', line 20

def self.in_cluster_config
  host = ENV.fetch("KUBERNETES_SERVICE_HOST", nil)
  port = ENV.fetch("KUBERNETES_SERVICE_PORT", nil)
  raise NotInClusterError unless host && port

  token_data = File.read(TOKEN_FILE)
  auth_info = AuthInfo.new(token: token_data, token_file: TOKEN_FILE)
  user = User.new(
    name: IN_CLUSTER_NAME,
    auth_info: auth_info
  )

  ca_data = Base64.encode64(File.read(ROOT_CA_FILE))
  cluster = Cluster.new(
    name: IN_CLUSTER_NAME,
    insecure_skip_tls_verify: false,
    certificate_authority: nil,
    certificate_authority_data: ca_data,
    server: "https://#{host}:#{port}",
  )
  context = Context.new(
    name: IN_CLUSTER_NAME,
    cluster: IN_CLUSTER_NAME,
    namespace: nil,
    user: IN_CLUSTER_NAME
  )

  Config.new(
    clusters: [cluster],
    contexts: [context],
    current_context: context,
    users: [user]
  )
end