github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/boom/templator/helm/chart/fetch/index.go (about)

     1  package fetch
     2  
     3  import (
     4  	helper2 "github.com/caos/orbos/internal/utils/helper"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/caos/orbos/mntr"
    12  	"gopkg.in/yaml.v3"
    13  )
    14  
    15  type index struct {
    16  	APIVersion string             `yaml:"apiVersion"`
    17  	Entries    map[string][]entry `yaml:"entries"`
    18  }
    19  
    20  type entry struct {
    21  	Version    string `yaml:"version"`
    22  	AppVersion string `yaml:"appVersion"`
    23  }
    24  
    25  func CompareVersions(monitor mntr.Monitor, basePath string, charts []*ChartInfo) error {
    26  
    27  	indexFolderPathAbs, err := helper2.GetAbsPath(basePath, "helm", "repository", "cache")
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	indexFiles := make(map[string]*index, 0)
    33  	err = filepath.Walk(indexFolderPathAbs, func(path string, info os.FileInfo, err error) error {
    34  		if info.IsDir() {
    35  			return nil
    36  		}
    37  
    38  		data, err := ioutil.ReadFile(path)
    39  		if err != nil {
    40  			return err
    41  		}
    42  
    43  		var indexFile index
    44  		if err := yaml.Unmarshal(data, &indexFile); err != nil {
    45  			return err
    46  		}
    47  
    48  		indexFiles[strings.TrimSuffix(info.Name(), "-index.yaml")] = &indexFile
    49  		// for k, v := range indexFile.Entries {
    50  		// 	indexFiles[strings.TrimSuffix(info.Name(), "-index.yaml")].Entries[k] = v
    51  		// }
    52  		return nil
    53  	})
    54  	if err != nil {
    55  		return err
    56  	}
    57  	for _, chart := range charts {
    58  		indexFile := indexFiles[chart.IndexName]
    59  		for _, entry := range indexFile.Entries[chart.Name] {
    60  			entryParts := strings.Split(entry.Version, ".")
    61  			entryPartsInt := make([]int, 3)
    62  			for k, v := range entryParts {
    63  				entryPartsInt[k], _ = strconv.Atoi(v)
    64  			}
    65  
    66  			chartParts := strings.Split(chart.Version, ".")
    67  			chartPartsInt := make([]int, 3)
    68  			for k, v := range chartParts {
    69  				chartPartsInt[k], _ = strconv.Atoi(v)
    70  			}
    71  			if entryPartsInt[0] > chartPartsInt[0] ||
    72  				(entryPartsInt[0] == chartPartsInt[0] && entryPartsInt[1] > chartPartsInt[1]) ||
    73  				(entryPartsInt[0] == chartPartsInt[0] && entryPartsInt[1] == chartPartsInt[1] && entryPartsInt[2] > chartPartsInt[2]) {
    74  
    75  				logFields := map[string]interface{}{
    76  					"oldVersion":    chart.Version,
    77  					"newVersion":    entry.Version,
    78  					"index":         chart.IndexName,
    79  					"chart":         chart.Name,
    80  					"newAppVersion": entry.AppVersion,
    81  				}
    82  				monitor.WithFields(logFields).Info("There is a newer version")
    83  			}
    84  		}
    85  	}
    86  
    87  	return nil
    88  }