github.com/jenkins-x/jx/v2@v2.1.155/pkg/kube/resources/installer.go (about)

     1  package resources
     2  
     3  import (
     4  	"github.com/jenkins-x/jx/v2/pkg/util"
     5  )
     6  
     7  const kubeCtlBinary = "kubectl"
     8  
     9  // Installer provides support for installing Kuberntes resources directly from files
    10  //go:generate pegomock generate github.com/jenkins-x/jx/v2/pkg/kube/resources Installer -o mocks/installer.go
    11  type Installer interface {
    12  	// Install installs the Kubernetes resources provided in the file
    13  	Install(file string) (string, error)
    14  	// InstallDir installs the Kubernetes resources provided in the directory
    15  	InstallDir(dir string) (string, error)
    16  }
    17  
    18  // KubeCtlInstaller kubectl based resources installer
    19  type KubeCtlInstaller struct {
    20  	runner util.Commander
    21  }
    22  
    23  // NewKubeCtlInstaller creates a new kubectl installer
    24  func NewKubeCtlInstaller(cwd string, wait, validate bool) *KubeCtlInstaller {
    25  	args := []string{"apply"}
    26  	if wait {
    27  		args = append(args, "--wait")
    28  	}
    29  	if validate {
    30  		args = append(args, "--validate=true")
    31  	} else {
    32  		args = append(args, "--validate=false")
    33  	}
    34  	runner := &util.Command{
    35  		Name: kubeCtlBinary,
    36  		Args: args,
    37  		Dir:  cwd,
    38  	}
    39  	return &KubeCtlInstaller{
    40  		runner: runner,
    41  	}
    42  }
    43  
    44  // Install installs the resources provided in the file
    45  func (i *KubeCtlInstaller) Install(file string) (string, error) {
    46  	args := i.runner.CurrentArgs()
    47  	args = append(args, "-f", file)
    48  	i.runner.SetArgs(args)
    49  	return i.runner.RunWithoutRetry()
    50  }
    51  
    52  // InstallDir installs the resources provided in the directory
    53  func (i *KubeCtlInstaller) InstallDir(dir string) (string, error) {
    54  	args := i.runner.CurrentArgs()
    55  	args = append(args, "--recursive", "-f", dir)
    56  	i.runner.SetArgs(args)
    57  	return i.runner.RunWithoutRetry()
    58  }