Skip to content

Commit

Permalink
Merge pull request #34 from sl1pm4t/cronjob-updates
Browse files Browse the repository at this point in the history
Implement Update for `cron_job` resources.
  • Loading branch information
sl1pm4t authored Jul 18, 2018
2 parents 77f8c0d + 23e0010 commit fa9c74a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
32 changes: 15 additions & 17 deletions kubernetes/resource_kubernetes_cron_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"k8s.io/api/batch/v2alpha1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
pkgApi "k8s.io/apimachinery/pkg/types"
)

const cronJobResourceGroupName = "cronjobs"
Expand Down Expand Up @@ -100,27 +99,24 @@ func resourceKubernetesCronJobUpdate(d *schema.ResourceData, meta interface{}) e
kp := meta.(*kubernetesProvider)
conn := kp.conn

namespace, name, err := idParts(d.Id())
namespace, _, err := idParts(d.Id())
if err != nil {
return err
}

ops := patchMetadata("metadata.0.", "/metadata/", d)

if d.HasChange("spec") {
specOps, err := patchCronJobSpec("/spec", "spec.0.", d)
if err != nil {
return err
}
ops = append(ops, specOps...)
metadata := expandMetadata(d.Get("metadata").([]interface{}))
spec, err := expandCronJobSpec(d.Get("spec").([]interface{}))
if err != nil {
return err
}
spec.JobTemplate.ObjectMeta.Annotations = metadata.Annotations

data, err := ops.MarshalJSON()
if err != nil {
return fmt.Errorf("Failed to marshal update operations: %s", err)
cronjob := &v1beta1.CronJob{
ObjectMeta: metadata,
Spec: spec,
}

log.Printf("[INFO] Updating cron job %s: %s", d.Id(), ops)
log.Printf("[INFO] Updating cron job %s: %s", d.Id(), cronjob)

out := &v1beta1.CronJob{}
apiGroup, err := kp.highestSupportedAPIGroup(cronJobResourceGroupName, cronJobAPIGroups...)
Expand All @@ -129,15 +125,17 @@ func resourceKubernetesCronJobUpdate(d *schema.ResourceData, meta interface{}) e
}
switch apiGroup {
case batchV1beta1:
out, err = conn.BatchV1beta1().CronJobs(namespace).Patch(name, pkgApi.JSONPatchType, data)
out, err = conn.BatchV1beta1().CronJobs(namespace).Update(cronjob)

case batchV2alpha1:
beta, err2 := conn.BatchV2alpha1().CronJobs(namespace).Patch(name, pkgApi.JSONPatchType, data)
alpha := &v2alpha1.CronJob{}
Convert(cronjob, alpha)
alphaOut, err2 := conn.BatchV2alpha1().CronJobs(namespace).Update(alpha)
if err2 != nil {
err = err2
break
}
Convert(beta, out)
Convert(alphaOut, out)

default:
err = cronJobNotSupportedError
Expand Down
43 changes: 43 additions & 0 deletions kubernetes/resource_kubernetes_cron_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ func TestAccKubernetesCronJob_extra(t *testing.T) {
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "spec.0.job_template.0.spec.0.backoff_limit", "2"),
),
},
{
Config: testAccKubernetesCronJobConfig_extraModified(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesCronJobExists("kubernetes_cron_job.test", &conf),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "metadata.0.name", name),
resource.TestCheckResourceAttrSet("kubernetes_cron_job.test", "spec.0.schedule"),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "spec.0.concurrency_policy", "Forbid"),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "spec.0.successful_jobs_history_limit", "2"),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "spec.0.failed_jobs_history_limit", "2"),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "spec.0.starting_deadline_seconds", "120"),
resource.TestCheckResourceAttr("kubernetes_cron_job.test", "spec.0.job_template.0.spec.0.backoff_limit", "3"),
),
},
},
})
}
Expand Down Expand Up @@ -219,3 +232,33 @@ resource "kubernetes_cron_job" "test" {
}
}`, name)
}

func testAccKubernetesCronJobConfig_extraModified(name string) string {
return fmt.Sprintf(`
resource "kubernetes_cron_job" "test" {
metadata {
name = "%s"
}
spec {
schedule = "1 0 * * *"
concurrency_policy = "Forbid"
successful_jobs_history_limit = 2
failed_jobs_history_limit = 2
starting_deadline_seconds = 120
job_template {
spec {
backoff_limit = 3
template {
spec {
container {
name = "hello"
image = "alpine"
command = ["echo", "'hello'"]
}
}
}
}
}
}
}`, name)
}
6 changes: 0 additions & 6 deletions kubernetes/structure_cron_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,3 @@ func expandJobTemplate(in []interface{}) (batchv1beta1.JobTemplateSpec, error) {

return obj, nil
}

func patchCronJobSpec(pathPrefix, prefix string, d *schema.ResourceData) (PatchOperations, error) {
ops := make([]PatchOperation, 0)

return ops, nil
}

0 comments on commit fa9c74a

Please sign in to comment.