Skip to content

Commit e539ff4

Browse files
authored
Merge pull request #1814 from pratikjagrut/dry.run.flag
fix: adjust --dry-run flag value depending on version of kubectl
2 parents 33781bf + e529fd6 commit e539ff4

File tree

11 files changed

+122
-31
lines changed

11 files changed

+122
-31
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: 41 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"
@@ -72,6 +74,33 @@ func NewKubectlBuilder(path string, config *latest.DeploymentConfig, context, na
7274
}
7375
}
7476

77+
// this function is called in Build function
78+
// to decide the --dry-run value
79+
var useOldDryRun = func(path string) (bool, error) {
80+
// compare kubectl version for --dry-run flag value
81+
out, err := command.NewStreamCommand(path, []string{"version", "--client", "--short"}).Output()
82+
if err != nil {
83+
return false, err
84+
}
85+
86+
v1, err := version.NewVersion(strings.TrimPrefix(strings.TrimSpace(string(out)), "Client Version: v"))
87+
if err != nil {
88+
89+
return false, err
90+
}
91+
92+
v2, err := version.NewVersion("1.18.0")
93+
if err != nil {
94+
return false, err
95+
}
96+
97+
if v1.LessThan(v2) {
98+
return true, nil
99+
}
100+
101+
return false, nil
102+
}
103+
75104
func (k *kubectlBuilder) Build(manifest string, cmd RunCommand) ([]*unstructured.Unstructured, error) {
76105
args := []string{"create"}
77106
if k.context != "" && !k.isInCluster {
@@ -81,7 +110,18 @@ func (k *kubectlBuilder) Build(manifest string, cmd RunCommand) ([]*unstructured
81110
args = append(args, "--namespace", k.namespace)
82111
}
83112

84-
args = append(args, "--dry-run", "--output", "yaml", "--validate=false")
113+
// decides which --dry-run value is to be used
114+
uodr, err := useOldDryRun(k.path)
115+
if err != nil {
116+
return nil, err
117+
}
118+
119+
if uodr {
120+
args = append(args, "--dry-run", "--output", "yaml", "--validate=false")
121+
} else {
122+
args = append(args, "--dry-run=client", "--output", "yaml", "--validate=false")
123+
}
124+
85125
if k.config.Kubectl.Kustomize != nil && *k.config.Kubectl.Kustomize {
86126
args = append(args, "--kustomize", manifest)
87127
} else {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ func TestRender(t *testing.T) {
192192
},
193193
}
194194

195+
useOldDryRun = func(path string) (bool, error) {
196+
return true, nil
197+
}
198+
195199
reader, writer := io.Pipe()
196200
defer reader.Close()
197201

@@ -318,6 +322,9 @@ func TestDelete(t *testing.T) {
318322
}
319323
}
320324

325+
useOldDryRun = func(path string) (bool, error) {
326+
return true, nil
327+
}
321328
err := deployer.Delete()
322329
if testCase.expectedErr == "" {
323330
assert.NilError(t, err, "Error in testCase %s", testCase.name)
@@ -402,6 +409,9 @@ func TestDeploy(t *testing.T) {
402409
}
403410
}
404411

412+
useOldDryRun = func(path string) (bool, error) {
413+
return true, nil
414+
}
405415
deployed, err := deployer.Deploy(testCase.forceDeploy, testCase.builtImages)
406416

407417
if testCase.expectedErr == "" {
@@ -479,7 +489,9 @@ func TestGetReplacedManifest(t *testing.T) {
479489
Images: testCase.imageConfigs,
480490
}, cache, nil, constants.DefaultConfigPath),
481491
}
482-
492+
useOldDryRun = func(path string) (bool, error) {
493+
return true, nil
494+
}
483495
shouldRedeploy, replacedManifest, err := deployer.getReplacedManifest(testCase.manifest, testCase.builtImages)
484496

485497
if testCase.expectedErr == "" {

pkg/util/downloader/commands/kubectl.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package commands
22

33
import (
4-
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
5-
"github.com/loft-sh/devspace/pkg/util/command"
6-
"github.com/mitchellh/go-homedir"
7-
"github.com/otiai10/copy"
4+
"io/ioutil"
5+
"net/http"
86
"path/filepath"
97
"runtime"
108
"strings"
11-
)
129

13-
var (
14-
kubectlVersion = "v1.21.2"
15-
kubectlDownload = "https://storage.googleapis.com/kubernetes-release/release/" + kubectlVersion + "/bin/" + runtime.GOOS + "/" + runtime.GOARCH + "/kubectl"
10+
"github.com/loft-sh/devspace/pkg/devspace/config/constants"
11+
"github.com/loft-sh/devspace/pkg/util/command"
12+
"github.com/mitchellh/go-homedir"
13+
"github.com/otiai10/copy"
1614
)
1715

1816
func NewKubectlCommand() Command {
@@ -40,7 +38,21 @@ func (k *kubectlCommand) InstallPath() (string, error) {
4038
}
4139

4240
func (k *kubectlCommand) DownloadURL() string {
43-
url := kubectlDownload
41+
// let the default kubectl version be 1.22.0
42+
kubectlVersion := "v1.22.0"
43+
44+
// try to fetch latest kubectl version if it fails use default version
45+
res, err := http.Get("https://storage.googleapis.com/kubernetes-release/release/stable.txt")
46+
if err == nil {
47+
content, err := ioutil.ReadAll(res.Body)
48+
res.Body.Close()
49+
if err == nil {
50+
kubectlVersion = string(content)
51+
}
52+
}
53+
54+
url := "https://storage.googleapis.com/kubernetes-release/release/" + kubectlVersion + "/bin/" + runtime.GOOS + "/" + runtime.GOARCH + "/kubectl"
55+
4456
if runtime.GOOS == "windows" {
4557
url += ".exe"
4658
}

pkg/util/downloader/downloader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package downloader
22

33
import (
4-
"github.com/loft-sh/devspace/pkg/util/downloader/commands"
54
"io"
65
"io/ioutil"
76
"net/http"
87
"os"
98
"path/filepath"
109

10+
"github.com/loft-sh/devspace/pkg/util/downloader/commands"
11+
1112
logpkg "github.com/loft-sh/devspace/pkg/util/log"
1213
"github.com/pkg/errors"
1314
)

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.

0 commit comments

Comments
 (0)