Created
July 8, 2019 09:09
-
-
Save paulodiniz/68c978468de354cb9c79578f696c3efb to your computer and use it in GitHub Desktop.
Kubernetes go
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
package main | |
import( | |
"fmt" | |
"flag" | |
"os" | |
"log" | |
"path/filepath" | |
"k8s.io/client-go/tools/clientcmd" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/kubernetes/pkg/apis/core/v1" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
) | |
func main() { | |
var ns, label, field string | |
flag.StringVar(&ns, "namespace", "", "namespace") | |
flag.StringVar(&label, "l", "", "Label selector") | |
flag.StringVar(&field, "f", "", "Field selector") | |
kubeconfig := filepath.Join( | |
os.Getenv("HOME"), ".kube", "config", | |
) | |
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) | |
if err != nil { | |
log.Fatal(err) | |
} | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
log.Fatal(err) | |
} | |
api := clientset.CoreV1() | |
// setup list options | |
listOptions := metav1.ListOptions{ | |
LabelSelector: label, | |
FieldSelector: field, | |
} | |
pvcs, err := api.PersistentVolumeClaims(ns).List(listOptions) | |
if err != nil { | |
log.Fatal(err) | |
} | |
printPVCs(pvcs) | |
} | |
func printPVCs(pvcs *v1.PersistentVolumeClaimList) { | |
template := "%-32s%-8s%-8s\n" | |
fmt.Printf(template, "NAME", "STATUS", "CAPACITY") | |
for _, pvc := range pvcs.Items { | |
quant := pvc.Spec.Resources.Requests[v1.ResourceStorage] | |
fmt.Printf( | |
template, | |
pvc.Name, | |
string(pvc.Status.Phase), | |
quant.String()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment