Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batch/jobdef: Add image_pull_secret #38517

Merged
merged 11 commits into from
Jul 24, 2024
3 changes: 3 additions & 0 deletions .changelog/38517.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_batch_job_definition: Add `eks_properties.*.pod_properties.*.image_pull_secret` argument
```
37 changes: 37 additions & 0 deletions internal/service/batch/eks_properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,22 @@ func expandEKSPodProperties(podPropsMap map[string]interface{}) *batch.EksPodPro
if v, ok := podPropsMap["host_network"]; ok {
podProps.HostNetwork = aws.Bool(v.(bool))
}

if v, ok := podPropsMap["image_pull_secret"]; ok {
podProps.ImagePullSecrets = expandImagePullSecrets(v.([]interface{}))
}

if m, ok := podPropsMap["metadata"].([]interface{}); ok && len(m) > 0 {
if v, ok := m[0].(map[string]interface{})["labels"]; ok {
podProps.Metadata = &batch.EksMetadata{}
podProps.Metadata.Labels = flex.ExpandStringMap(v.(map[string]interface{}))
}
}

if v, ok := podPropsMap["service_account_name"].(string); ok && v != "" {
podProps.ServiceAccountName = aws.String(v)
}

if v, ok := podPropsMap["volumes"]; ok {
podProps.Volumes = expandVolumes(v.([]interface{}))
}
Expand Down Expand Up @@ -153,6 +160,19 @@ func expandContainers(containers []interface{}) []*batch.EksContainer {
return result
}

func expandImagePullSecrets(ipss []interface{}) (result []*batch.ImagePullSecret) {
for _, v := range ipss {
ips := &batch.ImagePullSecret{}
m := v.(map[string]interface{})
if v, ok := m[names.AttrName].(string); ok {
ips.Name = aws.String(v)
result = append(result, ips) // move out of "if" when more fields are added
}
}

return result
}

func expandVolumes(volumes []interface{}) []*batch.EksVolume {
var result []*batch.EksVolume
for _, v := range volumes {
Expand Down Expand Up @@ -242,6 +262,10 @@ func flattenEKSPodProperties(podProperties *batch.EksPodProperties) (tfList []in
tfMap["host_network"] = aws.BoolValue(v)
}

if v := podProperties.ImagePullSecrets; v != nil {
tfMap["image_pull_secret"] = flattenImagePullSecrets(v)
}

if v := podProperties.Metadata; v != nil {
metaData := make([]map[string]interface{}, 0)
if v := v.Labels; v != nil {
Expand All @@ -262,6 +286,19 @@ func flattenEKSPodProperties(podProperties *batch.EksPodProperties) (tfList []in
return tfList
}

func flattenImagePullSecrets(ipss []*batch.ImagePullSecret) (tfList []interface{}) {
for _, ips := range ipss {
tfMap := map[string]interface{}{}

if v := ips.Name; v != nil {
tfMap[names.AttrName] = aws.StringValue(v)
tfList = append(tfList, tfMap) // move out of "if" when more fields are added
}
}

return tfList
}

func flattenEKSContainers(containers []*batch.EksContainer) (tfList []interface{}) {
for _, container := range containers {
tfMap := map[string]interface{}{}
Expand Down
12 changes: 12 additions & 0 deletions internal/service/batch/job_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ func ResourceJobDefinition() *schema.Resource {
Optional: true,
Default: true,
},
"image_pull_secret": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
names.AttrName: {
Type: schema.TypeString,
Required: true,
},
},
},
},
"metadata": {
Type: schema.TypeList,
Optional: true,
Expand Down
81 changes: 81 additions & 0 deletions internal/service/batch/job_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ func TestAccBatchJobDefinition_EKSProperties_basic(t *testing.T) {
},
})
}

func TestAccBatchJobDefinition_EKSProperties_update(t *testing.T) {
ctx := acctest.Context(t)
var jd batch.JobDefinition
Expand Down Expand Up @@ -842,6 +843,47 @@ func TestAccBatchJobDefinition_EKSProperties_update(t *testing.T) {
})
}

func TestAccBatchJobDefinition_EKSProperties_imagePullSecrets(t *testing.T) {
ctx := acctest.Context(t)
var jd batch.JobDefinition
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_batch_job_definition.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.BatchServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckJobDefinitionDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccJobDefinitionConfig_EKSProperties_imagePullSecrets(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckJobDefinitionExists(ctx, resourceName, &jd),
resource.TestCheckResourceAttr(resourceName, "eks_properties.0.pod_properties.0.containers.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "eks_properties.0.pod_properties.0.containers.0.image_pull_policy", ""),
resource.TestCheckResourceAttr(resourceName, names.AttrName, rName),
resource.TestCheckResourceAttr(resourceName, names.AttrType, "container"),
resource.TestCheckResourceAttr(resourceName, "eks_properties.0.pod_properties.0.image_pull_secret.#", acctest.Ct2),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "eks_properties.*.pod_properties.*.image_pull_secret.*", map[string]string{
names.AttrName: "chihiro",
}),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "eks_properties.*.pod_properties.*.image_pull_secret.*", map[string]string{
names.AttrName: "haku",
}),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"deregister_on_new_revision",
},
},
},
})
}

func TestAccBatchJobDefinition_createTypeContainerWithNodeProperties(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down Expand Up @@ -1608,6 +1650,45 @@ resource "aws_batch_job_definition" "test" {
`, rName)
}

func testAccJobDefinitionConfig_EKSProperties_imagePullSecrets(rName string) string {
return fmt.Sprintf(`
resource "aws_batch_job_definition" "test" {
name = %[1]q
type = "container"
eks_properties {
pod_properties {
host_network = true
containers {
image = "public.ecr.aws/amazonlinux/amazonlinux:1"
command = [
"sleep",
"60"
]
resources {
limits = {
cpu = "1"
memory = "1024Mi"
}
}
}
image_pull_secret {
name = "chihiro"
}
image_pull_secret {
name = "haku"
}
metadata {
labels = {
environment = "test"
name = %[1]q
}
}
}
}
}
`, rName)
}

func testAccJobDefinitionConfig_EKSProperties_advancedUpdate(rName string) string {
return fmt.Sprintf(`
resource "aws_batch_job_definition" "test" {
Expand Down
Loading
Loading