Skip to content

Commit

Permalink
Allow cluster autoscaler to get EC2 instance types
Browse files Browse the repository at this point in the history
When the cluster autoscaler builds its EC2 instance type catalog
dynamically instead of using only its statically defined set, grant it
the additional IAM permissions required to fetch the instance types
from the AWS API.
  • Loading branch information
seh committed Apr 20, 2022
1 parent 91bce66 commit de1ecd8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
9 changes: 7 additions & 2 deletions pkg/model/components/addonmanifests/clusterautoscaler/iam.go
Expand Up @@ -19,9 +19,10 @@ package clusterautoscaler
import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/kops/pkg/model/iam"
"k8s.io/kops/upup/pkg/fi"
)

// ServiceAccount represents the service-account used by the dns-controller.
// ServiceAccount represents the service account used by the cluster autoscaler.
// It implements iam.Subject to get AWS IAM permissions.
type ServiceAccount struct{}

Expand All @@ -32,7 +33,11 @@ func (r *ServiceAccount) BuildAWSPolicy(b *iam.PolicyBuilder) (*iam.Policy, erro
clusterName := b.Cluster.ObjectMeta.Name
p := iam.NewPolicy(clusterName, b.Partition)

iam.AddClusterAutoscalerPermissions(p)
var useStaticInstanceList bool
if ca := b.Cluster.Spec.ClusterAutoscaler; ca != nil && fi.BoolValue(ca.AWSUseStaticInstanceList) {
useStaticInstanceList = true
}
iam.AddClusterAutoscalerPermissions(p, useStaticInstanceList)

return p, nil
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/model/iam/iam_builder.go
Expand Up @@ -427,7 +427,12 @@ func (r *NodeRoleMaster) BuildAWSPolicy(b *PolicyBuilder) (*Policy, error) {
if b.Cluster.Spec.AWSLoadBalancerController != nil && fi.BoolValue(b.Cluster.Spec.AWSLoadBalancerController.Enabled) {
AddAWSLoadbalancerControllerPermissions(p)
}
AddClusterAutoscalerPermissions(p)

var useStaticInstanceList bool
if ca := b.Cluster.Spec.ClusterAutoscaler; ca != nil && fi.BoolValue(ca.AWSUseStaticInstanceList) {
useStaticInstanceList = true
}
AddClusterAutoscalerPermissions(p, useStaticInstanceList)

nth := b.Cluster.Spec.NodeTerminationHandler
if nth != nil && fi.BoolValue(nth.Enabled) && fi.BoolValue(nth.EnableSQSTerminationDraining) {
Expand Down Expand Up @@ -1013,7 +1018,7 @@ func AddAWSLoadbalancerControllerPermissions(p *Policy) {
)
}

func AddClusterAutoscalerPermissions(p *Policy) {
func AddClusterAutoscalerPermissions(p *Policy, useStaticInstanceList bool) {
p.clusterTaggedAction.Insert(
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup",
Expand All @@ -1024,6 +1029,11 @@ func AddClusterAutoscalerPermissions(p *Policy) {
"autoscaling:DescribeLaunchConfigurations",
"ec2:DescribeLaunchTemplateVersions",
)
if !useStaticInstanceList {
p.unconditionalAction.Insert(
"ec2:DescribeInstanceTypes",
)
}
}

// AddAWSEBSCSIDriverPermissions appens policy statements that the AWS EBS CSI Driver needs to operate.
Expand Down
Expand Up @@ -5,6 +5,7 @@
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"ec2:DescribeInstanceTypes",
"ec2:DescribeLaunchTemplateVersions"
],
"Effect": "Allow",
Expand Down
Expand Up @@ -5,6 +5,7 @@
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"ec2:DescribeInstanceTypes",
"ec2:DescribeLaunchTemplateVersions"
],
"Effect": "Allow",
Expand Down

0 comments on commit de1ecd8

Please sign in to comment.