Skip to content

Commit

Permalink
feat: add version mismatch detection for package-operator (glasskube#373
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Baalekshan authored Mar 20, 2024
1 parent 1b69fde commit af2e2ed
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cmd/glasskube/cmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var bootstrapCmd = &cobra.Command{
Long: "Bootstraps Glasskube in a Kubernetes cluster, " +
"thereby installing the Glasskube operator and checking if the installation was successful.",
Args: cobra.NoArgs,
PreRun: cliutils.SetupClientContext(false),
PreRun: cliutils.SetupClientContext(false, &rootCmdOptions.SkipUpdateCheck),
Run: func(cmd *cobra.Command, args []string) {
cfg, _ := cliutils.RequireConfig(config.Kubeconfig)
client := bootstrap.NewBootstrapClient(cfg)
Expand Down
2 changes: 1 addition & 1 deletion cmd/glasskube/cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var describeCmd = &cobra.Command{
Short: "Describe a package",
Long: "Shows additional information about the given package.",
Args: cobra.ExactArgs(1),
PreRun: cliutils.SetupClientContext(true),
PreRun: cliutils.SetupClientContext(true, &rootCmdOptions.SkipUpdateCheck),
ValidArgsFunction: completeAvailablePackageNames,
Run: func(cmd *cobra.Command, args []string) {
pkgName := args[0]
Expand Down
2 changes: 1 addition & 1 deletion cmd/glasskube/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var installCmd = &cobra.Command{
Short: "Install a package",
Long: `Install a package.`,
Args: cobra.ExactArgs(1),
PreRun: cliutils.SetupClientContext(true),
PreRun: cliutils.SetupClientContext(true, &rootCmdOptions.SkipUpdateCheck),
ValidArgsFunction: completeAvailablePackageNames,
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
Expand Down
2 changes: 1 addition & 1 deletion cmd/glasskube/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var listCmd = &cobra.Command{
Short: "List packages",
Long: "List packages. By default, all available packages of the given repository are shown, " +
"as well as their installation status in your cluster.\nYou can choose to only show installed packages.",
PreRun: cliutils.SetupClientContext(true),
PreRun: cliutils.SetupClientContext(true, &rootCmdOptions.SkipUpdateCheck),
Run: func(cmd *cobra.Command, args []string) {
if listCmdOptions.More {
listCmdOptions.ShowLatestVersion = true
Expand Down
2 changes: 1 addition & 1 deletion cmd/glasskube/cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var openCmd = &cobra.Command{
Long: `Open the Web UI of a package.
If the package manifest has more than one entrypoint, specify the name of the entrypoint to open.`,
Args: cobra.RangeArgs(1, 2),
PreRun: cliutils.SetupClientContext(true),
PreRun: cliutils.SetupClientContext(true, &rootCmdOptions.SkipUpdateCheck),
Run: func(cmd *cobra.Command, args []string) {
pkgName := args[0]
var entrypointName string
Expand Down
2 changes: 1 addition & 1 deletion cmd/glasskube/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var uninstallCmd = &cobra.Command{
Short: "Uninstall a package",
Long: `Uninstall a package.`,
Args: cobra.ExactArgs(1),
PreRun: cliutils.SetupClientContext(true),
PreRun: cliutils.SetupClientContext(true, &rootCmdOptions.SkipUpdateCheck),
Run: func(cmd *cobra.Command, args []string) {
client := pkgClient.FromContext(cmd.Context())
pkgName := args[0]
Expand Down
2 changes: 1 addition & 1 deletion cmd/glasskube/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
var updateCmd = &cobra.Command{
Use: "update [packages...]",
Short: "Update some or all packages in your cluster",
PreRun: cliutils.SetupClientContext(true),
PreRun: cliutils.SetupClientContext(true, &rootCmdOptions.SkipUpdateCheck),
ValidArgsFunction: completeInstalledPackageNames,
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
Expand Down
36 changes: 2 additions & 34 deletions cmd/glasskube/cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package cmd

import (
"context"
"fmt"
"os"

"github.com/glasskube/glasskube/internal/cliutils"
"github.com/glasskube/glasskube/internal/config"
"github.com/glasskube/glasskube/pkg/client"
"github.com/google/go-containerregistry/pkg/name"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

var versioncmd = &cobra.Command{
Use: "version",
Short: "Print the version of glasskube and package-operator",
Long: `Print the version of glasskube and package-operator`,
PreRun: cliutils.SetupClientContext(false),
PreRun: cliutils.SetupClientContext(false, &rootCmdOptions.SkipUpdateCheck),
Run: func(cmd *cobra.Command, args []string) {
glasskubeVersion := config.Version
fmt.Fprintf(os.Stderr, "glasskube: v%s\n", glasskubeVersion)
operatorVersion, err := getPackageOperatorVersion(cmd.Context())
operatorVersion, err := cliutils.GetPackageOperatorVersion(cmd.Context())
if err != nil {
fmt.Fprintf(os.Stderr, "✗ no deployments found in the glasskube-system namespace\n")
} else {
Expand All @@ -34,30 +29,3 @@ var versioncmd = &cobra.Command{
func init() {
RootCmd.AddCommand(versioncmd)
}

func getPackageOperatorVersion(ctx context.Context) (string, error) {
config := client.ConfigFromContext(ctx)
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return "", err
}

namespace := "glasskube-system"
deploymentName := "glasskube-controller-manager"
deployment, err := clientset.AppsV1().Deployments(namespace).Get(ctx, deploymentName, metav1.GetOptions{})
if err != nil {
return "", err
}

containers := deployment.Spec.Template.Spec.Containers
for _, container := range containers {
if container.Name == "manager" {
ref, err := name.ParseReference(container.Image)
if err != nil {
return "", err
}
return ref.Identifier(), nil
}
}
return "", nil
}
7 changes: 6 additions & 1 deletion internal/cliutils/setupclientcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/spf13/cobra"
)

func SetupClientContext(requireBootstrapped bool) func(cmd *cobra.Command, args []string) {
func SetupClientContext(requireBootstrapped bool, skipUpdateCheck *bool) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
cfg, rawCfg := RequireConfig(config.Kubeconfig)
if requireBootstrapped {
Expand All @@ -26,6 +26,11 @@ func SetupClientContext(requireBootstrapped bool) func(cmd *cobra.Command, args
} else {
cmd.SetContext(ctx)
}
if !*skipUpdateCheck {
if err := CheckPackageOperatorVersion(cmd.Context()); err != nil {
fmt.Fprintf(os.Stderr, "Error checking PackageOperator version:\n\n%v\n", err)
}
}
}
}

Expand Down
52 changes: 52 additions & 0 deletions internal/cliutils/versionutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cliutils

import (
"context"
"fmt"
"os"

"github.com/glasskube/glasskube/internal/config"
"github.com/glasskube/glasskube/pkg/client"
"github.com/google/go-containerregistry/pkg/name"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

func GetPackageOperatorVersion(ctx context.Context) (string, error) {
config := client.ConfigFromContext(ctx)
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return "", err
}

namespace := "glasskube-system"
deploymentName := "glasskube-controller-manager"
deployment, err := clientset.AppsV1().Deployments(namespace).Get(ctx, deploymentName, metav1.GetOptions{})
if err != nil {
return "", err
}

containers := deployment.Spec.Template.Spec.Containers
for _, container := range containers {
if container.Name == "manager" {
ref, err := name.ParseReference(container.Image)
if err != nil {
return "", err
}
return ref.Identifier(), nil
}
}
return "", nil
}

func CheckPackageOperatorVersion(ctx context.Context) error {
operatorVersion, err := GetPackageOperatorVersion(ctx)
if err != nil {
return err
}
if operatorVersion[1:] != config.Version {
fmt.Fprintf(os.Stderr, "❗ Glasskube PackageOperator needs to be updated: %s -> %s\n", operatorVersion[1:], config.Version)
fmt.Fprintf(os.Stderr, "💡 Please run `glasskube bootstrap` again to update Glasskube PackageOperator\n")
}
return nil
}

0 comments on commit af2e2ed

Please sign in to comment.