github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/internal/packager/helm/jackal.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package helm contains operations for working with helm charts.
     5  package helm
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/Racer159/jackal/src/pkg/cluster"
    11  	"github.com/Racer159/jackal/src/pkg/k8s"
    12  	"github.com/Racer159/jackal/src/pkg/message"
    13  	"github.com/Racer159/jackal/src/pkg/transform"
    14  	"github.com/Racer159/jackal/src/pkg/utils"
    15  	"github.com/Racer159/jackal/src/types"
    16  	"helm.sh/helm/v3/pkg/action"
    17  )
    18  
    19  // UpdateJackalRegistryValues updates the Jackal registry deployment with the new state values
    20  func (h *Helm) UpdateJackalRegistryValues() error {
    21  	pushUser, err := utils.GetHtpasswdString(h.cfg.State.RegistryInfo.PushUsername, h.cfg.State.RegistryInfo.PushPassword)
    22  	if err != nil {
    23  		return fmt.Errorf("error generating htpasswd string: %w", err)
    24  	}
    25  
    26  	pullUser, err := utils.GetHtpasswdString(h.cfg.State.RegistryInfo.PullUsername, h.cfg.State.RegistryInfo.PullPassword)
    27  	if err != nil {
    28  		return fmt.Errorf("error generating htpasswd string: %w", err)
    29  	}
    30  
    31  	registryValues := map[string]interface{}{
    32  		"secrets": map[string]interface{}{
    33  			"htpasswd": fmt.Sprintf("%s\n%s", pushUser, pullUser),
    34  		},
    35  	}
    36  
    37  	h.chart = types.JackalChart{
    38  		Namespace:   "jackal",
    39  		ReleaseName: "jackal-docker-registry",
    40  	}
    41  
    42  	err = h.UpdateReleaseValues(registryValues)
    43  	if err != nil {
    44  		return fmt.Errorf("error updating the release values: %w", err)
    45  	}
    46  
    47  	return nil
    48  }
    49  
    50  // UpdateJackalAgentValues updates the Jackal agent deployment with the new state values
    51  func (h *Helm) UpdateJackalAgentValues() error {
    52  	spinner := message.NewProgressSpinner("Gathering information to update Jackal Agent TLS")
    53  	defer spinner.Stop()
    54  
    55  	err := h.createActionConfig(cluster.JackalNamespaceName, spinner)
    56  	if err != nil {
    57  		return fmt.Errorf("unable to initialize the K8s client: %w", err)
    58  	}
    59  
    60  	// Get the current agent image from one of its pods.
    61  	pods := h.cluster.WaitForPodsAndContainers(k8s.PodLookup{
    62  		Namespace: cluster.JackalNamespaceName,
    63  		Selector:  "app=agent-hook",
    64  	}, nil)
    65  
    66  	var currentAgentImage transform.Image
    67  	if len(pods) > 0 && len(pods[0].Spec.Containers) > 0 {
    68  		currentAgentImage, err = transform.ParseImageRef(pods[0].Spec.Containers[0].Image)
    69  		if err != nil {
    70  			return fmt.Errorf("unable to parse current agent image reference: %w", err)
    71  		}
    72  	} else {
    73  		return fmt.Errorf("unable to get current agent pod")
    74  	}
    75  
    76  	// List the releases to find the current agent release name.
    77  	listClient := action.NewList(h.actionConfig)
    78  
    79  	releases, err := listClient.Run()
    80  	if err != nil {
    81  		return fmt.Errorf("unable to list helm releases: %w", err)
    82  	}
    83  
    84  	spinner.Success()
    85  
    86  	for _, release := range releases {
    87  		// Update the Jackal Agent release with the new values
    88  		if release.Chart.Name() == "raw-init-jackal-agent-jackal-agent" {
    89  			h.chart = types.JackalChart{
    90  				Namespace:   "jackal",
    91  				ReleaseName: release.Name,
    92  			}
    93  			h.component = types.JackalComponent{
    94  				Name: "jackal-agent",
    95  			}
    96  			h.cfg.Pkg.Constants = []types.JackalPackageConstant{
    97  				{
    98  					Name:  "AGENT_IMAGE",
    99  					Value: currentAgentImage.Path,
   100  				},
   101  				{
   102  					Name:  "AGENT_IMAGE_TAG",
   103  					Value: currentAgentImage.Tag,
   104  				},
   105  			}
   106  
   107  			err := h.UpdateReleaseValues(map[string]interface{}{})
   108  			if err != nil {
   109  				return fmt.Errorf("error updating the release values: %w", err)
   110  			}
   111  		}
   112  	}
   113  
   114  	spinner = message.NewProgressSpinner("Cleaning up Jackal Agent pods after update")
   115  	defer spinner.Stop()
   116  
   117  	// Force pods to be recreated to get the updated secret.
   118  	err = h.cluster.DeletePods(k8s.PodLookup{
   119  		Namespace: cluster.JackalNamespaceName,
   120  		Selector:  "app=agent-hook",
   121  	})
   122  	if err != nil {
   123  		return fmt.Errorf("error recycling pods for the Jackal Agent: %w", err)
   124  	}
   125  
   126  	spinner.Success()
   127  
   128  	return nil
   129  }