Skip to content

Commit

Permalink
Allow context override
Browse files Browse the repository at this point in the history
Signed-off-by: Aidan Jensen <aidan@artificial.com>
  • Loading branch information
artificial-aidan committed Aug 22, 2024
1 parent 4a2afe9 commit a5fa793
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/fixer"
"program": "${workspaceFolder}/cmd/fixer",
"args": [
"-k8s-context",
"aidan-sandbox-aks-artificial-cluster",
]
}
]
}
5 changes: 5 additions & 0 deletions cmd/fixer/fixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main

import (
"context"
"flag"
"os"

"github.com/artificialinc/cm-429-fixer/pkg/cm"
Expand All @@ -24,6 +25,9 @@ func init() {
}

func main() {
k8sContext := flag.String("k8s-context", "", "Kubernetes context to use")
flag.Parse()

zapCfg := zap.NewProductionConfig()
level, err := zapcore.ParseLevel(logLevel)
if err != nil {
Expand All @@ -37,6 +41,7 @@ func main() {

watcher := cm.NewWatcher(
cm.WithLogger(zapr.NewLogger(logger).WithName("watcher")),
cm.WithClient(cm.GetLocalClient(&cm.ClientOpts{Context: *k8sContext})),
)

ctx := context.Background()
Expand Down
14 changes: 12 additions & 2 deletions pkg/cm/cm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

func getLocalClient() versioned.Interface {
// ClientOpts is a set of options for the client
type ClientOpts struct {
Context string
}

// GetLocalClient returns a client for the local cluster
func GetLocalClient(opts *ClientOpts) versioned.Interface {
// Get kubeconfig
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
// if you want to change the loading rules (which files in which order), you can do so here

configOverrides := &clientcmd.ConfigOverrides{}

if opts != nil && opts.Context != "" {
configOverrides.CurrentContext = opts.Context
}

config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)

clientConfig, err := config.ClientConfig()
Expand Down Expand Up @@ -103,7 +113,7 @@ func NewWatcher(opts ...Option) *Watcher {
}

if w.c == nil {
c := getLocalClient()
c := GetLocalClient(nil)
w.c = c
}

Expand Down

0 comments on commit a5fa793

Please sign in to comment.