kubectl get pod <pod> -n <namespace> -o yaml in kubernetes client-go

huangapple go评论73阅读模式
英文:

kubectl get pod <pod> -n <namespace> -o yaml in kubernetes client-go

问题

现在我有了使用命令 pods, err := clientset.CoreV1().Pods("namespace_String").List(context.TODO(), metav1.ListOptions{}) 获得的 Kubernetes 结构体作为 Pods。现在我想将其作为单独的 YAML 文件获取,我应该使用哪个命令?

for i, pod := range pods.Items {
    if i == 0 {
        t := reflect.TypeOf(&pod)
        for j := 0; j < t.NumMethod(); j++ {
            m := t.Method(j)
            fmt.Println(m.Name)
        }
    }
}

这个函数将打印出 Pod 项中的函数列表,我应该使用哪个函数?

谢谢回答。

英文:

Now i have Pods as Kubernetes structs wiht the help of the command

pods , err := clientset.CoreV1().Pods(&quot;namespace_String&quot;).List(context.TODO(), metav1.ListOptions{})

now i do i get it as individual yaml files
which command should i use

for i , pod := range pods.Items{
    if i==0{
        t := reflect.TypeOF(&amp;pod)
        for j := 0; j&lt;t.NumMethod(); j++{
            m := t.Method(j)
            fmt.Println(m.Name)
}
}
}

this function will print the list of functions in the pod item which should i use

Thanks for the answer

答案1

得分: 1

yaml只是在etcd中以kubernetes内部存储的Pod对象的表示形式。通过你的client-go,你获得的是Pod实例,类型为v1.Pod。因此,你应该能够直接使用这个对象并获取你想要的任何内容,例如p.Labels()等。但是,如果出于某种原因,你坚持要获取一个yaml文件,你可以通过以下方式实现:

import (
  "sigs.k8s.io/yaml"
)

b, err := yaml.Marshal(pod)
if err != nil {
  // 处理错误
}
log.Printf("Pod的Yaml是:%q", string(b))

请注意,这里使用的yaml库不是来自client-go库。yaml库的文档可以在以下链接找到:https://pkg.go.dev/sigs.k8s.io/yaml#Marshal

如果你想使用json而不是yaml,你可以直接使用v1.Pod结构体本身提供的Marshal函数,就像使用其他Go对象一样。

英文:

The yaml is just a representation of the Pod object in the kubernetes internal storage in etcd. With your client-go what you have got is the Pod instance, of the type v1.Pod. So you should be able to work with this object itself and get whatever you want, for example p.Labels() etc. But if for some reason, you are insisting on getting a yaml, you can do that via:

import (
  &quot;sigs.k8s.io/yaml&quot;
)

b, err := yaml.Marshal(pod)
if err != nil {
  // handle err
}
log.Printf(&quot;Yaml of the pod is: %q&quot;, string(b))

Note that yaml library coming here is not coming from client-go library. The documentation for the yaml library can be found in: https://pkg.go.dev/sigs.k8s.io/yaml#Marshal

Instead of yaml if you want to use json, you can simply use the Marshal function https://pkg.go.dev/k8s.io/apiserver/pkg/apis/example/v1#Pod.Marshal provided by the v1.Pod struct itself, like any other Go object.

答案2

得分: 0

使用client-go获取单个Pod:

pod, err := clientset.CoreV1().Pods("pod_namespace").Get(context.TODO(),"pod_name", metav1.GetOptions{})
if err!=nil {
  log.Fatalln(err)
}
// 对pod进行操作
英文:

To get individual pod using client-go:

pod, err := clientset.CoreV1().Pods(&quot;pod_namespace&quot;).Get(context.TODO(),&quot;pod_name&quot;, metav1.GetOptions{})
if err!=nil {
  log.Fatalln(err)
}
// do something with pod

huangapple
  • 本文由 发表于 2022年7月4日 02:33:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/72848993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定