Created
October 25, 2019 14:43
-
-
Save chuckha/032086240945cfd3281b6137d7b4d1da to your computer and use it in GitHub Desktop.
client test
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 ( | |
"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