github.com/grafana/tanka@v0.26.1-0.20240506093700-c22cfc35c21a/pkg/kubernetes/client/apply.go (about) 1 package client 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "strings" 8 9 "github.com/Masterminds/semver" 10 11 "github.com/grafana/tanka/pkg/kubernetes/manifest" 12 ) 13 14 // Test-ability: isolate applyCtl to build and return exec.Cmd from ApplyOpts 15 func (k Kubectl) applyCtl(_ manifest.List, opts ApplyOpts) *exec.Cmd { 16 argv := []string{"-f", "-"} 17 serverSide := (opts.ApplyStrategy == "server") 18 if serverSide { 19 argv = append(argv, "--server-side") 20 if k.info.ClientVersion.GreaterThan(semver.MustParse("1.19.0")) { 21 argv = append(argv, "--field-manager=tanka") 22 } 23 } 24 if opts.Force { 25 if serverSide { 26 argv = append(argv, "--force-conflicts") 27 } else { 28 argv = append(argv, "--force") 29 } 30 } 31 32 if !opts.Validate { 33 argv = append(argv, "--validate=false") 34 } 35 36 if opts.DryRun != "" { 37 dryRun := fmt.Sprintf("--dry-run=%s", opts.DryRun) 38 argv = append(argv, dryRun) 39 } 40 41 return k.ctl("apply", argv...) 42 } 43 44 // Apply applies the given yaml to the cluster 45 func (k Kubectl) Apply(data manifest.List, opts ApplyOpts) error { 46 cmd := k.applyCtl(data, opts) 47 48 cmd.Stdout = os.Stdout 49 cmd.Stderr = os.Stderr 50 51 cmd.Stdin = strings.NewReader(data.String()) 52 53 return cmd.Run() 54 }