upup/pkg/fi/cloudup/ upup/pkg/fi/nodeup/ hack/.staticcheck_failures : simplify code and remove code

This commit is contained in:
tanjunchen 2019-12-18 09:47:58 +08:00
parent 252a733415
commit d5fef40863
7 changed files with 95 additions and 134 deletions

View File

@ -19,20 +19,13 @@ pkg/resources/ali
pkg/resources/aws pkg/resources/aws
pkg/resources/digitalocean pkg/resources/digitalocean
pkg/resources/digitalocean/dns pkg/resources/digitalocean/dns
pkg/resources/gce
pkg/resources/openstack pkg/resources/openstack
pkg/sshcredentials pkg/sshcredentials
protokube/pkg/gossip/dns/hosts protokube/pkg/gossip/dns/hosts
protokube/pkg/gossip/mesh protokube/pkg/gossip/mesh
protokube/pkg/protokube protokube/pkg/protokube
upup/pkg/fi upup/pkg/fi
upup/pkg/fi/assettasks
upup/pkg/fi/cloudup upup/pkg/fi/cloudup
upup/pkg/fi/cloudup/awstasks 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/pkg/kutil
upup/tools/generators/pkg/codegen upup/tools/generators/pkg/codegen

View File

@ -48,13 +48,6 @@ func (s *SecurityGroupRule) CompareWithID() *string {
return s.Name 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) { func (s *SecurityGroupRule) Find(c *fi.Context) (*SecurityGroupRule, error) {
if s.SecurityGroup == nil || s.SecurityGroup.SecurityGroupId == nil { if s.SecurityGroup == nil || s.SecurityGroup.SecurityGroupId == nil {
klog.V(4).Infof("SecurityGroup / SecurityGroupId not found for %s, skipping Find", fi.StringValue(s.Name)) klog.V(4).Infof("SecurityGroup / SecurityGroupId not found for %s, skipping Find", fi.StringValue(s.Name))
@ -89,7 +82,7 @@ func (s *SecurityGroupRule) Find(c *fi.Context) (*SecurityGroupRule, error) {
actual := &SecurityGroupRule{} actual := &SecurityGroupRule{}
// Find securityGroupRule with specified ipProtocol, securityGroupId,SourceGroupId // Find securityGroupRule with specified ipProtocol, securityGroupId,SourceGroupId
for _, securityGroupRule := range describeResponse.Permissions.Permission { 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 continue
} }
if s.SourceGroup != nil && securityGroupRule.SourceGroupId != fi.StringValue(s.SourceGroup.SecurityGroupId) { if s.SourceGroup != nil && securityGroupRule.SourceGroupId != fi.StringValue(s.SourceGroup.SecurityGroupId) {

View File

@ -707,7 +707,8 @@ var tagsEventualConsistencyErrors = map[string]bool{
"InvalidInternetGatewayID.NotFound": true, "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 { func isTagsEventualConsistencyError(err error) bool {
if awsErr, ok := err.(awserr.Error); ok { if awsErr, ok := err.(awserr.Error); ok {
isEventualConsistency, found := tagsEventualConsistencyErrors[awsErr.Code()] isEventualConsistency, found := tagsEventualConsistencyErrors[awsErr.Code()]
@ -720,21 +721,22 @@ func isTagsEventualConsistencyError(err error) bool {
return false 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) { func (c *awsCloudImplementation) GetTags(resourceID string) (map[string]string, error) {
return getTags(c, resourceID) return getTags(c, resourceID)
} }
func getTags(c AWSCloud, resourceId string) (map[string]string, error) { func getTags(c AWSCloud, resourceID string) (map[string]string, error) {
if resourceId == "" { if resourceID == "" {
return nil, fmt.Errorf("resourceId not provided to getTags") return nil, fmt.Errorf("resourceID not provided to getTags")
} }
tags := map[string]string{} tags := map[string]string{}
request := &ec2.DescribeTagsInput{ request := &ec2.DescribeTagsInput{
Filters: []*ec2.Filter{ Filters: []*ec2.Filter{
NewEC2Filter("resource-id", resourceId), NewEC2Filter("resource-id", resourceID),
}, },
} }
@ -746,19 +748,19 @@ func getTags(c AWSCloud, resourceId string) (map[string]string, error) {
if err != nil { if err != nil {
if isTagsEventualConsistencyError(err) { if isTagsEventualConsistencyError(err) {
if attempt > DescribeTagsMaxAttempts { 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 { 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) time.Sleep(DescribeTagsRetryInterval)
continue 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 { for _, tag := range response.Tags {
@ -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 // 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 { func (c *awsCloudImplementation) CreateTags(resourceID string, tags map[string]string) error {
return createTags(c, resourceId, tags) 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 { if len(tags) == 0 {
return nil return nil
} }
@ -794,38 +796,39 @@ func createTags(c AWSCloud, resourceId string, tags map[string]string) error {
request := &ec2.CreateTagsInput{ request := &ec2.CreateTagsInput{
Tags: ec2Tags, Tags: ec2Tags,
Resources: []*string{&resourceId}, Resources: []*string{&resourceID},
} }
_, err := c.EC2().CreateTags(request) _, err := c.EC2().CreateTags(request)
if err != nil { if err != nil {
if isTagsEventualConsistencyError(err) { if isTagsEventualConsistencyError(err) {
if attempt > CreateTagsMaxAttempts { 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 { 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) time.Sleep(CreateTagsRetryInterval)
continue 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 return nil
} }
} }
// DeleteTags will remove tags from the specified resource, retrying up to MaxCreateTagsAttempts times if it hits an eventual-consistency type error // DeleteTags will remove tags from the specified resource,
func (c *awsCloudImplementation) DeleteTags(resourceId string, tags map[string]string) error { // retrying up to MaxCreateTagsAttempts times if it hits an eventual-consistency type error
return deleteTags(c, resourceId, tags) 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 { if len(tags) == 0 {
return nil return nil
} }
@ -841,26 +844,26 @@ func deleteTags(c AWSCloud, resourceId string, tags map[string]string) error {
request := &ec2.DeleteTagsInput{ request := &ec2.DeleteTagsInput{
Tags: ec2Tags, Tags: ec2Tags,
Resources: []*string{&resourceId}, Resources: []*string{&resourceID},
} }
_, err := c.EC2().DeleteTags(request) _, err := c.EC2().DeleteTags(request)
if err != nil { if err != nil {
if isTagsEventualConsistencyError(err) { if isTagsEventualConsistencyError(err) {
if attempt > DeleteTagsMaxAttempts { 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 { 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) time.Sleep(DeleteTagsRetryInterval)
continue 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 return nil
@ -908,27 +911,21 @@ func getELBTags(c AWSCloud, loadBalancerName string) (map[string]string, error)
request := &elb.DescribeTagsInput{ request := &elb.DescribeTagsInput{
LoadBalancerNames: []*string{&loadBalancerName}, LoadBalancerNames: []*string{&loadBalancerName},
} }
response, err := c.ELB().DescribeTags(request)
attempt := 0 if err != nil {
for { return nil, fmt.Errorf("error listing tags on %v: %v", loadBalancerName, err)
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)
}
}
return tags, nil
} }
for _, tagset := range response.TagDescriptions {
for _, tag := range tagset.Tags {
tags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
}
}
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 { func (c *awsCloudImplementation) CreateELBTags(loadBalancerName string, tags map[string]string) error {
return createELBTags(c, loadBalancerName, tags) return createELBTags(c, loadBalancerName, tags)
} }
@ -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)}) elbTags = append(elbTags, &elb.Tag{Key: aws.String(k), Value: aws.String(v)})
} }
attempt := 0 request := &elb.AddTagsInput{
for { Tags: elbTags,
attempt++ LoadBalancerNames: []*string{&loadBalancerName},
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)
}
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 // RemoveELBTags will remove tags to the specified loadBalancer, retrying up to MaxCreateTagsAttempts times if it hits an eventual-consistency type error
@ -976,22 +968,17 @@ func removeELBTags(c AWSCloud, loadBalancerName string, tags map[string]string)
elbTagKeysOnly = append(elbTagKeysOnly, &elb.TagKeyOnly{Key: aws.String(k)}) elbTagKeysOnly = append(elbTagKeysOnly, &elb.TagKeyOnly{Key: aws.String(k)})
} }
attempt := 0 request := &elb.RemoveTagsInput{
for { Tags: elbTagKeysOnly,
attempt++ LoadBalancerNames: []*string{&loadBalancerName},
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)
}
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) { func (c *awsCloudImplementation) GetELBV2Tags(ResourceArn string) (map[string]string, error) {
@ -1004,24 +991,18 @@ func getELBV2Tags(c AWSCloud, ResourceArn string) (map[string]string, error) {
request := &elbv2.DescribeTagsInput{ request := &elbv2.DescribeTagsInput{
ResourceArns: []*string{&ResourceArn}, ResourceArns: []*string{&ResourceArn},
} }
response, err := c.ELBV2().DescribeTags(request)
attempt := 0 if err != nil {
for { return nil, fmt.Errorf("error listing tags on %v: %v", ResourceArn, err)
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)
}
}
return tags, nil
} }
for _, tagset := range response.TagDescriptions {
for _, tag := range tagset.Tags {
tags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
}
}
return tags, nil
} }
func (c *awsCloudImplementation) CreateELBV2Tags(ResourceArn string, tags map[string]string) error { func (c *awsCloudImplementation) CreateELBV2Tags(ResourceArn string, tags map[string]string) error {
@ -1037,23 +1018,17 @@ func createELBV2Tags(c AWSCloud, ResourceArn string, tags map[string]string) err
for k, v := range tags { for k, v := range tags {
elbv2Tags = append(elbv2Tags, &elbv2.Tag{Key: aws.String(k), Value: aws.String(v)}) elbv2Tags = append(elbv2Tags, &elbv2.Tag{Key: aws.String(k), Value: aws.String(v)})
} }
request := &elbv2.AddTagsInput{
attempt := 0 Tags: elbv2Tags,
for { ResourceArns: []*string{&ResourceArn},
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 { func (c *awsCloudImplementation) BuildTags(name *string) map[string]string {

View File

@ -147,10 +147,10 @@ func (_ *StorageBucketIam) RenderGCE(t *gce.GCEAPITarget, a, e, changes *Storage
return nil return nil
} }
type terraformStorageBucketIam struct { // type terraformStorageBucketIam struct {
Bucket string `json:"bucket,omitempty"` // Bucket string `json:"bucket,omitempty"`
RoleEntity []string `json:"role_entity,omitempty"` // RoleEntity []string `json:"role_entity,omitempty"`
} // }
func (_ *StorageBucketIam) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *StorageBucketIam) error { func (_ *StorageBucketIam) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *StorageBucketIam) error {
//var roleEntities []string //var roleEntities []string

View File

@ -172,7 +172,7 @@ func (_ *Subnet) RenderGCE(t *gce.GCEAPITarget, a, e, changes *Subnet) error {
return fmt.Errorf("error patching Subnet: %v", err) return fmt.Errorf("error patching Subnet: %v", err)
} }
patch = false 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 { if err != nil {
return fmt.Errorf("error fetching subnet for patch: %v", err) return fmt.Errorf("error fetching subnet for patch: %v", err)
} }

View File

@ -66,10 +66,11 @@ func NewLBListenerTaskFromCloud(cloud openstack.OpenstackCloud, lifecycle *fi.Li
poolTask, err := NewLBPoolTaskFromCloud(cloud, lifecycle, &pool, find.Pool) poolTask, err := NewLBPoolTaskFromCloud(cloud, lifecycle, &pool, find.Pool)
if err != nil { if err != nil {
return nil, fmt.Errorf("NewLBListenerTaskFromCloud: Failed to create new LBListener task for pool %s: %v", pool.Name, err) return nil, fmt.Errorf("NewLBListenerTaskFromCloud: Failed to create new LBListener task for pool %s: %v", pool.Name, err)
} else {
listenerTask.Pool = poolTask
// TODO: Support Multiple?
break
} }
listenerTask.Pool = poolTask
// TODO: Support Multiple?
break
} }
if find != nil { if find != nil {
// Update all search terms // Update all search terms

View File

@ -37,9 +37,8 @@ import (
type Loader struct { type Loader struct {
Builders []fi.ModelBuilder Builders []fi.ModelBuilder
templates []*template.Template config *nodeup.Config
config *nodeup.Config cluster *api.Cluster
cluster *api.Cluster
assets *fi.AssetStore assets *fi.AssetStore
tasks map[string]fi.Task tasks map[string]fi.Task