forked from szuecs/routegroup-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
99 lines (83 loc) · 2.12 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Package rgclient implements client-go style API client to access
// RouteGroups and Kubernetes resources.
package rgclient
import (
"errors"
"fmt"
zclient "github.com/szuecs/routegroup-client/client/clientset/versioned"
zalandov1 "github.com/szuecs/routegroup-client/client/clientset/versioned/typed/zalando.org/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
const (
LocalAPIServer = "http://127.0.0.1:8001"
)
// Interface is the unified interface for Kubernetes and Zalando
// RouteGroup access
type Interface interface {
kubernetes.Interface
ZalandoInterface
}
// ZalandoInterface to access RouteGroups
type ZalandoInterface interface {
ZalandoV1() zalandov1.ZalandoV1Interface
}
// Clientset is the unified client implementation
type Clientset struct {
*kubernetes.Clientset
zclient *zclient.Clientset
}
// ZalandoV1 implements ZalandoInterface
func (cs *Clientset) ZalandoV1() zalandov1.ZalandoV1Interface {
return cs.zclient.ZalandoV1()
}
// NewClientset returns the unified client, but users should in
// general prefer rgclient.CreateUnified().
func NewClientset(config *rest.Config) (*Clientset, error) {
zcli, err := zclient.NewForConfig(config)
if err != nil {
return nil, err
}
k8scli, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return &Clientset{
Clientset: k8scli,
zclient: zcli,
}, err
}
// Create returns the Zalandointerface client
func Create() (ZalandoInterface, error) {
config, err := getRestConfig()
if err != nil {
return nil, err
}
return zclient.NewForConfig(config)
}
// CreateUnified returns the unified client
func CreateUnified() (Interface, error) {
config, err := getRestConfig()
if err != nil {
return nil, err
}
client, err := NewClientset(config)
if err != nil {
return nil, fmt.Errorf("unable to create a unified client: %v", err)
}
return client, nil
}
func getRestConfig() (*rest.Config, error) {
config, err := rest.InClusterConfig()
if err != nil {
if errors.Is(err, rest.ErrNotInCluster) {
config = &rest.Config{
Host: LocalAPIServer,
}
err = nil
} else {
return nil, err
}
}
return config, err
}