Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functionality to get release info from acikubectl
To get the git commit ID and build time, run on any host: acikubectl version or acikubectl version --kubeconfig=<path> Git commit info is saved inside the aci-containers-controller container and can be accessed by running "aci-containers-controller --version" Also, replaced redundant code for initClient() with existing func initClientPrintError()
- Loading branch information
Apoorva
committed
Jan 15, 2019
1 parent
b5b2565
commit ea022de
Showing
7 changed files
with
156 additions
and
17 deletions.
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
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
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,66 @@ | ||
// Copyright © 2019 Cisco Systems, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func getVersion(cmd *cobra.Command, args []string) { | ||
if len(args) != 0 { | ||
fmt.Fprintln(os.Stderr, "More arguments than required") | ||
return | ||
} | ||
kubeClient := initClientPrintError() | ||
if kubeClient == nil { | ||
return | ||
} | ||
systemNamespace, err := findSystemNamespace(kubeClient) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, "Could not find aci-containers system namespace:", err) | ||
return | ||
} | ||
systemNamespacePods, err1 := kubeClient.CoreV1().Pods(systemNamespace).List(metav1.ListOptions{}) | ||
if err1 != nil { | ||
fmt.Fprintln(os.Stderr, "Could not list pods:", err1) | ||
} | ||
for _, pod := range systemNamespacePods.Items { | ||
if strings.Contains(pod.Name, "aci-containers-controller") { | ||
buffer := new(bytes.Buffer) | ||
mylist := []string{"exec", "-n" + systemNamespace, pod.Name, "--", "/bin/sh", "-c", "aci-containers-controller -version"} | ||
execKubectl(mylist, buffer) | ||
trimString := strings.TrimSpace(buffer.String()) | ||
fmt.Println(trimString) | ||
break | ||
} | ||
} | ||
} | ||
|
||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the client and server versions for the current context acikubectl version", | ||
Example: `acikubectl version`, | ||
Run: getVersion, | ||
} | ||
|
||
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
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
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,51 @@ | ||
// Copyright 2019 Cisco Systems, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package controller | ||
|
||
import "fmt" | ||
|
||
// Variables set during build time for release info | ||
var ( | ||
gitCommit string | ||
buildTime string | ||
) | ||
|
||
// Info enlists version and build information | ||
type VersionInfo struct { | ||
GitCommit string | ||
BuildTime string | ||
} | ||
|
||
// Get gets the version information | ||
func GetVersion() *VersionInfo { | ||
ver := VersionInfo{} | ||
ver.GitCommit = gitCommit | ||
ver.BuildTime = buildTime | ||
|
||
return &ver | ||
} | ||
|
||
// String returns printable version string | ||
func VersionString() string { | ||
ver := GetVersion() | ||
return StringFromInfo(ver) | ||
} | ||
|
||
// StringFromInfo prints the versioning details | ||
func StringFromInfo(ver *VersionInfo) string { | ||
return fmt.Sprintf("GitCommit: %s\n", ver.GitCommit) + | ||
fmt.Sprintf("BuildTime: %s\n", ver.BuildTime) | ||
} |
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,11 @@ | ||
#!/bin/bash | ||
|
||
set -euo pipefail | ||
|
||
if command -v git &>/dev/null && git rev-parse &>/dev/null; then | ||
GIT_COMMIT=$(git rev-parse HEAD) | ||
echo $GIT_COMMIT | ||
exit 0 | ||
fi | ||
echo >&2 'error: unable to determine the git revision' | ||
exit 1 |