@@ -6,10 +6,15 @@ package k8s
66import (
77 "context"
88 "fmt"
9+ "os"
10+ "path/filepath"
911
12+ authv1 "k8s.io/api/authentication/v1"
1013 corev1 "k8s.io/api/core/v1"
1114 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1215 "k8s.io/client-go/kubernetes"
16+ "k8s.io/client-go/rest"
17+ "k8s.io/client-go/tools/clientcmd"
1318)
1419
1520// Client wraps the Kubernetes clientset and provides helper methods
@@ -24,6 +29,52 @@ func NewClientWithInterface(clientset kubernetes.Interface) *Client {
2429 return & Client {clientset : clientset }
2530}
2631
32+ // NewClient creates a new Kubernetes client using the standard kubeconfig location
33+ // or in-cluster config if running inside a Kubernetes cluster.
34+ func NewClient () (* Client , error ) {
35+ config , err := getKubeConfig ()
36+ if err != nil {
37+ return nil , fmt .Errorf ("failed to get kubeconfig: %w" , err )
38+ }
39+
40+ clientset , err := kubernetes .NewForConfig (config )
41+ if err != nil {
42+ return nil , fmt .Errorf ("failed to create Kubernetes client: %w" , err )
43+ }
44+
45+ return & Client {clientset : clientset }, nil
46+ }
47+
48+ // getKubeConfig attempts to load Kubernetes configuration from the following sources in order:
49+ // 1. In-cluster config (if running inside a pod)
50+ // 2. KUBECONFIG environment variable
51+ // 3. ~/.kube/config (default kubeconfig location)
52+ func getKubeConfig () (* rest.Config , error ) {
53+ // Try in-cluster config first
54+ config , err := rest .InClusterConfig ()
55+ if err == nil {
56+ return config , nil
57+ }
58+
59+ // Fall back to kubeconfig file
60+ kubeconfig := os .Getenv ("KUBECONFIG" )
61+ if kubeconfig == "" {
62+ // Use default kubeconfig location
63+ home , err := os .UserHomeDir ()
64+ if err != nil {
65+ return nil , fmt .Errorf ("failed to get home directory: %w" , err )
66+ }
67+ kubeconfig = filepath .Join (home , ".kube" , "config" )
68+ }
69+
70+ config , err = clientcmd .BuildConfigFromFlags ("" , kubeconfig )
71+ if err != nil {
72+ return nil , fmt .Errorf ("failed to build config from kubeconfig: %w" , err )
73+ }
74+
75+ return config , nil
76+ }
77+
2778// ListPods lists all pods in the specified namespace.
2879// If namespace is empty, it lists pods across all namespaces.
2980func (c * Client ) ListPods (ctx context.Context , namespace string ) (* corev1.PodList , error ) {
@@ -59,6 +110,24 @@ func (c *Client) ListAllPods(ctx context.Context) (*corev1.PodList, error) {
59110 return pods , nil
60111}
61112
113+ // DeletePod deletes a pod by namespace and name
114+ func (c * Client ) DeletePod (ctx context.Context , namespace , name string ) error {
115+ err := c .clientset .CoreV1 ().Pods (namespace ).Delete (ctx , name , metav1.DeleteOptions {})
116+ if err != nil {
117+ return fmt .Errorf ("failed to delete pod %s/%s: %w" , namespace , name , err )
118+ }
119+ return nil
120+ }
121+
122+ // GetPodByName retrieves a specific pod by namespace and name
123+ func (c * Client ) GetPodByName (ctx context.Context , namespace , name string ) (* corev1.Pod , error ) {
124+ pod , err := c .clientset .CoreV1 ().Pods (namespace ).Get (ctx , name , metav1.GetOptions {})
125+ if err != nil {
126+ return nil , fmt .Errorf ("failed to get pod %s/%s: %w" , namespace , name , err )
127+ }
128+ return pod , nil
129+ }
130+
62131// GetPodStatus returns a simplified status string for a pod
63132func GetPodStatus (pod * corev1.Pod ) string {
64133 // Check if pod is being deleted
@@ -74,3 +143,12 @@ func GetPodStatus(pod *corev1.Pod) string {
74143func IsPodRunning (pod * corev1.Pod ) bool {
75144 return pod .Status .Phase == corev1 .PodRunning
76145}
146+
147+ // ValidateToken validates a service account token using the TokenReview API
148+ func (c * Client ) ValidateToken (ctx context.Context , tokenReview * authv1.TokenReview ) (* authv1.TokenReview , error ) {
149+ result , err := c .clientset .AuthenticationV1 ().TokenReviews ().Create (ctx , tokenReview , metav1.CreateOptions {})
150+ if err != nil {
151+ return nil , fmt .Errorf ("failed to create TokenReview: %w" , err )
152+ }
153+ return result , nil
154+ }
0 commit comments