This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
/
client.go
124 lines (103 loc) · 3.59 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
122
123
124
// Copyright 2018 The ksonnet authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cluster
import (
"github.com/ksonnet/ksonnet/pkg/app"
"github.com/ksonnet/ksonnet/pkg/client"
"github.com/ksonnet/ksonnet/utils"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
// client go auth plugins
_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
_ "k8s.io/client-go/plugin/pkg/client/auth/openstack"
)
// ResourceClient is a wrapper for a resource client.
type ResourceClient interface {
Create() (*unstructured.Unstructured, error)
Delete(options *metav1.DeleteOptions) error
Get(options metav1.GetOptions) (*unstructured.Unstructured, error)
Patch(pt types.PatchType, data []byte) (*unstructured.Unstructured, error)
}
// Clients is a tuple of Kubernetes clients: dynamic.ClientPool, discovery.DiscoveryInterface.
type Clients struct {
clientPool dynamic.ClientPool
discovery discovery.DiscoveryInterface
namespace string
}
type resourceClientOpt func(*resourceClient)
type resourceClient struct {
object *unstructured.Unstructured
clients Clients
c dynamic.ResourceInterface
}
var _ ResourceClient = (*resourceClient)(nil)
func newResourceClient(clients Clients, object runtime.Object, rcOpts ...resourceClientOpt) (*resourceClient, error) {
o, ok := object.(*unstructured.Unstructured)
if !ok {
return nil, errors.Errorf("unsupported runtime object type %T", object)
}
rc := &resourceClient{
object: o,
clients: clients,
}
for _, opt := range rcOpts {
opt(rc)
}
if rc.c == nil {
c, err := utils.ClientForResource(clients.clientPool, clients.discovery, object, clients.namespace)
if err != nil {
return nil, err
}
rc.c = c
}
return rc, nil
}
func resourceClientFactory(opts Clients, object runtime.Object) (ResourceClient, error) {
return newResourceClient(opts, object)
}
func (c *resourceClient) Create() (*unstructured.Unstructured, error) {
return c.c.Create(c.object)
}
func (c *resourceClient) Delete(options *metav1.DeleteOptions) error {
name := c.object.GetName()
return c.c.Delete(name, options)
}
func (c *resourceClient) Get(options metav1.GetOptions) (*unstructured.Unstructured, error) {
name := c.object.GetName()
return c.c.Get(name, options)
}
func (c *resourceClient) Patch(pt types.PatchType, data []byte) (*unstructured.Unstructured, error) {
name := c.object.GetName()
return c.c.Patch(name, pt, data)
}
// GenClients returns a cluster.Clients structure initialized to the provided environment cluster/namespace
func GenClients(a app.App, clientConfig *client.Config, envName string) (Clients, error) {
clientPool, discovery, namespace, err := clientConfig.RestClient(a, &envName)
if err != nil {
return Clients{}, err
}
return Clients{
clientPool: clientPool,
discovery: discovery,
namespace: namespace,
}, nil
}