Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubernetesapply: error if kubeconfig has not been resolved #6458

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions internal/controllers/core/kubernetesapply/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion internal/controllers/core/kubernetesapply/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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",
},
},
},
Expand Down