github.com/oam-dev/cluster-gateway@v1.9.0/examples/single-cluster-informer/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	multicluster "github.com/oam-dev/cluster-gateway/pkg/apis/cluster/transport"
     9  	"github.com/spf13/cobra"
    10  	corev1 "k8s.io/api/core/v1"
    11  	"k8s.io/apimachinery/pkg/runtime"
    12  	"k8s.io/client-go/informers"
    13  	"k8s.io/client-go/kubernetes"
    14  	"k8s.io/client-go/kubernetes/scheme"
    15  	"k8s.io/client-go/tools/cache"
    16  	"k8s.io/client-go/tools/clientcmd"
    17  	controllers "sigs.k8s.io/controller-runtime"
    18  	"sigs.k8s.io/controller-runtime/pkg/manager"
    19  )
    20  
    21  var kubeconfig string
    22  var clusterName string
    23  
    24  func main() {
    25  
    26  	cmd := cobra.Command{
    27  		RunE: func(cmd *cobra.Command, args []string) error {
    28  			cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
    29  			if err != nil {
    30  				return err
    31  			}
    32  			cfg.Wrap(multicluster.NewProxyPathPrependingClusterGatewayRoundTripper(clusterName).NewRoundTripper)
    33  
    34  			// Native kubernetes client informer
    35  			nativeClient := kubernetes.NewForConfigOrDie(cfg)
    36  
    37  			sharedInformer := informers.NewSharedInformerFactory(nativeClient, 0)
    38  			podInformer := sharedInformer.Core().V1().Pods().Informer()
    39  
    40  			fmt.Printf("Native client cache pod info:\n")
    41  			podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{AddFunc: addFunc})
    42  
    43  			ctx, cancel := context.WithCancel(context.TODO())
    44  			go sharedInformer.Start(ctx.Done())
    45  			for !podInformer.HasSynced() {
    46  				time.Sleep(time.Millisecond * 100)
    47  			}
    48  			cancel()
    49  
    50  			// Controller-runtime client informer
    51  			s := runtime.NewScheme()
    52  			scheme.AddToScheme(s)
    53  
    54  			mgr, err := controllers.NewManager(cfg, manager.Options{Scheme: s})
    55  
    56  			runtimePodInformer, err := mgr.GetCache().GetInformer(context.TODO(), &corev1.Pod{})
    57  			if err != nil {
    58  				return err
    59  			}
    60  
    61  			fmt.Printf("Controller-runtime cache pod info:\n")
    62  			runtimePodInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{AddFunc: addFunc})
    63  
    64  			ctx, cancel = context.WithCancel(context.TODO())
    65  			go mgr.Start(ctx)
    66  			for !runtimePodInformer.HasSynced() {
    67  				time.Sleep(time.Millisecond * 100)
    68  			}
    69  			cancel()
    70  
    71  			return nil
    72  		},
    73  	}
    74  
    75  	cmd.Flags().StringVarP(&kubeconfig, "kubeconfig", "", "", "the client kubeconfig")
    76  	cmd.Flags().StringVarP(&clusterName, "cluster-name", "", "", "the target cluster name")
    77  
    78  	if err := cmd.Execute(); err != nil {
    79  		panic(err)
    80  	}
    81  }
    82  
    83  func addFunc(obj interface{}) {
    84  	pod, ok := obj.(*corev1.Pod)
    85  	if !ok {
    86  		return
    87  	}
    88  	if pod.Namespace == "kube-system" {
    89  		fmt.Printf("%s\t%s\t%s\t%s\n", pod.Namespace, pod.Name, pod.Status.PodIP, pod.Status.HostIP)
    90  	}
    91  }