-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
144 lines (126 loc) · 3.89 KB
/
root.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
Copyright 2021 Shiwen Cheng
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 (
"context"
"encoding/json"
"fmt"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
var Version = "unknown"
type command struct {
cobraCmd *cobra.Command
kubeConfig string
}
func defaultKubeConfig() string {
if home := homedir.HomeDir(); home != "" {
return filepath.Join(home, ".kube", "config")
}
return ""
}
func Execute() {
cmd := NewCommand()
if err := cmd.Execute(); err != nil {
fmt.Println(err)
}
}
func NewCommand() *cobra.Command {
cmd := &command{}
cmd.cobraCmd = &cobra.Command{
Use: "kubectl-resource-versions",
Short: "Print the API resources with the supported API versions",
Long: "Print the API resources along with the supported API versions in the form of group/version on the server",
Example: " # Print the API resources with the supported API versions\n kubectl-resource-versions\n # Print by kubectl plugin\n kubectl resource-versions",
Args: cobra.NoArgs,
SilenceUsage: true,
SilenceErrors: true,
Version: Version,
RunE: func(c *cobra.Command, args []string) error {
return cmd.runE()
},
}
pflags := cmd.cobraCmd.PersistentFlags()
pflags.StringVar(&cmd.kubeConfig, "kubeconfig", defaultKubeConfig(), "path to the kubeconfig file to use for CLI requests")
return cmd.cobraCmd
}
func (cmd *command) runE() (err error) {
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", cmd.kubeConfig)
if err != nil {
return
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return
}
// request /openapi/v2
body, err := clientset.RESTClient().Get().AbsPath("/openapi/v2").Do(context.TODO()).Raw()
if err != nil {
return
}
var document struct {
Paths map[string]interface{} `json:"paths,omitempty"`
}
err = json.Unmarshal(body, &document)
if err != nil {
return
}
cmd.printResult(document.Paths)
return
}
func (cmd *command) printResult(paths map[string]interface{}) {
// filter valid paths
validPaths := make([]string, 0)
for path := range paths {
if (strings.HasPrefix(path, "/api/") && !strings.HasSuffix(path, "/") && len(strings.Split(path, "/")) == 4) ||
(strings.HasPrefix(path, "/apis/") && !strings.HasSuffix(path, "/") && len(strings.Split(path, "/")) == 5) {
validPaths = append(validPaths, path)
}
}
sort.Strings(validPaths)
// valid paths -> resource map { resource: apiVersions }
resources := make(map[string][]string)
for _, path := range validPaths {
items := strings.Split(path, "/")
resource := items[len(items)-1]
apiVersion := strings.Join(items[2:len(items)-1], "/")
if _, ok := resources[resource]; !ok {
resources[resource] = make([]string, 0)
}
resources[resource] = append(resources[resource], apiVersion)
}
// sort resources
maxLen := 0
resourceKeys := make([]string, 0)
for resource := range resources {
resourceKeys = append(resourceKeys, resource)
if len(resource) > maxLen {
maxLen = len(resource)
}
}
sort.Strings(resourceKeys)
// print resource versions
format := "%-" + strconv.Itoa(maxLen+4) + "s%s\n"
fmt.Printf(format, "RESOURCE", "API VERSIONS")
for _, resource := range resourceKeys {
fmt.Printf(format, resource, strings.Join(resources[resource], ", "))
}
}