Skip to content

Commit

Permalink
utils: Remove IsInHelmMode()
Browse files Browse the repository at this point in the history
Finally it's over.

Ref: #2327

Signed-off-by: Michi Mutsuzaki <[email protected]>
  • Loading branch information
michi-covalent committed Feb 27, 2024
1 parent 54d6b78 commit d0bd727
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 154 deletions.
2 changes: 1 addition & 1 deletion connectivity/check/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (ct *ConnectivityTest) generateManifestsNodeAffinity(ctx context.Context, h
}

func (ct *ConnectivityTest) generateDefaultHelmState(ctx context.Context, client *k8s.Client, namespace string) (*helm.State, error) {

Check warning on line 184 in connectivity/check/delete.go

View workflow job for this annotation

GitHub Actions / build

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
version, err := client.GetRunningCiliumVersion(ctx, namespace)
version, err := client.GetRunningCiliumVersion(namespace)
if version == "" || err != nil {
return nil, fmt.Errorf("unable to obtain cilium version, no Cilium pods found in namespace %q", namespace)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/cli/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cmd

import (
"context"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -44,7 +43,7 @@ func newCmdVersion() *cobra.Command {
if clientOnly {
return nil
}
version, err := k8sClient.GetRunningCiliumVersion(context.Background(), namespace)
version, err := k8sClient.GetRunningCiliumVersion(namespace)
if err != nil {
fmt.Printf("cilium image (running): unknown. Unable to obtain cilium version. Reason: %s\n", err.Error())
} else {
Expand Down
8 changes: 0 additions & 8 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package utils

import (
"fmt"
"os"
"strconv"

"github.com/blang/semver/v4"
Expand All @@ -15,13 +14,6 @@ func ParseCiliumVersion(version string) (semver.Version, error) {
return semver.ParseTolerant(version)
}

const CLIModeVariableName = "CILIUM_CLI_MODE"

// IsInHelmMode returns true if cilium-cli is in "helm" mode. Otherwise, it returns false.
func IsInHelmMode() bool {
return os.Getenv(CLIModeVariableName) != "classic"
}

func MustParseBool(v string) bool {
b, err := strconv.ParseBool(v)
if err != nil {
Expand Down
15 changes: 0 additions & 15 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
package utils

import (
"os"
"reflect"
"testing"

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

func TestParseCiliumVersion(t *testing.T) {
Expand Down Expand Up @@ -62,16 +60,3 @@ func TestParseCiliumVersion(t *testing.T) {
})
}
}

func TestIsInHelmMode(t *testing.T) {
orig := os.Getenv(CLIModeVariableName)
defer func() {
assert.NoError(t, os.Setenv(CLIModeVariableName, orig))
}()
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.True(t, IsInHelmMode())
}
74 changes: 7 additions & 67 deletions k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
Expand All @@ -18,7 +17,6 @@ import (
"time"

"github.com/blang/semver/v4"
"github.com/distribution/reference"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/cli/output"
Expand Down Expand Up @@ -957,47 +955,6 @@ func (c *Client) GetLogs(ctx context.Context, namespace, name, container string,
return b.String(), nil
}

func getCiliumVersionFromImage(image string) (string, error) {
// default to "latest" as k8s may not include it explicitly
version := "latest"

ref, err := reference.Parse(image)
if err != nil {
// Image has an invalid name, skip it
return "", err
}

tagged, isTagged := ref.(reference.Tagged)
if isTagged {
version = tagged.Tag()
}

named, isNamed := ref.(reference.Named)
if isNamed {
path := reference.Path(named)
strs := strings.Split(path, "/")

// Take the last element as an image name
imageName := strs[len(strs)-1]
if !strings.HasPrefix(imageName, "cilium") {
// Not likely to be Cilium
return "", fmt.Errorf("image name %s is not prefixed with cilium", imageName)
}

// Add any part in the pod image separated by a '-` to the version,
// e.g., "quay.io/cilium/cilium-ci:1234" -> "-ci:1234"
dash := strings.Index(imageName, "-")
if dash >= 0 {
version = imageName[dash:] + ":" + version
}
} else {
// Image somehow doesn't contain name, skip it.
return "", fmt.Errorf("image does't contain name")
}

return version, nil
}

// GetCiliumVersion returns a semver.Version representing the version of cilium
// running in the cilium-agent pod
func (c *Client) GetCiliumVersion(ctx context.Context, p *corev1.Pod) (*semver.Version, error) {
Expand All @@ -1021,32 +978,15 @@ func (c *Client) GetCiliumVersion(ctx context.Context, p *corev1.Pod) (*semver.V
return &podVersion, nil
}

func (c *Client) GetRunningCiliumVersion(ctx context.Context, namespace string) (string, error) {
if utils.IsInHelmMode() {
release, err := helm.Get(c.HelmActionConfig, helm.GetParameters{
Namespace: namespace,
Name: defaults.HelmReleaseName,
})
if err != nil {
return "", err
}
return release.Chart.Metadata.Version, nil
}
pods, err := c.ListPods(ctx, namespace, metav1.ListOptions{LabelSelector: defaults.AgentPodSelector})
func (c *Client) GetRunningCiliumVersion(namespace string) (string, error) {
release, err := helm.Get(c.HelmActionConfig, helm.GetParameters{
Namespace: namespace,
Name: defaults.HelmReleaseName,
})
if err != nil {
return "", fmt.Errorf("unable to list cilium pods: %w", err)
}
if len(pods.Items) > 0 && len(pods.Items[0].Spec.Containers) > 0 {
for _, container := range pods.Items[0].Spec.Containers {
version, err := getCiliumVersionFromImage(container.Image)
if err != nil {
continue
}
return version, nil
}
return "", errors.New("unable to obtain cilium version: no cilium container found")
return "", err
}
return "", errors.New("unable to obtain cilium version: no cilium pods found")
return release.Chart.Metadata.Version, nil
}

func (c *Client) ListCiliumLoadBalancerIPPools(ctx context.Context, opts metav1.ListOptions) (*ciliumv2alpha1.CiliumLoadBalancerIPPoolList, error) {
Expand Down
37 changes: 0 additions & 37 deletions k8s/client_test.go

This file was deleted.

37 changes: 17 additions & 20 deletions status/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

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

Expand Down Expand Up @@ -512,25 +511,23 @@ func (k *K8sStatusCollector) status(ctx context.Context) *Status {
},
}

if utils.IsInHelmMode() {
tasks = append(tasks, statusTask{
name: "Helm chart version",
task: func(_ context.Context) error {
client, ok := k.client.(*k8s.Client)
if !ok {
return fmt.Errorf("failed to initialize Helm client")
}
release, err := helm.Get(client.HelmActionConfig, helm.GetParameters{
Namespace: k.params.Namespace,
Name: defaults.HelmReleaseName,
})
if err != nil {
return err
}
status.HelmChartVersion = release.Chart.Metadata.Version
return nil
}})
}
tasks = append(tasks, statusTask{
name: "Helm chart version",
task: func(_ context.Context) error {
client, ok := k.client.(*k8s.Client)
if !ok {
return fmt.Errorf("failed to initialize Helm client")
}
release, err := helm.Get(client.HelmActionConfig, helm.GetParameters{
Namespace: k.params.Namespace,
Name: defaults.HelmReleaseName,
})
if err != nil {
return err
}
status.HelmChartVersion = release.Chart.Metadata.Version
return nil
}})

// for the sake of sanity, don't get pod logs more than once
var agentLogsOnce = sync.Once{}
Expand Down
5 changes: 1 addition & 4 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/cilium/cilium/api/v1/models"

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

const (
Expand Down Expand Up @@ -380,9 +379,7 @@ func (s *Status) Format() string {

fmt.Fprintf(w, "Cluster Pods:\t%s\n", formatPodsCount(s.PodsCount))

if utils.IsInHelmMode() {
fmt.Fprintf(w, "Helm chart version:\t%s\n", s.HelmChartVersion)
}
fmt.Fprintf(w, "Helm chart version:\t%s\n", s.HelmChartVersion)
if len(s.ImageCount) > 0 {
header := "Image versions"
for name, imageCount := range s.ImageCount {
Expand Down

0 comments on commit d0bd727

Please sign in to comment.