Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
add plan description
Browse files Browse the repository at this point in the history
  • Loading branch information
imsky committed Mar 16, 2018
1 parent 3f0edd6 commit e334732
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
14 changes: 7 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ func main() {

flag.Parse()

args := flag.Args()
filenames := flag.Args()

if len(args) == 0 {
if len(filenames) == 0 {
flag.Usage()
return
}
Expand All @@ -157,11 +157,11 @@ func main() {
panic(err.Error())
}

plan := generatePlan(args, label, clientset)
plan := generatePlan(filenames, label, clientset)

fmt.Println(plan)

if *execute == true {
executePlan(plan, clientset)
if *execute != true {
fmt.Printf("This is a preview. Run kubechange with -e to make cluster updates.\n\n")
}

executePlan(plan, clientset, *execute)
}
44 changes: 42 additions & 2 deletions plan.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"time"

"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -71,7 +72,7 @@ func getObjectNamespaces(objects []runtime.Object) []string {
}

func waitForObjectDeletion(object runtime.Object, clientset *kubernetes.Clientset) error {
return wait.PollImmediate(time.Millisecond*100, time.Second*60, func() (bool, error) {
return wait.PollImmediate(time.Second, time.Second*60, func() (bool, error) {
var err error
switch t := object.(type) {
case *batchv1.Job:
Expand Down Expand Up @@ -155,20 +156,32 @@ func generatePlan(filenames []string, label *string, clientset *kubernetes.Clien
return plan
}

func executePlan(plan []Step, clientset *kubernetes.Clientset) {
func executePlan(plan []Step, clientset *kubernetes.Clientset, execute bool) {
for _, step := range plan {
if step.action == "create" {
src := *step.pair.src
srcMetadata, _ := getObjectMetadata(src)
switch srcType := src.(type) {
case *batchv2alpha1.CronJob:
fmt.Println(`Creating CronJob "` + srcMetadata.GetName() + `"`)

if !execute {
break
}

_, err := clientset.BatchV2alpha1().CronJobs(srcMetadata.GetNamespace()).Create(src.(*batchv2alpha1.CronJob))

if err != nil {
panic(err)
}

case *batchv1.Job:
fmt.Println(`Creating Job "` + srcMetadata.GetName() + `"`)

if !execute {
break
}

_, err := clientset.BatchV1().Jobs(srcMetadata.GetNamespace()).Create(src.(*batchv1.Job))

if err != nil {
Expand All @@ -188,6 +201,12 @@ func executePlan(plan []Step, clientset *kubernetes.Clientset) {
dstGVK := getObjectGroupVersionKind(dst)

if dstGVK.Kind == "Job" {
fmt.Println(`Replacing Job "` + dstMetadata.GetName() + `" with Job "` + srcMetadata.GetName() + `"`)

if !execute {
break
}

//todo: set propagation policy?
err := clientset.BatchV1().Jobs(dstMetadata.GetNamespace()).Delete(dstMetadata.GetName(), nil)

Expand All @@ -203,6 +222,11 @@ func executePlan(plan []Step, clientset *kubernetes.Clientset) {
panic(err)
}
} else if dstGVK.Kind == "CronJob" {
fmt.Println(`Replacing CronJob "` + dstMetadata.GetName() + `" with Job "` + srcMetadata.GetName() + `"`)

if !execute {
break
}
//todo: set propagation policy?
//todo: delete current CronJob child Jobs
err := clientset.BatchV2alpha1().CronJobs(dstMetadata.GetNamespace()).Delete(dstMetadata.GetName(), nil)
Expand All @@ -222,6 +246,11 @@ func executePlan(plan []Step, clientset *kubernetes.Clientset) {
dstGVK := getObjectGroupVersionKind(dst)

if dstGVK.Kind == "Job" {
fmt.Println(`Replacing Job "` + dstMetadata.GetName() + `" with CronJob "` + srcMetadata.GetName() + `"`)

if !execute {
break
}
//todo: set propagation policy?
err := clientset.BatchV1().Jobs(dstMetadata.GetNamespace()).Delete(dstMetadata.GetName(), nil)

Expand All @@ -237,6 +266,11 @@ func executePlan(plan []Step, clientset *kubernetes.Clientset) {
panic(err)
}
} else if dstGVK.Kind == "CronJob" {
fmt.Println(`Replacing CronJob "` + dstMetadata.GetName() + `" with CronJob "` + srcMetadata.GetName() + `"`)

if !execute {
break
}
_, err := clientset.BatchV2alpha1().CronJobs(srcMetadata.GetNamespace()).Update(src.(*batchv2alpha1.CronJob))

if err != nil {
Expand All @@ -248,4 +282,10 @@ func executePlan(plan []Step, clientset *kubernetes.Clientset) {
}
}
}

if len(plan) == 0 {
fmt.Println("Nothing to do")
} else {
fmt.Println("Finished")
}
}

0 comments on commit e334732

Please sign in to comment.