Skip to content

Commit

Permalink
status: Print Helm chart version in Helm mode
Browse files Browse the repository at this point in the history
Print Helm chart version as a part of the "cilium status" output.

Signed-off-by: Michi Mutsuzaki <[email protected]>
  • Loading branch information
michi-covalent committed Jun 6, 2023
1 parent 9f9f614 commit 411d6ef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,27 @@ func Upgrade(

return helmClient.RunWithContext(ctx, defaults.HelmReleaseName, params.Chart, params.Values)
}

// GetParameters contains parameters for helm get operation.
type GetParameters struct {
// Namespace in which the Helm release is installed.
Namespace string
// Name of the Helm release to get.
Name string
}

// Get returns the Helm release specified by GetParameters.
func Get(
k8sClient genericclioptions.RESTClientGetter,
params GetParameters,
) (*release.Release, error) {
actionConfig := action.Configuration{}
// Use the default Helm driver (Kubernetes secret).
helmDriver := ""
logger := func(format string, v ...interface{}) {}
if err := actionConfig.Init(k8sClient, params.Namespace, helmDriver, logger); err != nil {
return nil, err
}
helmClient := action.NewGet(&actionConfig)
return helmClient.Run(params.Name)
}
23 changes: 23 additions & 0 deletions status/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"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"
)

const (
Expand Down Expand Up @@ -534,6 +537,26 @@ 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.RESTClientGetter, 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{}
err := k.podStatus(ctx, status, defaults.AgentDaemonSetName, defaults.AgentPodSelector, func(ctx context.Context, status *Status, name string, pod *corev1.Pod) {
Expand Down
8 changes: 8 additions & 0 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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 @@ -106,6 +107,10 @@ type Status struct {
// status
CollectionErrors []error `json:"collection_errors,omitempty"`

// HelmChartVersion is the Helm chart version that is currently installed.
// For Helm mode only.
HelmChartVersion string `json:"helm_chart_version,omitempty"`

mutex *sync.Mutex
}

Expand Down Expand Up @@ -346,6 +351,9 @@ 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)
}
if len(s.ImageCount) > 0 {
header := "Image versions"
for name, imageCount := range s.ImageCount {
Expand Down

0 comments on commit 411d6ef

Please sign in to comment.