Last active
February 4, 2020 11:22
-
-
Save bells17/48fe549d52160e43a5db6ab363a27b55 to your computer and use it in GitHub Desktop.
fetch pod & node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"flag" | |
"fmt" | |
"html/template" | |
"os" | |
"path/filepath" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/rest" | |
"k8s.io/client-go/tools/clientcmd" | |
) | |
func main() { | |
// creates the in-cluster config | |
config, err := rest.InClusterConfig() | |
if err != nil { | |
// creates the out-of-cluster config | |
var kubeconfig *string | |
if home := os.Getenv("HOME"); home != "" { | |
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") | |
} else { | |
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") | |
} | |
flag.Parse() | |
// use the current context in kubeconfig | |
config, err = clientcmd.BuildConfigFromFlags("", *kubeconfig) | |
if err != nil { | |
panic(err.Error()) | |
} | |
} | |
// creates the clientset | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
panic(err.Error()) | |
} | |
output, err := outputPods(clientset, "docker-desktop", Tmpl) | |
if err != nil { | |
panic(err.Error()) | |
} | |
fmt.Printf(output) | |
} | |
var Tmpl = ` | |
{{range .Items}}Node Name: {{.Spec.NodeName}} | |
Pod Name: {{.ObjectMeta.Name}} | |
Pod Namespace: {{.ObjectMeta.Namespace}} | |
{{end}} | |
` | |
func outputPods(clientset *kubernetes.Clientset, nodeName string, tmpl string) (string, error) { | |
pods, err := clientset.CoreV1().Pods("default").List(metav1.ListOptions{ | |
FieldSelector: "spec.nodeName=" + nodeName, | |
}) | |
if err != nil { | |
return "", err | |
} | |
t, err := template.New("output").Parse(tmpl) | |
if err != nil { | |
return "", err | |
} | |
b := new(bytes.Buffer) | |
err = t.Execute(b, pods) | |
return b.String(), err | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go run main.go | |
Node Name: docker-desktop | |
Pod Name: nginx-pod | |
Pod Namespace: default | |
Node Name: docker-desktop | |
Pod Name: nginx-pod2 | |
Pod Namespace: default | |
Recently our team had changed the cluster configuration as insecure
Sorry, I have never tried this setting.
Could you tell me about the details of this setting?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bells17, I'm sorry to trouble you again.
Recently our team had changed the cluster configuration as insecure and while i'm doing InClusterConfig(), it would be make secure communication. So, i am not able retrieve pods.
However, i did try to make the TLSConfig as
insecure: true
.I get error as
specifying a root certificates file with the insecure flag is not allowed
.I even tried to make the null configuration of certificates in TLSConfig as below.
Can you please help me out in this step?