hashicorp / terraform-provider-kubernetes

Terraform Kubernetes provider
https://www.terraform.io/docs/providers/kubernetes/
Mozilla Public License 2.0
1.59k stars 972 forks source link

AWS EKS CronJob cannot be found #2115

Closed generalpiston closed 1 year ago

generalpiston commented 1 year ago

Terraform Version, Provider Version and Kubernetes Version

Terraform v1.4.5
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v4.66.1
+ provider registry.terraform.io/hashicorp/kubernetes v2.20.0

Affected Resource(s)

Terraform Configuration Files

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "4.66.1"
    }
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = ">= 2.20.0"
    }
  }
}

provider "aws" {
  region              = var.region
  allowed_account_ids = [var.allowed_account_id]
}

provider "kubernetes" {
  host                   = var.cluster_endpoint
  cluster_ca_certificate = base64decode(var.cluster_ca_certificate_base64)
  exec {
    api_version = "client.authentication.k8s.io/v1beta1"
    command     = "aws"
    args = [
      "eks",
      "get-token",
      "--cluster-name",
      var.cluster_name
    ]
  }
}

resource "kubernetes_cron_job" "scheduler" {
  metadata {
    name = "cronjob-${var.service_name}"
  }
  spec {
    concurrency_policy            = "Forbid"
    failed_jobs_history_limit     = 3
    schedule                      = var.service_schedule
    starting_deadline_seconds     = 10
    successful_jobs_history_limit = 3
    suspend                       = false

    job_template {
      metadata {}
      spec {
        backoff_limit              = 1
        ttl_seconds_after_finished = 60
        template {
          metadata {}
          spec {
            node_selector = {
              group = "machine-learning-scheduler"
            }
            container {
              name    = var.service_name
              image   = var.mlops_docker_image
              command = ["bash", "-c", local.script]
            }
          }
        }
      }
    }
  }
}

Debug Output

https://gist.github.com/generalpiston/fc56c03d1529fca363cd83c8de2713a2

Steps to Reproduce

  1. Create a kubernetes cronjob in Terraform using the provider.
  2. terraform apply.

Expected Behavior

It should create a cronjob in kubernetes.

Actual Behavior

A run time error with the message Error: the server could not find the requested resource is returned.

Important Factoids

Nothing in my account.

I did notice the api version in the request is old: batch/v1beta1. This component was marked as stable as of 1.21. It's possible this component needs to be upgraded.

Community Note

generalpiston commented 1 year ago

it looks like I needed to use kubernetes_cron_job_v1