github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/modules/k8s/kubectl.go (about)

     1  package k8s
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/url"
     6  	"os"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/gruntwork-io/terratest/modules/shell"
    11  	"github.com/gruntwork-io/terratest/modules/testing"
    12  )
    13  
    14  // RunKubectl will call kubectl using the provided options and args, failing the test on error.
    15  func RunKubectl(t testing.TestingT, options *KubectlOptions, args ...string) {
    16  	require.NoError(t, RunKubectlE(t, options, args...))
    17  }
    18  
    19  // RunKubectlE will call kubectl using the provided options and args.
    20  func RunKubectlE(t testing.TestingT, options *KubectlOptions, args ...string) error {
    21  	_, err := RunKubectlAndGetOutputE(t, options, args...)
    22  	return err
    23  }
    24  
    25  // RunKubectlAndGetOutputE will call kubectl using the provided options and args, returning the output of stdout and
    26  // stderr.
    27  func RunKubectlAndGetOutputE(t testing.TestingT, options *KubectlOptions, args ...string) (string, error) {
    28  	cmdArgs := []string{}
    29  	if options.ContextName != "" {
    30  		cmdArgs = append(cmdArgs, "--context", options.ContextName)
    31  	}
    32  	if options.ConfigPath != "" {
    33  		cmdArgs = append(cmdArgs, "--kubeconfig", options.ConfigPath)
    34  	}
    35  	if options.Namespace != "" {
    36  		cmdArgs = append(cmdArgs, "--namespace", options.Namespace)
    37  	}
    38  	cmdArgs = append(cmdArgs, args...)
    39  	command := shell.Command{
    40  		Command: "kubectl",
    41  		Args:    cmdArgs,
    42  		Env:     options.Env,
    43  	}
    44  	return shell.RunCommandAndGetOutputE(t, command)
    45  }
    46  
    47  // KubectlDelete will take in a file path and delete it from the cluster targeted by KubectlOptions. If there are any
    48  // errors, fail the test immediately.
    49  func KubectlDelete(t testing.TestingT, options *KubectlOptions, configPath string) {
    50  	require.NoError(t, KubectlDeleteE(t, options, configPath))
    51  }
    52  
    53  // KubectlDeleteE will take in a file path and delete it from the cluster targeted by KubectlOptions.
    54  func KubectlDeleteE(t testing.TestingT, options *KubectlOptions, configPath string) error {
    55  	return RunKubectlE(t, options, "delete", "-f", configPath)
    56  }
    57  
    58  // KubectlDeleteFromString will take in a kubernetes resource config as a string and delete it on the cluster specified
    59  // by the provided kubectl options.
    60  func KubectlDeleteFromString(t testing.TestingT, options *KubectlOptions, configData string) {
    61  	require.NoError(t, KubectlDeleteFromStringE(t, options, configData))
    62  }
    63  
    64  // KubectlDeleteFromStringE will take in a kubernetes resource config as a string and delete it on the cluster specified
    65  // by the provided kubectl options. If it fails, this will return the error.
    66  func KubectlDeleteFromStringE(t testing.TestingT, options *KubectlOptions, configData string) error {
    67  	tmpfile, err := StoreConfigToTempFileE(t, configData)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	defer os.Remove(tmpfile)
    72  	return KubectlDeleteE(t, options, tmpfile)
    73  }
    74  
    75  // KubectlApply will take in a file path and apply it to the cluster targeted by KubectlOptions. If there are any
    76  // errors, fail the test immediately.
    77  func KubectlApply(t testing.TestingT, options *KubectlOptions, configPath string) {
    78  	require.NoError(t, KubectlApplyE(t, options, configPath))
    79  }
    80  
    81  // KubectlApplyE will take in a file path and apply it to the cluster targeted by KubectlOptions.
    82  func KubectlApplyE(t testing.TestingT, options *KubectlOptions, configPath string) error {
    83  	return RunKubectlE(t, options, "apply", "-f", configPath)
    84  }
    85  
    86  // KubectlApplyFromString will take in a kubernetes resource config as a string and apply it on the cluster specified
    87  // by the provided kubectl options.
    88  func KubectlApplyFromString(t testing.TestingT, options *KubectlOptions, configData string) {
    89  	require.NoError(t, KubectlApplyFromStringE(t, options, configData))
    90  }
    91  
    92  // KubectlApplyFromStringE will take in a kubernetes resource config as a string and apply it on the cluster specified
    93  // by the provided kubectl options. If it fails, this will return the error.
    94  func KubectlApplyFromStringE(t testing.TestingT, options *KubectlOptions, configData string) error {
    95  	tmpfile, err := StoreConfigToTempFileE(t, configData)
    96  	if err != nil {
    97  		return err
    98  	}
    99  	defer os.Remove(tmpfile)
   100  	return KubectlApplyE(t, options, tmpfile)
   101  }
   102  
   103  // StoreConfigToTempFile will store the provided config data to a temporary file created on the os and return the
   104  // filename.
   105  func StoreConfigToTempFile(t testing.TestingT, configData string) string {
   106  	out, err := StoreConfigToTempFileE(t, configData)
   107  	require.NoError(t, err)
   108  	return out
   109  }
   110  
   111  // StoreConfigToTempFileE will store the provided config data to a temporary file created on the os and return the
   112  // filename, or error.
   113  func StoreConfigToTempFileE(t testing.TestingT, configData string) (string, error) {
   114  	escapedTestName := url.PathEscape(t.Name())
   115  	tmpfile, err := ioutil.TempFile("", escapedTestName)
   116  	if err != nil {
   117  		return "", err
   118  	}
   119  	defer tmpfile.Close()
   120  
   121  	_, err = tmpfile.WriteString(configData)
   122  	return tmpfile.Name(), err
   123  }