github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/cmd/kcfi/cf_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 main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  	"path/filepath"
    24  	"time"
    25  
    26  	"github.com/spf13/cobra"
    27  
    28  	"github.com/codefresh-io/kcfi/pkg/action"
    29  	"helm.sh/helm/v3/cmd/helm/require"
    30  	helm "helm.sh/helm/v3/pkg/action"
    31  	"helm.sh/helm/v3/pkg/cli/output"
    32  	"helm.sh/helm/v3/pkg/cli/values"
    33  	"helm.sh/helm/v3/pkg/getter"
    34  
    35  	c "github.com/codefresh-io/kcfi/pkg/config"
    36  )
    37  
    38  const cfApplyDesc = `
    39  This command deploys Codefresh product with parameters defined in configuration file
    40     kcfi deploy [-c|--config /path/to/codefresh/config.yaml ]
    41  by default it looks for config.yaml in current directory,
    42  `
    43  
    44  func cfApplyCmd(cfg *helm.Configuration, out io.Writer) *cobra.Command {
    45  	client := action.NewCfApply(cfg)
    46  	valueOpts := &values.Options{}
    47  	var outfmt output.Format
    48  	//var createNamespace bool
    49  
    50  	cmd := &cobra.Command{
    51  		Use:     "deploy",
    52  		Short:   "install/upgrade/reconfigure Codefresh",
    53  		Aliases: []string{"apply", "install", "upgrade"},
    54  		Long:    cfApplyDesc,
    55  		Args:    require.NoArgs,
    56  		RunE: func(cmd *cobra.Command, args []string) error {
    57  			// merging configFile with valueOpts
    58  			valueOpts.ValueFiles = append([]string{client.ConfigFile}, valueOpts.ValueFiles...)
    59  			var baseDir string
    60  			if fInfo, err := os.Stat(client.ConfigFile); err == nil && !fInfo.IsDir() {
    61  				valueOpts.ValueFiles = []string{client.ConfigFile}
    62  				baseDir = filepath.Dir(client.ConfigFile)
    63  			} else {
    64  				return fmt.Errorf("%s is not a valid file", client.ConfigFile)
    65  			}
    66  			valueOpts.Values = append(valueOpts.Values, fmt.Sprintf("%s=%s", c.KeyBaseDir, baseDir))
    67  
    68  			valueOpts.Values = append(valueOpts.Values, fmt.Sprintf("%s=%s", c.KeyKubeNamespace, configuredNamespace))
    69  			vals, err := valueOpts.MergeValues(getter.All(settings))
    70  			if err != nil {
    71  				return err
    72  			}
    73  			client.Helm.Namespace = configuredNamespace
    74  
    75  			return client.Run(vals)
    76  		},
    77  	}
    78  
    79  	f := cmd.Flags()
    80  	f.StringVarP(&client.ConfigFile, flagConfig, "c", defaultConfigFileName(), "Codefresh config file")
    81  	//f.BoolVar(&createNamespace, "create-namespace", false, "if --install is set, create the release namespace if not present")
    82  	//f.BoolVarP(&client.Install, "install", "i", true, "if a release by this name doesn't already exist, run an install")
    83  	//f.BoolVar(&client.Helm.Devel, "devel", false, "use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored")
    84  	f.BoolVar(&client.Helm.DryRun, "dry-run", false, "simulate an upgrade")
    85  	//f.BoolVar(&client.Helm.Recreate, "recreate-pods", false, "performs pods restart for the resource if applicable")
    86  	//f.MarkDeprecated("recreate-pods", "functionality will no longer be updated. Consult the documentation for other methods to recreate pods")
    87  	f.BoolVar(&client.Helm.Force, "force", false, "force resource updates through a replacement strategy")
    88  	f.BoolVar(&client.Helm.DisableHooks, "no-hooks", false, "disable pre/post upgrade hooks")
    89  	f.BoolVar(&client.Helm.DisableOpenAPIValidation, "disable-openapi-validation", false, "if set, the upgrade process will not validate rendered templates against the Kubernetes OpenAPI Schema")
    90  	//f.BoolVar(&client.Helm.SkipCRDs, "skip-crds", false, "if set, no CRDs will be installed when an upgrade is performed with install flag enabled. By default, CRDs are installed if not already present, when an upgrade is performed with install flag enabled")
    91  	f.DurationVar(&client.Helm.Timeout, "timeout", 300*time.Second, "time to wait for any individual Kubernetes operation (like Jobs for hooks)")
    92  	f.BoolVar(&client.Helm.ResetValues, "reset-values", false, "when upgrading, reset the values to the ones built into the chart")
    93  	f.BoolVar(&client.Helm.ReuseValues, "reuse-values", false, "when upgrading, reuse the last release's values and merge in any overrides from the command line via --set and -f. If '--reset-values' is specified, this is ignored")
    94  	f.BoolVar(&client.Helm.Wait, "wait", false, "if set, will wait until all Pods, PVCs, Services, and minimum number of Pods of a Deployment, StatefulSet, or ReplicaSet are in a ready state before marking the release as successful. It will wait for as long as --timeout")
    95  	f.BoolVar(&client.Helm.Atomic, "atomic", false, "if set, upgrade process rolls back changes made in case of failed upgrade. The --wait flag will be set automatically if --atomic is used")
    96  	f.IntVar(&client.Helm.MaxHistory, "history-max", 10, "limit the maximum number of revisions saved per release. Use 0 for no limit")
    97  	//f.BoolVar(&client.Helm.CleanupOnFail, "cleanup-on-fail", false, "allow deletion of new resources created in this upgrade when upgrade fails")
    98  	//f.BoolVar(&client.Helm.SubNotes, "render-subchart-notes", false, "if set, render subchart notes along with the parent")
    99  	//f.StringVar(&client.Helm.Description, "description", "", "add a custom description")
   100  	////addChartPathOptionsFlags(f, &client.ChartPathOptions)
   101  	addValueOptionsFlags(f, valueOpts)
   102  	bindOutputFlag(cmd, &outfmt)
   103  	bindPostRenderFlag(cmd, &client.Helm.PostRenderer)
   104  
   105  	origHelpFunc := cmd.HelpFunc()
   106  	cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
   107  		hideHelmCommonFlags(cmd)
   108  		origHelpFunc(cmd, args)
   109  	})
   110  	return cmd
   111  }