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

Version check for kcp cli #3308

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
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
36 changes: 35 additions & 1 deletion tools/cli/pkg/command/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package command

import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"

Expand All @@ -11,6 +14,10 @@ import (
"github.com/spf13/viper"
)

const (
gitHubAPIEndpoint = "https://api.github.com/repos/kyma-project/control-plane/commits/main"
)

// Version is the CLI version to be filled in by the build system
var Version string = "N/A"

Expand All @@ -28,7 +35,7 @@ The CLI supports configuration file for common (global) options needed for all c

The configuration file is in YAML format and supports the following global options: %s, %s, %s, %s, %s.
See the **Global Options** section of each command for the description of these options.`, GlobalOpts.oidcIssuerURL, GlobalOpts.oidcClientID, GlobalOpts.kebAPIURL, GlobalOpts.kubeconfigAPIURL, GlobalOpts.gardenerKubeconfig)

checkForLatestRelease(Version)
cmd := &cobra.Command{
Use: "kcp",
Short: "Day-two operations tool for Kyma Runtimes.",
Expand Down Expand Up @@ -95,3 +102,30 @@ func initConfig() {
func CLICredentialManager(logger logger.Logger) credential.Manager {
return credential.NewManager(GlobalOpts.OIDCIssuerURL(), GlobalOpts.OIDCClientID(), GlobalOpts.OIDCClientSecret(), GlobalOpts.Username(), logger)
}

type latestGitTag struct {
Sha string `json:"sha"`
}

func checkForLatestRelease(version string) {
response, err := http.Get(gitHubAPIEndpoint)
if err != nil {
return
}
defer response.Body.Close()

responseData, err := io.ReadAll(response.Body)
if err != nil {
return
}

var latestGitTag latestGitTag
if err := json.Unmarshal(responseData, &latestGitTag); err != nil {
return
}

if Version != latestGitTag.Sha[:8] {
fmt.Println("CAUTION: You're using an outdated version of the KCP CLI.\n" +
"To update, please visit https://github.tools.sap/kyma/documentation/blob/main/kyma-internal/on-call-guides/common/kcp-cli.md")
}
}