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

     1  package fetch
     2  
     3  import (
     4  	"github.com/caos/orbos/internal/operator/boom/application"
     5  	"github.com/caos/orbos/internal/operator/boom/bundle/bundles"
     6  	"github.com/caos/orbos/internal/operator/boom/templator/helm/chart"
     7  	"github.com/caos/orbos/internal/operator/boom/templator/helm/helmcommand"
     8  	"github.com/caos/orbos/mntr"
     9  )
    10  
    11  type ChartKey struct {
    12  	Name    string
    13  	Version string
    14  }
    15  
    16  type ChartInfo struct {
    17  	Name      string
    18  	Version   string
    19  	IndexName string
    20  }
    21  
    22  func All(monitor mntr.Monitor, basePath string, newVersions bool) error {
    23  	allApps := bundles.GetAll()
    24  
    25  	monitor.Info("Ingest Helm")
    26  
    27  	// helm init to create a HELMHOME
    28  	if err := helmcommand.Init(basePath); err != nil {
    29  		return err
    30  	}
    31  
    32  	//indexes in a map so that no doublicates exist
    33  	indexes := make(map[*ChartKey]*chart.Index, 0)
    34  	charts := make([]*ChartInfo, 0)
    35  	monitor.Info("Preparing lists of indexes and charts")
    36  
    37  	for _, appName := range allApps {
    38  		app := application.New(monitor, appName, "")
    39  		temp, ok := app.(application.HelmApplication)
    40  		// if application doenst implement helm interface then no charts are defined
    41  		if ok {
    42  			// get chartinfo from application
    43  			chart := temp.GetChartInfo()
    44  
    45  			// when no index defined then it the helm stable repository
    46  			var indexName string
    47  			if chart.Index != nil {
    48  				indexName = chart.Index.Name
    49  				indexes[&ChartKey{Name: chart.Name, Version: chart.Version}] = chart.Index
    50  			} else {
    51  				indexName = "stable"
    52  			}
    53  
    54  			// only add chart if chart is not used by another application, no doublicates
    55  			var found bool
    56  			found = false
    57  			for _, checkChart := range charts {
    58  				if checkChart.Name == chart.Name && checkChart.Version == chart.Version && checkChart.IndexName == indexName {
    59  					found = true
    60  				}
    61  			}
    62  			if !found {
    63  				charts = append(charts, &ChartInfo{Name: chart.Name, Version: chart.Version, IndexName: indexName})
    64  			}
    65  		} else {
    66  			logFields := map[string]interface{}{
    67  				"application": appName.String(),
    68  			}
    69  			monitor.WithFields(logFields).Info("Not helm templated")
    70  		}
    71  	}
    72  
    73  	monitor.Info("Adding all indexes")
    74  	// add all indexes in a map so that no dublicates exist
    75  	for _, v := range indexes {
    76  		if err := addIndex(monitor, basePath, v); err != nil {
    77  			return err
    78  		}
    79  	}
    80  
    81  	monitor.Info("Repo update")
    82  	if err := helmcommand.RepoUpdate(basePath); err != nil {
    83  		return err
    84  	}
    85  
    86  	if newVersions {
    87  		monitor.Info("Checking newer chart versions")
    88  		if err := CompareVersions(monitor, basePath, charts); err != nil {
    89  			return err
    90  		}
    91  	}
    92  
    93  	monitor.Info("Fetching all charts")
    94  	for _, chart := range charts {
    95  		if err := fetch(monitor, basePath, chart); err != nil {
    96  			return err
    97  		}
    98  	}
    99  	return nil
   100  }
   101  
   102  func fetch(monitor mntr.Monitor, basePath string, chart *ChartInfo) error {
   103  	logFields := map[string]interface{}{
   104  		"application": chart.Name,
   105  		"version":     chart.Version,
   106  		"indexname":   chart.IndexName,
   107  		"folder":      basePath,
   108  	}
   109  
   110  	monitor.WithFields(logFields).Info("Fetching chart")
   111  	return helmcommand.FetchChart(&helmcommand.FetchConfig{
   112  		TempFolderPath: basePath,
   113  		ChartName:      chart.Name,
   114  		ChartVersion:   chart.Version,
   115  		IndexName:      chart.IndexName,
   116  	})
   117  }
   118  
   119  func addIndex(monitor mntr.Monitor, basePath string, index *chart.Index) error {
   120  	logFields := map[string]interface{}{
   121  		"index": index.Name,
   122  		"url":   index.URL,
   123  	}
   124  	monitor.WithFields(logFields).Info("Adding index")
   125  	return helmcommand.AddIndex(&helmcommand.IndexConfig{
   126  		TempFolderPath: basePath,
   127  		IndexName:      index.Name,
   128  		IndexURL:       index.URL,
   129  	})
   130  }