From c70ba666bfb39b125a0807ee8cfc26580a49d4e6 Mon Sep 17 00:00:00 2001 From: Max Williams <8859277+max-rocket-internet@users.noreply.github.com> Date: Wed, 23 Aug 2023 22:57:14 +0200 Subject: [PATCH] Don't load kubernetes cliet until cli app starts (#3) Co-authored-by: Max Williams --- .github/workflows/test.yaml | 4 ++-- README.md | 22 +++++++++++----------- pkg/doctor/checkup.go | 2 ++ pkg/kubernetes/kubernetes.go | 10 +++++++--- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4bf17d6..e187e59 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,10 +1,10 @@ -name: run-tests +name: test-and-build on: [push] jobs: build: - name: run-tests + name: test-and-build runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 diff --git a/README.md b/README.md index 013fa81..d470b67 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Is your Kubernetes cluster unhealthy? Do your workloads have symptoms? Then maybe it needs a checkup with `kube-doctor` 🏥 -```shell +```console $ kube-doctor --warning-symptoms --non-namespaced-resources == Checking DaemonSet resources 👀 DaemonSet kube-system/efs-csi-node: efs-plugin no resources specified @@ -93,31 +93,31 @@ This tool will check for the following symptoms: By default `kube-doctor` will check all namespaces but it can also target a specific namespace: -```shell +```console kube-doctor --namespace kube-system ``` Or label selector;: -```shell +```console kube-doctor --label-selector app.kubernetes.io/name=prometheus ``` Or a combination of both: -```shell +```console kube-doctor --label-selector app.kubernetes.io/name=prometheus --namespace monitoring ``` Non-namespaced resources like nodes can be checked with the `--non-namespaced-resources` flag: -```shell +```console kube-doctor --non-namespaced-resources ``` To see other options, including debug logging, consult the help: -```shell +```console kube-doctor --help ``` @@ -125,7 +125,7 @@ kube-doctor --help Check out code and build: -```shell +```console git clone git@github.com:max-rocket-internet/kube-doctor.git cd kube-doctor go build ./... && go install ./... @@ -133,13 +133,13 @@ go build ./... && go install ./... Run from `main` branch without `git`: -```shell +```console go install github.com/max-rocket-internet/kube-doctor@latest cd $GOPATH/pkg/mod/github.com/max-rocket-internet/kube-doctor*/ go run main.go ``` -To get a binary, check [the releases](https://github.com/max-rocket-internet/kube-doctor/releases). +To download a binary, check [the releases](https://github.com/max-rocket-internet/kube-doctor/releases). ## Contributing @@ -147,12 +147,12 @@ Pull requests welcome 💙 To run all tests: -```shell +```console go test ./... ``` Or just a single package: -```shell +```console go test ./.../checkup ``` diff --git a/pkg/doctor/checkup.go b/pkg/doctor/checkup.go index 39f365a..32065bf 100644 --- a/pkg/doctor/checkup.go +++ b/pkg/doctor/checkup.go @@ -11,6 +11,8 @@ import ( ) func DoCheckUp(cCtx *cli.Context) { + kubernetes.Init() + log.Setup(cCtx.Bool("debug"), cCtx.Bool("warning-symptoms")) log.Debug(fmt.Sprintf("Connected to cluster from context %s running version %s", kubernetes.ContextName, kubernetes.ServerVersion)) diff --git a/pkg/kubernetes/kubernetes.go b/pkg/kubernetes/kubernetes.go index a5922e4..61c3953 100644 --- a/pkg/kubernetes/kubernetes.go +++ b/pkg/kubernetes/kubernetes.go @@ -18,11 +18,15 @@ import ( ) var ( - client = createClient() - ContextName = "" - ServerVersion = "" + client *kubernetes.Clientset + ContextName string + ServerVersion string ) +func Init() { + client = createClient() +} + func createClient() *kubernetes.Clientset { loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() configOverrides := &clientcmd.ConfigOverrides{}