Skip to content

Commit

Permalink
Add a function to check if the CLI is in Helm mode
Browse files Browse the repository at this point in the history
There are multiple places where we check if the CLI is in Helm mode. Add
a function utils.IsInHelmMode() so that this logic is in a single place.

Signed-off-by: Michi Mutsuzaki <[email protected]>
  • Loading branch information
michi-covalent committed May 9, 2023
1 parent 4e66985 commit 840b570
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion internal/cli/cmd/clustermesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/cilium/cilium-cli/clustermesh"
"github.com/cilium/cilium-cli/defaults"
"github.com/cilium/cilium-cli/internal/utils"
"github.com/cilium/cilium-cli/status"
)

Expand All @@ -32,7 +33,7 @@ func newCmdClusterMesh() *cobra.Command {
newCmdClusterMeshExternalWorkload(),
)

if os.Getenv("CILIUM_CLI_MODE") == "helm" {
if utils.IsInHelmMode() {
cmd.AddCommand(
newCmdClusterMeshEnableWithHelm(),
newCmdClusterMeshDisableWithHelm(),
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/spf13/cobra"

"github.com/cilium/cilium-cli/internal/utils"
"github.com/cilium/cilium-cli/k8s"
)

Expand Down Expand Up @@ -79,7 +80,7 @@ cilium connectivity test`,
newCmdSysdump(),
newCmdVersion(),
)
if os.Getenv("CILIUM_CLI_MODE") == "helm" {
if utils.IsInHelmMode() {
cmd.AddCommand(
newCmdInstallWithHelm(),
newCmdUninstallWithHelm(),
Expand Down
3 changes: 2 additions & 1 deletion internal/cli/cmd/hubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/cilium/cilium-cli/defaults"
"github.com/cilium/cilium-cli/hubble"
"github.com/cilium/cilium-cli/internal/utils"

"github.com/spf13/cobra"
)
Expand All @@ -24,7 +25,7 @@ func newCmdHubble() *cobra.Command {
newCmdPortForwardCommand(),
newCmdUI(),
)
if os.Getenv("CILIUM_CLI_MODE") == "helm" {
if utils.IsInHelmMode() {
cmd.AddCommand(
newCmdHubbleEnableWithHelm(),
newCmdHubbleDisableWithHelm(),
Expand Down
7 changes: 7 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package utils
import (
"context"
"fmt"
"os"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -33,6 +34,7 @@ type ImagePathMode int
const (
ImagePathExcludeDigest ImagePathMode = iota
ImagePathIncludeDigest
CLIModeVariableName = "CILIUM_CLI_MODE"
)

var imageRegexp = regexp.MustCompile(`\A(.*?)(?:(:.*?)(@sha256:[0-9a-f]{64})?)?\z`)
Expand Down Expand Up @@ -172,3 +174,8 @@ func Contains(l []string, v string) bool {
}
return false
}

// IsInHelmMode returns true if cilium-cli is in "helm" mode. Otherwise, it returns false.
func IsInHelmMode() bool {
return os.Getenv(CLIModeVariableName) == "helm"
}
13 changes: 13 additions & 0 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package utils

import (
"fmt"
"os"
"reflect"
"testing"

"github.com/blang/semver/v4"
"github.com/stretchr/testify/assert"
)

func TestCheckVersion(t *testing.T) {
Expand Down Expand Up @@ -203,3 +205,14 @@ func TestBuildImagePath(t *testing.T) {
})
}
}

func TestIsInHelmMode(t *testing.T) {
orig := os.Getenv(CLIModeVariableName)
assert.NoError(t, os.Setenv(CLIModeVariableName, "helm"))
assert.True(t, IsInHelmMode())
assert.NoError(t, os.Setenv(CLIModeVariableName, "classic"))
assert.False(t, IsInHelmMode())
assert.NoError(t, os.Setenv(CLIModeVariableName, "random"))
assert.False(t, IsInHelmMode())
assert.NoError(t, os.Setenv(CLIModeVariableName, orig))
}

0 comments on commit 840b570

Please sign in to comment.