Skip to content

Instantly share code, notes, and snippets.

@chuckha
Created October 25, 2019 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuckha/032086240945cfd3281b6137d7b4d1da to your computer and use it in GitHub Desktop.
Save chuckha/032086240945cfd3281b6137d7b4d1da to your computer and use it in GitHub Desktop.
client test
package main
import (
"context"
"fmt"
"os"
"time"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func main() {
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG"))
if err != nil {
panic(err.Error())
}
c, err := client.New(config, client.Options{})
if err != nil {
panic(err)
}
ctx := context.Background()
pod := v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "my-pod",
Namespace: "default",
},
}
t := time.Now()
fmt.Println("Deleting...", t.String())
if err := c.Delete(ctx, &pod, client.PropagationPolicy(metav1.DeletePropagationForeground)); err != nil {
panic(err)
}
fmt.Println("Done deleting", time.Now().Sub(t))
key := client.ObjectKey{
Name: "my-pod",
Namespace: "default",
}
p := &v1.Pod{}
if err := c.Get(ctx, key, p); err != nil {
panic(err)
}
fmt.Println("did not wait!")
fmt.Println(p)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment