mirror of https://github.com/kubernetes/kops.git
Merge pull request #16213 from rifelpet/targetlifecyclestate
aws: Use instance metadata to get warm pool state
This commit is contained in:
commit
988befb20d
|
|
@ -1116,6 +1116,9 @@ func addASLifecyclePolicies(p *Policy, enableHookSupport bool) {
|
||||||
"autoscaling:DescribeLifecycleHooks",
|
"autoscaling:DescribeLifecycleHooks",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
// TODO: remove this after k8s 1.29 support is removed
|
||||||
|
// It is no longer needed as of kops 1.29 but to prevent node bootstrap issues
|
||||||
|
// during kops upgrades we keep the permission until it is guaranteed to not be needed.
|
||||||
p.unconditionalAction.Insert(
|
p.unconditionalAction.Insert(
|
||||||
"autoscaling:DescribeAutoScalingInstances",
|
"autoscaling:DescribeAutoScalingInstances",
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||||
"github.com/aws/aws-sdk-go/aws/ec2metadata"
|
"github.com/aws/aws-sdk-go/aws/ec2metadata"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
"github.com/aws/aws-sdk-go/service/autoscaling"
|
"github.com/aws/aws-sdk-go/service/autoscaling"
|
||||||
|
|
@ -245,10 +246,13 @@ func (c *NodeUpCommand) Run(out io.Writer) error {
|
||||||
}
|
}
|
||||||
modelContext.InstanceID = string(instanceIDBytes)
|
modelContext.InstanceID = string(instanceIDBytes)
|
||||||
|
|
||||||
|
// Check if WarmPool is enabled first, to avoid additional API calls
|
||||||
|
if len(modelContext.NodeupConfig.WarmPoolImages) > 0 {
|
||||||
modelContext.ConfigurationMode, err = getAWSConfigurationMode(ctx, modelContext)
|
modelContext.ConfigurationMode, err = getAWSConfigurationMode(ctx, modelContext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
modelContext.MachineType, err = getMachineType()
|
modelContext.MachineType, err = getMachineType()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -712,6 +716,11 @@ func getNodeConfigFromServers(ctx context.Context, bootConfig *nodeup.BootConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAWSConfigurationMode(ctx context.Context, c *model.NodeupModelContext) (string, error) {
|
func getAWSConfigurationMode(ctx context.Context, c *model.NodeupModelContext) (string, error) {
|
||||||
|
// Check if WarmPool is enabled first, to avoid additional API calls
|
||||||
|
if len(c.NodeupConfig.WarmPoolImages) == 0 {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
// Only worker nodes and apiservers can actually autoscale.
|
// Only worker nodes and apiservers can actually autoscale.
|
||||||
// We are not adding describe permissions to the other roles
|
// We are not adding describe permissions to the other roles
|
||||||
role := c.BootConfig.InstanceGroupRole
|
role := c.BootConfig.InstanceGroupRole
|
||||||
|
|
@ -719,20 +728,17 @@ func getAWSConfigurationMode(ctx context.Context, c *model.NodeupModelContext) (
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
svc := c.Cloud.(awsup.AWSCloud).Autoscaling()
|
targetLifecycleState, err := vfs.Context.ReadFile("metadata://aws/meta-data/autoscaling/target-lifecycle-state")
|
||||||
|
|
||||||
result, err := svc.DescribeAutoScalingInstancesWithContext(ctx, &autoscaling.DescribeAutoScalingInstancesInput{
|
|
||||||
InstanceIds: []*string{&c.InstanceID},
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("error describing instances: %v", err)
|
var awsErr awserr.RequestFailure
|
||||||
}
|
if errors.As(err, &awsErr) && awsErr.StatusCode() == 404 {
|
||||||
// If the instance is not a part of an ASG, it won't be in a warm pool either.
|
// The instance isn't in an ASG (karpenter, etc.)
|
||||||
if len(result.AutoScalingInstances) < 1 {
|
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
lifecycle := fi.ValueOf(result.AutoScalingInstances[0].LifecycleState)
|
return "", fmt.Errorf("error reading target-lifecycle-state from instance metadata: %v", err)
|
||||||
if strings.HasPrefix(lifecycle, "Warmed:") {
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(string(targetLifecycleState), "Warmed:") {
|
||||||
klog.Info("instance is entering warm pool")
|
klog.Info("instance is entering warm pool")
|
||||||
return model.ConfigurationModeWarming, nil
|
return model.ConfigurationModeWarming, nil
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue