"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"
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
deploymentName := flag.String("deployment", "", "deployment name")
imageName := flag.String("image", "", "new image name")
appName := flag.String("app", "app", "application name")
if *deploymentName == "" {
fmt.Println("You must specify the deployment name.")
fmt.Println("You must specify the new image name.")
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
clientset, err := kubernetes.NewForConfig(config)
deployment, err := clientset.AppsV1beta1().Deployments("default").Get(*deploymentName, metav1.GetOptions{})
if errors.IsNotFound(err) {
fmt.Printf("Deployment not found\n")
} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
fmt.Printf("Error getting deployment%v\n", statusError.ErrStatus.Message)
fmt.Printf("Found deployment\n")
name := deployment.GetName()
fmt.Println("name ->", name)
containers := &deployment.Spec.Template.Spec.Containers
for i := range *containers {
if c[i].Name == *appName {
fmt.Println("Old image ->", c[i].Image)
fmt.Println("New image ->", *imageName)
fmt.Println("The application container not exist in the deployment pods.")
_, err := clientset.AppsV1beta1().Deployments("default").Update(deployment)
if h := os.Getenv("HOME"); h != "" {
return os.Getenv("USERPROFILE") // windows