6
6
"encoding/json"
7
7
"fmt"
8
8
"os"
9
+ "path"
9
10
10
11
"github.com/pkg/errors"
11
12
log "github.com/sirupsen/logrus"
@@ -14,20 +15,20 @@ import (
14
15
corev1 "k8s.io/api/core/v1"
15
16
"k8s.io/apimachinery/pkg/api/resource"
16
17
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17
- "k8s.io/cli-runtime/pkg/genericclioptions"
18
18
"k8s.io/cli-runtime/pkg/printers"
19
19
"k8s.io/client-go/kubernetes"
20
- "k8s.io/client-go/rest "
20
+ "k8s.io/client-go/tools/clientcmd "
21
21
)
22
22
23
23
type flagpole struct {
24
- genericCliConfigFlags * genericclioptions. ConfigFlags
25
- outputFormat string
26
- logLevel string
24
+ outputFormat string
25
+ logLevel string
26
+ namespace string
27
27
}
28
28
29
29
func setupRootCommand () * cobra.Command {
30
- flags := & flagpole {genericCliConfigFlags : genericclioptions .NewConfigFlags (false )}
30
+ // flags := &flagpole{genericCliConfigFlags: genericclioptions.NewConfigFlags(false)}
31
+ flags := & flagpole {}
31
32
var rootCmd = & cobra.Command {
32
33
Use : "df-pv" ,
33
34
Short : "df-pv" ,
@@ -100,10 +101,12 @@ func setupRootCommand() *cobra.Command {
100
101
return nil
101
102
},
102
103
}
104
+
105
+ rootCmd .Flags ().StringVarP (& flags .namespace , "namespace" , "n" , "" , "if present, the namespace scope for this CLI request (default is all namespaces)" )
103
106
rootCmd .Flags ().StringVarP (& flags .outputFormat , "output" , "o" , "Gi" , "output format for bytes; one of [Ki, Mi, Gi], see: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#meaning-of-memory" )
104
107
rootCmd .PersistentFlags ().StringVarP (& flags .logLevel , "verbosity" , "v" , "info" , "log level; one of [info, debug, trace, warn, error, fatal, panic]" )
105
108
106
- flags .genericCliConfigFlags .AddFlags (rootCmd .Flags ())
109
+ // flags.genericCliConfigFlags.AddFlags(rootCmd.Flags())
107
110
return rootCmd
108
111
}
109
112
@@ -243,12 +246,24 @@ type Volume struct {
243
246
}
244
247
245
248
func ListPVCs (flags * flagpole ) ([]* OutputRow , error ) {
246
- config , err := GetKubeConfigFromGenericCliConfigFlags (flags .genericCliConfigFlags )
249
+ // kubeConfig, err := GetKubeConfigFromGenericCliConfigFlags(flags.genericCliConfigFlags)
250
+ // if err != nil {
251
+ // return nil, err
252
+ // }
253
+
254
+ kubeConfigPath , err := KubeConfigPath ()
247
255
if err != nil {
248
256
return nil , err
249
257
}
250
258
251
- clientset , err := kubernetes .NewForConfig (config )
259
+ log .Debugf ("instantiating k8s client from config path: '%s'" , kubeConfigPath )
260
+ kubeConfig , err := clientcmd .BuildConfigFromFlags ("" , kubeConfigPath )
261
+ // kubeConfig, err := rest.InClusterConfig()
262
+ if err != nil {
263
+ return nil , errors .Wrapf (err , "unable to build config from flags" )
264
+ }
265
+
266
+ clientset , err := kubernetes .NewForConfig (kubeConfig )
252
267
if err != nil {
253
268
return nil , errors .Wrapf (err , "failed to create clientset" )
254
269
}
@@ -258,7 +273,9 @@ func ListPVCs(flags *flagpole) ([]*OutputRow, error) {
258
273
return nil , errors .Wrapf (err , "failed to list nodes" )
259
274
}
260
275
261
- desiredNamespace := * flags .genericCliConfigFlags .Namespace
276
+ // desiredNamespace := *flags.genericCliConfigFlags.Namespace
277
+ desiredNamespace := flags .namespace
278
+
262
279
ctx := context .TODO ()
263
280
return GetSliceOfOutputRow (ctx , clientset , nodes , desiredNamespace )
264
281
}
@@ -365,11 +382,19 @@ func GetOutputRowFromVolume(pod *Pod, vol *Volume, desiredNamespace string) *Out
365
382
return outputRow
366
383
}
367
384
368
- func GetKubeConfigFromGenericCliConfigFlags (genericCliConfigFlags * genericclioptions.ConfigFlags ) (* rest.Config , error ) {
369
- config , err := genericCliConfigFlags .ToRESTConfig ()
370
- return config , errors .Wrap (err , "failed to read kubeconfig" )
371
- }
385
+ // func GetKubeConfigFromGenericCliConfigFlags(genericCliConfigFlags *genericclioptions.ConfigFlags) (*rest.Config, error) {
386
+ // config, err := genericCliConfigFlags.ToRESTConfig()
387
+ // return config, errors.Wrap(err, "failed to read kubeconfig")
388
+ // }
372
389
373
390
func ListNodes (ctx context.Context , clientset * kubernetes.Clientset ) (* corev1.NodeList , error ) {
374
391
return clientset .CoreV1 ().Nodes ().List (ctx , metav1.ListOptions {})
375
392
}
393
+
394
+ func KubeConfigPath () (string , error ) {
395
+ home , err := os .UserHomeDir ()
396
+ if err != nil {
397
+ return "" , errors .Wrapf (err , "unable to get home dir" )
398
+ }
399
+ return path .Join (home , ".kube" , "config" ), nil
400
+ }
0 commit comments