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

Refactoring to use cloud method for getting cloud groups #3446

Merged
merged 5 commits into from
Sep 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 4 additions & 38 deletions pkg/resources/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,52 +130,18 @@ type clusterDiscoveryGCE struct {
zones []string
}

// findInstanceTemplates finds all instance templates that are associated with the current cluster
// It matches them by looking for instance metadata with key='cluster-name' and value of our cluster name
func (d *clusterDiscoveryGCE) findInstanceTemplates() ([]*compute.InstanceTemplate, error) {
if d.instanceTemplates != nil {
return d.instanceTemplates, nil
}

c := d.gceCloud

//clusterTag := gce.SafeClusterName(strings.TrimSpace(d.clusterName))

findClusterName := strings.TrimSpace(d.clusterName)

var matches []*compute.InstanceTemplate

ctx := context.Background()

err := c.Compute().InstanceTemplates.List(c.Project()).Pages(ctx, func(page *compute.InstanceTemplateList) error {
for _, t := range page.Items {
match := false
for _, item := range t.Properties.Metadata.Items {
if item.Key == "cluster-name" {
if strings.TrimSpace(item.Value) == findClusterName {
match = true
} else {
match = false
break
}
}
}

if !match {
continue
}

matches = append(matches, t)
}
return nil
})
instanceTemplates, err := d.gceCloud.FindInstanceTemplates(d.clusterName)
if err != nil {
return nil, fmt.Errorf("error listing instance groups: %v", err)
return nil, fmt.Errorf("unable to find intance templates: %v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably just return nil, err because one assumes that FindInstanceTemplates already wrapped the error, but NBD

}

d.instanceTemplates = matches

return matches, nil
d.instanceTemplates = instanceTemplates
return d.instanceTemplates, nil
}

func (d *clusterDiscoveryGCE) listGCEInstanceTemplates() ([]*tracker.Resource, error) {
Expand Down
7 changes: 4 additions & 3 deletions upup/pkg/fi/cloudup/gce/gce_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ type GCECloud interface {
// FindClusterStatus gets the status of the cluster as it exists in GCE, inferred from volumes
FindClusterStatus(cluster *kops.Cluster) (*kops.ClusterStatus, error)

// FindInstanceTemplates finds all instance templates that are associated with the current cluster
// It matches them by looking for instance metadata with key='cluster-name' and value of our cluster name
FindInstanceTemplates(clusterName string) ([]*compute.InstanceTemplate, error)

Zones() ([]string, error)
}

Expand Down Expand Up @@ -239,11 +243,8 @@ func (c *gceCloudImplementation) GetCloudGroups(cluster *kops.Cluster, instanceg
// FindInstanceTemplates finds all instance templates that are associated with the current cluster
// It matches them by looking for instance metadata with key='cluster-name' and value of our cluster name
func (c *gceCloudImplementation) FindInstanceTemplates(clusterName string) ([]*compute.InstanceTemplate, error) {

findClusterName := strings.TrimSpace(clusterName)

var matches []*compute.InstanceTemplate

ctx := context.Background()

err := c.Compute().InstanceTemplates.List(c.Project()).Pages(ctx, func(page *compute.InstanceTemplateList) error {
Expand Down
7 changes: 7 additions & 0 deletions upup/pkg/fi/cloudup/gce/mock_gce_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func (c *mockGCECloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*k
return nil, fmt.Errorf("mockGCECloud cloud provider does not support getting cloud groups at this time")
}

// FindInstanceTemplates finds all instance templates that are associated with the current cluster
// It matches them by looking for instance metadata with key='cluster-name' and value of our cluster name
func (c *mockGCECloud) FindInstanceTemplates(clusterName string) ([]*compute.InstanceTemplate, error) {
glog.V(8).Infof("mockGCECloud cloud provider FindInstanceTemplates not implemented yet")
return nil, fmt.Errorf("mockGCECloud cloud provider does not support finding instance templates at this time")
}

// DeleteGroup is not implemented yet
func (c *mockGCECloud) DeleteGroup(name string, template string) error {
glog.V(8).Infof("mockGCECloud cloud provider DeleteGroup not implemented yet")
Expand Down