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

Added FIS support for EKS Cluster #27337

Merged
merged 5 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/service/fis/experiment_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,12 +802,13 @@ func validExperimentTemplateStopConditionSource() schema.SchemaValidateFunc {

func validExperimentTemplateActionTargetKey() schema.SchemaValidateFunc {
allowedStopConditionSources := []string{
"Cluster",
"Clusters",
"DBInstances",
"Instances",
"SpotInstances",
"Nodegroups",
"Roles",
"SpotInstances",
"Subnets",
}

Expand Down
201 changes: 201 additions & 0 deletions internal/service/fis/experiment_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,62 @@ func TestAccFISExperimentTemplate_spot(t *testing.T) {
})
}

func TestAccFISExperimentTemplate_eks(t *testing.T) {
ctx := acctest.Context(t)
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_fis_experiment_template.test"
var conf types.ExperimentTemplate

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, fis.ServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckExperimentTemplateDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccExperimentTemplateConfig_eks(rName, "kubernetes custom resource creation", "k8s-pod-delete", "k8s pod delete", "aws:eks:inject-kubernetes-custom-resource", "Cluster", "kubernetes-custom-resource-creation-target", "kubernetesApiVersion", "litmuschaos.io/v1alpha1", "kubernetesKind", "ChaosEngine", "kubernetesNamespace", "observability", "kubernetesSpec", "{\"engineState\":\"active\",\"appinfo\":{\"appns\":\"observability\",\"applabel\":\"app=nginx\",\"appkind\":\"deployment\"},\"chaosServiceAccount\":\"pod-delete-sa\",\"experiments\":[{\"name\":\"pod-delete\",\"spec\":{\"components\":{\"env\":[{\"name\":\"TOTAL_CHAOS_DURATION\",\"value\":\"60\"},{\"name\":\"CHAOS_INTERVAL\",\"value\":\"60\"},{\"name\":\"PODS_AFFECTED_PERC\",\"value\":\"30\"}]},\"probe\":[]}}],\"annotationCheck\":\"false\"}", "maxDuration", "PT2M", "aws:eks:cluster", "ALL", "env", "test"),
Check: resource.ComposeTestCheckFunc(
testAccExperimentTemplateExists(ctx, resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "description", "kubernetes custom resource creation"),
resource.TestCheckResourceAttrPair(resourceName, "role_arn", "aws_iam_role.test_fis", "arn"),
resource.TestCheckResourceAttr(resourceName, "stop_condition.0.source", "none"),
resource.TestCheckResourceAttr(resourceName, "stop_condition.0.value", ""),
resource.TestCheckResourceAttr(resourceName, "stop_condition.#", "1"),
resource.TestCheckResourceAttr(resourceName, "action.0.name", "k8s-pod-delete"),
resource.TestCheckResourceAttr(resourceName, "action.0.description", "k8s pod delete"),
resource.TestCheckResourceAttr(resourceName, "action.0.action_id", "aws:eks:inject-kubernetes-custom-resource"),
resource.TestCheckResourceAttr(resourceName, "action.0.parameter.#", "5"),
resource.TestCheckResourceAttr(resourceName, "action.0.parameter.0.key", "kubernetesApiVersion"),
resource.TestCheckResourceAttr(resourceName, "action.0.parameter.0.value", "litmuschaos.io/v1alpha1"),
resource.TestCheckResourceAttr(resourceName, "action.0.parameter.1.key", "kubernetesKind"),
resource.TestCheckResourceAttr(resourceName, "action.0.parameter.1.value", "ChaosEngine"),
resource.TestCheckResourceAttr(resourceName, "action.0.parameter.2.key", "kubernetesNamespace"),
resource.TestCheckResourceAttr(resourceName, "action.0.parameter.2.value", "observability"),
resource.TestCheckResourceAttr(resourceName, "action.0.parameter.3.key", "kubernetesSpec"),
resource.TestCheckResourceAttr(resourceName, "action.0.parameter.3.value", "{\"engineState\":\"active\",\"appinfo\":{\"appns\":\"observability\",\"applabel\":\"app=nginx\",\"appkind\":\"deployment\"},\"chaosServiceAccount\":\"pod-delete-sa\",\"experiments\":[{\"name\":\"pod-delete\",\"spec\":{\"components\":{\"env\":[{\"name\":\"TOTAL_CHAOS_DURATION\",\"value\":\"60\"},{\"name\":\"CHAOS_INTERVAL\",\"value\":\"60\"},{\"name\":\"PODS_AFFECTED_PERC\",\"value\":\"30\"}]},\"probe\":[]}}],\"annotationCheck\":\"false\"}"),
resource.TestCheckResourceAttr(resourceName, "action.0.parameter.4.key", "maxDuration"),
resource.TestCheckResourceAttr(resourceName, "action.0.parameter.4.value", "PT2M"),
resource.TestCheckResourceAttr(resourceName, "action.0.start_after.#", "0"),
resource.TestCheckResourceAttr(resourceName, "action.0.target.0.key", "Cluster"),
resource.TestCheckResourceAttr(resourceName, "action.0.target.0.value", "kubernetes-custom-resource-creation-target"),
resource.TestCheckResourceAttr(resourceName, "action.0.target.#", "1"),
resource.TestCheckResourceAttr(resourceName, "action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "target.0.name", "kubernetes-custom-resource-creation-target"),
resource.TestCheckResourceAttr(resourceName, "target.0.resource_type", "aws:eks:cluster"),
resource.TestCheckResourceAttr(resourceName, "target.0.selection_mode", "ALL"),
resource.TestCheckResourceAttr(resourceName, "target.0.filter.#", "0"),
resource.TestCheckResourceAttrPair(resourceName, "target.0.resource_arns.0", "aws_eks_cluster.test", "arn"),
resource.TestCheckResourceAttr(resourceName, "target.0.resource_tag.#", "0"),
resource.TestCheckResourceAttr(resourceName, "target.#", "1"),
),
},
},
})
}

func testAccExperimentTemplateExists(ctx context.Context, resourceName string, config *types.ExperimentTemplate) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
Expand Down Expand Up @@ -373,3 +429,148 @@ resource "aws_fis_experiment_template" "test" {
}
`, rName, desc, actionName, actionDesc, actionID, actionTargetK, actionTargetV, paramK, paramV, targetResType, targetSelectMode, targetResTagK, targetResTagV)
}
func testAccExperimentTemplateConfig_baseEKSCluster(rName string) string {
return acctest.ConfigCompose(acctest.ConfigAvailableAZsNoOptIn(), fmt.Sprintf(`
data "aws_partition" "current" {}

resource "aws_iam_role" "test" {
name = %[1]q

assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "eks.${data.aws_partition.current.dns_suffix}"
},
"Action": "sts:AssumeRole"
}
]
}
POLICY
}

resource "aws_iam_role_policy_attachment" "test-AmazonEKSClusterPolicy" {
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonEKSClusterPolicy"
role = aws_iam_role.test.name
}

resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"

assign_generated_ipv6_cidr_block = true

tags = {
Name = %[1]q
"kubernetes.io/cluster/%[1]s" = "shared"
}
}

resource "aws_subnet" "test" {
count = 2

availability_zone = data.aws_availability_zones.available.names[count.index]
cidr_block = "10.0.${count.index}.0/24"
vpc_id = aws_vpc.test.id

ipv6_cidr_block = cidrsubnet(aws_vpc.test.ipv6_cidr_block, 8, count.index)
assign_ipv6_address_on_creation = true

tags = {
Name = %[1]q
"kubernetes.io/cluster/%[1]s" = "shared"
}
}

resource "aws_eks_cluster" "test" {
name = %[1]q
role_arn = aws_iam_role.test.arn

vpc_config {
subnet_ids = aws_subnet.test[*].id
}

depends_on = [aws_iam_role_policy_attachment.test-AmazonEKSClusterPolicy]
}
`, rName))
}

func testAccExperimentTemplateConfig_eks(rName, desc, actionName, actionDesc, actionID, actionTargetK, actionTargetV, paramK1, paramV1, paramK2, paramV2, paramK3, paramV3, paramK4, paramV4, paramK5, paramV5, targetResType, targetSelectMode, targetResTagK, targetResTagV string) string {
return acctest.ConfigCompose(testAccExperimentTemplateConfig_baseEKSCluster(rName), fmt.Sprintf(`
resource "aws_iam_role" "test_fis" {
name = %[1]q

assume_role_policy = jsonencode({
Statement = [{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = [
"fis.${data.aws_partition.current.dns_suffix}",
]
}
}]
Version = "2012-10-17"
})
}

resource "aws_fis_experiment_template" "test" {
description = %[2]q
role_arn = aws_iam_role.test_fis.arn

stop_condition {
source = "none"
}

action {
name = %[3]q
description = %[4]q
action_id = %[5]q

target {
key = %[6]q
value = %[7]q
}

parameter {
key = %[8]q
value = %[9]q
}

parameter {
key = %[10]q
value = %[11]q
}

parameter {
key = %[12]q
value = %[13]q
}

parameter {
key = %[14]q
value = %[15]q
}

parameter {
key = %[16]q
value = %[17]q
}
}

target {
name = %[7]q
resource_type = %[18]q
selection_mode = %[19]q

resource_arns = tolist([aws_eks_cluster.test.arn])
}

tags = {
Name = %[1]q
}
}
`, rName+"-fis", desc, actionName, actionDesc, actionID, actionTargetK, actionTargetV, paramK1, paramV1, paramK2, paramV2, paramK3, paramV3, paramK4, paramV4, paramK5, paramV5, targetResType, targetSelectMode, targetResTagK, targetResTagV))
}
2 changes: 1 addition & 1 deletion website/docs/r/fis_experiment_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ For a list of parameters supported by each action, see [AWS FIS actions referenc

#### `target` (`action.*.target`)

* `key` - (Required) Target type. Valid values are `Clusters` (ECS Clusters), `DBInstances` (RDS DB Instances), `Instances` (EC2 Instances), `Nodegroups` (EKS Node groups), `Roles` (IAM Roles), `SpotInstances` (EC2 Spot Instances), `Subnets` (VPC Subnets).
* `key` - (Required) Target type. Valid values are `Cluster` (EKS Cluster), `Clusters` (ECS Clusters), `DBInstances` (RDS DB Instances), `Instances` (EC2 Instances), `Nodegroups` (EKS Node groups), `Roles` (IAM Roles), `SpotInstances` (EC2 Spot Instances), `Subnets` (VPC Subnets).
* `value` - (Required) Target name, referencing a corresponding target.

### `stop_condition`
Expand Down