From 6b97183f1526c45189396011b28e539345885565 Mon Sep 17 00:00:00 2001 From: Aliaksandr Panasiuk Date: Mon, 24 Jun 2024 17:47:55 +0200 Subject: [PATCH] #12 - fix a "panic" log output --- commands/release_category.go | 23 ++++++++++++++++++----- docs/release-notes.md | 2 +- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/commands/release_category.go b/commands/release_category.go index 7b2f8e7..79e3c08 100644 --- a/commands/release_category.go +++ b/commands/release_category.go @@ -263,7 +263,11 @@ func (rc *ReleaseCommands) releaseHelmfile(args ...string) error { } func (rc *ReleaseCommands) getKubeContext() (string, string, error) { - var contextNames []string + var ( + contextNames []string + contextName string + ) + kubeConfig := &KubeConfig{} rc.SpecCMD = rc.kubeConfig() @@ -287,17 +291,22 @@ func (rc *ReleaseCommands) getKubeContext() (string, string, error) { } } - if len(contextNames) > 1 { + switch { + case len(contextNames) > 1: return "", "", fmt.Errorf("detected more than one Kubernetes context with names %s leading to conflict, "+ "please delete or rename all contexts except one", strings.Join(contextNames, ", ")) + case len(contextNames) > 0: + contextName = contextNames[0] + default: + contextName = "" } - if rc.K3DCluster && len(contextNames) == 1 && !strings.Contains(contextNames[0], system.K3DConfigPrefix) { - return "", "", fmt.Errorf("remote Kubernetes context already exists %s for this branch", contextNames[0]) + if rc.K3DCluster && len(contextName) > 0 && !strings.Contains(contextName, system.K3DConfigPrefix) { + return "", "", fmt.Errorf("remote Kubernetes context already exists %s for this branch", contextName) } - return contextNames[0], kubeConfig.CurrentContext, nil + return contextName, kubeConfig.CurrentContext, nil } func (rc *ReleaseCommands) releaseKubeContext() error { @@ -317,6 +326,10 @@ func (rc *ReleaseCommands) releaseKubeContext() error { return nil } + if strings.Contains(contextName, system.K3DConfigPrefix) && rc.UpdateContext { + return fmt.Errorf("current context %s already used for K3D cluster, --force flag cannot be used", contextName) + } + cc := &ClusterCommands{ Conf: rc.Conf, Ctx: rc.Ctx, diff --git a/docs/release-notes.md b/docs/release-notes.md index 77c1e44..91b55fa 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -1 +1 @@ -- #12 - Implemented logic for strict selection of Kubernetes contexts to prevent conflicts during context selection. +- #12 - Fixed a "panic" log output that occurred when the Kubernetes context was not created before.