Skip to content

Commit

Permalink
feat:plugin version will show the operator's version (#1781)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiuker authored Oct 7, 2023
1 parent 52bd365 commit dbdf9d7
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion kubectl-minio/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@
package cmd

import (
"context"
"fmt"
"io"
"os"
"strings"

v1 "k8s.io/api/apps/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"

"github.com/minio/kubectl-minio/cmd/helpers"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,6 +56,10 @@ func newVersionCmd(out io.Writer, errOut io.Writer) *cobra.Command {
Example: operatorVersionExample,
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
path, _ := rootCmd.Flags().GetString(kubeconfig)
if path != "" {
os.Setenv(clientcmd.RecommendedConfigPathEnvVar, path)
}
err := o.run()
if err != nil {
klog.Warning(err)
Expand All @@ -64,6 +75,40 @@ func newVersionCmd(out io.Writer, errOut io.Writer) *cobra.Command {

// run initializes local config and installs MinIO Operator to Kubernetes cluster.
func (o *operatorVersionCmd) run() error {
fmt.Println(version)
fmt.Println("Kubectl-Plugin Version:", version)
cfg := config.GetConfigOrDie()
// If config is passed as a flag use that instead
k8sClient, err := client.New(cfg, client.Options{})
if err != nil {
return err
}
deployList := &v1.DeploymentList{}
listOpt := &client.ListOptions{}
client.MatchingLabels{"app.kubernetes.io/name": "minio-operator"}.ApplyToList(listOpt)
err = k8sClient.List(context.Background(), deployList, listOpt)
if err != nil {
return err
}
for _, item := range deployList.Items {
image := ""
if len(item.Spec.Template.Spec.Containers) == 1 {
image = item.Spec.Template.Spec.Containers[0].Image
} else {
for _, container := range item.Spec.Template.Spec.Containers {
if strings.Contains(container.Image, "operator") {
image = container.Image
break
}
}
}
if image != "" {
if item.Name == "console" {
fmt.Printf("Minio-Operator Console Version: %s/%s:%s \r\n", item.Namespace, item.Name, strings.SplitN(image, ":", 2)[1])
}
if item.Name == "minio-operator" {
fmt.Printf("Minio-Operator Controller Version: %s/%s:%s \r\n", item.Namespace, item.Name, strings.SplitN(image, ":", 2)[1])
}
}
}
return nil
}

0 comments on commit dbdf9d7

Please sign in to comment.