Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server-side apply mode #651

Merged
merged 10 commits into from
Apr 27, 2022
Prev Previous commit
Next Next commit
Move selection of apply strategy to spec.json
Also mark the field manager as being tanka
  • Loading branch information
smuth4 committed Apr 26, 2022
commit 63e691efa7a0b96b444b849c5be11a8970170d3c
1 change: 0 additions & 1 deletion cmd/tk/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func applyCmd() *cli.Command {
cmd.Flags().BoolVar(&opts.Validate, "validate", true, "validation of resources (kubectl --validate=false)")
cmd.Flags().BoolVar(&opts.AutoApprove, "dangerous-auto-approve", false, "skip interactive approval. Only for automation!")
cmd.Flags().StringVar(&opts.DryRun, "dry-run", "", `--dry-run parameter to pass down to kubectl, must be "none", "server", or "client"`)
cmd.Flags().BoolVar(&opts.ServerSide, "server-side", false, `pass --server-side flag down to kubectl`)

vars := workflowFlags(cmd.Flags())
getJsonnetOpts := jsonnetFlags(cmd.Flags())
Expand Down
10 changes: 2 additions & 8 deletions pkg/kubernetes/client/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import (
"os/exec"
"strings"

"github.com/Masterminds/semver"

"github.com/grafana/tanka/pkg/kubernetes/manifest"
)

// Test-ability: isolate applyCtl to build and return exec.Cmd from ApplyOpts
func (k Kubectl) applyCtl(data manifest.List, opts ApplyOpts) *exec.Cmd {
argv := []string{"-f", "-"}
serverSide := !k.info.ServerVersion.LessThan(semver.MustParse("1.22.0"))
serverSide := (opts.ApplyStrategy == "server")
if serverSide {
argv = append(argv, "--server-side")
argv = append(argv, "--server-side", "--field-manager=tanka")
}
if opts.Force {
if serverSide {
Expand All @@ -35,10 +33,6 @@ func (k Kubectl) applyCtl(data manifest.List, opts ApplyOpts) *exec.Cmd {
argv = append(argv, dryRun)
}

if opts.ServerSide {
argv = append(argv, "--server-side")
}

return k.ctl("apply", argv...)
}

Expand Down
7 changes: 0 additions & 7 deletions pkg/kubernetes/client/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ func TestKubectl_applyCtl(t *testing.T) {
expectedArgs: []string{"--dry-run=server"},
unExpectedArgs: []string{"--validate=false"},
},
{
name: "test server-side",
args: args{
opts: ApplyOpts{ServerSide: true},
},
expectedArgs: []string{"--server-side"},
},
}

for _, tt := range tests {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubernetes/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ type ApplyOpts struct {
// DryRun string passed to kubectl as --dry-run=<DryRun>
DryRun string

// ServerSide flag passed to kubectl as --server-side
ServerSide bool
// ApplyStrategy to pick a final method for deploying generated objects
ApplyStrategy string
}

// DeleteOpts allow to specify additional parameters for delete operations
Expand Down
1 change: 1 addition & 0 deletions pkg/spec/v1alpha1/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Spec struct {
APIServer string `json:"apiServer"`
Namespace string `json:"namespace"`
DiffStrategy string `json:"diffStrategy,omitempty"`
ApplyStrategy string `json:"applyStrategy,omitempty"`
InjectLabels bool `json:"injectLabels,omitempty"`
ResourceDefaults ResourceDefaults `json:"resourceDefaults"`
ExpectVersions ExpectVersions `json:"expectVersions"`
Expand Down
11 changes: 6 additions & 5 deletions pkg/tanka/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ type ApplyOpts struct {
AutoApprove bool
// DiffStrategy to use for printing the diff before approval
DiffStrategy string

// DiffStrategy decides how kubectl will apply the manifest
ApplyStrategy string
// Force ignores any warnings kubectl might have
Force bool
// Validate set to false ignores invalid Kubernetes schemas
Expand Down Expand Up @@ -69,10 +70,10 @@ func Apply(baseDir string, opts ApplyOpts) error {
}

return kube.Apply(l.Resources, kubernetes.ApplyOpts{
Force: opts.Force,
Validate: opts.Validate,
DryRun: opts.DryRun,
ServerSide: opts.ServerSide,
Force: opts.Force,
Validate: opts.Validate,
DryRun: opts.DryRun,
ApplyStrategy: l.Env.Spec.ApplyStrategy,
})
}

Expand Down