This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
client.go
121 lines (102 loc) · 2.9 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package client
import (
"context"
"net"
"strconv"
"github.com/pkg/errors"
"github.com/rancher/wrangler/pkg/apply"
appsctl "github.com/rancher/wrangler/pkg/generated/controllers/apps"
appsctlv1 "github.com/rancher/wrangler/pkg/generated/controllers/apps/v1"
corectl "github.com/rancher/wrangler/pkg/generated/controllers/core"
corectlv1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
rbacctl "github.com/rancher/wrangler/pkg/generated/controllers/rbac"
rbacctlv1 "github.com/rancher/wrangler/pkg/generated/controllers/rbac/v1"
"github.com/rancher/wrangler/pkg/kubeconfig"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
DefaultNamespace = "kube-image"
)
var DefaultConfig = Config{
Namespace: DefaultNamespace,
}
type Config struct {
Namespace string `usage:"namespace" short:"n" env:"NAMESPACE" default:"kube-image"`
Kubeconfig string `usage:"kubeconfig for authentication" short:"k" env:"KUBECONFIG"`
Context string `usage:"kubeconfig context for authentication" short:"x" env:"KUBECONTEXT"`
}
func (c *Config) Interface() (*Interface, error) {
if c == nil {
return nil, errors.Errorf("client is not configured, please set client config")
}
return NewInterface(c.Kubeconfig, c.Context, c.Namespace)
}
type Interface struct {
Core corectlv1.Interface
Apps appsctlv1.Interface
RBAC rbacctlv1.Interface
Apply apply.Apply
Namespace string
}
func NewInterface(kubecfg, kubectx, kubens string) (*Interface, error) {
cc := kubeconfig.GetNonInteractiveClientConfigWithContext(kubecfg, kubectx)
ns, _, err := cc.Namespace()
if err != nil {
return nil, err
}
if kubens != "" {
ns = kubens
}
rc, err := cc.ClientConfig()
if err != nil {
return nil, err
}
c := &Interface{
Namespace: ns,
}
core, err := corectl.NewFactoryFromConfig(rc)
if err != nil {
return nil, err
}
c.Core = core.Core().V1()
apps, err := appsctl.NewFactoryFromConfig(rc)
if err != nil {
return nil, err
}
c.Apps = apps.Apps().V1()
rbac, err := rbacctl.NewFactoryFromConfig(rc)
if err != nil {
return nil, err
}
c.RBAC = rbac.Rbac().V1()
c.Apply, err = apply.NewForConfig(rc)
if err != nil {
return nil, err
}
if c.Namespace == "" {
c.Namespace = DefaultNamespace
}
c.Apply = c.Apply.
WithDynamicLookup().
WithDefaultNamespace(c.Namespace).
WithListerNamespace(c.Namespace).
WithRestrictClusterScoped()
return c, nil
}
func GetServiceAddress(_ context.Context, k8s *Interface, port string) (string, error) {
// TODO handle multiple addresses
endpoints, err := k8s.Core.Endpoints().Get(k8s.Namespace, "builder", metav1.GetOptions{})
if err != nil {
return "", err
}
for _, sub := range endpoints.Subsets {
if len(sub.Addresses) > 0 {
for _, p := range sub.Ports {
if p.Name == port {
return net.JoinHostPort(sub.Addresses[0].IP, strconv.FormatInt(int64(p.Port), 10)), nil
}
}
}
}
return "", errors.New("unknown service port")
}