-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Displays version info of the cli and running kubearmor image Signed-off-by: Barun Acharya <barun.acharya@accuknox.com>
- Loading branch information
1 parent
701880e
commit bb0577c
Showing
3 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |