Last active
June 19, 2019 14:36
-
-
Save clcollins/eafc21d546152542e1ec5dfa78159f9b to your computer and use it in GitHub Desktop.
Help a Go Newbie
This file contains hidden or 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" | |
"os" | |
"path/filepath" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/rest" | |
"k8s.io/client-go/tools/clientcmd" | |
) | |
func loadConfig() (*rest.Config, error) { | |
host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT") | |
if len(host) == 0 || len(port) == 0 { | |
// creates the out-of-cluster config | |
fmt.Println("Using out-of-cluster config") | |
configFile := filepath.Join( | |
os.Getenv("HOME"), ".kube", "config", | |
) | |
config, err := clientcmd.BuildConfigFromFlags("", configFile) | |
if err != nil { | |
panic(err.Error()) | |
} | |
return config, nil | |
} | |
// creates the in-cluster config | |
fmt.Println("Using in-cluster config") | |
config, err := rest.InClusterConfig() | |
if err != nil { | |
panic(err.Error()) | |
} | |
return config, nil | |
return nil, fmt.Errorf("Could not locate client configuration") | |
} | |
func main() { | |
config, err := loadConfig() | |
// creates the clientset | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
panic(err.Error()) | |
} | |
api := clientset.CoreV1() | |
getOptions := metav1.GetOptions{} | |
pvc, err := api.PersistentVolumeClaims("webregistry").Get("static-files", getOptions) | |
if err != nil { | |
panic(err.Error()) | |
} | |
fmt.Print(pvc) | |
} |
This file contains hidden or 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
# The following is the output of the main.go code. | |
# | |
# Using the Kubernetes client-go package, the code retrieves a persistent volume claim from the cluster. | |
# | |
# I am not sure what data type this is. It's it a struct? The "Type" is, unhelpfully "PersistentVolumeClaim", | |
# which is why I suspect the struct, I think. | |
# | |
# I have found this: https://github.com/kubernetes/client-go/blob/master/kubernetes/typed/core/v1/persistentvolumeclaim.go | |
# And this: https://godoc.org/k8s.io/client-go/kubernetes/typed/core/v1#CoreV1Client.PersistentVolumeClaims | |
# And this: https://godoc.org/k8s.io/api/core/v1#PersistentVolumeClaim | |
# | |
# The last link also makes me think what I have is a struct, but it also contains other structs, I think. | |
# | |
# I can get at all the values for "item.Name", "item.Status", etc., but have these questions: | |
# | |
# 1. What IS this data type - is it in fact a struct? | |
# 2. How could I iterate or otherwise get the keys themselves, to see what is included in this thing - the "reflect" package? | |
# 3. What is "ObjectMeta"? I cannot seem to get or reference it in my code. | |
# 4. Are any of my assumptions correct? Am I barking up the wrong tree? | |
&PersistentVolumeClaim{ObjectMeta:k8s_io_apimachinery_pkg_apis_meta_v1.ObjectMeta{Name:static-files,GenerateName:,Namespace:webregistry,SelfLink:/api/v1/nam | |
espaces/webregistry/persistentvolumeclaims/static-files,UID:8bb48838-8eb0-11e9-832f-005056a1102f,ResourceVersion:92297070,Generation:0,CreationTimestamp:201 | |
9-06-14 10:27:32 -0400 EDT,DeletionTimestamp:<nil>,DeletionGracePeriodSeconds:nil,Labels:map[string]string{},Annotations:map[string]string{pv.kubernetes.io/ | |
bind-completed: yes,pv.kubernetes.io/bound-by-controller: yes,volume.beta.kubernetes.io/storage-provisioner: default,},OwnerReferences:[],Finalizers:[kubern | |
etes.io/pvc-protection],ClusterName:,Initializers:nil,ManagedFields:[],},Spec:PersistentVolumeClaimSpec{AccessModes:[ReadWriteOnce],Resources:ResourceRequir | |
ements{Limits:ResourceList{},Requests:ResourceList{storage: {{1073741824 0} {<nil>} 1Gi BinarySI},},},VolumeName:pvc-8bb48838-8eb0-11e9-832f-005056a1102f,Se | |
lector:nil,StorageClassName:*standard,VolumeMode:nil,DataSource:nil,},Status:PersistentVolumeClaimStatus{Phase:Bound,AccessModes:[ReadWriteOnce],Capacity:Re | |
sourceList{storage: {{1073741824 0} {<nil>} 1Gi BinarySI},},Conditions:[],},} | |
Jmainguy
commented
Jun 19, 2019
- It is a struct, *"k8s.io/api/core/v1".PersistentVolumeClaim specifically
- I just cheated and googled it. https://godoc.org/k8s.io/api/core/v1#PersistentVolumeClaim explains the keys in it.
- See above
- Your assumptions were correct.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment