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

Pick review commit from metalctl regarding updater. #275

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions cmd/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"time"

"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
"gopkg.in/yaml.v3"
k8syaml "sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -177,3 +179,8 @@ func MustPrintKubernetesResource(in any) {
}
fmt.Printf("---\n%s", string(y))
}

func ClientNoAuth() runtime.ClientAuthInfoWriterFunc {
noAuth := func(_ runtime.ClientRequest, _ strfmt.Registry) error { return nil }
return runtime.ClientAuthInfoWriterFunc(noAuth)
}
17 changes: 12 additions & 5 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/Masterminds/semver/v3"
"github.com/fi-ts/cloudctl/cmd/helper"
"github.com/fi-ts/cloudctl/pkg/api"
"github.com/metal-stack/metal-lib/auth"
"github.com/metal-stack/v"
Expand Down Expand Up @@ -69,10 +70,7 @@ func newLoginCmd(c *config) *cobra.Command {
return err
}

// We need to reread the written kubeconfig
c := getConfig(c.name)

resp, err := c.cloud.Version.Info(nil, nil)
resp, err := c.cloud.Version.Info(nil, helper.ClientNoAuth())
if err != nil {
return err
}
Expand All @@ -82,16 +80,25 @@ func newLoginCmd(c *config) *cobra.Command {
if err != nil {
return fmt.Errorf("required cloudctl minimum version:%q is not semver parsable:%w", minVersion, err)
}

// This is a developer build
if !strings.HasPrefix(v.Version, "v") {
return nil
}

thisVersion, err := semver.NewVersion(v.Version)
if err != nil {
return fmt.Errorf("cloudctl version:%q is not semver parsable:%w", v.Version, err)
}

if thisVersion.LessThan(parsedMinVersion) {
return fmt.Errorf("your cloudctl version:%s is smaller than the required minimum version:%s, please run `cloudctl update do` to get the latest version", thisVersion, minVersion)
return fmt.Errorf("your cloudctl version:%s is smaller than the required minimum version:%s, please run `cloudctl update do` to update to the supported version", thisVersion, minVersion)
}

if !thisVersion.Equal(parsedMinVersion) {
fmt.Println()
fmt.Printf("WARNING: Your cloudctl version %q might not compatible with the cloud-api (supported version is %q). Please run `cloudctl update do` to update to the supported version.", thisVersion, minVersion)
fmt.Println()
}
}

Expand Down
31 changes: 24 additions & 7 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package cmd

import (
"github.com/metal-stack/updater"
"github.com/fi-ts/cloudctl/cmd/helper"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/metal-stack/metal-lib/pkg/pointer"
"github.com/metal-stack/updater"
)

func newUpdateCmd(c *config, name string) *cobra.Command {
Expand All @@ -14,7 +18,7 @@ func newUpdateCmd(c *config, name string) *cobra.Command {
Use: "check",
Short: "check for update of the program",
RunE: func(cmd *cobra.Command, args []string) error {
desired, err := getDesiredVersion(c)
desired, err := getMinimumClientVersion(c)
if err != nil {
return err
}
Expand All @@ -29,26 +33,39 @@ func newUpdateCmd(c *config, name string) *cobra.Command {
Use: "do",
Short: "do the update of the program",
RunE: func(cmd *cobra.Command, args []string) error {
desired, err := getDesiredVersion(c)
if err != nil {
return err
var desired *string

if !viper.IsSet("version") {
var err error
desired, err = getMinimumClientVersion(c)
if err != nil {
return err
}
}

if viper.IsSet("version") && viper.GetString("version") != "latest" {
desired = pointer.Pointer(viper.GetString("version"))
}

u, err := updater.New("fi-ts", name, name, desired)
if err != nil {
return err
}

return u.Do()
},
}

updateCmd.Flags().StringP("version", "v", "", `the version to update to, by default updates to the supported version, use "latest" to update to latest version`)

updateCmd.AddCommand(updateCheckCmd)
updateCmd.AddCommand(updateDoCmd)

return updateCmd
}

func getDesiredVersion(c *config) (*string, error) {
resp, err := c.cloud.Version.Info(nil, nil)
func getMinimumClientVersion(c *config) (*string, error) {
resp, err := c.cloud.Version.Info(nil, helper.ClientNoAuth())
if err != nil {
return nil, err
}
Expand Down