github.com/verrazzano/verrazzano@v1.7.1/tools/psr/psrctl/cmd/update/update.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package update
     5  
     6  import (
     7  	"fmt"
     8  	"github.com/spf13/cobra"
     9  	helmcli "github.com/verrazzano/verrazzano/pkg/helm"
    10  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/cmd/constants"
    11  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/pkg/manifest"
    12  	"github.com/verrazzano/verrazzano/tools/psr/psrctl/pkg/scenario"
    13  	cmdhelpers "github.com/verrazzano/verrazzano/tools/vz/cmd/helpers"
    14  	"github.com/verrazzano/verrazzano/tools/vz/pkg/helpers"
    15  )
    16  
    17  const (
    18  	CommandName = "update"
    19  	helpShort   = "Update a running PSR scenario configuration"
    20  	helpLong    = `The command 'update' updates the configuration of a running PSR scenario.  
    21  The underlying use case helm charts will be updated with any overrides you provide.  
    22  If you provide any overrides then they will be applied to all the helm charts in the scenario.  
    23  The only way to modify a use case specific configuration is to put the changes in the scenario files 
    24  and apply them.  For example, if you are running a scenario with the -d parameter providing 
    25  a custom scenario, you can modify those scenario files and update the running scenario.  
    26  You cannot change the scenario.yaml file, you can only change the usecase-override files`
    27  	helpExample = `
    28  // Update the backend image for all workers in running scenario ops-s1
    29  psrctl update -s ops-s1 -w=ghcr.io/verrazzano/psr-backend:xyz
    30  
    31  // Update the scenario with usecase overrides at directory tmp/myscenario
    32  psrctl update -s ops-s1 -d=tmp/myscenario
    33  `
    34  )
    35  
    36  var scenarioID string
    37  var namespace string
    38  var scenarioDir string
    39  var workerImage string
    40  var imagePullSecret string
    41  
    42  func NewCmdUpdate(vzHelper helpers.VZHelper) *cobra.Command {
    43  	cmd := cmdhelpers.NewCommand(vzHelper, CommandName, helpShort, helpLong)
    44  	cmd.RunE = func(cmd *cobra.Command, args []string) error {
    45  		return RunCmdUpdate(cmd, vzHelper)
    46  	}
    47  	cmd.Example = helpExample
    48  
    49  	cmd.PersistentFlags().StringVarP(&scenarioID, constants.FlagScenario, constants.FlagsScenarioShort, "", constants.FlagScenarioHelp)
    50  	cmd.PersistentFlags().StringVarP(&namespace, constants.FlagNamespace, constants.FlagNamespaceShort, "default", constants.FlagNamespaceHelp)
    51  	cmd.PersistentFlags().StringVarP(&scenarioDir, constants.FlagScenarioDir, constants.FlagScenarioDirShort, "", constants.FlagScenarioDirHelp)
    52  	cmd.PersistentFlags().StringVarP(&workerImage, constants.WorkerImageName, constants.WorkerImageNameShort, "", constants.WorkerImageNameHelp)
    53  	cmd.PersistentFlags().StringVarP(&imagePullSecret, constants.ImagePullSecretName, constants.ImagePullSecretNameShort, constants.ImagePullSecDefault, constants.ImagePullSecretNameHelp)
    54  
    55  	return cmd
    56  }
    57  
    58  // RunCmdUpdate - update the "psrctl update" command
    59  func RunCmdUpdate(cmd *cobra.Command, vzHelper helpers.VZHelper) error {
    60  	// GetScenarioManifest gets the ScenarioManifest for the given scenarioID
    61  	manifestMan, err := manifest.NewManager(scenarioDir)
    62  	if err != nil {
    63  		return fmt.Errorf("Failed to create scenario ScenarioMananger %v", err)
    64  	}
    65  	scenarioMan, err := manifestMan.FindScenarioManifestByID(scenarioID)
    66  	if err != nil {
    67  		return fmt.Errorf("Failed to find scenario manifest %s: %v", scenarioID, err)
    68  	}
    69  	if scenarioMan == nil {
    70  		return fmt.Errorf("Failed to find scenario manifest with ID %s", scenarioID)
    71  	}
    72  
    73  	m, err := scenario.NewManager(namespace, buildHelmOverrides()...)
    74  	if err != nil {
    75  		return fmt.Errorf("Failed to create scenario ScenarioMananger %v", err)
    76  	}
    77  
    78  	fmt.Fprintf(vzHelper.GetOutputStream(), fmt.Sprintf("Updating scenario %s\n", scenarioID))
    79  	msg, err := m.UpdateScenario(manifestMan, scenarioMan, vzHelper)
    80  	if err != nil {
    81  		// Cobra will display failure message
    82  		return fmt.Errorf("Failed to update scenario %s/%s: %v\n%s", namespace, scenarioID, err, msg)
    83  	}
    84  	fmt.Fprintf(vzHelper.GetOutputStream(), fmt.Sprintf("Scenario %s successfully updated\n", scenarioID))
    85  
    86  	return nil
    87  }
    88  
    89  func buildHelmOverrides() []helmcli.HelmOverrides {
    90  	if len(workerImage) == 0 {
    91  		return []helmcli.HelmOverrides{
    92  			{SetOverrides: fmt.Sprintf("%s=%s", constants.ImagePullSecKey, imagePullSecret)},
    93  		}
    94  	}
    95  	return []helmcli.HelmOverrides{
    96  		{SetOverrides: fmt.Sprintf("%s=%s", constants.ImageNameKey, workerImage)},
    97  		{SetOverrides: fmt.Sprintf("%s=%s", constants.ImagePullSecKey, imagePullSecret)},
    98  	}
    99  }