Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various aws-sdk-go-v2 fixes #16443

Merged
merged 8 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions cloudmock/aws/mockiam/iaminstanceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func (m *MockIAM) GetInstanceProfile(ctx context.Context, request *iam.GetInstan
m.mutex.Lock()
defer m.mutex.Unlock()

ip, ok := m.InstanceProfiles[aws.ToString(request.InstanceProfileName)]
if !ok || strings.Contains(aws.ToString(ip.InstanceProfileName), "__no_entity__") {
ip := m.InstanceProfiles[aws.ToString(request.InstanceProfileName)]
if ip == nil || strings.Contains(aws.ToString(ip.InstanceProfileName), "__no_entity__") {
return nil, &iamtypes.NoSuchEntityException{}
}
response := &iam.GetInstanceProfileOutput{
Expand Down Expand Up @@ -83,8 +83,8 @@ func (m *MockIAM) TagInstanceProfile(ctx context.Context, request *iam.TagInstan

klog.Infof("CreateInstanceProfile: %v", request)

ip, ok := m.InstanceProfiles[aws.ToString(request.InstanceProfileName)]
if !ok {
ip := m.InstanceProfiles[aws.ToString(request.InstanceProfileName)]
if ip == nil {
return nil, fmt.Errorf("InstanceProfile not found")
}

Expand All @@ -111,13 +111,13 @@ func (m *MockIAM) AddRoleToInstanceProfile(ctx context.Context, request *iam.Add

klog.Infof("AddRoleToInstanceProfile: %v", request)

ip, ok := m.InstanceProfiles[aws.ToString(request.InstanceProfileName)]
if !ok {
return nil, fmt.Errorf("InstanceProfile not found")
ip := m.InstanceProfiles[aws.ToString(request.InstanceProfileName)]
if ip == nil {
return nil, fmt.Errorf("instance profile not found")
}
r, ok := m.Roles[aws.ToString(request.RoleName)]
if !ok {
return nil, fmt.Errorf("Role not found")
r := m.Roles[aws.ToString(request.RoleName)]
if r == nil {
return nil, fmt.Errorf("role not found")
}

ip.Roles = append(ip.Roles, *r)
Expand All @@ -131,9 +131,9 @@ func (m *MockIAM) RemoveRoleFromInstanceProfile(ctx context.Context, request *ia

klog.Infof("RemoveRoleFromInstanceProfile: %v", request)

ip, ok := m.InstanceProfiles[aws.ToString(request.InstanceProfileName)]
if !ok {
return nil, fmt.Errorf("InstanceProfile not found")
ip := m.InstanceProfiles[aws.ToString(request.InstanceProfileName)]
if ip == nil {
return nil, fmt.Errorf("instance profile not found")
}

found := false
Expand All @@ -147,7 +147,7 @@ func (m *MockIAM) RemoveRoleFromInstanceProfile(ctx context.Context, request *ia
}

if !found {
return nil, fmt.Errorf("Role not found")
return nil, fmt.Errorf("role not found")
}
ip.Roles = newRoles

Expand Down Expand Up @@ -185,8 +185,8 @@ func (m *MockIAM) DeleteInstanceProfile(ctx context.Context, request *iam.Delete
klog.Infof("DeleteInstanceProfile: %v", request)

id := aws.ToString(request.InstanceProfileName)
_, ok := m.InstanceProfiles[id]
if !ok {
o := m.InstanceProfiles[id]
if o == nil {
return nil, fmt.Errorf("InstanceProfile %q not found", id)
}
delete(m.InstanceProfiles, id)
Expand Down
10 changes: 5 additions & 5 deletions cloudmock/aws/mockiam/iamrole.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (m *MockIAM) GetRole(ctx context.Context, request *iam.GetRoleInput, optFns
m.mutex.Lock()
defer m.mutex.Unlock()

role, ok := m.Roles[aws.ToString(request.RoleName)]
if !ok {
role := m.Roles[aws.ToString(request.RoleName)]
if role == nil {
return nil, &iamtypes.NoSuchEntityException{}
}
response := &iam.GetRoleOutput{
Expand Down Expand Up @@ -97,9 +97,9 @@ func (m *MockIAM) DeleteRole(ctx context.Context, request *iam.DeleteRoleInput,
klog.Infof("DeleteRole: %v", request)

id := aws.ToString(request.RoleName)
_, ok := m.Roles[id]
if !ok {
return nil, fmt.Errorf("Role %q not found", id)
o := m.Roles[id]
if o == nil {
return nil, fmt.Errorf("role %q not found", id)
}
delete(m.Roles, id)

Expand Down
4 changes: 2 additions & 2 deletions cloudmock/aws/mockiam/oidcprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func (m *MockIAM) DeleteOpenIDConnectProvider(ctx context.Context, request *iam.
klog.Infof("DeleteOpenIDConnectProvider: %v", request)

arn := aws.ToString(request.OpenIDConnectProviderArn)
_, ok := m.OIDCProviders[arn]
if !ok {
provider := m.OIDCProviders[arn]
if provider == nil {
return nil, fmt.Errorf("OIDCProvider %q not found", arn)
}
delete(m.OIDCProviders, arn)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.0
github.com/aws/aws-sdk-go-v2/service/sqs v1.31.4
github.com/aws/aws-sdk-go-v2/service/ssm v1.49.5
github.com/aws/aws-sdk-go-v2/service/sts v1.28.5
github.com/aws/smithy-go v1.20.2
github.com/blang/semver/v4 v4.0.0
github.com/cert-manager/cert-manager v1.14.4
Expand Down Expand Up @@ -120,7 +121,6 @@ require (
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.3 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.3 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
Expand Down
12 changes: 4 additions & 8 deletions pkg/model/awsmodel/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"sort"
"strings"

awsIam "github.com/aws/aws-sdk-go-v2/service/iam"
awsiam "github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go/aws/endpoints"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -476,9 +476,8 @@ func (b *IAMModelBuilder) FindDeletions(context *fi.CloudupModelBuilderContext,
ctx := context.Context()
iamapi := cloud.(awsup.AWSCloud).IAM()
ownershipTag := "kubernetes.io/cluster/" + b.Cluster.ObjectMeta.Name
request := &awsIam.ListRolesInput{}
var getRoleErr error
paginator := awsIam.NewListRolesPaginator(iamapi, request)
request := &awsiam.ListRolesInput{}
paginator := awsiam.NewListRolesPaginator(iamapi, request)
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
Expand All @@ -488,7 +487,7 @@ func (b *IAMModelBuilder) FindDeletions(context *fi.CloudupModelBuilderContext,
if !strings.HasSuffix(fi.ValueOf(role.RoleName), "."+b.Cluster.ObjectMeta.Name) {
continue
}
getRequest := &awsIam.GetRoleInput{RoleName: role.RoleName}
getRequest := &awsiam.GetRoleInput{RoleName: role.RoleName}
roleOutput, err := iamapi.GetRole(ctx, getRequest)
if err != nil {
return fmt.Errorf("calling IAM GetRole on %s: %w", fi.ValueOf(role.RoleName), err)
Expand All @@ -506,8 +505,5 @@ func (b *IAMModelBuilder) FindDeletions(context *fi.CloudupModelBuilderContext,
}
}
}
if getRoleErr != nil {
return getRoleErr
}
return nil
}
41 changes: 16 additions & 25 deletions pkg/resources/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/smithy-go"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/dns"
Expand Down Expand Up @@ -1844,16 +1843,13 @@ func DeleteIAMRole(cloud fi.Cloud, r *resources.Resource) error {
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
var nse *iamtypes.NoSuchEntityException
if errors.As(err, &nse) {
if awsup.IsIAMNoSuchEntityException(err) {
klog.V(2).Infof("Got NoSuchEntity describing IAM RolePolicy %q; will treat as already-deleted", roleName)
return nil
}
return fmt.Errorf("error listing IAM role policies for %q: %v", roleName, err)
}
for _, policy := range page.PolicyNames {
policyNames = append(policyNames, policy)
}
policyNames = append(policyNames, page.PolicyNames...)
}
}

Expand All @@ -1866,8 +1862,7 @@ func DeleteIAMRole(cloud fi.Cloud, r *resources.Resource) error {
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
var nse *iamtypes.NoSuchEntityException
if errors.As(err, &nse) {
if awsup.IsIAMNoSuchEntityException(err) {
klog.V(2).Infof("Got NoSuchEntity describing IAM RolePolicy %q; will treat as already-deleted", roleName)
return nil
}
Expand Down Expand Up @@ -1939,12 +1934,11 @@ func ListIAMRoles(cloud fi.Cloud, vpcID, clusterName string) ([]*resources.Resou
getRequest := &iam.GetRoleInput{RoleName: r.RoleName}
roleOutput, err := c.IAM().GetRole(ctx, getRequest)
if err != nil {
var nse *iamtypes.NoSuchEntityException
if errors.As(err, &nse) {
klog.Warningf("could not find role %q. Resource may already have been deleted: %v", name, nse)
if awsup.IsIAMNoSuchEntityException(err) {
klog.Warningf("could not find role %q. Resource may already have been deleted: %v", name, err)
continue
} else if awserror, ok := err.(smithy.APIError); ok && awserror.ErrorCode() == "403" {
klog.Warningf("failed to determine ownership of %q: %v", name, awserror)
} else if awsup.AWSErrorCode(err) == "403" {
klog.Warningf("failed to determine ownership of %q: %v", name, err)
continue
}
return nil, fmt.Errorf("calling IAM GetRole on %s: %w", name, err)
Expand Down Expand Up @@ -2024,12 +2018,11 @@ func ListIAMInstanceProfiles(cloud fi.Cloud, vpcID, clusterName string) ([]*reso
getRequest := &iam.GetInstanceProfileInput{InstanceProfileName: p.InstanceProfileName}
profileOutput, err := c.IAM().GetInstanceProfile(ctx, getRequest)
if err != nil {
var nse *iamtypes.NoSuchEntityException
if errors.As(err, &nse) {
klog.Warningf("could not find role %q. Resource may already have been deleted: %v", name, nse)
if awsup.IsIAMNoSuchEntityException(err) {
klog.Warningf("could not find role %q. Resource may already have been deleted: %v", name, err)
continue
} else if awserror, ok := err.(smithy.APIError); ok && awserror.ErrorCode() == "403" {
klog.Warningf("failed to determine ownership of %q: %v", *p.InstanceProfileName, awserror)
} else if awsup.AWSErrorCode(err) == "403" {
klog.Warningf("failed to determine ownership of %q: %v", *p.InstanceProfileName, err)
continue
}
return nil, fmt.Errorf("calling IAM GetInstanceProfile on %s: %w", name, err)
Expand Down Expand Up @@ -2080,12 +2073,11 @@ func ListIAMOIDCProviders(cloud fi.Cloud, vpcID, clusterName string) ([]*resourc
}
resp, err := c.IAM().GetOpenIDConnectProvider(ctx, descReq)
if err != nil {
var nse *iamtypes.NoSuchEntityException
if errors.As(err, &nse) {
klog.Warningf("could not find IAM OIDC Provider %q. Resource may already have been deleted: %v", aws.StringValue(arn), nse)
if awsup.IsIAMNoSuchEntityException(err) {
klog.Warningf("could not find IAM OIDC Provider %q. Resource may already have been deleted: %v", aws.StringValue(arn), err)
continue
} else if awserror, ok := err.(smithy.APIError); ok && awserror.ErrorCode() == "403" {
klog.Warningf("failed to determine ownership of %q: %v", aws.StringValue(arn), awserror)
} else if awsup.AWSErrorCode(err) == "403" {
klog.Warningf("failed to determine ownership of %q: %v", aws.StringValue(arn), err)
continue
}
return nil, fmt.Errorf("error getting IAM OIDC Provider %q: %w", aws.StringValue(arn), err)
Expand Down Expand Up @@ -2123,8 +2115,7 @@ func DeleteIAMOIDCProvider(cloud fi.Cloud, r *resources.Resource) error {
}
_, err := c.IAM().DeleteOpenIDConnectProvider(ctx, request)
if err != nil {
var nse *iamtypes.NoSuchEntityException
if errors.As(err, &nse) {
if awsup.IsIAMNoSuchEntityException(err) {
klog.V(2).Infof("Got NoSuchEntity deleting IAM OIDC Provider %v; will treat as already-deleted", arn)
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion upup/pkg/fi/cloudup/apply_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func (c *ApplyClusterCmd) Run(ctx context.Context) error {
{
awsCloud := cloud.(awsup.AWSCloud)

accountID, partition, err := awsCloud.AccountInfo()
accountID, partition, err := awsCloud.AccountInfo(ctx)
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions upup/pkg/fi/cloudup/awstasks/iaminstanceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package awstasks

import (
"context"
"errors"
"fmt"

"k8s.io/kops/upup/pkg/fi"
Expand Down Expand Up @@ -55,8 +54,7 @@ func findIAMInstanceProfile(ctx context.Context, cloud awsup.AWSCloud, name stri
request := &iam.GetInstanceProfileInput{InstanceProfileName: aws.String(name)}

response, err := cloud.IAM().GetInstanceProfile(ctx, request)
var nse *iamtypes.NoSuchEntityException
if errors.As(err, &nse) {
if awsup.IsIAMNoSuchEntityException(err) {
return nil, nil
}

Expand Down
5 changes: 1 addition & 4 deletions upup/pkg/fi/cloudup/awstasks/iaminstanceprofilerole.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ package awstasks

import (
"context"
"errors"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/iam"
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
"k8s.io/klog/v2"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
Expand Down Expand Up @@ -53,8 +51,7 @@ func (e *IAMInstanceProfileRole) Find(c *fi.CloudupContext) (*IAMInstanceProfile
request := &iam.GetInstanceProfileInput{InstanceProfileName: e.InstanceProfile.Name}

response, err := cloud.IAM().GetInstanceProfile(ctx, request)
var nse *iamtypes.NoSuchEntityException
if errors.As(err, &nse) {
if awsup.IsIAMNoSuchEntityException(err) {
return nil, nil
}

Expand Down
14 changes: 4 additions & 10 deletions upup/pkg/fi/cloudup/awstasks/iamrole.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package awstasks
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"reflect"
Expand Down Expand Up @@ -69,8 +68,7 @@ func (e *IAMRole) Find(c *fi.CloudupContext) (*IAMRole, error) {
request := &iam.GetRoleInput{RoleName: e.Name}

response, err := cloud.IAM().GetRole(ctx, request)
var nse *iamtypes.NoSuchEntityException
if errors.As(err, &nse) {
if awsup.IsIAMNoSuchEntityException(err) {
return nil, nil
}
if err != nil {
Expand Down Expand Up @@ -171,16 +169,13 @@ func (_ *IAMRole) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *IAMRole) error
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
var nse *iamtypes.NoSuchEntityException
if errors.As(err, &nse) {
if awsup.IsIAMNoSuchEntityException(err) {
klog.V(2).Infof("Got NoSuchEntity describing IAM RolePolicy; will treat as already-deleted")
return nil
}
return fmt.Errorf("error listing IAM role policies: %v", err)
}
for _, policy := range page.PolicyNames {
policyNames = append(policyNames, policy)
}
policyNames = append(policyNames, page.PolicyNames...)
}
}

Expand All @@ -193,8 +188,7 @@ func (_ *IAMRole) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *IAMRole) error
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
var nse *iamtypes.NoSuchEntityException
if errors.As(err, &nse) {
if awsup.IsIAMNoSuchEntityException(err) {
klog.V(2).Infof("Got NoSuchEntity describing IAM RolePolicy; will treat as already-deleted")
return nil
}
Expand Down
Loading
Loading