-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get pods from context default config
- Loading branch information
Showing
3 changed files
with
249 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,59 @@ | ||
module github.com/ldemailly/go-scratch | ||
|
||
go 1.20 | ||
go 1.22.0 | ||
|
||
toolchain go1.22.4 | ||
|
||
require ( | ||
fortio.org/fortio v1.60.3 | ||
fortio.org/log v1.11.0 | ||
fortio.org/scli v1.12.0 | ||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d | ||
k8s.io/apimachinery v0.30.1 | ||
k8s.io/client-go v0.30.1 | ||
) | ||
|
||
require ( | ||
fortio.org/cli v1.4.2 // indirect | ||
fortio.org/dflag v1.6.0 // indirect | ||
fortio.org/sets v1.0.3 // indirect | ||
fortio.org/version v1.0.3 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/emicklei/go-restful/v3 v3.11.0 // indirect | ||
github.com/fsnotify/fsnotify v1.6.0 // indirect | ||
github.com/go-logr/logr v1.4.1 // indirect | ||
github.com/go-openapi/jsonpointer v0.19.6 // indirect | ||
github.com/go-openapi/jsonreference v0.20.2 // indirect | ||
github.com/go-openapi/swag v0.22.3 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/protobuf v1.5.4 // indirect | ||
github.com/google/gnostic-models v0.6.8 // indirect | ||
github.com/google/gofuzz v1.2.0 // indirect | ||
github.com/google/uuid v1.3.1 // indirect | ||
golang.org/x/net v0.17.0 // indirect | ||
golang.org/x/sys v0.13.0 // indirect | ||
golang.org/x/text v0.13.0 // indirect | ||
github.com/imdario/mergo v0.3.6 // indirect | ||
github.com/josharian/intern v1.0.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/mailru/easyjson v0.7.7 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
golang.org/x/net v0.23.0 // indirect | ||
golang.org/x/oauth2 v0.10.0 // indirect | ||
golang.org/x/sys v0.18.0 // indirect | ||
golang.org/x/term v0.18.0 // indirect | ||
golang.org/x/text v0.14.0 // indirect | ||
golang.org/x/time v0.3.0 // indirect | ||
google.golang.org/appengine v1.6.7 // indirect | ||
google.golang.org/protobuf v1.33.0 // indirect | ||
gopkg.in/inf.v0 v0.9.1 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
k8s.io/api v0.30.1 // indirect | ||
k8s.io/klog/v2 v2.120.1 // indirect | ||
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect | ||
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect | ||
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect | ||
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect | ||
sigs.k8s.io/yaml v1.3.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Modified from https://trstringer.com/connect-to-kubernetes-from-go/ | ||
// to get the users's default namespace from their context | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
func main() { | ||
fmt.Println("Get Kubernetes pods") | ||
|
||
userHomeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
fmt.Printf("error getting user home dir: %v\n", err) | ||
os.Exit(1) | ||
} | ||
kubeConfigPath := filepath.Join(userHomeDir, ".kube", "config") | ||
fmt.Printf("Using kubeconfig: %s\n", kubeConfigPath) | ||
|
||
cfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( | ||
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfigPath}, nil) | ||
|
||
rCfg, err := cfg.RawConfig() | ||
if err != nil { | ||
fmt.Printf("error getting raw config: %v\n", err) | ||
os.Exit(1) | ||
} | ||
namespace := rCfg.Contexts[rCfg.CurrentContext].Namespace | ||
fmt.Printf("Using (default) namespace: %s\n", namespace) | ||
kubeConfig, err := cfg.ClientConfig() | ||
if err != nil { | ||
fmt.Printf("error getting Kubernetes config: %v\n", err) | ||
os.Exit(1) | ||
} | ||
clientset, err := kubernetes.NewForConfig(kubeConfig) | ||
if err != nil { | ||
fmt.Printf("error getting Kubernetes clientset: %v\n", err) | ||
os.Exit(1) | ||
} | ||
|
||
pods, err := clientset.CoreV1().Pods(namespace).List(context.Background(), v1.ListOptions{}) | ||
if err != nil { | ||
fmt.Printf("error getting pods: %v\n", err) | ||
os.Exit(1) | ||
} | ||
for _, pod := range pods.Items { | ||
fmt.Printf("Pod name: %s %s\n", pod.Namespace, pod.Name) | ||
} | ||
} |