Skip to content

Commit 5eb2a4f

Browse files
authored
kubernetesapply: error if kubeconfig has not been resolved (#6458)
in theory, we shouldn't even start going down this codepath if the cluster connection hasn't been established, so let's make it a hard error and see what happens. Signed-off-by: Nick Santos <[email protected]>
1 parent b8a4d01 commit 5eb2a4f

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

internal/controllers/core/kubernetesapply/reconciler.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,18 @@ func (r *Reconciler) runYAMLDeploy(ctx context.Context, spec v1alpha1.Kubernetes
361361
return deployed, nil
362362
}
363363

364-
func (r *Reconciler) maybeInjectKubeconfig(cmd *model.Cmd, cluster *v1alpha1.Cluster) {
364+
func (r *Reconciler) maybeInjectKubeconfig(cmd *model.Cmd, cluster *v1alpha1.Cluster) error {
365365
if cluster == nil ||
366366
cluster.Status.Connection == nil ||
367367
cluster.Status.Connection.Kubernetes == nil {
368-
return
368+
return errors.New("no kubernetes connection")
369369
}
370370
kubeconfig := cluster.Status.Connection.Kubernetes.ConfigPath
371371
if kubeconfig == "" {
372-
return
372+
return fmt.Errorf("missing kubeconfig in cluster %s", cluster.Name)
373373
}
374374
cmd.Env = append(cmd.Env, fmt.Sprintf("KUBECONFIG=%s", kubeconfig))
375+
return nil
375376
}
376377

377378
func (r *Reconciler) runCmdDeploy(ctx context.Context, spec v1alpha1.KubernetesApplySpec,
@@ -395,7 +396,10 @@ func (r *Reconciler) runCmdDeploy(ctx context.Context, spec v1alpha1.KubernetesA
395396
if err != nil {
396397
return nil, err
397398
}
398-
r.maybeInjectKubeconfig(&cmd, cluster)
399+
err = r.maybeInjectKubeconfig(&cmd, cluster)
400+
if err != nil {
401+
return nil, err
402+
}
399403

400404
logger.Get(ctx).Infof("Running cmd: %s", cmd.String())
401405
exitCode, err := r.execer.Run(ctx, cmd, runIO)
@@ -889,7 +893,10 @@ func (r *Reconciler) bestEffortDelete(ctx context.Context, nn types.NamespacedNa
889893

890894
if toDelete.deleteCmd != nil {
891895
deleteCmd := toModelCmd(*toDelete.deleteCmd)
892-
r.maybeInjectKubeconfig(&deleteCmd, toDelete.cluster)
896+
err := r.maybeInjectKubeconfig(&deleteCmd, toDelete.cluster)
897+
if err != nil {
898+
l.Errorf("Error %s: %v", reason, err)
899+
}
893900
if err := localexec.OneShotToLogger(ctx, r.execer, deleteCmd); err != nil {
894901
l.Errorf("Error %s: %v", reason, err)
895902
}

internal/controllers/core/kubernetesapply/reconciler_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ func TestApplyCmdWithImages(t *testing.T) {
179179
assert.Equal(t, []string{
180180
"TILT_IMAGE_MAP_0=image-a",
181181
"TILT_IMAGE_0=image-a:my-tag",
182+
"KUBECONFIG=/path/to/default/kubeconfig",
182183
}, call.Cmd.Env)
183184
}
184185
}
@@ -800,7 +801,8 @@ func newFixture(t *testing.T) *fixture {
800801
Status: v1alpha1.ClusterStatus{
801802
Connection: &v1alpha1.ClusterConnectionStatus{
802803
Kubernetes: &v1alpha1.KubernetesClusterConnectionStatus{
803-
Context: "default",
804+
Context: "default",
805+
ConfigPath: "/path/to/default/kubeconfig",
804806
},
805807
},
806808
},

0 commit comments

Comments
 (0)