Last active
June 10, 2020 03:02
-
-
Save flyer103/eb7bbe8c2f7f653140a10d012e30a0ff to your computer and use it in GitHub Desktop.
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" | |
"log" | |
policy "k8s.io/api/policy/v1beta1" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/rest" | |
"k8s.io/client-go/tools/clientcmd" | |
) | |
var kubeConfigPath = flag.String("kubeconfig", "", "path to kube config") | |
var ns = flag.String("ns", "default", "namespace") | |
var podName = flag.String("pod-name", "", "pod name") | |
func init() { | |
flag.Parse() | |
} | |
func main() { | |
var kubeconfig *rest.Config | |
var err error | |
if *kubeConfigPath != "" { | |
kubeconfig, err = clientcmd.BuildConfigFromFlags("", *kubeConfigPath) | |
} else { | |
kubeconfig, err = rest.InClusterConfig() | |
} | |
if err != nil { | |
log.Fatalf("Failed to construct kubeconfig: %s\n", err) | |
} | |
kclient, err := kubernetes.NewForConfig(kubeconfig) | |
if err != nil { | |
log.Fatalf("Failed to new kclient: %s\n", err) | |
} | |
// Evict | |
log.Printf("Begin to evict pod %s in ns %s\n", *podName, *ns) | |
err = kclient.CoreV1().Pods(*ns).Evict(&policy.Eviction{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Name: *podName, | |
Namespace: *ns, | |
}, | |
DeleteOptions: &metav1.DeleteOptions{}, | |
}) | |
if err != nil { | |
log.Fatalf("Failed to evict pod %s in ns %s: %s", *podName, *ns, err) | |
} | |
log.Printf("Successfully evict pod %s in %s.\n", *podName, *ns) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment