Skip to content

Commit 3fabe2e

Browse files
committed
remove usage of genericclioptions; add namespace flag in flagpole
1 parent 3007974 commit 3fabe2e

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

pkg/df-pv/root.go

+39-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"os"
9+
"path"
910

1011
"github.com/pkg/errors"
1112
log "github.com/sirupsen/logrus"
@@ -14,20 +15,20 @@ import (
1415
corev1 "k8s.io/api/core/v1"
1516
"k8s.io/apimachinery/pkg/api/resource"
1617
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17-
"k8s.io/cli-runtime/pkg/genericclioptions"
1818
"k8s.io/cli-runtime/pkg/printers"
1919
"k8s.io/client-go/kubernetes"
20-
"k8s.io/client-go/rest"
20+
"k8s.io/client-go/tools/clientcmd"
2121
)
2222

2323
type flagpole struct {
24-
genericCliConfigFlags *genericclioptions.ConfigFlags
25-
outputFormat string
26-
logLevel string
24+
outputFormat string
25+
logLevel string
26+
namespace string
2727
}
2828

2929
func setupRootCommand() *cobra.Command {
30-
flags := &flagpole{genericCliConfigFlags: genericclioptions.NewConfigFlags(false)}
30+
// flags := &flagpole{genericCliConfigFlags: genericclioptions.NewConfigFlags(false)}
31+
flags := &flagpole{}
3132
var rootCmd = &cobra.Command{
3233
Use: "df-pv",
3334
Short: "df-pv",
@@ -100,10 +101,12 @@ func setupRootCommand() *cobra.Command {
100101
return nil
101102
},
102103
}
104+
105+
rootCmd.Flags().StringVarP(&flags.namespace, "namespace", "n", "", "if present, the namespace scope for this CLI request (default is all namespaces)")
103106
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")
104107
rootCmd.PersistentFlags().StringVarP(&flags.logLevel, "verbosity", "v", "info", "log level; one of [info, debug, trace, warn, error, fatal, panic]")
105108

106-
flags.genericCliConfigFlags.AddFlags(rootCmd.Flags())
109+
// flags.genericCliConfigFlags.AddFlags(rootCmd.Flags())
107110
return rootCmd
108111
}
109112

@@ -243,12 +246,24 @@ type Volume struct {
243246
}
244247

245248
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()
247255
if err != nil {
248256
return nil, err
249257
}
250258

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)
252267
if err != nil {
253268
return nil, errors.Wrapf(err, "failed to create clientset")
254269
}
@@ -258,7 +273,9 @@ func ListPVCs(flags *flagpole) ([]*OutputRow, error) {
258273
return nil, errors.Wrapf(err, "failed to list nodes")
259274
}
260275

261-
desiredNamespace := *flags.genericCliConfigFlags.Namespace
276+
// desiredNamespace := *flags.genericCliConfigFlags.Namespace
277+
desiredNamespace := flags.namespace
278+
262279
ctx := context.TODO()
263280
return GetSliceOfOutputRow(ctx, clientset, nodes, desiredNamespace)
264281
}
@@ -365,11 +382,19 @@ func GetOutputRowFromVolume(pod *Pod, vol *Volume, desiredNamespace string) *Out
365382
return outputRow
366383
}
367384

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+
// }
372389

373390
func ListNodes(ctx context.Context, clientset *kubernetes.Clientset) (*corev1.NodeList, error) {
374391
return clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
375392
}
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

Comments
 (0)