Skip to content

Commit e529fd6

Browse files
committed
refactor: Refactor downloadURL function of kubectl
1 parent 3b26f96 commit e529fd6

File tree

4 files changed

+60
-35
lines changed

4 files changed

+60
-35
lines changed

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

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,34 +74,49 @@ func NewKubectlBuilder(path string, config *latest.DeploymentConfig, context, na
7474
}
7575
}
7676

77-
func (k *kubectlBuilder) Build(manifest string, cmd RunCommand) ([]*unstructured.Unstructured, error) {
78-
args := []string{"create"}
79-
if k.context != "" && !k.isInCluster {
80-
args = append(args, "--context", k.context)
81-
}
82-
if k.namespace != "" {
83-
args = append(args, "--namespace", k.namespace)
84-
}
85-
77+
// this function is called in Build function
78+
// to decide the --dry-run value
79+
var useOldDryRun = func(path string) (bool, error) {
8680
// compare kubectl version for --dry-run flag value
87-
out, err := command.NewStreamCommand(k.path, []string{"version", "--client", "--short"}).Output()
81+
out, err := command.NewStreamCommand(path, []string{"version", "--client", "--short"}).Output()
8882
if err != nil {
89-
return nil, err
83+
return false, err
9084
}
9185

92-
versionSlice := strings.Split(string(out), "v")
93-
v1, err := version.NewVersion(strings.TrimSpace(versionSlice[1]))
86+
v1, err := version.NewVersion(strings.TrimPrefix(strings.TrimSpace(string(out)), "Client Version: v"))
9487
if err != nil {
9588

96-
return nil, err
89+
return false, err
9790
}
9891

9992
v2, err := version.NewVersion("1.18.0")
10093
if err != nil {
101-
return nil, err
94+
return false, err
10295
}
10396

10497
if v1.LessThan(v2) {
98+
return true, nil
99+
}
100+
101+
return false, nil
102+
}
103+
104+
func (k *kubectlBuilder) Build(manifest string, cmd RunCommand) ([]*unstructured.Unstructured, error) {
105+
args := []string{"create"}
106+
if k.context != "" && !k.isInCluster {
107+
args = append(args, "--context", k.context)
108+
}
109+
if k.namespace != "" {
110+
args = append(args, "--namespace", k.namespace)
111+
}
112+
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 {
105120
args = append(args, "--dry-run", "--output", "yaml", "--validate=false")
106121
} else {
107122
args = append(args, "--dry-run=client", "--output", "yaml", "--validate=false")

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: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package commands
22

33
import (
44
"io/ioutil"
5-
"log"
65
"net/http"
76
"path/filepath"
87
"runtime"
@@ -14,22 +13,6 @@ import (
1413
"github.com/otiai10/copy"
1514
)
1615

17-
var (
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"
31-
)
32-
3316
func NewKubectlCommand() Command {
3417
return &kubectlCommand{}
3518
}
@@ -55,7 +38,21 @@ func (k *kubectlCommand) InstallPath() (string, error) {
5538
}
5639

5740
func (k *kubectlCommand) DownloadURL() string {
58-
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+
5956
if runtime.GOOS == "windows" {
6057
url += ".exe"
6158
}

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
)

0 commit comments

Comments
 (0)