diff --git a/internal/controllers/core/kubernetesapply/reconciler.go b/internal/controllers/core/kubernetesapply/reconciler.go index b223e30627..1a434f39f0 100644 --- a/internal/controllers/core/kubernetesapply/reconciler.go +++ b/internal/controllers/core/kubernetesapply/reconciler.go @@ -361,17 +361,18 @@ func (r *Reconciler) runYAMLDeploy(ctx context.Context, spec v1alpha1.Kubernetes return deployed, nil } -func (r *Reconciler) maybeInjectKubeconfig(cmd *model.Cmd, cluster *v1alpha1.Cluster) { +func (r *Reconciler) maybeInjectKubeconfig(cmd *model.Cmd, cluster *v1alpha1.Cluster) error { if cluster == nil || cluster.Status.Connection == nil || cluster.Status.Connection.Kubernetes == nil { - return + return errors.New("no kubernetes connection") } kubeconfig := cluster.Status.Connection.Kubernetes.ConfigPath if kubeconfig == "" { - return + return fmt.Errorf("missing kubeconfig in cluster %s", cluster.Name) } cmd.Env = append(cmd.Env, fmt.Sprintf("KUBECONFIG=%s", kubeconfig)) + return nil } func (r *Reconciler) runCmdDeploy(ctx context.Context, spec v1alpha1.KubernetesApplySpec, @@ -395,7 +396,10 @@ func (r *Reconciler) runCmdDeploy(ctx context.Context, spec v1alpha1.KubernetesA if err != nil { return nil, err } - r.maybeInjectKubeconfig(&cmd, cluster) + err = r.maybeInjectKubeconfig(&cmd, cluster) + if err != nil { + return nil, err + } logger.Get(ctx).Infof("Running cmd: %s", cmd.String()) exitCode, err := r.execer.Run(ctx, cmd, runIO) @@ -889,7 +893,10 @@ func (r *Reconciler) bestEffortDelete(ctx context.Context, nn types.NamespacedNa if toDelete.deleteCmd != nil { deleteCmd := toModelCmd(*toDelete.deleteCmd) - r.maybeInjectKubeconfig(&deleteCmd, toDelete.cluster) + err := r.maybeInjectKubeconfig(&deleteCmd, toDelete.cluster) + if err != nil { + l.Errorf("Error %s: %v", reason, err) + } if err := localexec.OneShotToLogger(ctx, r.execer, deleteCmd); err != nil { l.Errorf("Error %s: %v", reason, err) } diff --git a/internal/controllers/core/kubernetesapply/reconciler_test.go b/internal/controllers/core/kubernetesapply/reconciler_test.go index a9fb97b10d..85c98e1ce2 100644 --- a/internal/controllers/core/kubernetesapply/reconciler_test.go +++ b/internal/controllers/core/kubernetesapply/reconciler_test.go @@ -179,6 +179,7 @@ func TestApplyCmdWithImages(t *testing.T) { assert.Equal(t, []string{ "TILT_IMAGE_MAP_0=image-a", "TILT_IMAGE_0=image-a:my-tag", + "KUBECONFIG=/path/to/default/kubeconfig", }, call.Cmd.Env) } } @@ -800,7 +801,8 @@ func newFixture(t *testing.T) *fixture { Status: v1alpha1.ClusterStatus{ Connection: &v1alpha1.ClusterConnectionStatus{ Kubernetes: &v1alpha1.KubernetesClusterConnectionStatus{ - Context: "default", + Context: "default", + ConfigPath: "/path/to/default/kubeconfig", }, }, },