除了认证外,还判断了操作系统。
且根据不同的系统,生成不同的文件。
在集群外认证时,使用的是k8s官方的方式,
而不是第三方库。
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
//"time"
"strings"
//"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/api/core/v1"
//"k8s.io/apimachinery/pkg/labels"
//"k8s.io/apimachinery/pkg/watch"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
//"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
//
// Uncomment to load all auth plugins
// \_ "k8s.io/client-go/plugin/pkg/client/auth"
//
// Or uncomment to load specific auth plugins
// \_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
// \_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
// \_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
// \_ "k8s.io/client-go/plugin/pkg/client/auth/openstack"
)
func main() {
// creates the in-cluster config
//config, err := rest.InClusterConfig()
//if err != nil {
// panic(err.Error())
//}
// creates the clientset
//clientset, err := kubernetes.NewForConfig(config)
//if err != nil {
// panic(err.Error())
//}
// creates the out-cluster config
var kubeconfig \*string
if home := 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())
}
// get pods in all the namespaces by omitting namespace
// Or specify namespace to get pods in particular namespace
services, err := clientset.CoreV1().Services("").List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
for i := 0; i < len(services.Items); i++ {
item := services.Items\[i\].Annotations
for k, v := range item {
if strings.Contains(k, "getambassador") && strings.Contains(v, "ambassador") && strings.Contains(v, "Mapping") {
fmt.Println(v)
fmt.Println("@@@@@@@@@@@@@@@@@@@@@@@")
}
}
}
//time.Sleep(10 \* time.Second)
handleNewServices(clientset)
}
func handleNewServices(clientset *kubernetes.Clientset) {
for {
serviceStreamWatcher, err := clientset.CoreV1().Services("").Watch(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
fmt.Printf("%T\n", serviceStreamWatcher)
for {
select {
case event := <-serviceStreamWatcher.ResultChan():
service := event.Object.(\*v1.Service)
for key, value := range service.Annotations {
if strings.Contains(key, "getambassador") && strings.Contains(value, "ambassador") && strings.Contains(value, "Mapping") {
toFileStr := fmt.Sprintf("%s\\n%s\\n=============\\n", event.Type, value)
//fmt.Println(toFileStr)
sysType := runtime.GOOS
absFilePath := ""
if sysType == "linux" {
absFilePath = "/app/k8s-ambassador"
} else {
absFilePath = "k8s-ambassador"
}
\_appendToFile(absFilePath, toFileStr)
}
}
}
}
}
}
func _appendToFile(file, str string) {
f, err := os.OpenFile(file, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0664)
if err != nil {
fmt.Printf("Cannot open file %s!\n", file)
return
}
defer f.Close()
f.WriteString(str)
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章