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

Instance template management #7

Merged
merged 1 commit into from
Mar 19, 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
6 changes: 6 additions & 0 deletions cloud/services/compute/instancegroups/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Client interface {
ListInstanceGroupInstances(ctx context.Context, project, zone, name string) (*compute.InstanceGroupManagersListManagedInstancesResponse, error)
// InstanceGroupTemplate Interfaces
GetInstanceTemplate(ctx context.Context, project, name string) (*compute.InstanceTemplate, error)
ListInstanceTemplates(ctx context.Context, project string) (*compute.InstanceTemplateList, error)
CreateInstanceTemplate(ctx context.Context, project string, instanceTemplate *compute.InstanceTemplate) (*compute.Operation, error)
DeleteInstanceTemplate(ctx context.Context, project, name string) (*compute.Operation, error)
WaitUntilOperationCompleted(project, operation string) error
Expand Down Expand Up @@ -98,6 +99,11 @@ func (c *GCPClient) GetInstanceTemplate(_ context.Context, project, name string)
return c.service.InstanceTemplates.Get(project, name).Do()
}

// ListInstanceTemplates returns a list of instance templates in a project.
func (c *GCPClient) ListInstanceTemplates(_ context.Context, project string) (*compute.InstanceTemplateList, error) {
return c.service.InstanceTemplates.List(project).Do()
}

// CreateInstanceTemplate creates a new instance template in a project.
func (c *GCPClient) CreateInstanceTemplate(_ context.Context, project string, instanceTemplate *compute.InstanceTemplate) (*compute.Operation, error) {
return c.service.InstanceTemplates.Insert(project, instanceTemplate).Do()
Expand Down
47 changes: 47 additions & 0 deletions cloud/services/compute/instancegroups/instancegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func (s *Service) Reconcile(ctx context.Context) (ctrl.Result, error) {
log.Error(err, "Error updating instance group")
return ctrl.Result{}, err
}
err = s.removeOldInstanceTemplate(ctx, instanceTemplateName)
if err != nil {
log.Error(err, "Error removing old instance templates")
return ctrl.Result{}, err
}
}

// Re-get the instance group after updating it. This is needed to get the latest status.
Expand Down Expand Up @@ -234,6 +239,48 @@ func (s *Service) patchInstanceGroup(ctx context.Context, instanceTemplateName s
return nil
}

// removeOldInstanceTemplate removes the old instance templates.
func (s *Service) removeOldInstanceTemplate(ctx context.Context, instanceTemplateName string) error {
log := log.FromContext(ctx)
log.Info("Starting to remove old instance templates", "templateName", instanceTemplateName)

// List all instance templates.
instanceTemplates, err := s.Client.ListInstanceTemplates(ctx, s.scope.Project())
if err != nil {
log.Error(err, "Error listing instance templates")
return err
}

// Prepare to identify instance templates to remove.
lastIndex := strings.LastIndex(instanceTemplateName, "-")
if lastIndex == -1 {
log.Error(fmt.Errorf("invalid instance template name format"), "Invalid template name", "templateName", instanceTemplateName)
return fmt.Errorf("invalid instance template name format: %s", instanceTemplateName)
}

trimmedInstanceTemplateName := instanceTemplateName[:lastIndex]
var errors []error

for _, instanceTemplate := range instanceTemplates.Items {
if strings.HasPrefix(instanceTemplate.Name, trimmedInstanceTemplateName) && instanceTemplate.Name != instanceTemplateName {
log.Info("Deleting instance template", "templateName", instanceTemplate.Name)
_, err := s.Client.DeleteInstanceTemplate(ctx, s.scope.Project(), instanceTemplate.Name)
if err != nil {
log.Error(err, "Error deleting instance template", "templateName", instanceTemplate.Name)
errors = append(errors, err)
continue // Proceed to next template instead of returning immediately.
}
}
}

// Aggregate errors (if any).
if len(errors) > 0 {
return fmt.Errorf("encountered errors during deletion: %v", errors)
}

return nil
}

// Delete deletes the instance group.
func (s *Service) Delete(ctx context.Context) (ctrl.Result, error) {
log := log.FromContext(ctx)
Expand Down
Loading