Skip to content

Commit

Permalink
Merge pull request #8118 from tanjunchen/fix-staticcheck-001
Browse files Browse the repository at this point in the history
upup/pkg/fi/ upup/pkg/kutil : simplify code and remove code
  • Loading branch information
k8s-ci-robot committed Dec 20, 2019
2 parents 4621df3 + d5fef40 commit 6856358
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 127 deletions.
7 changes: 0 additions & 7 deletions hack/.staticcheck_failures
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,13 @@ pkg/resources/ali
pkg/resources/aws
pkg/resources/digitalocean
pkg/resources/digitalocean/dns
pkg/resources/gce
pkg/resources/openstack
pkg/sshcredentials
protokube/pkg/gossip/dns/hosts
protokube/pkg/gossip/mesh
protokube/pkg/protokube
upup/pkg/fi
upup/pkg/fi/assettasks
upup/pkg/fi/cloudup
upup/pkg/fi/cloudup/awstasks
upup/pkg/fi/cloudup/awsup
upup/pkg/fi/cloudup/gcetasks
upup/pkg/fi/cloudup/openstack
upup/pkg/fi/cloudup/openstacktasks
upup/pkg/fi/nodeup
upup/pkg/kutil
upup/tools/generators/pkg/codegen
9 changes: 1 addition & 8 deletions upup/pkg/fi/cloudup/alitasks/securitygrouprule.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ func (s *SecurityGroupRule) CompareWithID() *string {
return s.Name
}

func equalsIgnoreCase(a, b string) bool {
if strings.ToLower(a) == strings.ToLower(b) {
return true
}
return false
}

func (s *SecurityGroupRule) Find(c *fi.Context) (*SecurityGroupRule, error) {
if s.SecurityGroup == nil || s.SecurityGroup.SecurityGroupId == nil {
klog.V(4).Infof("SecurityGroup / SecurityGroupId not found for %s, skipping Find", fi.StringValue(s.Name))
Expand Down Expand Up @@ -89,7 +82,7 @@ func (s *SecurityGroupRule) Find(c *fi.Context) (*SecurityGroupRule, error) {
actual := &SecurityGroupRule{}
// Find securityGroupRule with specified ipProtocol, securityGroupId,SourceGroupId
for _, securityGroupRule := range describeResponse.Permissions.Permission {
if !equalsIgnoreCase(string(securityGroupRule.IpProtocol), fi.StringValue(s.IpProtocol)) {
if !strings.EqualFold(string(securityGroupRule.IpProtocol), fi.StringValue(s.IpProtocol)) {
continue
}
if s.SourceGroup != nil && securityGroupRule.SourceGroupId != fi.StringValue(s.SourceGroup.SecurityGroupId) {
Expand Down
177 changes: 76 additions & 101 deletions upup/pkg/fi/cloudup/awsup/aws_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,8 @@ var tagsEventualConsistencyErrors = map[string]bool{
"InvalidInternetGatewayID.NotFound": true,
}

// isTagsEventualConsistencyError checks if the error is one of the errors encountered when we try to create/get tags before the resource has fully 'propagated' in EC2
// isTagsEventualConsistencyError checks if the error is one of the errors encountered
// when we try to create/get tags before the resource has fully 'propagated' in EC2
func isTagsEventualConsistencyError(err error) bool {
if awsErr, ok := err.(awserr.Error); ok {
isEventualConsistency, found := tagsEventualConsistencyErrors[awsErr.Code()]
Expand All @@ -720,21 +721,22 @@ func isTagsEventualConsistencyError(err error) bool {
return false
}

// GetTags will fetch the tags for the specified resource, retrying (up to MaxDescribeTagsAttempts) if it hits an eventual-consistency type error
// GetTags will fetch the tags for the specified resource,
// retrying (up to MaxDescribeTagsAttempts) if it hits an eventual-consistency type error
func (c *awsCloudImplementation) GetTags(resourceID string) (map[string]string, error) {
return getTags(c, resourceID)
}

func getTags(c AWSCloud, resourceId string) (map[string]string, error) {
if resourceId == "" {
return nil, fmt.Errorf("resourceId not provided to getTags")
func getTags(c AWSCloud, resourceID string) (map[string]string, error) {
if resourceID == "" {
return nil, fmt.Errorf("resourceID not provided to getTags")
}

tags := map[string]string{}

request := &ec2.DescribeTagsInput{
Filters: []*ec2.Filter{
NewEC2Filter("resource-id", resourceId),
NewEC2Filter("resource-id", resourceID),
},
}

Expand All @@ -746,19 +748,19 @@ func getTags(c AWSCloud, resourceId string) (map[string]string, error) {
if err != nil {
if isTagsEventualConsistencyError(err) {
if attempt > DescribeTagsMaxAttempts {
return nil, fmt.Errorf("Got retryable error while getting tags on %q, but retried too many times without success: %v", resourceId, err)
return nil, fmt.Errorf("Got retryable error while getting tags on %q, but retried too many times without success: %v", resourceID, err)
}

if (attempt % DescribeTagsLogInterval) == 0 {
klog.Infof("waiting for eventual consistency while describing tags on %q", resourceId)
klog.Infof("waiting for eventual consistency while describing tags on %q", resourceID)
}

klog.V(2).Infof("will retry after encountering error getting tags on %q: %v", resourceId, err)
klog.V(2).Infof("will retry after encountering error getting tags on %q: %v", resourceID, err)
time.Sleep(DescribeTagsRetryInterval)
continue
}

return nil, fmt.Errorf("error listing tags on %v: %v", resourceId, err)
return nil, fmt.Errorf("error listing tags on %v: %v", resourceID, err)
}

for _, tag := range response.Tags {
Expand All @@ -774,11 +776,11 @@ func getTags(c AWSCloud, resourceId string) (map[string]string, error) {
}

// CreateTags will add tags to the specified resource, retrying up to MaxCreateTagsAttempts times if it hits an eventual-consistency type error
func (c *awsCloudImplementation) CreateTags(resourceId string, tags map[string]string) error {
return createTags(c, resourceId, tags)
func (c *awsCloudImplementation) CreateTags(resourceID string, tags map[string]string) error {
return createTags(c, resourceID, tags)
}

func createTags(c AWSCloud, resourceId string, tags map[string]string) error {
func createTags(c AWSCloud, resourceID string, tags map[string]string) error {
if len(tags) == 0 {
return nil
}
Expand All @@ -794,38 +796,39 @@ func createTags(c AWSCloud, resourceId string, tags map[string]string) error {

request := &ec2.CreateTagsInput{
Tags: ec2Tags,
Resources: []*string{&resourceId},
Resources: []*string{&resourceID},
}

_, err := c.EC2().CreateTags(request)
if err != nil {
if isTagsEventualConsistencyError(err) {
if attempt > CreateTagsMaxAttempts {
return fmt.Errorf("Got retryable error while creating tags on %q, but retried too many times without success: %v", resourceId, err)
return fmt.Errorf("Got retryable error while creating tags on %q, but retried too many times without success: %v", resourceID, err)
}

if (attempt % CreateTagsLogInterval) == 0 {
klog.Infof("waiting for eventual consistency while creating tags on %q", resourceId)
klog.Infof("waiting for eventual consistency while creating tags on %q", resourceID)
}

klog.V(2).Infof("will retry after encountering error creating tags on %q: %v", resourceId, err)
klog.V(2).Infof("will retry after encountering error creating tags on %q: %v", resourceID, err)
time.Sleep(CreateTagsRetryInterval)
continue
}

return fmt.Errorf("error creating tags on %v: %v", resourceId, err)
return fmt.Errorf("error creating tags on %v: %v", resourceID, err)
}

return nil
}
}

// DeleteTags will remove tags from the specified resource, retrying up to MaxCreateTagsAttempts times if it hits an eventual-consistency type error
func (c *awsCloudImplementation) DeleteTags(resourceId string, tags map[string]string) error {
return deleteTags(c, resourceId, tags)
// DeleteTags will remove tags from the specified resource,
// retrying up to MaxCreateTagsAttempts times if it hits an eventual-consistency type error
func (c *awsCloudImplementation) DeleteTags(resourceID string, tags map[string]string) error {
return deleteTags(c, resourceID, tags)
}

func deleteTags(c AWSCloud, resourceId string, tags map[string]string) error {
func deleteTags(c AWSCloud, resourceID string, tags map[string]string) error {
if len(tags) == 0 {
return nil
}
Expand All @@ -841,26 +844,26 @@ func deleteTags(c AWSCloud, resourceId string, tags map[string]string) error {

request := &ec2.DeleteTagsInput{
Tags: ec2Tags,
Resources: []*string{&resourceId},
Resources: []*string{&resourceID},
}

_, err := c.EC2().DeleteTags(request)
if err != nil {
if isTagsEventualConsistencyError(err) {
if attempt > DeleteTagsMaxAttempts {
return fmt.Errorf("Got retryable error while deleting tags on %q, but retried too many times without success: %v", resourceId, err)
return fmt.Errorf("Got retryable error while deleting tags on %q, but retried too many times without success: %v", resourceID, err)
}

if (attempt % DeleteTagsLogInterval) == 0 {
klog.Infof("waiting for eventual consistency while deleting tags on %q", resourceId)
klog.Infof("waiting for eventual consistency while deleting tags on %q", resourceID)
}

klog.V(2).Infof("will retry after encountering error deleting tags on %q: %v", resourceId, err)
klog.V(2).Infof("will retry after encountering error deleting tags on %q: %v", resourceID, err)
time.Sleep(DeleteTagsRetryInterval)
continue
}

return fmt.Errorf("error deleting tags on %v: %v", resourceId, err)
return fmt.Errorf("error deleting tags on %v: %v", resourceID, err)
}

return nil
Expand Down Expand Up @@ -908,27 +911,21 @@ func getELBTags(c AWSCloud, loadBalancerName string) (map[string]string, error)
request := &elb.DescribeTagsInput{
LoadBalancerNames: []*string{&loadBalancerName},
}
response, err := c.ELB().DescribeTags(request)
if err != nil {
return nil, fmt.Errorf("error listing tags on %v: %v", loadBalancerName, err)
}

attempt := 0
for {
attempt++

response, err := c.ELB().DescribeTags(request)
if err != nil {
return nil, fmt.Errorf("error listing tags on %v: %v", loadBalancerName, err)
}

for _, tagset := range response.TagDescriptions {
for _, tag := range tagset.Tags {
tags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
}
for _, tagset := range response.TagDescriptions {
for _, tag := range tagset.Tags {
tags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
}

return tags, nil
}
return tags, nil
}

// CreateELBTags will add tags to the specified loadBalancer, retrying up to MaxCreateTagsAttempts times if it hits an eventual-consistency type error
// CreateELBTags will add tags to the specified loadBalancer,
// retrying up to MaxCreateTagsAttempts times if it hits an eventual-consistency type error
func (c *awsCloudImplementation) CreateELBTags(loadBalancerName string, tags map[string]string) error {
return createELBTags(c, loadBalancerName, tags)
}
Expand All @@ -943,22 +940,17 @@ func createELBTags(c AWSCloud, loadBalancerName string, tags map[string]string)
elbTags = append(elbTags, &elb.Tag{Key: aws.String(k), Value: aws.String(v)})
}

attempt := 0
for {
attempt++

request := &elb.AddTagsInput{
Tags: elbTags,
LoadBalancerNames: []*string{&loadBalancerName},
}

_, err := c.ELB().AddTags(request)
if err != nil {
return fmt.Errorf("error creating tags on %v: %v", loadBalancerName, err)
}
request := &elb.AddTagsInput{
Tags: elbTags,
LoadBalancerNames: []*string{&loadBalancerName},
}

return nil
_, err := c.ELB().AddTags(request)
if err != nil {
return fmt.Errorf("error creating tags on %v: %v", loadBalancerName, err)
}

return nil
}

// RemoveELBTags will remove tags to the specified loadBalancer, retrying up to MaxCreateTagsAttempts times if it hits an eventual-consistency type error
Expand All @@ -976,22 +968,17 @@ func removeELBTags(c AWSCloud, loadBalancerName string, tags map[string]string)
elbTagKeysOnly = append(elbTagKeysOnly, &elb.TagKeyOnly{Key: aws.String(k)})
}

attempt := 0
for {
attempt++

request := &elb.RemoveTagsInput{
Tags: elbTagKeysOnly,
LoadBalancerNames: []*string{&loadBalancerName},
}

_, err := c.ELB().RemoveTags(request)
if err != nil {
return fmt.Errorf("error creating tags on %v: %v", loadBalancerName, err)
}
request := &elb.RemoveTagsInput{
Tags: elbTagKeysOnly,
LoadBalancerNames: []*string{&loadBalancerName},
}

return nil
_, err := c.ELB().RemoveTags(request)
if err != nil {
return fmt.Errorf("error creating tags on %v: %v", loadBalancerName, err)
}

return nil
}

func (c *awsCloudImplementation) GetELBV2Tags(ResourceArn string) (map[string]string, error) {
Expand All @@ -1004,24 +991,18 @@ func getELBV2Tags(c AWSCloud, ResourceArn string) (map[string]string, error) {
request := &elbv2.DescribeTagsInput{
ResourceArns: []*string{&ResourceArn},
}
response, err := c.ELBV2().DescribeTags(request)
if err != nil {
return nil, fmt.Errorf("error listing tags on %v: %v", ResourceArn, err)
}

attempt := 0
for {
attempt++

response, err := c.ELBV2().DescribeTags(request)
if err != nil {
return nil, fmt.Errorf("error listing tags on %v: %v", ResourceArn, err)
}

for _, tagset := range response.TagDescriptions {
for _, tag := range tagset.Tags {
tags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
}
for _, tagset := range response.TagDescriptions {
for _, tag := range tagset.Tags {
tags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
}

return tags, nil
}

return tags, nil
}

func (c *awsCloudImplementation) CreateELBV2Tags(ResourceArn string, tags map[string]string) error {
Expand All @@ -1037,23 +1018,17 @@ func createELBV2Tags(c AWSCloud, ResourceArn string, tags map[string]string) err
for k, v := range tags {
elbv2Tags = append(elbv2Tags, &elbv2.Tag{Key: aws.String(k), Value: aws.String(v)})
}
request := &elbv2.AddTagsInput{
Tags: elbv2Tags,
ResourceArns: []*string{&ResourceArn},
}

attempt := 0
for {
attempt++

request := &elbv2.AddTagsInput{
Tags: elbv2Tags,
ResourceArns: []*string{&ResourceArn},
}

_, err := c.ELBV2().AddTags(request)
if err != nil {
return fmt.Errorf("error creating tags on %v: %v", ResourceArn, err)
}

return nil
_, err := c.ELBV2().AddTags(request)
if err != nil {
return fmt.Errorf("error creating tags on %v: %v", ResourceArn, err)
}

return nil
}

func (c *awsCloudImplementation) BuildTags(name *string) map[string]string {
Expand Down
8 changes: 4 additions & 4 deletions upup/pkg/fi/cloudup/gcetasks/storagebucketiam.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ func (_ *StorageBucketIam) RenderGCE(t *gce.GCEAPITarget, a, e, changes *Storage
return nil
}

type terraformStorageBucketIam struct {
Bucket string `json:"bucket,omitempty"`
RoleEntity []string `json:"role_entity,omitempty"`
}
// type terraformStorageBucketIam struct {
// Bucket string `json:"bucket,omitempty"`
// RoleEntity []string `json:"role_entity,omitempty"`
// }

func (_ *StorageBucketIam) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *StorageBucketIam) error {
//var roleEntities []string
Expand Down
2 changes: 1 addition & 1 deletion upup/pkg/fi/cloudup/gcetasks/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (_ *Subnet) RenderGCE(t *gce.GCEAPITarget, a, e, changes *Subnet) error {
return fmt.Errorf("error patching Subnet: %v", err)
}
patch = false
subnet, err = cloud.Compute().Subnetworks.Get(cloud.Project(), cloud.Region(), *e.GCEName).Do()
_, err = cloud.Compute().Subnetworks.Get(cloud.Project(), cloud.Region(), *e.GCEName).Do()
if err != nil {
return fmt.Errorf("error fetching subnet for patch: %v", err)
}
Expand Down
Loading

0 comments on commit 6856358

Please sign in to comment.