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

status: Print Helm chart version in Helm mode #1703

Merged
merged 1 commit into from
Jun 7, 2023
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
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