Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose more info: current context, is in kluster, is dev env #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The main aspect of this is around saving and cleaning the code for in-cluster an
Run `go get` to get the *k8s-discovery* module as follows.

```
go get github.com/gkarthiks/k8s-discovery
go get github.com/sysbind/k8s-discovery
```

Declare a variable as `var k8s *discovery.K8s` and initialize it as `k8s, _ = discovery.NewK8s()`. Now the **k8s** will hold the interface that will provide the clientset for Kubernetes communication that is pulled either via `in-cluster` or via `kubeconfig` file.
Expand Down
74 changes: 74 additions & 0 deletions discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
log "github.com/sirupsen/logrus"
coreClient "k8s.io/client-go/kubernetes"
restClient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
cmdClient "k8s.io/client-go/tools/clientcmd"
metricsClient "k8s.io/metrics/pkg/client/clientset/versioned"
)
Expand All @@ -18,8 +19,31 @@ type K8s struct {
Clientset coreClient.Interface
MetricsClientSet *metricsClient.Clientset
RestConfig *restClient.Config
inKluster bool
isDevEnv bool
currentContext string // only for out-of-kluster config
}

// dev/production env detection, copied from Tilt
type Env string

const (
EnvUnknown Env = "unknown"
EnvGKE Env = "gke"
EnvMinikube Env = "minikube"
EnvDockerDesktop Env = "docker-for-desktop"
EnvMicroK8s Env = "microk8s"
EnvCRC Env = "crc"
EnvKrucible Env = "krucible"
// Kind v0.6 substantially changed the protocol for detecting and pulling,
// so we represent them as two separate envs.
EnvKIND5 Env = "kind-0.5-"
EnvKIND6 Env = "kind-0.6+"
EnvK3D Env = "k3d"
EnvRancherDesktop Env = "rancher-desktop"
EnvNone Env = "none" // k8s not running (not neces. a problem, e.g. if using Tilt x Docker Compose)
)

var logEnabled bool

// NewK8s will provide a new k8s client interface
Expand All @@ -28,16 +52,26 @@ var logEnabled bool
// While running inside the cluster resolved via pod environment uses the in-cluster config
func NewK8s() (*K8s, error) {
client := K8s{}

_, logEnabled = os.LookupEnv("CLIENTSET_LOG")

client.inKluster = true
client.isDevEnv = false
config, err := restClient.InClusterConfig()
if err != nil {
client.inKluster = false
kubeConfig :=
cmdClient.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
config, err = cmdClient.BuildConfigFromFlags("", kubeConfig)
if err != nil {
return nil, err
}

err = client.addContextInfo(kubeConfig)
if err != nil {
return nil, err
}

if logEnabled {
log.Info("Program running from outside of the cluster")
}
Expand Down Expand Up @@ -80,3 +114,43 @@ func (o *K8s) GetNamespace() (string, error) {
}
return "", nil
}

func (o *K8s) IsInKluster() bool {
return o.inKluster
}

func (o *K8s) IsDevEnv() bool {
return o.isDevEnv
}

func (o *K8s) CurrentContext() string {
return o.currentContext
}

func (e Env) isDevCluster() bool {
return e == EnvMinikube ||
e == EnvDockerDesktop ||
e == EnvMicroK8s ||
e == EnvCRC ||
e == EnvKIND5 ||
e == EnvKIND6 ||
e == EnvK3D ||
e == EnvKrucible ||
e == EnvRancherDesktop
}

func (o *K8s) addContextInfo(kubeConfig string) (err error) {
// write down current context
rawconfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfig},
&clientcmd.ConfigOverrides{
CurrentContext: "",
}).RawConfig()
if err != nil {
return
}
o.currentContext = rawconfig.CurrentContext
env := Env(o.currentContext)
o.isDevEnv = env.isDevCluster()
return
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/gkarthiks/k8s-discovery
module github.com/sysbind/k8s-discovery

go 1.19

Expand Down