-
Notifications
You must be signed in to change notification settings - Fork 80
/
version.go
117 lines (96 loc) · 3.23 KB
/
version.go
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* version.go
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2020 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmd
import (
"fmt"
"strings"
"sigs.k8s.io/controller-runtime/pkg/client"
"k8s.io/cli-runtime/pkg/genericclioptions"
"github.com/spf13/cobra"
)
var pluginVersion = "latest"
func newVersionCmd(streams genericclioptions.IOStreams) *cobra.Command {
o := newFDBOptions(streams)
cmd := &cobra.Command{
Use: "version",
Short: "version of kubectl-fdb & foundationdb-operator",
Long: `version of kubectl-fdb and current running foundationdb-operator`,
RunE: func(cmd *cobra.Command, _ []string) error {
operatorName, err := cmd.Root().Flags().GetString("operator-name")
if err != nil {
return err
}
clientOnly, err := cmd.Flags().GetBool("client-only")
if err != nil {
return err
}
containerName, err := cmd.Flags().GetString("container-name")
if err != nil {
return err
}
if !clientOnly {
kubeClient, err := getKubeClient(cmd.Context(), o)
if err != nil {
return err
}
namespace, err := getNamespace(*o.configFlags.Namespace)
if err != nil {
return err
}
operatorVersion, err := version(kubeClient, operatorName, namespace, containerName)
if err != nil {
return err
}
cmd.Printf("foundationdb-operator: %s\n", operatorVersion)
}
cmd.Printf("kubectl-fdb: %s\n", pluginVersion)
return nil
},
Example: `
# Lists the version of kubectl fdb plugin and foundationdb operator in current namespace
kubectl fdb version
# Lists the version of kubectl fdb plugin and foundationdb operator in provided namespace
kubectl fdb -n default version
# Lists the version of kubectl fdb plugin without checking the operator version
kubectl fdb version --client-only
`,
}
cmd.SetOut(o.Out)
cmd.SetErr(o.ErrOut)
cmd.SetIn(o.In)
o.configFlags.AddFlags(cmd.Flags())
cmd.Flags().Bool("client-only", false, "Prints out the plugin version only without checking the operator version.")
cmd.Flags().String("container-name", "manager", "The container name of Kubernetes Deployment.")
return cmd
}
func version(kubeClient client.Client, operatorName string, namespace string, containerName string) (string, error) {
operatorDeployment, err := getOperator(kubeClient, operatorName, namespace)
if err != nil {
return "", err
}
for _, container := range operatorDeployment.Spec.Template.Spec.Containers {
if container.Name != containerName {
continue
}
imageName := strings.Split(container.Image, ":")
return imageName[len(imageName)-1], nil
}
return "", fmt.Errorf("could not find container: %s in %s/%s", containerName, namespace, operatorName)
}