/
instances.go
129 lines (101 loc) · 2.84 KB
/
instances.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
package list
import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"github.com/xlab/treeprint"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/clientcmd"
"log"
"os"
"os/user"
"path/filepath"
kudov1alpha1 "github.com/kudobuilder/kudo/pkg/apis/kudo/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/dynamic"
)
var (
kubeConfig string
namespace string
)
const (
defaultConfigPath = ".kube/config"
)
func NewListInstancesCmd() *cobra.Command {
listCmd := &cobra.Command{
Use: "instances",
Short: "Lists all available instances.",
Long: `
# List all available instances
kudoctl list instances`,
Run: instancesListCmd,
}
listCmd.Flags().StringVar(&kubeConfig, "kubeconfig", "", "The file path to kubernetes configuration file; defaults to $HOME/.kube/config")
listCmd.Flags().StringVar(&namespace, "namespace", "default", "The namespace where the operator watches for changes.")
return listCmd
}
func instancesListCmd(cmd *cobra.Command, args []string) {
mustKubeConfig()
_, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
log.Printf("Flag Error: %v", err)
}
p, err := listInstances()
if err != nil {
log.Printf("Error: %v", err)
}
tree := treeprint.New()
for _, plan := range p {
tree.AddBranch(plan)
}
fmt.Printf("List of current instances in namespace \"%s\":\n", namespace)
fmt.Println(tree.String())
}
func listInstances() ([]string, error) {
instanceList := make([]string, 0)
config, err := clientcmd.BuildConfigFromFlags("", kubeConfig)
if err != nil {
return nil, err
}
// Create a Dynamic Client to interface with CRDs.
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return nil, err
}
instancesGVR := schema.GroupVersionResource{
Group: "kudo.k8s.io",
Version: "v1alpha1",
Resource: "instances",
}
instObj, err := dynamicClient.Resource(instancesGVR).Namespace(namespace).List(metav1.ListOptions{})
if err != nil {
log.Printf("Error: %v", err)
return nil, err
}
mInstObj, err := instObj.MarshalJSON()
instance := kudov1alpha1.InstanceList{}
//log.Println(instObj)
err = json.Unmarshal(mInstObj, &instance)
if err != nil {
return nil, err
}
for _, i := range instance.Items {
instanceList = append(instanceList, i.Name)
}
return instanceList, nil
}
// mustKubeConfig checks if the kubeconfig file exists.
func mustKubeConfig() {
// if kubeConfig is not specified, search for the default kubeconfig file under the $HOME/.kube/config.
if len(kubeConfig) == 0 {
usr, err := user.Current()
if err != nil {
fmt.Printf("Error: failed to determine user's home dir: %v", err)
}
kubeConfig = filepath.Join(usr.HomeDir, defaultConfigPath)
}
_, err := os.Stat(kubeConfig)
if err != nil && os.IsNotExist(err) {
fmt.Printf("Error: failed to find the kubeconfig file (%v): %v", kubeConfig, err)
}
}