Skip to content

Commit 1ff15dd

Browse files
authored
Merge pull request #2067 from tukobadnyanoba/skip-k8s-config
removed fake k8s client
2 parents 6dde63b + 276d71e commit 1ff15dd

13 files changed

Lines changed: 118 additions & 46 deletions

File tree

cmd/run_pipeline.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/loft-sh/devspace/pkg/devspace/devpod"
1818
"github.com/loft-sh/devspace/pkg/devspace/hook"
1919
"github.com/loft-sh/devspace/pkg/devspace/kubectl"
20-
fakekube "github.com/loft-sh/devspace/pkg/devspace/kubectl/testing"
2120
"github.com/loft-sh/devspace/pkg/devspace/pipeline"
2221
"github.com/loft-sh/devspace/pkg/devspace/pipeline/types"
2322
"github.com/loft-sh/devspace/pkg/devspace/plugin"
@@ -32,7 +31,6 @@ import (
3231
"gopkg.in/yaml.v3"
3332
"io"
3433
"io/ioutil"
35-
"k8s.io/client-go/kubernetes/fake"
3634
"os"
3735
)
3836

@@ -256,7 +254,7 @@ func (cmd *RunPipelineCmd) Run(cobraCmd *cobra.Command, args []string, f factory
256254
cmd.Ctx = values.WithFlags(cmd.Ctx, cobraCmd.Flags())
257255
}
258256
options := cmd.BuildOptions(cmd.ToConfigOptions())
259-
ctx, err := initialize(cmd.Ctx, f, false, options, cmd.Log)
257+
ctx, err := initialize(cmd.Ctx, f, options, cmd.Log)
260258
if err != nil {
261259
return err
262260
}
@@ -277,7 +275,7 @@ type CommandOptions struct {
277275
UIPort int
278276
}
279277

280-
func initialize(ctx context.Context, f factory.Factory, allowFailingKubeClient bool, options *CommandOptions, logger log.Logger) (devspacecontext.Context, error) {
278+
func initialize(ctx context.Context, f factory.Factory, options *CommandOptions, logger log.Logger) (devspacecontext.Context, error) {
281279
// start file logging
282280
log.StartFileLogging()
283281

@@ -305,18 +303,9 @@ func initialize(ctx context.Context, f factory.Factory, allowFailingKubeClient b
305303
// create kubectl client
306304
client, err := f.NewKubeClientFromContext(options.KubeContext, options.Namespace)
307305
if err != nil {
308-
if allowFailingKubeClient {
309-
logger.Warnf("Unable to create new kubectl client: %v", err)
310-
logger.Warn("Using fake client to render resources")
311-
logger.WriteString(logrus.WarnLevel, "\n")
312-
313-
kube := fake.NewSimpleClientset()
314-
client = &fakekube.Client{
315-
Client: kube,
316-
}
317-
} else {
318-
return nil, errors.Errorf("error creating Kubernetes client: %v. Please make sure you have a valid Kubernetes context that points to a working Kubernetes cluster. If in doubt, please check if the following command works locally: `kubectl get namespaces`", err)
319-
}
306+
logger.Warnf("Unable to create new kubectl client: %v", err)
307+
logger.WriteString(logrus.WarnLevel, "\n")
308+
client = nil
320309
}
321310

322311
// load generated config
@@ -325,11 +314,13 @@ func initialize(ctx context.Context, f factory.Factory, allowFailingKubeClient b
325314
return nil, errors.Errorf("error loading local cache: %v", err)
326315
}
327316

328-
// If the current kube context or namespace is different than old,
329-
// show warnings and reset kube client if necessary
330-
client, err = kubectl.CheckKubeContext(client, localCache, options.NoWarn, options.SwitchContext, logger)
331-
if err != nil {
332-
return nil, err
317+
if client != nil {
318+
// If the current kube context or namespace is different than old,
319+
// show warnings and reset kube client if necessary
320+
client, err = kubectl.CheckKubeContext(client, localCache, options.NoWarn, options.SwitchContext, logger)
321+
if err != nil {
322+
return nil, err
323+
}
333324
}
334325

335326
// load config
@@ -370,15 +361,17 @@ func initialize(ctx context.Context, f factory.Factory, allowFailingKubeClient b
370361

371362
func updateLastKubeContext(ctx devspacecontext.Context) error {
372363
// Update generated if we deploy the application
373-
if ctx.Config() != nil && ctx.Config().LocalCache() != nil {
374-
ctx.Config().LocalCache().SetLastContext(&localcache.LastContextConfig{
375-
Context: ctx.KubeClient().CurrentContext(),
376-
Namespace: ctx.KubeClient().Namespace(),
377-
})
378-
379-
err := ctx.Config().LocalCache().Save()
380-
if err != nil {
381-
return errors.Wrap(err, "save generated")
364+
if ctx.KubeClient() != nil {
365+
if ctx.Config() != nil && ctx.Config().LocalCache() != nil {
366+
ctx.Config().LocalCache().SetLastContext(&localcache.LastContextConfig{
367+
Context: ctx.KubeClient().CurrentContext(),
368+
Namespace: ctx.KubeClient().Namespace(),
369+
})
370+
371+
err := ctx.Config().LocalCache().Save()
372+
if err != nil {
373+
return errors.Wrap(err, "save generated")
374+
}
382375
}
383376
}
384377

e2e/tests/build/build.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,63 @@ var _ = DevSpaceDescribe("build", func() {
7878
framework.ExpectEqual(found, true, "image not found in cache")
7979
})
8080

81+
ginkgo.It("should build dockerfile with docker even when KUBECONFIG is invalid", func() {
82+
tempDir, err := framework.CopyToTempDir("tests/build/testdata/docker")
83+
framework.ExpectNoError(err)
84+
defer framework.CleanupTempDir(initialDir, tempDir)
85+
86+
_ = os.Setenv("KUBECONFIG", "i-am-invalid-config")
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+
_ = os.Unsetenv("KUBECONFIG")
118+
})
119+
120+
ginkgo.It("should not build dockerfile with kaniko when KUBECONFIG is invalid", func() {
121+
tempDir, err := framework.CopyToTempDir("tests/build/testdata/kaniko")
122+
framework.ExpectNoError(err)
123+
defer framework.CleanupTempDir(initialDir, tempDir)
124+
_ = os.Setenv("KUBECONFIG", "i-am-invalid-config")
125+
// create build command
126+
buildCmd := &cmd.RunPipelineCmd{
127+
GlobalFlags: &flags.GlobalFlags{
128+
NoWarn: true,
129+
},
130+
SkipPush: true,
131+
Pipeline: "build",
132+
}
133+
err = buildCmd.RunDefault(f)
134+
framework.ExpectError(err)
135+
_ = os.Unsetenv("KUBECONFIG")
136+
})
137+
81138
ginkgo.It("should build dockerfile with buildkit", func() {
82139
tempDir, err := framework.CopyToTempDir("tests/build/testdata/buildkit")
83140
framework.ExpectNoError(err)

pkg/devspace/dependency/registry/registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (d *dependencyRegistry) MarkDependenciesExcluded(ctx devspacecontext.Contex
133133

134134
func (d *dependencyRegistry) excludeDependencies(ctx devspacecontext.Context, dependencyNames []string, forceLeader bool, retries int) (map[string]bool, error) {
135135
retMap := map[string]bool{}
136-
if len(dependencyNames) == 0 {
136+
if len(dependencyNames) == 0 || ctx.KubeClient() == nil {
137137
return retMap, nil
138138
}
139139

pkg/devspace/kubectl/util/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func NewClientByContext(context, namespace string, switchContext bool, kubeLoade
2727
// try to load in cluster config
2828
config, err := rest.InClusterConfig()
2929
if err != nil {
30-
return nil, "", "", false, errors.Errorf("kube config is invalid: please make sure you have an existing valid kube config. You might want to check one of the following things:\n\n* Make sure you can use 'kubectl get namespaces' locally\n* If you are using Loft, you might want to run 'devspace create space' or 'loft create space'\n")
30+
return nil, "", "", false, errors.Errorf("kube config is invalid")
3131
}
3232

3333
currentNamespace, err := inClusterNamespace()

pkg/devspace/pipeline/engine/pipelinehandler/commands/build_images.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ type BuildImagesOptions struct {
2424

2525
func BuildImages(ctx devspacecontext.Context, pipeline types.Pipeline, args []string) error {
2626
ctx.Log().Debugf("build_images %s", strings.Join(args, " "))
27-
err := pipeline.Exclude(ctx)
28-
if err != nil {
29-
return err
27+
if ctx.KubeClient() != nil {
28+
err := pipeline.Exclude(ctx)
29+
if err != nil {
30+
return err
31+
}
3032
}
3133

3234
options := &BuildImagesOptions{
3335
Options: pipeline.Options().BuildOptions,
3436
}
35-
args, err = flags.ParseArgs(options, args)
37+
args, err := flags.ParseArgs(options, args)
3638
if err != nil {
3739
return errors.Wrap(err, "parse args")
3840
}

pkg/devspace/pipeline/engine/pipelinehandler/commands/create_deployments.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ type CreateDeploymentsOptions struct {
2929
All bool `long:"all" description:"Deploy all deployments"`
3030
}
3131

32+
const ErrMsg = "Please make sure you have an existing valid kube config. You might want to check one of the following things:\n\n* Make sure you can use 'kubectl get namespaces' locally\n* If you are using Loft, you might want to run 'devspace create space' or 'loft create space'\n"
33+
3234
func CreateDeployments(ctx devspacecontext.Context, pipeline types.Pipeline, args []string, stdout io.Writer) error {
3335
ctx.Log().Debugf("create_deployments %s", strings.Join(args, " "))
3436
err := pipeline.Exclude(ctx)
3537
if err != nil {
3638
return err
3739
}
38-
40+
if ctx.KubeClient() == nil {
41+
return errors.Errorf(ErrMsg)
42+
}
3943
options := &CreateDeploymentsOptions{
4044
Options: pipeline.Options().DeployOptions,
4145
}

pkg/devspace/pipeline/engine/pipelinehandler/commands/ensure_pull_secrets.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ func EnsurePullSecrets(ctx devspacecontext.Context, pipeline types.Pipeline, arg
2727
if err != nil {
2828
return err
2929
}
30-
30+
if ctx.KubeClient() == nil {
31+
return errors.Errorf(ErrMsg)
32+
}
3133
options := &EnsurePullSecretsOptions{}
3234
args, err = flags.ParseArgs(options, args)
3335
if err != nil {

pkg/devspace/pipeline/engine/pipelinehandler/commands/exec_container.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ type ExecContainerOptions struct {
2323

2424
func ExecContainer(ctx devspacecontext.Context, args []string) error {
2525
hc := interp.HandlerCtx(ctx.Context())
26+
if ctx.KubeClient() == nil {
27+
return errors.Errorf(ErrMsg)
28+
}
2629
options := &ExecContainerOptions{
2730
Namespace: ctx.KubeClient().Namespace(),
2831
}

pkg/devspace/pipeline/engine/pipelinehandler/commands/purge_deployments.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ func PurgeDeployments(ctx devspacecontext.Context, pipeline types.Pipeline, args
2727
if err != nil {
2828
return err
2929
}
30-
30+
if ctx.KubeClient() == nil {
31+
return errors.Errorf(ErrMsg)
32+
}
3133
options := &PurgeDeploymentsOptions{
3234
PurgeOptions: pipeline.Options().PurgeOptions,
3335
}

pkg/devspace/pipeline/engine/pipelinehandler/commands/select_pod.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ type SelectPodOptions struct {
2222

2323
func SelectPod(ctx devspacecontext.Context, args []string) error {
2424
hc := interp.HandlerCtx(ctx.Context())
25+
if ctx.KubeClient() == nil {
26+
return errors.Errorf(ErrMsg)
27+
}
2528
options := &SelectPodOptions{
2629
Namespace: ctx.KubeClient().Namespace(),
2730
}

0 commit comments

Comments
 (0)