This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
/
clientset.go
96 lines (83 loc) · 2.18 KB
/
clientset.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
package clientset
import (
"fmt"
"net/url"
platform "kolihub.io/koli/pkg/apis/core/v1alpha1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
// Interface core
type Interface interface {
Core() CoreInterface
}
// Clientset contains the clients for groups. Each group has exactly one
// version included in a Clientset.
type Clientset struct {
*CoreClient
}
// Core retrieves the CoreClient
func (c *Clientset) Core() CoreInterface {
if c == nil {
return nil
}
return c.CoreClient
}
// NewClusterConfig creates a new customized *kubernetes.Config
func NewClusterConfig(host string, tlsInsecure bool, tlsConfig *rest.TLSClientConfig) (*rest.Config, error) {
var cfg *rest.Config
var err error
if len(host) == 0 {
if cfg, err = rest.InClusterConfig(); err != nil {
return nil, err
}
} else {
cfg = &rest.Config{
Host: host,
}
hostURL, err := url.Parse(host)
if err != nil {
return nil, fmt.Errorf("error parsing host url %s : %v", host, err)
}
if hostURL.Scheme == "https" {
cfg.TLSClientConfig = *tlsConfig
cfg.Insecure = tlsInsecure
}
}
cfg.QPS = 100
cfg.Burst = 100
return cfg, nil
}
// NewSysRESTClient generates a new *kubernetes.Interface
// to communicate with Custom Resource Defintions resources
func NewSysRESTClient(c *rest.Config) (*CoreClient, error) {
c.APIPath = "/apis"
c.GroupVersion = &schema.GroupVersion{
Group: platform.GroupName,
Version: platform.SchemeGroupVersion.Version,
}
contentConfig := dynamic.ContentConfig()
contentConfig.GroupVersion = &platform.SchemeGroupVersion
c.ContentConfig = contentConfig
// c.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: api.Codecs}
cl, err := rest.RESTClientFor(c)
if err != nil {
return nil, err
}
return &CoreClient{restClient: cl}, nil
}
func NewKubernetesClient(c *rest.Config) (kubernetes.Interface, error) {
var err error
if len(c.Host) == 0 {
c, err = rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("error creating client configuration: %v", err)
}
}
clientset, err := kubernetes.NewForConfig(c)
if err != nil {
return nil, err
}
return clientset, nil
}