-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathversion.go
More file actions
86 lines (66 loc) · 2.15 KB
/
version.go
File metadata and controls
86 lines (66 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2021 - 2026 Crunchy Data Solutions, Inc.
//
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"context"
"fmt"
"github.com/spf13/cobra"
v1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/crunchydata/postgres-operator-client/internal"
)
// newVersionCommand returns the CLI client version and the Postgres operator
// version.
func newVersionCommand(config *internal.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "PGO client and operator versions",
Long: `Version displays the versions of the PGO client and the Crunchy Postgres Operator
### RBAC Requirements
Resources Verbs
--------- -----
customresourcedefinitions.apiextensions.k8s.io [get]
Note: This RBAC needs to be cluster-scoped.
### Usage`,
}
// No arguments for 'version'
cmd.Args = cobra.NoArgs
var clientOnly bool
cmd.Flags().BoolVar(&clientOnly, "client", false, "If true, shows client version only (no server required).")
cmd.Example = internal.FormatExample(fmt.Sprintf(`# Request the version of the client and the operator
pgo version
### Example output
Client Version: %s
Operator Version: v5.7.0`, clientVersion))
cmd.RunE = func(cmd *cobra.Command, args []string) error {
cmd.Printf("Client Version: %s\n", clientVersion)
if clientOnly {
return nil
}
ctx := context.Background()
restConfig, err := config.ToRESTConfig()
if err != nil {
return err
}
// get a client capable of retrieving the PostgresCluster CRD
client, err := v1.NewForConfig(restConfig)
if err != nil {
return err
}
crd, err := client.CustomResourceDefinitions().
Get(ctx, "postgresclusters.postgres-operator.crunchydata.com", metav1.GetOptions{})
if err != nil {
return err
}
if crd != nil &&
crd.Labels != nil &&
crd.Labels["app.kubernetes.io/version"] != "" {
cmd.Printf("Operator Version: v%s\n", crd.Labels["app.kubernetes.io/version"])
} else {
cmd.Println("Operator version not found.")
}
return nil
}
return cmd
}