github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/utils/kustomize/kustomize.go (about)

     1  package kustomize
     2  
     3  import (
     4  	"os/exec"
     5  	"path/filepath"
     6  	"strings"
     7  )
     8  
     9  type File struct {
    10  	Namespace    string   `yaml:"namespace"`
    11  	Transformers []string `yaml:"transformers,omitempty"`
    12  	Resources    []string `yaml:"resources"`
    13  }
    14  type LabelTransformer struct {
    15  	ApiVersion string            `yaml:"apiVersion"`
    16  	Kind       string            `yaml:"kind"`
    17  	Metadata   *Metadata         `yaml:"metadata"`
    18  	Labels     map[string]string `yaml:"labels"`
    19  	FieldSpecs []*FieldSpec      `yaml:"fieldSpecs"`
    20  }
    21  type Metadata struct {
    22  	Name string `yaml:"name"`
    23  }
    24  type FieldSpec struct {
    25  	Kind   string `yaml:"kind,omitempty"`
    26  	Path   string `yaml:"path"`
    27  	Create bool   `yaml:"create"`
    28  }
    29  
    30  type Kustomize struct {
    31  	path   string
    32  	apply  bool
    33  	delete bool
    34  	force  bool
    35  }
    36  
    37  func New(path string) (*Kustomize, error) {
    38  	abspath, err := filepath.Abs(path)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	return &Kustomize{
    44  		path:   abspath,
    45  		apply:  false,
    46  		delete: false,
    47  		force:  false,
    48  	}, nil
    49  }
    50  
    51  func (k *Kustomize) Apply(force bool) *Kustomize {
    52  	k.apply = true
    53  	k.delete = false
    54  	k.force = force
    55  	return k
    56  }
    57  func (k *Kustomize) Delete() *Kustomize {
    58  	k.apply = false
    59  	k.delete = true
    60  	return k
    61  }
    62  
    63  func (k *Kustomize) Build() exec.Cmd {
    64  	all := strings.Join([]string{"kustomize", "build", k.path}, " ")
    65  	if k.apply {
    66  		all = strings.Join([]string{all, "| kubectl apply -f -"}, " ")
    67  		if k.force {
    68  			all = strings.Join([]string{all, "--force"}, " ")
    69  		}
    70  	} else if k.delete {
    71  		all = strings.Join([]string{all, "| kubectl delete -f -"}, " ")
    72  	}
    73  
    74  	cmd := exec.Command("/bin/sh", "-c", all)
    75  	return *cmd
    76  }