github.com/verrazzano/verrazzano@v1.7.0/pkg/istio/istioctl.go (about)

     1  // Copyright (c) 2021, 2023, 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 istio
     5  
     6  import (
     7  	"os/exec"
     8  	"strings"
     9  
    10  	"github.com/verrazzano/verrazzano/pkg/constants"
    11  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    12  	"github.com/verrazzano/verrazzano/pkg/namespace"
    13  	vzos "github.com/verrazzano/verrazzano/pkg/os"
    14  
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  // cmdRunner needed for unit tests
    19  var runner vzos.CmdRunner = vzos.DefaultRunner{}
    20  
    21  // fakeIstioInstalledRunner is used to test if Istio is installed
    22  type fakeIstioInstalledRunner struct {
    23  }
    24  
    25  // Upgrade function gets called from istio_component to perform istio upgrade
    26  func Upgrade(log vzlog.VerrazzanoLogger, imageOverrideString string, overridesFiles ...string) (stdout []byte, stderr []byte, err error) {
    27  	args := []string{"install", "-y"}
    28  
    29  	// Add override files to arg array
    30  	for _, overridesFileName := range overridesFiles {
    31  		args = append(args, "-f")
    32  		args = append(args, overridesFileName)
    33  	}
    34  
    35  	// Add the image override strings
    36  	if len(imageOverrideString) > 0 {
    37  		segs := strings.Split(imageOverrideString, ",")
    38  		for i := range segs {
    39  			args = append(args, "--set")
    40  			args = append(args, segs[i])
    41  		}
    42  	}
    43  
    44  	// Perform istioctl call of type upgrade
    45  	stdout, stderr, err = runIstioctl(log, args, "upgrade", true)
    46  	if err != nil {
    47  		return stdout, stderr, err
    48  	}
    49  
    50  	return stdout, stderr, nil
    51  }
    52  
    53  // Install does an Istio installation using one or more IstioOperator YAML files
    54  func Install(log vzlog.VerrazzanoLogger, overrideStrings string, overridesFiles ...string) (stdout []byte, stderr []byte, err error) {
    55  	args := []string{"install", "-y"}
    56  
    57  	for _, overridesFileName := range overridesFiles {
    58  		args = append(args, "-f")
    59  		args = append(args, overridesFileName)
    60  	}
    61  
    62  	// Add the override strings
    63  	if len(overrideStrings) > 0 {
    64  		segs := strings.Split(overrideStrings, ",")
    65  		for i := range segs {
    66  			args = append(args, "--set")
    67  			args = append(args, segs[i])
    68  		}
    69  	}
    70  
    71  	// Perform istioctl call of type upgrade
    72  	stdout, stderr, err = runIstioctl(log, args, "install", true)
    73  	if err != nil {
    74  		return stdout, stderr, err
    75  	}
    76  
    77  	return stdout, stderr, nil
    78  }
    79  
    80  // Uninstall does an Istio uninstall removing the default revision installation. Istio CRDs are not removed.
    81  func Uninstall(log vzlog.VerrazzanoLogger) (stdout []byte, stderr []byte, err error) {
    82  	args := []string{"uninstall", "--revision", "default", "-y"}
    83  
    84  	// Perform istioctl call of type uninstall
    85  	stdout, stderr, err = runIstioctl(log, args, "uninstall", true)
    86  	if err != nil {
    87  		return stdout, stderr, errors.Wrapf(err, "uninstall failed, stderr: %s", stderr)
    88  	}
    89  
    90  	return stdout, stderr, nil
    91  }
    92  
    93  // IsInstalled returns true if Istio is installed
    94  func IsInstalled(log vzlog.VerrazzanoLogger) (bool, error) {
    95  	// check to make sure we own the namespace first
    96  	vzManaged, err := namespace.CheckIfVerrazzanoManagedNamespaceExists(constants.IstioSystemNamespace)
    97  	if err != nil {
    98  		return false, err
    99  	}
   100  	if !vzManaged {
   101  		return false, nil
   102  	}
   103  
   104  	// Perform istioctl call of type upgrade
   105  	stdout, _, err := VerifyInstall(log)
   106  	if err != nil {
   107  		return false, err
   108  	}
   109  	if strings.Contains(string(stdout), "Istio is installed and verified successfully") {
   110  		return true, nil
   111  	}
   112  	return false, nil
   113  }
   114  
   115  // VerifyInstall verifies the Istio installation
   116  func VerifyInstall(log vzlog.VerrazzanoLogger) (stdout []byte, stderr []byte, err error) {
   117  	args := []string{"verify-install"}
   118  
   119  	// Perform istioctl call of type upgrade
   120  	stdout, stderr, err = runIstioctl(log, args, "verify-install", false)
   121  	if err != nil {
   122  		return stdout, stderr, errors.Wrapf(err, "verify-install failed, stderr: %s", stderr)
   123  	}
   124  
   125  	return stdout, stderr, nil
   126  }
   127  
   128  // runIstioctl will perform istioctl calls with specified arguments  for operations
   129  // Note that operation name as of now does not affect the istioctl call (both upgrade and install call istioctl install)
   130  // The operationName field is just used for visibility of operation in logging at the moment
   131  func runIstioctl(log vzlog.VerrazzanoLogger, cmdArgs []string, operationName string, verbose bool) (stdout []byte, stderr []byte, err error) {
   132  	cmd := exec.Command("istioctl", cmdArgs...)
   133  	if verbose {
   134  		log.Progressf("Running istioctl command: %s", cmd.String())
   135  	}
   136  	stdout, stderr, err = runner.Run(cmd)
   137  	if err != nil {
   138  		if verbose {
   139  			log.Progressf("Failed running istioctl command %s: %s", cmd.String(), stderr)
   140  		}
   141  		return stdout, stderr, err
   142  	}
   143  	log.Debugf("istioctl %s succeeded: %s", operationName, stdout)
   144  	return stdout, stderr, nil
   145  }
   146  
   147  // SetCmdRunner sets the command runner as needed by unit tests
   148  func SetCmdRunner(r vzos.CmdRunner) {
   149  	runner = r
   150  }
   151  
   152  // SetDefaultRunner sets the command runner to default
   153  func SetDefaultRunner() {
   154  	runner = vzos.DefaultRunner{}
   155  }