Skip to content

Commit

Permalink
Add Version Command
Browse files Browse the repository at this point in the history
Displays version info of the cli and running kubearmor image

Signed-off-by: Barun Acharya <barun.acharya@accuknox.com>
  • Loading branch information
daemon1024 committed Sep 21, 2021
1 parent 701880e commit bb0577c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
CURDIR=$(shell pwd)
VERSION=$(shell git describe --tags --dirty --always)

.PHONY: build
build:
cd $(CURDIR); go mod tidy; go build -o karmor
cd $(CURDIR)
go mod tidy
CGO_ENABLED=0 go build \
-ldflags "-w -s -X github.com/kubearmor/kubearmor-client/version.version=${VERSION}" \
-o karmor
26 changes: 26 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2021 Authors of KubeArmor

package cmd

import (
"github.com/kubearmor/kubearmor-client/version"
"github.com/spf13/cobra"
)

// versionCmd represents the get command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Display version information",
Long: `Display version information`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := version.PrintVersion(client); err != nil {
return err
}
return nil
},
}

func init() {
rootCmd.AddCommand(versionCmd)
}
41 changes: 41 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2021 Authors of KubeArmor

package version

import (
"context"
"fmt"
"runtime"

"github.com/kubearmor/kubearmor-client/k8s"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var version string

func PrintVersion(c *k8s.Client) error {
fmt.Printf("karmor version %s %s/%s\n", version, runtime.GOOS, runtime.GOARCH)
kubearmorVersion, err := getKubeArmorVersion(c)
if err != nil {
return nil
}
if kubearmorVersion == "" {
fmt.Printf("kubearmor not running\n")
return nil
}
fmt.Printf("kubearmor image (running) version %s\n", kubearmorVersion)
return nil
}

func getKubeArmorVersion(c *k8s.Client) (string, error) {
pods, err := c.K8sClientset.CoreV1().Pods("kube-system").List(context.Background(), metav1.ListOptions{LabelSelector: "kubearmor-app=kubearmor"})
if err != nil {
return "", err
}
if len(pods.Items) > 0 {
image := pods.Items[0].Spec.Containers[0].Image
return image, nil
}
return "", nil
}

0 comments on commit bb0577c

Please sign in to comment.