github.com/Azure/aad-pod-identity@v1.8.17/cmd/simple/main.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"flag"
     6  	"os"
     7  
     8  	aadpodid "github.com/Azure/aad-pod-identity/pkg/apis/aadpodidentity"
     9  	"github.com/Azure/aad-pod-identity/pkg/crd"
    10  	"github.com/Azure/aad-pod-identity/version"
    11  
    12  	"k8s.io/client-go/rest"
    13  	"k8s.io/client-go/tools/clientcmd"
    14  	"k8s.io/klog/v2"
    15  )
    16  
    17  var (
    18  	kubeconfig string
    19  )
    20  
    21  func main() {
    22  	defer klog.Flush()
    23  	flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to the kube config")
    24  
    25  	_ = flag.Set("logtostderr", "true")
    26  	_ = flag.Set("v", "10")
    27  
    28  	flag.Parse()
    29  
    30  	klog.V(2).Infof("starting simple process. Version: %v. Build date: %v", version.MICVersion, version.BuildDate)
    31  	if kubeconfig == "" {
    32  		klog.Warningf("--kubeconfig not passed will use InClusterConfig")
    33  	}
    34  
    35  	klog.V(2).Infof("kubeconfig (%s)", kubeconfig)
    36  	config, err := buildConfig(kubeconfig)
    37  	if err != nil {
    38  		klog.Fatalf("failed to build config from %s, error: %+v", kubeconfig, err)
    39  	}
    40  
    41  	eventCh := make(chan aadpodid.EventType, 100)
    42  	crdClient, err := crd.NewCRDClient(config, eventCh)
    43  	if err != nil {
    44  		klog.Fatalf("%+v", err)
    45  	}
    46  
    47  	// Starts the leader election loop
    48  	var exit <-chan struct{}
    49  	crdClient.Start(exit)
    50  	crdClient.SyncCacheAll(exit, true)
    51  
    52  	ids, err := crdClient.ListIds()
    53  	if err != nil {
    54  		klog.Fatalf("could not get the identities: %+v", err)
    55  	}
    56  	klog.Infof("Identities len: %d", len(*ids))
    57  	for _, v := range *ids {
    58  		buf, err := json.MarshalIndent(v, "", "    ")
    59  		if err != nil {
    60  			klog.Errorf("failed to marshal JSON, error: %+v", err)
    61  			os.Exit(1)
    62  		}
    63  		klog.Infof("\n%s", string(buf))
    64  	}
    65  
    66  	bindings, err := crdClient.ListBindings()
    67  	if err != nil {
    68  		klog.Fatalf("could not get the bindings: %+v", err)
    69  	}
    70  	klog.Infof("bindings len: %d", len(*bindings))
    71  	for _, v := range *bindings {
    72  		buf, err := json.MarshalIndent(v, "", "    ")
    73  		if err != nil {
    74  			klog.Errorf("failed to marshal JSON, error: %+v", err)
    75  			os.Exit(1)
    76  		}
    77  		klog.Infof("\n%s", string(buf))
    78  	}
    79  
    80  	assignedIDs, err := crdClient.ListAssignedIDs()
    81  	if err != nil {
    82  		klog.Fatalf("could not get assigned ID")
    83  	}
    84  
    85  	for _, a := range *assignedIDs {
    86  		buf, err := json.MarshalIndent(a, "", "    ")
    87  		if err != nil {
    88  			klog.Errorf("failed to marshal JSON, error: %+v", err)
    89  			os.Exit(1)
    90  		}
    91  		klog.Infof("\n%s", string(buf))
    92  	}
    93  	klog.Info("\ndone !")
    94  }
    95  
    96  // Create the client config. Use kubeconfig if given, otherwise assume in-cluster.
    97  func buildConfig(kubeconfigPath string) (*rest.Config, error) {
    98  	if kubeconfigPath != "" {
    99  		return clientcmd.BuildConfigFromFlags("", kubeconfigPath)
   100  	}
   101  	return rest.InClusterConfig()
   102  }