diff --git a/checks/updates.go b/checks/updates.go new file mode 100644 index 00000000..3a991c4d --- /dev/null +++ b/checks/updates.go @@ -0,0 +1,53 @@ +package checks + +import ( + "context" + "fmt" + "github.com/fatih/color" + "github.com/google/go-github/github" + "github.com/kubearmor/kubearmor-client/selfupdate" + "strings" +) + +func CheckForUpdates() error { + client := github.NewClient(nil) + + // Fetch all releases from the GitHub API + releases, _, err := client.Repositories.ListReleases(context.Background(), + "kubearmor", + "kubearmor-client", + &github.ListOptions{ + Page: 1, + PerPage: 100, + }, + ) + if err != nil { + fmt.Println("Error fetching releases:", err) + return err + } + + // Find the latest release with the keyword "mandatory" in its release notes + var latestMandatoryRelease *github.RepositoryRelease + for _, release := range releases { + if strings.Contains(*release.Body, "mandatory") { + latestMandatoryRelease = release + break + } + } + + // Compare the current version with the latest release version + currentVersion := selfupdate.GitSummary + if err != nil { + return err + } + if latestMandatoryRelease != nil && !strings.EqualFold(currentVersion, *latestMandatoryRelease.TagName) { + color.HiMagenta("A mandatory update is available (current version: %s, latest release: %s).\n", + currentVersion, + *latestMandatoryRelease.TagName, + ) + } else { + fmt.Println("The client is up to date.") + } + + return nil +} diff --git a/main.go b/main.go index ffe45611..4399822d 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,17 @@ // Package main is responsible for the execution of CLI package main -import "github.com/kubearmor/kubearmor-client/cmd" +import ( + "fmt" + "github.com/kubearmor/kubearmor-client/checks" + "github.com/kubearmor/kubearmor-client/cmd" +) func main() { + if err := checks.CheckForUpdates(); err != nil { + fmt.Println("Error checking for updates:", err) + return + } cmd.Execute() + } diff --git a/version/version.go b/version/version.go index 72ab76ee..83e85c0a 100644 --- a/version/version.go +++ b/version/version.go @@ -23,7 +23,7 @@ func PrintVersion(c *k8s.Client) error { color.HiMagenta("update available version " + latestVer) color.HiMagenta("use [karmor selfupdate] to update to latest") } - kubearmorVersion, err := getKubeArmorVersion(c) + kubearmorVersion, err := GetKubeArmorVersion(c) if err != nil { return nil } @@ -35,7 +35,7 @@ func PrintVersion(c *k8s.Client) error { return nil } -func getKubeArmorVersion(c *k8s.Client) (string, error) { +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