Last active
August 12, 2019 18:21
-
-
Save clcollins/1130ae2431be6c60a2a3ee9acc5b0dac to your computer and use it in GitHub Desktop.
Trying to figure out how to use client-go for oc and kubectl...
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 ( | |
"flag" | |
"fmt" | |
"os" | |
"path/filepath" | |
// "time" | |
// "k8s.io/client-go/rest" | |
// "k8s.io/apimachinery/pkg/api/errors" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/tools/clientcmd" | |
) | |
func main() { | |
var kubeconfig *string | |
if home := os.Getenv("HOME"); home != "" { | |
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") | |
} else { | |
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") | |
} | |
flag.Parse() | |
// use the current context in kubeconfig | |
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig) | |
if err != nil { | |
panic(err.Error()) | |
} | |
clientset, err := kubernetes.NewForConfig(config) | |
if err != nil { | |
panic(err.Error()) | |
} | |
// pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{}) | |
// if err != nil { | |
// panic(err.Error()) | |
// } | |
// fmt.Printf("There are %d pods in the cluster\n", len(pods.Items)) | |
namespace := "memcached-operator-external" | |
deployments, err := clientset.AppsV1().Deployments(namespace).List(metav1.ListOptions{}) | |
if err != nil { | |
panic(err.Error()) | |
} | |
fmt.Printf("There are %d deployments in the namespace %s\n", len(deployments.Items), namespace) | |
// The _ is silently assigned to the index (range returns index,item) | |
for _, deployment := range deployments.Items { | |
// This is kinda cool... | |
// fmt.Printf("Deployment OwnerReference%v \n", deployment.GetOwnerReferences()) | |
// Print name and namespace | |
fmt.Printf("Deployment Namespace/Name: %v/%v \n", deployment.GetObjectMeta().GetNamespace(), deployment.GetObjectMeta().GetName()) | |
// Why does this return a giant number: 824637082989 | |
// Should be 1... | |
// EDIT FOR THOSE WHO COME AFTER: deployment.Spec.Replicas is a POINTER | |
// See the off-hand comment in the docs here: | |
// https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.11/#deploymentspec-v1-app | |
fmt.Printf("Deployment spec.replicas: %d \n", deployment.Spec.Replicas) | |
// So, this line should be: | |
// fmt.Printf("Deployment spec.replicas: %d \n", *(deployment.Spec.Replicas)) | |
// Why does this return 824633986528, should be 600... | |
// EDIT: Same as above - this is a pointer (not documented in the docs linked above...) | |
fmt.Printf("Deployment spec.progressDeadlineSeconds: %d \n", deployment.Spec.ProgressDeadlineSeconds) | |
// Should be ... *(deployment.Spec.ProgressDeadlineSeconds)) | |
// These two return correct values... | |
fmt.Printf("Deployment Status.Replicas: %d \n", deployment.Status.Replicas) | |
fmt.Printf("Deployment Status.AvailableReplicas: %d \n", deployment.Status.AvailableReplicas) | |
// fmt.Printf("Deployment spec: %v \n", deployment.Spec) | |
} | |
// DeploymentLists are made up of deployemnt objects | |
// Deployment objects are made up of, among other things, deploymentSpec objects | |
// DeploymentSpec objects have actual values, maybe. I get weird values from them sometimes. | |
// Sheesh | |
} |
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
# Output of the command: $ oc get deployment memcached-cluster-1 -o yaml | |
# | |
# | |
apiVersion: extensions/v1beta1 | |
kind: Deployment | |
metadata: | |
annotations: | |
deployment.kubernetes.io/revision: "1" | |
creationTimestamp: "2019-07-30T14:58:13Z" | |
generation: 1 | |
name: memcached-cluster-1 | |
namespace: memcached-operator-external | |
ownerReferences: | |
- apiVersion: cache.example.com/v1alpha1 | |
blockOwnerDeletion: true | |
controller: true | |
kind: Memcached | |
name: memcached-cluster-1 | |
uid: 7428005f-b2da-11e9-8deb-5254008c19b7 | |
resourceVersion: "432448" | |
selfLink: /apis/extensions/v1beta1/namespaces/memcached-operator-external/deployments/memcached-cluster-1 | |
uid: 7429b444-b2da-11e9-8deb-5254008c19b7 | |
spec: | |
progressDeadlineSeconds: 600 | |
replicas: 3 | |
revisionHistoryLimit: 10 | |
selector: | |
matchLabels: | |
app: memcached | |
memcached_cr: memcached-cluster-1 | |
strategy: | |
rollingUpdate: | |
maxSurge: 25% | |
maxUnavailable: 25% | |
type: RollingUpdate | |
template: | |
metadata: | |
creationTimestamp: null | |
labels: | |
app: memcached | |
memcached_cr: memcached-cluster-1 | |
spec: | |
containers: | |
- command: | |
- memcached | |
- -m=64 | |
- -o | |
- modern | |
- -v | |
image: memcached:1.4.36-alpine | |
imagePullPolicy: IfNotPresent | |
name: memcached | |
ports: | |
- containerPort: 11211 | |
name: memcached | |
protocol: TCP | |
resources: {} | |
terminationMessagePath: /dev/termination-log | |
terminationMessagePolicy: File | |
dnsPolicy: ClusterFirst | |
restartPolicy: Always | |
schedulerName: default-scheduler | |
securityContext: {} | |
terminationGracePeriodSeconds: 30 | |
status: | |
availableReplicas: 3 | |
conditions: | |
- lastTransitionTime: "2019-07-30T14:58:13Z" | |
lastUpdateTime: "2019-07-30T14:58:18Z" | |
message: ReplicaSet "memcached-cluster-1-68444bb64f" has successfully progressed. | |
reason: NewReplicaSetAvailable | |
status: "True" | |
type: Progressing | |
- lastTransitionTime: "2019-08-12T17:45:15Z" | |
lastUpdateTime: "2019-08-12T17:45:15Z" | |
message: Deployment has minimum availability. | |
reason: MinimumReplicasAvailable | |
status: "True" | |
type: Available | |
observedGeneration: 1 | |
readyReplicas: 3 | |
replicas: 3 | |
updatedReplicas: 3 |
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
$ go run oc-dummy-go.go | |
There are 1 deployments in the namespace memcached-operator-external | |
Deployment Namespace/Name: memcached-operator-external/memcached-cluster-1 | |
Deployment spec.replicas: 824633989260 | |
Deployment spec.progressDeadlineSeconds: 824633989600 | |
Deployment Status.Replicas: 3 | |
Deployment Status.AvailableReplicas: 3 | |
# Why are the values from "deployment.Spec.ProgressDeadlineSeconds" and "deployment.Spec.Replicas", | |
# line 67 and 73 of oc-dummy-go.go, returning huge numbers? Results *should* be 600, and 3, respectively. | |
# See lines 24 and 25 of oc_cli_output.txt... | |
# EDIT FOR THOSE WHO COME AFTER: the items on 67 and 73 are pointers, and I didn't realize it. They need to be referred to as such, with the `*(thing)` notation... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment