关于kubernetes:循环获取kubernetes-pod日志流

42次阅读

共计 1054 个字符,预计需要花费 3 分钟才能阅读完成。

package main

import (
    "bufio"
    "flag"
    "io"
    "path/filepath"
    "context"
    "fmt"

    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/clientcmd"
    "k8s.io/client-go/util/homedir"
    "k8s.io/api/core/v1"
)

func log() error {
    var kubeconfig *string
    if home := homedir.HomeDir(); 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())
    }

    // create the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {panic(err.Error())
    }
    opts := &v1.PodLogOptions{Follow:                       true, // 对应 kubectl logs - f 参数}
    request := clientset.CoreV1().Pods("default").GetLogs("your pod name", opts)
    readCloser, err := request.Stream(context.TODO())
    if err != nil {return err}
    defer readCloser.Close()

    r := bufio.NewReader(readCloser)
    for {bytes, err := r.ReadBytes('\n')
        fmt.Println(string(bytes))
        if err != nil {
            if err != io.EOF {return err}
            return nil
        }
    }
    return nil
}

func main(){log()
}

正文完
 0