I'm making a K8s API request with kubeclient
ruby gem and it returns bad-request (400)
ruby: 2.5.0
k8s client version: 1.13.4
k8s server version: 1.14.5
kubeclient: 4.4.0
My code:
require 'kubeclient'
require 'json'
require 'open3'
content = File.open("nginx.yaml").read
p suffix_name = File.extname("nginx.yaml")
case suffix_name
when '.yaml' then
hash = YAML.safe_load(content)
p "yaml content is:#{hash}"
when '.json' then
hash = JSON.parse(content)
p "json content is:#{hash}"
end
hash.each do |key, value|
puts "#{key} is: #{value}"
if key == "kind"
@kind = value.to_s.downcase
end
end
config = Kubeclient::Config.read("rancher_kube_config.kubeconfig")
kubeconfig_text = File.read("rancher_kube_config.kubeconfig")
config_context = Kubeclient::Config.new(YAML.safe_load(kubeconfig_text), nil)
@config = config_context.instance_eval { @kcfg }
config = @config
deployment = Kubeclient::Resource.new(hash)
cert_store = OpenSSL::X509::Store.new
ssl_options = {
cert_store: cert_store,
verify_ssl: OpenSSL::SSL::VERIFY_NONE
}
context = config_context.context
auth_options = {
bearer_token: context.auth_options[:bearer_token]
}
client = Kubeclient::Client.new('https://{my_host}/k8s/clusters/c-h4mdn/api', auth_options: auth_options, ssl_options: ssl_options)
begin
@create_entity = client.send("create_#{@kind}", deployment)
rescue Kubeclient::HttpError => e
p e
@error_code = e.error_code
@message = e.message
end
if @create_entity.nil?
p "false"
else
p "success"
end
The contents of config.kubeconfig
:
apiVersion: v1
kind: Config
clusters:
- name: "k8s"
cluster:
server: "https://MY_HOST/k8s/clusters/c-h4mdn"
certificate-authority-data: ...
- name: "k8s-k8smaster"
cluster:
server: "https://MY_HOST:6443"
certificate-authority-data: ...
users:
- name: "user-8lhbv"
user:
token: my_token
contexts:
- name: "k8s"
context:
user: "user-8lhbv"
cluster: "k8s"
- name: "k8s-k8smaster"
context:
user: "user-8lhbv"
cluster: "k8s-k8smaster"
current-context: "k8s"
The contents of nginx.yaml
:
apiVersion: apps/v1
kind: Pod
metadata:
name: nginx-test2
labels:
app: nginx
namespace: default
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
restartPolicy: Always
The error I'm getting is this, and I think it maybe the apiVersion
is wrong:
#<Kubeclient::HttpError: HTTP status code 400, Pod in version "v1" cannot be handled as a Pod: no kind "Pod" is registered for version "clusters/v1" in scheme "k8s.io/kubernetes/pkg/api/legacyscheme/scheme.go:29" for POST https://MY_HOST/k8s/clusters/c-h4mdn/api/v1/namespaces/default/pods>