来自Kubernetes API的Ruby Data摘录

时间:2017-12-11 13:19:51

标签: ruby kubernetes

尝试从Kubeclient api获取节点角色

命令:client.get_nodes()[0].metadata.labels

Kubeclient::Node beta.kubernetes.io/arch="amd64", 
  beta.kubernetes.io/instance-type="t2.medium", 
  beta.kubernetes.io/os="linux",
  failure-domain.beta.kubernetes.io/region="eu-west-1",
  failure-domain.beta.kubernetes.io/zone="eu-west-1a", 
  kubernetes.io/hostname="ip-X-X-XX-XX.eu-west-1.compute.internal", 
  kubernetes.io/role="**node**", node-role.kubernetes.io/node="">

我需要获取kubernetes.io/role="node"这个节点的值,有些人可以使用Ruby代码来格式化这个输出。

1 个答案:

答案 0 :(得分:1)

也许你可以这样做:

require 'kubeclient'

config = Kubeclient::Config.read('/path/to/.kube/config')

client = Kubeclient::Client.new(
    config.context.api_endpoint,
    config.context.api_version,
    {
        ssl_options: config.context.ssl_options,
        auth_options: config.context.auth_options
    }
)

// prints the label kubernetes.io/role
puts client.get_nodes()[0].metadata.labels['kubernetes.io/role']

// Iterate over all labels
client.get_nodes()[0].metadata.labels.each_pair do |key, value|
    puts "#{key} = #{value}"
end