Skip to content

Commit

Permalink
return set of issues when ng is in degraded state
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanSpectro committed Sep 5, 2024
1 parent abe918c commit e3ca115
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/cloud/services/eks/nodegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package eks
import (
"context"
"fmt"
"slices"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand All @@ -37,6 +39,7 @@ import (
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/wait"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/record"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
capierrors "sigs.k8s.io/cluster-api/errors"
"sigs.k8s.io/cluster-api/util/annotations"
)

Expand Down Expand Up @@ -592,6 +595,29 @@ func (s *NodegroupService) setStatus(ng *eks.Nodegroup) error {
managedPool.Status.Ready = false
case eks.NodegroupStatusUpdating:
managedPool.Status.Ready = true
case eks.NodegroupStatusDegraded:
issueErrMsgSet := make([]string, 0)
var errMsgStr string

for _, iss := range ng.Health.Issues {
errMsg := iss.GoString()
if slices.Contains(issueErrMsgSet, errMsg) {
continue
}
issueErrMsgSet = append(issueErrMsgSet, errMsg)
errMsgStr = fmt.Sprintf("%s %s", errMsgStr, errMsg)
}
reason := capierrors.InvalidConfigurationMachineError
// TODO: implement checks for other MachineStatusErrors and set reason accordingly
if strings.Contains(errMsgStr, "VcpuLimitExceeded") {
reason = capierrors.InsufficientResourcesMachineError
}

managedPool.Status.Ready = false
managedPool.Status.FailureReason = &reason
managedPool.Status.FailureMessage = &errMsgStr
return errors.Errorf("NodeGroup status is %s due to %v caused by error %s. This error may persist and recreating the Node Group may be required to return to %s status",
eks.NodegroupStatusDegraded, *s.scope.ManagedMachinePool.Status.FailureReason, *s.scope.ManagedMachinePool.Status.FailureMessage, eks.NodegroupStatusActive)
default:
return errors.Errorf("unexpected EKS nodegroup status %s", *ng.Status)
}
Expand Down
64 changes: 64 additions & 0 deletions pkg/cloud/services/eks/nodegroup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package eks

import (
"testing"

"github.com/aws/aws-sdk-go/service/eks"
. "github.com/onsi/gomega"
"sigs.k8s.io/cluster-api-provider-aws/v2/exp/api/v1beta2"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
capierrors "sigs.k8s.io/cluster-api/errors"
)

func TestSetStatus(t *testing.T) {
g := NewWithT(t)
degraded := eks.NodegroupStatusDegraded
code := eks.NodegroupIssueCodeAsgInstanceLaunchFailures
message := "VcpuLimitExceeded"
resourceID := "my-worker-nodes"

s := &NodegroupService{
scope: &scope.ManagedMachinePoolScope{
ManagedMachinePool: &v1beta2.AWSManagedMachinePool{
Status: v1beta2.AWSManagedMachinePoolStatus{
Ready: false,
},
},
},
}

issue := &eks.Issue{
Code: &code,
Message: &message,
ResourceIds: []*string{&resourceID},
}
ng := &eks.Nodegroup{
Status: &degraded,
Health: &eks.NodegroupHealth{
Issues: []*eks.Issue{issue},
},
}

err := s.setStatus(ng)
g.Expect(err).ToNot(BeNil())
// ensure machine pool status values are set as expected
g.Expect(*s.scope.ManagedMachinePool.Status.FailureMessage).To(ContainSubstring(issue.GoString()))
g.Expect(s.scope.ManagedMachinePool.Status.Ready).To(BeFalse())
g.Expect(*s.scope.ManagedMachinePool.Status.FailureReason).To(Equal(capierrors.InsufficientResourcesMachineError))
}

0 comments on commit e3ca115

Please sign in to comment.