Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ba8b56b

Browse files
committedJun 21, 2022
added kind support
1 parent d5416e5 commit ba8b56b

File tree

7 files changed

+153
-2
lines changed

7 files changed

+153
-2
lines changed
 

‎e2e/tests/build/build.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"os"
10+
"os/exec"
1011
"strings"
1112

1213
"github.com/docker/docker/api/types"
@@ -78,6 +79,52 @@ var _ = DevSpaceDescribe("build", func() {
7879
framework.ExpectEqual(found, true, "image not found in cache")
7980
})
8081

82+
ginkgo.It("should build dockerfile with docker and load in kind cluster", func() {
83+
tempDir, err := framework.CopyToTempDir("tests/build/testdata/docker")
84+
framework.ExpectNoError(err)
85+
defer framework.CleanupTempDir(initialDir, tempDir)
86+
87+
// create build command
88+
buildCmd := &cmd.RunPipelineCmd{
89+
GlobalFlags: &flags.GlobalFlags{
90+
NoWarn: true,
91+
},
92+
SkipPush: true,
93+
Pipeline: "build",
94+
}
95+
err = buildCmd.RunDefault(f)
96+
framework.ExpectNoError(err)
97+
98+
// create devspace docker client to access docker APIs
99+
devspaceDockerClient, err := docker.NewClient(context.TODO(), log)
100+
framework.ExpectNoError(err)
101+
102+
dockerClient := devspaceDockerClient.DockerAPIClient()
103+
imageList, err := dockerClient.ImageList(ctx, types.ImageListOptions{})
104+
framework.ExpectNoError(err)
105+
106+
found := false
107+
Outer:
108+
for _, image := range imageList {
109+
for _, tag := range image.RepoTags {
110+
if tag == "my-docker-username/helloworld:latest" {
111+
found = true
112+
break Outer
113+
}
114+
}
115+
}
116+
framework.ExpectEqual(found, true, "image not found in cache")
117+
118+
var stdout, stderr bytes.Buffer
119+
cmd := exec.Command("kind", "load", "docker-image", "my-docker-username/helloworld:latest")
120+
cmd.Stdout = &stdout
121+
cmd.Stderr = &stderr
122+
err = cmd.Run()
123+
framework.ExpectNoError(err)
124+
err = stderrContains(stderr.String(), "found to be already present")
125+
framework.ExpectNoError(err)
126+
})
127+
81128
ginkgo.It("should build dockerfile with docker even when KUBECONFIG is invalid", func() {
82129
tempDir, err := framework.CopyToTempDir("tests/build/testdata/docker")
83130
framework.ExpectNoError(err)
@@ -135,6 +182,50 @@ var _ = DevSpaceDescribe("build", func() {
135182
_ = os.Unsetenv("KUBECONFIG")
136183
})
137184

185+
ginkgo.It("should build dockerfile with buildkit and load in kind cluster", func() {
186+
tempDir, err := framework.CopyToTempDir("tests/build/testdata/buildkit")
187+
framework.ExpectNoError(err)
188+
defer framework.CleanupTempDir(initialDir, tempDir)
189+
190+
// create build command
191+
buildCmd := &cmd.RunPipelineCmd{
192+
GlobalFlags: &flags.GlobalFlags{
193+
NoWarn: true,
194+
},
195+
SkipPush: true,
196+
Pipeline: "build",
197+
}
198+
err = buildCmd.RunDefault(f)
199+
framework.ExpectNoError(err)
200+
201+
// create devspace docker client to access docker APIs
202+
devspaceDockerClient, err := docker.NewClient(context.TODO(), log)
203+
framework.ExpectNoError(err)
204+
205+
dockerClient := devspaceDockerClient.DockerAPIClient()
206+
imageList, err := dockerClient.ImageList(ctx, types.ImageListOptions{})
207+
framework.ExpectNoError(err)
208+
209+
for _, image := range imageList {
210+
if len(image.RepoTags) > 0 && image.RepoTags[0] == "my-docker-username/helloworld-buildkit:latest" {
211+
err = nil
212+
break
213+
} else {
214+
err = errors.New("image not found")
215+
}
216+
}
217+
framework.ExpectNoError(err)
218+
219+
var stdout, stderr bytes.Buffer
220+
cmd := exec.Command("kind", "load", "docker-image", "my-docker-username/helloworld-buildkit:latest")
221+
cmd.Stdout = &stdout
222+
cmd.Stderr = &stderr
223+
err = cmd.Run()
224+
framework.ExpectNoError(err)
225+
err = stderrContains(stderr.String(), "found to be already present")
226+
framework.ExpectNoError(err)
227+
})
228+
138229
ginkgo.It("should build dockerfile with buildkit", func() {
139230
tempDir, err := framework.CopyToTempDir("tests/build/testdata/buildkit")
140231
framework.ExpectNoError(err)
@@ -424,3 +515,10 @@ func stdoutContains(stdout, content string) error {
424515
}
425516
return fmt.Errorf("%s found in output", content)
426517
}
518+
519+
func stderrContains(stderr, content string) error {
520+
if strings.Contains(stderr, content) {
521+
return nil
522+
}
523+
return fmt.Errorf("%s found in output", content)
524+
}

‎helper/server/chown_unsupported.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build windows
12
// +build windows
23

34
package server

‎pkg/devspace/build/builder/buildkit/buildkit.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,27 @@ func buildWithCLI(ctx context.Context, dir string, context io.Reader, writer io.
204204
return fmt.Errorf("error retrieving minikube environment with 'minikube docker-env --shell none'. Try setting the option preferMinikube to false: %v", err)
205205
}
206206
}
207+
err = command2.CommandWithEnv(ctx, dir, writer, writer, context, minikubeEnv, command[0], completeArgs...)
208+
if err != nil {
209+
return err
210+
}
211+
//load image if it is a kind-context
212+
if skipPush && kubeClient != nil && kubectl.IsKindContext(kubeClient.CurrentContext()) {
213+
if len(options.Tags) > 0 {
214+
for _, tag := range options.Tags {
215+
command := []string{"kind", "load", "docker-image", tag}
216+
completeArgs := []string{}
217+
completeArgs = append(completeArgs, command[1:]...)
218+
err = command2.CommandWithEnv(ctx, dir, writer, writer, nil, minikubeEnv, command[0], completeArgs...)
219+
if err != nil {
220+
log.Info(errors.Errorf("error during image load to kind cluster: %v", err))
221+
}
222+
log.Info("Image loaded to kind cluster")
223+
}
224+
}
225+
}
207226

208-
return command2.CommandWithEnv(ctx, dir, writer, writer, context, minikubeEnv, command[0], completeArgs...)
227+
return nil
209228
}
210229

211230
type NodeGroup struct {

‎pkg/devspace/build/builder/docker/docker.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
88
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
9+
command2 "github.com/loft-sh/devspace/pkg/util/command"
910
"github.com/sirupsen/logrus"
1011
"io"
1112
"os"
@@ -182,6 +183,27 @@ func (b *Builder) BuildImage(ctx devspacecontext.Context, contextPath, dockerfil
182183
}
183184
} else {
184185
ctx.Log().Infof("Skip image push for %s", b.helper.ImageName)
186+
//load image if it is a kind-context
187+
if ctx.KubeClient() != nil && kubectl.IsKindContext(ctx.KubeClient().CurrentContext()) {
188+
for _, tag := range buildOptions.Tags {
189+
command := []string{"kind", "load", "docker-image", tag}
190+
completeArgs := []string{}
191+
completeArgs = append(completeArgs, command[1:]...)
192+
// Determine output writer
193+
var writeCloser io.WriteCloser
194+
if ctx.Log() == logpkg.GetInstance() {
195+
writeCloser = logpkg.WithNopCloser(stdout)
196+
} else {
197+
writeCloser = ctx.Log().Writer(logrus.InfoLevel, false)
198+
}
199+
defer writeCloser.Close()
200+
err = command2.CommandWithEnv(ctx.Context(), ctx.WorkingDir(), writeCloser, writeCloser, nil, nil, command[0], completeArgs...)
201+
if err != nil {
202+
ctx.Log().Info(errors.Errorf("error during image load to kind cluster: %v", err))
203+
}
204+
ctx.Log().Info("Image loaded to kind cluster")
205+
}
206+
}
185207
}
186208

187209
return nil

‎pkg/devspace/kubectl/util.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
)
1818

1919
const minikubeContext = "minikube"
20+
const kindContext = "kind-kind"
2021
const dockerDesktopContext = "docker-desktop"
2122
const dockerForDesktopContext = "docker-for-desktop"
2223

@@ -201,11 +202,19 @@ func NewPortForwarder(client Client, pod *corev1.Pod, ports []string, addresses
201202

202203
// IsLocalKubernetes returns true if the context belongs to a local Kubernetes cluster
203204
func IsLocalKubernetes(context string) bool {
204-
if context == minikubeContext || context == dockerDesktopContext || context == dockerForDesktopContext {
205+
if context == minikubeContext ||
206+
context == kindContext ||
207+
context == dockerDesktopContext ||
208+
context == dockerForDesktopContext {
205209
return true
206210
} else if strings.HasPrefix(context, "vcluster_") && (strings.HasSuffix(context, minikubeContext) || strings.HasSuffix(context, dockerDesktopContext) || strings.HasSuffix(context, dockerForDesktopContext)) {
207211
return true
208212
}
209213

210214
return false
211215
}
216+
217+
// IsKindContext returns true if the context is a kind Kubernetes cluster
218+
func IsKindContext(context string) bool {
219+
return context == kindContext
220+
}

‎pkg/util/idle/idle_darwin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build darwin
12
// +build darwin
23

34
package idle

‎pkg/util/idle/idle_windows.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build windows
12
// +build windows
23

34
package idle

0 commit comments

Comments
 (0)
Please sign in to comment.