-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
79 lines (69 loc) · 2.33 KB
/
client.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
package k3s
import (
"fmt"
"time"
// "context"
// "github.com/cnrancher/autok3s/pkg/types"
// yamlv3 "gopkg.in/yaml.v3"
// v1 "k8s.io/api/core/v1"
// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k3sv1alpha1 "github.com/grengojbo/k3ctl/api/v1alpha1"
"github.com/grengojbo/k3ctl/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
// "github.com/grengojbo/k3ctl/pkg/util"
)
// https://github.com/cnrancher/autok3s/blob/a9468516b89009a0d5488cdaad4eb0eb5370cedc/pkg/cluster/cluster.go#L5
func buildConfigFromFlags(context, kubeconfigPath string) (*rest.Config, error) {
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
&clientcmd.ConfigOverrides{
CurrentContext: context,
}).ClientConfig()
}
// GetClusterConfig generate kube config.
func GetClusterConfig(context string) (*kubernetes.Clientset, error) {
// config, err := buildConfigFromFlags(name, kubeconfig)
config, err := BuildKubeConfigFromFlags(context)
if err != nil {
return nil, err
}
config.Timeout = 15 * time.Second
c, err := kubernetes.NewForConfig(config)
return c, err
}
// GetKubeconfig подключаемся к мастеру и скачиваем kubeconfig
func GetKubeconfig(masters []k3sv1alpha1.ContrelPlanNodes, dryRun bool) (kubeconfig string, err error) {
if dryRun {
return "", nil
}
if len(masters) == 0 {
return "", fmt.Errorf("Is NOT set control plane nodes")
}
// runCommand := "cat /var/lib/rancher/k3s/server/node-token"
for _, item := range masters {
kubeconfig, err = ExecuteMaster(types.CatCfgCommand, &item, dryRun)
// if err != nil {
// log.Errorln(err)
// }
return kubeconfig, err
}
return kubeconfig, err
}
// GetClusterStatus get cluster status using cluster's /readyz API.
// func GetClusterStatus(c *kubernetes.Clientset) string {
// _, err := c.RESTClient().Get().Timeout(15 * time.Second).RequestURI("/readyz").DoRaw(context.TODO())
// if err != nil {
// return types.ClusterStatusStopped
// }
// return types.ClusterStatusRunning
// }
// // GetClusterVersion get kube cluster version.
// func GetClusterVersion(c *kubernetes.Clientset) string {
// v, err := c.DiscoveryClient.ServerVersion()
// if err != nil {
// // return types.ClusterStatusUnknown
// }
// return v.GitVersion
// }