Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Simple command to list CRDs via client-go
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmsft committed Feb 1, 2020
1 parent 0d07df5 commit ee325ac
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ REPO_PATH="$(ORG_PATH)/$(PROJECT_NAME)"
NMI_BINARY_NAME := nmi
MIC_BINARY_NAME := mic
DEMO_BINARY_NAME := demo
SIMPLE_CMD_BINARY_NAME := simple
GOOS ?= linux
TEST_GOOS ?= darwin
IDENTITY_VALIDATOR_BINARY_NAME := identityvalidator

DEFAULT_VERSION := 0.0.0-dev
Expand Down Expand Up @@ -62,6 +65,10 @@ clean-demo:
clean-identity-validator:
rm -rf bin/$(PROJECT_NAME)/$(IDENTITY_VALIDATOR_BINARY_NAME)

.PHONY: clean-simple
clean-simple:
rm -rf bin/$(PROJECT_NAME)/$(SIMPLE_CMD_BINARY_NAME)

.PHONY: clean
clean:
rm -rf bin/$(PROJECT_NAME)
Expand All @@ -74,13 +81,17 @@ build-nmi: clean-nmi
build-mic: clean-mic
CGO_ENABLED=0 PKG_NAME=github.com/Azure/$(PROJECT_NAME)/cmd/$(MIC_BINARY_NAME) $(MAKE) bin/$(PROJECT_NAME)/$(MIC_BINARY_NAME)

.PHONY: build-simple
build-simple:
CGO_ENABLED=0 PKG_NAME=github.com/Azure/$(PROJECT_NAME)/cmd/$(SIMPLE_CMD_BINARY_NAME) $(MAKE) bin/$(PROJECT_NAME)/$(SIMPLE_CMD_BINARY_NAME)

.PHONY: build-demo
build-demo: build_tags := netgo osusergo
build-demo: clean-demo
PKG_NAME=github.com/Azure/$(PROJECT_NAME)/cmd/$(DEMO_BINARY_NAME) ${MAKE} bin/$(PROJECT_NAME)/$(DEMO_BINARY_NAME)

bin/%:
GOOS=linux GOARCH=amd64 go build $(GO_BUILD_OPTIONS) -o "$(@)" "$(PKG_NAME)"
GOOS=$(GOOS) GOARCH=amd64 go build $(GO_BUILD_OPTIONS) -o "$(@)" "$(PKG_NAME)"

.PHONY: build-identity-validator
build-identity-validator: clean-identity-validator
Expand Down Expand Up @@ -137,11 +148,11 @@ push: push-nmi push-mic push-demo push-identity-validator

.PHONY: e2e
e2e:
go test github.com/Azure/$(PROJECT_NAME)/test/e2e $(E2E_TEST_OPTIONS)
GOOS=$(TEST_GOOS) go test github.com/Azure/$(PROJECT_NAME)/test/e2e $(E2E_TEST_OPTIONS)

.PHONY: unit-test
unit-test:
go test -race -count=1 $(shell go list ./... | grep -v /test/e2e) -v
GOOS=$(TEST_GOOS) go test -race -count=1 $(shell go list ./... | grep -v /test/e2e) -v

.PHONY: validate-version
validate-version: validate-version-NMI validate-version-MIC validate-version-IDENTITY_VALIDATOR validate-version-DEMO
Expand Down
93 changes: 93 additions & 0 deletions cmd/simple/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"flag"
"time"

"github.com/Azure/aad-pod-identity/pkg/mic"
"github.com/Azure/aad-pod-identity/version"

aadpodid "github.com/Azure/aad-pod-identity/pkg/apis/aadpodidentity"
"github.com/Azure/aad-pod-identity/pkg/crd"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
)

var (
kubeconfig string
cloudconfig string
forceNamespaced bool
versionInfo bool
syncRetryDuration time.Duration
leaderElectionCfg mic.LeaderElectionConfig
httpProbePort string
)

func main() {
defer klog.Flush()
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to the kube config")

flag.Set("logtostderr", "true")
flag.Set("v", "10")

flag.Parse()

if versionInfo {
version.PrintVersionAndExit()
}
klog.Infof("Starting mic process. Version: %v. Build date: %v", version.MICVersion, version.BuildDate)
if cloudconfig == "" {
klog.Warningf("--cloudconfig not passed will use aadpodidentity-admin-secret")
}
if kubeconfig == "" {
klog.Warningf("--kubeconfig not passed will use InClusterConfig")
}

klog.Infof("kubeconfig (%s) cloudconfig (%s)", kubeconfig, cloudconfig)
config, err := buildConfig(kubeconfig)
if err != nil {
klog.Fatalf("Could not read config properly. Check the k8s config file, %+v", err)
}

eventCh := make(chan aadpodid.EventType, 100)
crdClient, err := crd.NewCRDClient(config, eventCh)
if err != nil {
klog.Fatalf("%+v", err)
}

// Starts the leader election loop
var exit <-chan struct{}
crdClient.Start(exit)
crdClient.SyncCache(exit)

bindings, err := crdClient.ListBindings()
if err != nil {
klog.Fatalf("Could not get the bindings: %+v", err)
}

for _, v := range *bindings {
//log.Infof("\n=========>")
klog.Infof("\n%s", v.Spec.Selector)
//log.Infof("\n<=========>")
}

assignedIDs, err := crdClient.ListAssignedIDs()
if err != nil {
klog.Fatalf("Could not get assigned ID")
}

for _, a := range *assignedIDs {
klog.Infof("\n%s\n", a.Status.Status)
}

klog.Info("Done !")
}

// Create the client config. Use kubeconfig if given, otherwise assume in-cluster.
func buildConfig(kubeconfigPath string) (*rest.Config, error) {
if kubeconfigPath != "" {
return clientcmd.BuildConfigFromFlags("", kubeconfigPath)
}
return rest.InClusterConfig()
}

0 comments on commit ee325ac

Please sign in to comment.