From 1e8b02e4f64018685957082aa9c5c1b59c4e9260 Mon Sep 17 00:00:00 2001 From: bmurray Date: Mon, 18 Mar 2024 13:10:02 -0700 Subject: [PATCH] adding logic to remove old instance templates as they're no longer needed. --- .../services/compute/instancegroups/client.go | 6 +++ .../compute/instancegroups/instancegroups.go | 47 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/cloud/services/compute/instancegroups/client.go b/cloud/services/compute/instancegroups/client.go index 041dd3972..704f089d1 100644 --- a/cloud/services/compute/instancegroups/client.go +++ b/cloud/services/compute/instancegroups/client.go @@ -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 @@ -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() diff --git a/cloud/services/compute/instancegroups/instancegroups.go b/cloud/services/compute/instancegroups/instancegroups.go index c1622cd86..d6fb0e005 100644 --- a/cloud/services/compute/instancegroups/instancegroups.go +++ b/cloud/services/compute/instancegroups/instancegroups.go @@ -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. @@ -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)