Skip to content

Commit

Permalink
adding logic to remove old instance templates as they're no longer ne…
Browse files Browse the repository at this point in the history
…eded.
  • Loading branch information
BrennenMM7 committed Mar 18, 2024
1 parent 1f45913 commit 1e8b02e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
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

0 comments on commit 1e8b02e

Please sign in to comment.