github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/pkg/action/apply.go (about)

     1  /*
     2  Copyright The Codefresh Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package action
    18  
    19  import (
    20  	"fmt"
    21  	c "github.com/codefresh-io/kcfi/pkg/config"
    22  	"github.com/pkg/errors"
    23  	"github.com/stretchr/objx"
    24  	helm "helm.sh/helm/v3/pkg/action"
    25  	"path"
    26  	"path/filepath"
    27  )
    28  
    29  // CfApply is an action to create or update Codefresh
    30  type CfApply struct {
    31  	ConfigFile string
    32  	vals       map[string]interface{}
    33  	cfg        *helm.Configuration
    34  	Helm       *helm.Upgrade
    35  }
    36  
    37  // NewCfApply creates object
    38  func NewCfApply(cfg *helm.Configuration) *CfApply {
    39  	return &CfApply{
    40  		cfg:  cfg,
    41  		Helm: helm.NewUpgrade(cfg),
    42  	}
    43  }
    44  
    45  // Run the action
    46  func (o *CfApply) Run(vals map[string]interface{}) error {
    47  	info("Applying Codefresh configuration from %s\n", o.ConfigFile)
    48  	// info("Applying Codefresh configuration from %s\n", o.ConfigFile)
    49  	o.vals = vals
    50  	valsX := objx.New(vals)
    51  
    52  	// Includes additional value files
    53  	if valsX.Has(c.KeyInclude) {
    54  		var includeFiles []string
    55  		// cfgX.Get(c.KeyInclude).StrSlice() - not working, returns empty
    56  		includeFilesI := valsX.Get(c.KeyInclude).Data()
    57  		fileNamesI, includeIsList := includeFilesI.([]interface{})
    58  		if !includeIsList {
    59  			return fmt.Errorf("Error: %s - %v is not a list", c.KeyInclude, includeFilesI)
    60  		}
    61  		for _, f := range fileNamesI {
    62  			if str, isStr := f.(string); isStr {
    63  				includeFiles = append(includeFiles, str)
    64  			} else {
    65  				return fmt.Errorf("Error: %s - %v is not a string", c.KeyInclude, f)
    66  			}
    67  		}
    68  		debug("Processed include: %v - %v", includeFilesI, includeFiles)
    69  
    70  		for _, fileName := range includeFiles {
    71  			includeConfig, err := ReadYamlFile(path.Join(filepath.Dir(o.ConfigFile), fileName))
    72  			if err != nil {
    73  				return errors.Wrapf(err, "failed to parse included values file %s", fileName)
    74  			}
    75  			debug("merging included config change file %s", fileName)
    76  			o.vals = MergeMaps(o.vals, includeConfig)
    77  		}
    78  	}
    79  
    80  	kind := valsX.Get(c.KeyKind).String()
    81  	baseDir := filepath.Dir(o.ConfigFile)
    82  	o.vals[c.KeyBaseDir] = baseDir
    83  
    84  	switch kind {
    85  	case kindCodefresh:
    86  		return o.ApplyCodefresh()
    87  	case kindBackupManager:
    88  		return o.ApplyBackupMgr()
    89  	case "":
    90  		return fmt.Errorf("Please specifiy the installer kind")
    91  	default:
    92  		installerType := valsX.Get(c.KeyInstallerType).String()
    93  		if installerType == installerTypeHelm {
    94  			helmChartName := valsX.Get(c.KeyHelmChart).String()
    95  			helmReleaseName := valsX.Get(c.KeyHelmRelease).Str(kind)
    96  			rel, err := DeployHelmRelease(
    97  				helmReleaseName,
    98  				helmChartName,
    99  				o.vals,
   100  				o.cfg,
   101  				o.Helm,
   102  			)
   103  			if err != nil {
   104  				return errors.Wrapf(err, "Failed to deploy %s chart", helmChartName)
   105  			}
   106  			PrintHelmReleaseInfo(rel, c.Debug)
   107  			info("\n%s has been deployed to namespace %s\n", helmReleaseName, o.Helm.Namespace)
   108  			return nil
   109  		}
   110  		return fmt.Errorf("Wrong installer type %s", installerType)
   111  	}
   112  }
   113  
   114  func (o *CfApply) filePath(fileName string) string {
   115  	return path.Join(filepath.Dir(o.ConfigFile), fileName)
   116  }