为什么有时我会从AWS得到错误答案?

时间:2018-12-08 12:50:49

标签: amazon-web-services go aws-sdk autoscaling

我已经在Go中编写了一些代码,这些代码用于扩展AWS中的实例组。它需要两个环境变量:ASG_MAX_SCALE和ASG_MIN_SCALE,并检查AWS中指定ASG的所需容量。我在Kubernetes中将其作为cronjob运行,但是不幸的是,有时我会从AWS API中得到错误的答案。它的工作方式如下: 如果当前的所需容量等于ASG_MAX_SCALE,则将“所需容量”设置为ASG_MIN_SCALE,反之亦然。问题是,有时,例如,即使当前的“所需容量”为2(我可以在AWS控制台中看到),AWS API也会回答3,因此不会升级ASG。相反,它也发生了,实际的“期望容量”为3,但答案为2。问题很奇怪,因为它似乎是随机发生的。如果我使用该部件在本地(在笔记本电脑上)检查所需容量,那么它总是会给出正确的答案。

代码如下:

var newASGsize int64

maxScaleASG, err := strconv.Atoi(os.Getenv("ASG_MAX_SCALE"))

if err != nil {
    panic(err)
}

minScaleASG, err := strconv.Atoi(os.Getenv("ASG_MIN_SCALE"))

if err != nil {
    panic(err)
}

svc := autoscaling.New(session.New())
describeInput := &autoscaling.DescribeAutoScalingGroupsInput{
    AutoScalingGroupNames: []*string{
        aws.String(os.Getenv("ASG_NAME")),
    },
}

fmt.Println("Instance Group: " + os.Getenv("ASG_NAME"))

describeResult, err := svc.DescribeAutoScalingGroups(describeInput)

awsErr(err)

if *describeResult.AutoScalingGroups[0].DesiredCapacity == int64(maxScaleASG) {
    newASGsize = int64(minScaleASG)
} else {
    newASGsize = int64(maxScaleASG)
}

updateInput := &autoscaling.UpdateAutoScalingGroupInput{
    AutoScalingGroupName: aws.String(os.Getenv("ASG_NAME")),
    DesiredCapacity:      aws.Int64(newASGsize),
}

_, err = svc.UpdateAutoScalingGroup(updateInput)

awsErr(err)

for {
    describeResult, err := svc.DescribeAutoScalingGroups(describeInput)
    awsErr(err)

    if asgReadinessCheck(describeResult.AutoScalingGroups[0].Instances, newASGsize) == true {
        break
    }

    fmt.Println("The instance group is not ready. Sleeping for 5 seconds...")
    time.Sleep(5 * time.Second)
}

有人可以告诉我我做错了什么吗吗

谢谢。

0 个答案:

没有答案
相关问题