Skip to content

Commit

Permalink
eks-resource: process paginated results from list-stacks
Browse files Browse the repository at this point in the history
Cfn ListStacks might return paginated results. Go through all results
before erroring on missing stack.
  • Loading branch information
etungsten committed Sep 15, 2023
1 parent 2491a2a commit 2a70df6
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions bottlerocket/agents/src/bin/eks-resource-agent/eks_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,33 +458,53 @@ async fn nodegroup_iam_role(
cluster_name: &str,
cfn_client: &aws_sdk_cloudformation::Client,
) -> ProviderResult<String> {
let stack_name = cfn_client
let mut list_stack_output = cfn_client
.list_stacks()
.stack_status_filter(StackStatus::CreateComplete)
.stack_status_filter(StackStatus::UpdateComplete)
.send()
.await
.context(Resources::Remaining, "Unable to list CloudFormation stacks")?
.stack_summaries()
.context(
Resources::Remaining,
"Missing CloudFormation stack summaries",
)?
.iter()
.filter_map(|stack| stack.stack_name())
.find(|name|
// For eksctl created clusters
name.starts_with(&format!("eksctl-{cluster_name}-nodegroup"))
// For non-eksctl created clusters
|| name.starts_with(&format!("{cluster_name}-node-group")))
.context(
Resources::Remaining,
"Could not find nodegroup cloudformation stack for cluster",
)?
.to_string();
.context(Resources::Remaining, "Unable to list CloudFormation stacks")?;

let stack_name;
loop {
if let Some(name) = list_stack_output
.stack_summaries()
.context(
Resources::Remaining,
"Missing CloudFormation stack summaries",
)?
.iter()
.filter_map(|stack| stack.stack_name())
.find(|name|
// For eksctl created clusters
name.starts_with(&format!("eksctl-{cluster_name}-nodegroup"))
// For non-eksctl created clusters
|| name.starts_with(&format!("{cluster_name}-node-group")))
{
stack_name = name;
break;
} else if let Some(token) = list_stack_output.next_token() {
list_stack_output = cfn_client
.list_stacks()
.next_token(token)
.stack_status_filter(StackStatus::CreateComplete)
.stack_status_filter(StackStatus::UpdateComplete)
.send()
.await
.context(Resources::Remaining, "Unable to list CloudFormation stacks")?;
continue;
} else {
return Err(ProviderError::new_with_context(
Resources::Remaining,
"Could not find nodegroup cloudformation stack for cluster",
));
}
}

cfn_client
.describe_stack_resource()
.stack_name(&stack_name)
.stack_name(stack_name)
.logical_resource_id("NodeInstanceRole")
.send()
.await
Expand Down

0 comments on commit 2a70df6

Please sign in to comment.