Skip to content

Commit

Permalink
Merge pull request #5 from shiv3/add-incluster-config
Browse files Browse the repository at this point in the history
Add InCluster Config
  • Loading branch information
shiv3 committed Jul 14, 2021
2 parents af82f1a + 00aa4bb commit 0a5a63a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-build-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ jobs:
uses: docker/build-push-action@v2
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
push: true # ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
33 changes: 20 additions & 13 deletions app/adapter/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"flag"
"path/filepath"

"k8s.io/client-go/rest"

"github.com/shiv3/slackube/app/adapter/k8s/img"

"github.com/shiv3/slackube/app/adapter/k8s/get"
Expand All @@ -22,22 +24,27 @@ type K8SAdapter struct {
}

func NewK8SClientClient() (K8SAdapter, error) {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()

// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
// first use InClusterConfig
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
// if couldn't get InClusterConfig, try to use local kubeconfig file
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in kubeconfig
config, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
return K8SAdapter{}, err
}
}

ks, err := kubernetes.NewForConfig(config)

if err != nil {
return K8SAdapter{}, err
}
// create the clientset
return K8SAdapter{
List: list.ListAdapter{ClientSet: ks},
Expand Down
2 changes: 1 addition & 1 deletion manifests/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ spec:
labels:
app: slackube
spec:
serviceAccountName: slakube-sa
# serviceAccountName: slakube-sa
containers:
- name: slackube
image: ghcr.io/shiv3/slackube:latest
Expand Down

0 comments on commit 0a5a63a

Please sign in to comment.