Skip to content

Commit 3b26f96

Browse files
committed
fix: adjust --dry-run flag value depending on version of kubectl
1 parent 3019679 commit 3b26f96

File tree

9 files changed

+91
-25
lines changed

9 files changed

+91
-25
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
github.com/golang/protobuf v1.5.2
2121
github.com/google/uuid v1.1.2
2222
github.com/gorilla/websocket v1.4.2
23-
github.com/hashicorp/go-version v1.2.0
23+
github.com/hashicorp/go-version v1.3.0
2424
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf // indirect
2525
github.com/joho/godotenv v1.3.0
2626
github.com/json-iterator/go v1.1.11

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,9 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
614614
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
615615
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
616616
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
617-
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
618617
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
618+
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
619+
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
619620
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
620621
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
621622
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

pkg/devspace/deploy/deployer/kubectl/builder.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"strings"
88

99
"github.com/ghodss/yaml"
10+
version "github.com/hashicorp/go-version"
1011
"github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
12+
"github.com/loft-sh/devspace/pkg/util/command"
1113
"github.com/loft-sh/devspace/pkg/util/log"
1214
"github.com/pkg/errors"
1315
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -81,7 +83,30 @@ func (k *kubectlBuilder) Build(manifest string, cmd RunCommand) ([]*unstructured
8183
args = append(args, "--namespace", k.namespace)
8284
}
8385

84-
args = append(args, "--dry-run", "--output", "yaml", "--validate=false")
86+
// compare kubectl version for --dry-run flag value
87+
out, err := command.NewStreamCommand(k.path, []string{"version", "--client", "--short"}).Output()
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
versionSlice := strings.Split(string(out), "v")
93+
v1, err := version.NewVersion(strings.TrimSpace(versionSlice[1]))
94+
if err != nil {
95+
96+
return nil, err
97+
}
98+
99+
v2, err := version.NewVersion("1.18.0")
100+
if err != nil {
101+
return nil, err
102+
}
103+
104+
if v1.LessThan(v2) {
105+
args = append(args, "--dry-run", "--output", "yaml", "--validate=false")
106+
} else {
107+
args = append(args, "--dry-run=client", "--output", "yaml", "--validate=false")
108+
}
109+
85110
if k.config.Kubectl.Kustomize != nil && *k.config.Kubectl.Kustomize {
86111
args = append(args, "--kustomize", manifest)
87112
} else {

pkg/util/downloader/commands/kubectl.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
package commands
22

33
import (
4+
"io/ioutil"
5+
"log"
6+
"net/http"
7+
"path/filepath"
8+
"runtime"
9+
"strings"
10+
411
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
512
"github.com/loft-sh/devspace/pkg/util/command"
613
"github.com/mitchellh/go-homedir"
714
"github.com/otiai10/copy"
8-
"path/filepath"
9-
"runtime"
10-
"strings"
1115
)
1216

1317
var (
14-
kubectlVersion = "v1.21.2"
15-
kubectlDownload = "https://storage.googleapis.com/kubernetes-release/release/" + kubectlVersion + "/bin/" + runtime.GOOS + "/" + runtime.GOARCH + "/kubectl"
18+
kubectlVersion = func() string {
19+
res, err := http.Get("https://storage.googleapis.com/kubernetes-release/release/stable.txt")
20+
if err != nil {
21+
log.Fatal(err)
22+
}
23+
content, err := ioutil.ReadAll(res.Body)
24+
res.Body.Close()
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
return string(content)
29+
}
30+
kubectlDownload = "https://storage.googleapis.com/kubernetes-release/release/" + kubectlVersion() + "/bin/" + runtime.GOOS + "/" + runtime.GOARCH + "/kubectl"
1631
)
1732

1833
func NewKubectlCommand() Command {

vendor/github.com/hashicorp/go-version/.travis.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

vendor/github.com/hashicorp/go-version/CHANGELOG.md

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hashicorp/go-version/README.md

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hashicorp/go-version/version.go

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ github.com/gorilla/mux
220220
# github.com/gorilla/websocket v1.4.2
221221
## explicit
222222
github.com/gorilla/websocket
223-
# github.com/hashicorp/go-version v1.2.0
223+
# github.com/hashicorp/go-version v1.3.0
224224
## explicit
225225
github.com/hashicorp/go-version
226226
# github.com/imdario/mergo v0.3.12

0 commit comments

Comments
 (0)