Skip to content

Commit

Permalink
Added permissions check
Browse files Browse the repository at this point in the history
  • Loading branch information
edibble21 committed Nov 5, 2024
1 parent fe990a2 commit 6de755f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/controllers/nodeclass/status/launchtemplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ var _ = Describe("NodeClass Launch Template CIDR Resolution Controller", func()
KubernetesNetworkConfig: &eks.KubernetesNetworkConfigResponse{
ServiceIpv6Cidr: lo.ToPtr("2001:db8::/64"),
},
Version: lo.ToPtr("1.30"),
},
})
nodeClass.Spec.AMIFamily = lo.ToPtr(v1.AMIFamilyAL2023)
Expand Down
14 changes: 14 additions & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ var (
alreadyExistsErrorCodes = sets.New[string](
iam.ErrCodeEntityAlreadyExistsException,
)
hasAccessErrorCodes = sets.New[string](

Check failure on line 50 in pkg/errors/errors.go

View workflow job for this annotation

GitHub Actions / ci

var `hasAccessErrorCodes` is unused (unused)
"AccessDeniedException",
)
// unfulfillableCapacityErrorCodes signify that capacity is temporarily unable to be launched
unfulfillableCapacityErrorCodes = sets.New[string](
"InsufficientInstanceCapacity",
Expand All @@ -58,6 +61,17 @@ var (
)
)

func HasNoAccess(err error) bool {
if err == nil {
return false
}
var awsError awserr.Error
if errors.As(err, &awsError) {
return notFoundErrorCodes.Has(awsError.Code())
}
return false
}

// IsNotFound returns true if the err is an AWS error (even if it's
// wrapped) and is a known to mean "not found" (as opposed to a more
// serious or unexpected error)
Expand Down
17 changes: 12 additions & 5 deletions pkg/providers/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strconv"
"strings"

awserrors "github.com/aws/karpenter-provider-aws/pkg/errors"
"github.com/patrickmn/go-cache"
"github.com/samber/lo"
"k8s.io/apimachinery/pkg/util/version"
Expand Down Expand Up @@ -77,12 +78,18 @@ func (p *DefaultProvider) Get(ctx context.Context) (string, error) {
version = *serverVersion.Cluster.Version
log.FromContext(ctx).Info("Successfully retrieved Kubernetes version from EKS DescribeCluster", "version", version)
} else {
fallbackVersion, err := p.kubernetesInterface.Discovery().ServerVersion()
if err != nil {
return "", err
if awserrors.HasNoAccess(err) {
fallbackVersion, err := p.kubernetesInterface.Discovery().ServerVersion()
if err != nil {
return "", err
}
version = fmt.Sprintf("%s.%s", fallbackVersion.Major, strings.TrimSuffix(fallbackVersion.Minor, "+"))
log.FromContext(ctx).Info("Successfully retrieved Kubernetes version from Kubernetes API", "version", version)
} else {
if err != nil {
return "", err
}
}
version = fmt.Sprintf("%s.%s", fallbackVersion.Major, strings.TrimSuffix(fallbackVersion.Minor, "+"))
log.FromContext(ctx).Info("Successfully retrieved Kubernetes version from Kubernetes API", "version", version)
}
p.cache.SetDefault(kubernetesVersionCacheKey, version)
if p.cm.HasChanged("kubernetes-version", version) {
Expand Down

0 comments on commit 6de755f

Please sign in to comment.