github.com/SAP/jenkins-library@v1.362.0/pkg/versioning/helm.go (about)

     1  package versioning
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/ghodss/yaml"
     8  	"helm.sh/helm/v3/pkg/chart"
     9  )
    10  
    11  // JSONfile defines an artifact using a json file for versioning
    12  type HelmChart struct {
    13  	path             string
    14  	metadata         chart.Metadata
    15  	utils            Utils
    16  	updateAppVersion bool
    17  }
    18  
    19  func (h *HelmChart) init() error {
    20  	if h.utils == nil {
    21  		return fmt.Errorf("no file utils provided")
    22  	}
    23  	if len(h.path) == 0 {
    24  		charts, err := h.utils.Glob("**/Chart.yaml")
    25  		if len(charts) == 0 || err != nil {
    26  			return fmt.Errorf("failed to find a helm chart file")
    27  		}
    28  		// use first chart which can be found
    29  		h.path = charts[0]
    30  	}
    31  
    32  	if len(h.metadata.Version) == 0 {
    33  		content, err := h.utils.FileRead(h.path)
    34  		if err != nil {
    35  			return fmt.Errorf("failed to read file '%v': %w", h.path, err)
    36  		}
    37  
    38  		err = yaml.Unmarshal(content, &h.metadata)
    39  		if err != nil {
    40  			return fmt.Errorf("helm chart content invalid '%v': %w", h.path, err)
    41  		}
    42  	}
    43  
    44  	return nil
    45  }
    46  
    47  // VersioningScheme returns the relevant versioning scheme
    48  func (h *HelmChart) VersioningScheme() string {
    49  	return "semver2"
    50  }
    51  
    52  // GetVersion returns the current version of the artifact with a JSON-based build descriptor
    53  func (h *HelmChart) GetVersion() (string, error) {
    54  	if err := h.init(); err != nil {
    55  		return "", fmt.Errorf("failed to init helm chart versioning: %w", err)
    56  	}
    57  
    58  	return h.metadata.Version, nil
    59  }
    60  
    61  // SetVersion updates the version of the artifact with a JSON-based build descriptor
    62  func (h *HelmChart) SetVersion(version string) error {
    63  	if err := h.init(); err != nil {
    64  		return fmt.Errorf("failed to init helm chart versioning: %w", err)
    65  	}
    66  
    67  	h.metadata.Version = version
    68  	if h.updateAppVersion {
    69  		// k8s does not allow a plus sign in labels
    70  		h.metadata.AppVersion = strings.ReplaceAll(version, "+", "_")
    71  	}
    72  
    73  	content, err := yaml.Marshal(h.metadata)
    74  	if err != nil {
    75  		return fmt.Errorf("failed to create chart content for '%v': %w", h.path, err)
    76  	}
    77  	err = h.utils.FileWrite(h.path, content, 666)
    78  	if err != nil {
    79  		return fmt.Errorf("failed to write file '%v': %w", h.path, err)
    80  	}
    81  
    82  	return nil
    83  }
    84  
    85  // GetCoordinates returns the coordinates
    86  func (h *HelmChart) GetCoordinates() (Coordinates, error) {
    87  	result := Coordinates{}
    88  	projectVersion, err := h.GetVersion()
    89  	if err != nil {
    90  		return result, err
    91  	}
    92  
    93  	result.ArtifactID = h.metadata.Name
    94  	result.Version = projectVersion
    95  	result.GroupID = h.metadata.Home
    96  
    97  	return result, nil
    98  }