Skip to content

Commit

Permalink
Merge pull request #3987 from stevekuznetsov/skuznets/cli-report-supp…
Browse files Browse the repository at this point in the history
…orted-versions

OCPBUGS-32186: cmd: report server version, supported OCP
  • Loading branch information
openshift-merge-bot[bot] authored Jun 3, 2024
2 parents 94174f5 + c624e1e commit c0c55a0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
5 changes: 3 additions & 2 deletions cmd/install/assets/hypershift_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ const (
oidcProviderS3CredsSecretName = "hypershift-operator-oidc-provider-s3-credentials"
externaDNSCredsSecretName = "external-dns-credentials"

HypershiftOperatorName = "operator"
HypershiftOperatorName = "operator"
HyperShiftInstallCLIVersionAnnotation = "hypershift.openshift.io/install-cli-version"
)

type HyperShiftOperatorCredentialsSecret struct {
Expand Down Expand Up @@ -694,7 +695,7 @@ func (o HyperShiftOperatorDeployment) Build() *appsv1.Deployment {

if o.IncludeVersion {
deployment.Annotations = map[string]string{
"hypershift.openshift.io/install-cli-version": version.String(),
HyperShiftInstallCLIVersionAnnotation: version.String(),
}
}

Expand Down
40 changes: 38 additions & 2 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import (
"net/http"
"strings"

"github.com/openshift/hypershift/cmd/util"
manifests "github.com/openshift/hypershift/hypershift-operator/controllers/manifests/supportedversion"
"github.com/openshift/hypershift/hypershift-operator/controllers/supportedversion"
"github.com/openshift/hypershift/pkg/version"
"github.com/spf13/cobra"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
)

var (
Expand Down Expand Up @@ -61,7 +65,8 @@ func LookupDefaultOCPVersion(releaseStream string) (OCPVersion, error) {
}

func NewVersionCommand() *cobra.Command {
var commitOnly bool
var commitOnly, clientOnly bool
namespace := "hypershift"
cmd := &cobra.Command{
Use: "version",
Short: "Prints HyperShift CLI version",
Expand All @@ -71,9 +76,40 @@ func NewVersionCommand() *cobra.Command {
fmt.Printf("%s\n", version.GetRevision())
return
}
fmt.Printf("%s\n", version.String())
fmt.Printf("Client Version: %s\n", version.String())
if clientOnly {
return
}
client, err := util.GetClient()
if err != nil {
fmt.Printf("failed to connect to server: %v\n", err)
return
}

supportedVersions := manifests.ConfigMap(namespace)
if err := client.Get(cmd.Context(), crclient.ObjectKeyFromObject(supportedVersions), supportedVersions); err != nil {
fmt.Printf("failed to find supported versions on the server: %v\n", err)
return
}
if serverVersion, present := supportedVersions.Data[supportedversion.ConfigMapServerVersionKey]; present {
fmt.Printf("Server Version: %s\n", serverVersion)
} else {
fmt.Println("The server did not advertise its HyperShift version.")
}
if supportedVersionData, present := supportedVersions.Data[supportedversion.ConfigMapVersionsKey]; present {
var versions supportedversion.SupportedVersions
if err := json.Unmarshal([]byte(supportedVersionData), &versions); err != nil {
fmt.Printf("failed to parse supported versions on the server: %v\n", err)
return
}
fmt.Printf("Server Supports OCP Versions: %s\n", strings.Join(versions.Versions, ", "))
} else {
fmt.Println("The server did not advertise supported OCP versions.")
}
},
}
cmd.Flags().BoolVar(&commitOnly, "commit-only", commitOnly, "Output only the code commit")
cmd.Flags().BoolVar(&clientOnly, "client-only", clientOnly, "Output only the client version")
cmd.Flags().StringVar(&namespace, "namespace", namespace, "The namespace in which HyperShift is installed")
return cmd
}
13 changes: 8 additions & 5 deletions hypershift-operator/controllers/supportedversion/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"

"github.com/openshift/hypershift/pkg/version"
corev1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand All @@ -22,8 +23,9 @@ import (
)

const (
configMapKey = "supported-versions"
supportedVersionsLabel = "hypershift.openshift.io/supported-versions"
ConfigMapVersionsKey = "supported-versions"
ConfigMapServerVersionKey = "server-version"
supportedVersionsLabel = "hypershift.openshift.io/supported-versions"
)

type Reconciler struct {
Expand Down Expand Up @@ -56,7 +58,7 @@ func (r *Reconciler) SetupWithManager(mgr manager.Manager) error {
return nil
}

type supportedVersions struct {
type SupportedVersions struct {
Versions []string `json:"versions"`
}

Expand All @@ -71,7 +73,7 @@ func (r *Reconciler) ensureSupportedVersionConfigMap(ctx context.Context) error
}
cm.Labels[supportedVersionsLabel] = "true"
if _, err := r.CreateOrUpdate(ctx, r, cm, func() error {
content := &supportedVersions{
content := &SupportedVersions{
Versions: supportedversion.Supported(),
}
contentBytes, err := json.Marshal(content)
Expand All @@ -81,7 +83,8 @@ func (r *Reconciler) ensureSupportedVersionConfigMap(ctx context.Context) error
if cm.Data == nil {
cm.Data = map[string]string{}
}
cm.Data[configMapKey] = string(contentBytes)
cm.Data[ConfigMapVersionsKey] = string(contentBytes)
cm.Data[ConfigMapServerVersionKey] = version.GetRevision()
return nil
}); err != nil {
return fmt.Errorf("failed to update supported version configmap: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func TestEnsureSupportedVersionConfigMap(t *testing.T) {
cfgMap := manifests.ConfigMap("hypershift")
err = c.Get(context.Background(), client.ObjectKeyFromObject(cfgMap), cfgMap)
g.Expect(err).To(BeNil())
g.Expect(cfgMap.Data[configMapKey]).ToNot(BeEmpty())
data := &supportedVersions{}
err = json.Unmarshal([]byte(cfgMap.Data[configMapKey]), data)
g.Expect(cfgMap.Data[ConfigMapVersionsKey]).ToNot(BeEmpty())
data := &SupportedVersions{}
err = json.Unmarshal([]byte(cfgMap.Data[ConfigMapVersionsKey]), data)
g.Expect(err).To(BeNil())
g.Expect(len(data.Versions)).To(Equal(len(supportedversion.Supported())))
}
5 changes: 2 additions & 3 deletions product-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"os/signal"
"syscall"

cliversion "github.com/openshift/hypershift/cmd/version"
"github.com/spf13/cobra"

"github.com/openshift/hypershift/pkg/version"
"github.com/openshift/hypershift/product-cli/cmd/create"
"github.com/openshift/hypershift/product-cli/cmd/destroy"
)
Expand All @@ -40,14 +40,13 @@ func main() {
},
}

cmd.Version = version.String()

ctx, cancel := context.WithCancel(context.Background())

defer cancel()

cmd.AddCommand(create.NewCommand())
cmd.AddCommand(destroy.NewCommand())
cmd.AddCommand(cliversion.NewVersionCommand())

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT)
Expand Down

1 comment on commit c0c55a0

@stevekuznetsov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/retest

Please sign in to comment.