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

feat:plugin version will show the operator's version #1781

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Changes from 1 commit
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
42 changes: 41 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,35 @@ 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()
jiuker marked this conversation as resolved.
Show resolved Hide resolved
// 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)
jiuker marked this conversation as resolved.
Show resolved Hide resolved
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 != "" {
fmt.Printf("Minio-Operator Version: %s/%s:%s \r\n", item.Namespace, item.Name, strings.SplitN(image, ":", 2)[1])
jiuker marked this conversation as resolved.
Show resolved Hide resolved
}
}
return nil
}