I want to extract only pods with the specified label using Label Selector in Client-go

How to do

--Define in metav1.ListOptions --There are many other than Label Selector

Corresponding part

    pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{
        LabelSelector: "app=vamdemic111aaa-app",
    })

The entire

package main

import (
    "context"
    "encoding/json"
    "fmt"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
    "log"
    "os"
    "path/filepath"
)

func main() {
    //Specify the file path of Kubeconfig
    kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
    config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
    if err != nil {
        log.Fatal(err)
    }

    //Load Kubeconfig
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        log.Fatal(err)
    }

    //Call the pod list
    namespace := "default"
    pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{
        LabelSelector: "app=vamdemic111aaa-app",
    })
    if err != nil {
        log.Fatalln("failed to get pods:", err)
    }

    m := map[string]string{}

    for _, pod := range pods.Items {
       m[pod.Name] = string(pod.Status.Phase)
    }

    data, _ := json.Marshal(m)
    fmt.Printf(string(data))
}

reference

https://blog.nnn.dev/entry/2019/12/21/144550

Recommended Posts

I want to extract only pods with the specified label using Label Selector in Client-go
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
(Matplotlib) I want to draw a graph with a size specified in pixels
[C language] I want to generate random numbers in the specified range
I want to display the progress in Python!
Use PIL in Python to extract only the data you want from Exif
I want to transition with a button in flask
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I want to write in Python! (3) Utilize the mock
I want to automate ssh using the expect command!
I want to use the R dataset in python
I tried to extract the text in the image file using Tesseract of the OCR engine
I want to automate ssh using the expect command! part2
I want to use only the normalization process of SudachiPy
I want to change the Japanese flag to the Palau flag with Numpy
[Python] I want to use the -h option with argparse
I want to make the Dictionary type in the List unique
I didn't want to write the AWS key in the program
I tried to describe the traffic in real time with WebSocket
I want to know the weather with LINE bot feat.Heroku + Python
[Linux] I want to know the date when the user logged in
I want to extract an arbitrary URL from the character string of the html source with python
I want to run Rails with rails s even in vagrant environment
I tried to process the image in "sketch style" with OpenCV
LINEbot development, I want to check the operation in the local environment
I want to create a graph with wavy lines omitted in the middle with matplotlib (I want to manipulate the impression)
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I tried to process the image in "pencil style" with OpenCV
I want to make the second line the column name in pandas
I want to pass the G test in one month Day 1
I want to do ○○ with Pandas
I want to check the position of my face with OpenCV!
I want to know the population of each country in the world.
I want to debug with Python
Let's use the NAOqi OS VM. I want to resolve dependencies with pip even in Pepper development
I want to use only the SMTP MAIL FROM command and RCPT TO command without sending mail with Python's smtplib
I want to change the color by clicking the scatter point in matplotlib
Memorandum (Add name only to people with the same surname in the list)
I want to send a signal only from the sub thread to the main thread
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to sort a list in the order of other lists
I want to use the Django Debug Toolbar in my Ajax application
I tried to automatically extract the movements of PES players with software
Environment maintenance made with Docker (I want to post-process GrADS in Python
I tried using "Streamlit" which can do the Web only with Python
I tried to extract and illustrate the stage of the story using COTOHA
I want to stop the automatic deletion of the tmp area with RHEL7
I want to create an Ubuntu chrome User Profile with Colab only
I want to do a monkey patch only partially safely in Python
I want to pin Spyder to the taskbar
I want to detect objects with OpenCV
I want to output to the console coolly
I want to print in a comprehension
I want to handle the rhyme part1
I want to blog with Jupyter Notebook
I want to handle the rhyme part3
I want to pip install with PythonAnywhere
I want to analyze logs with Python
I want to play with aws with python
I want to display the progress bar