This repository has been archived by the owner on Mar 4, 2019. It is now read-only.
forked from kmodules/client-go
/
dynamic_clientset.go
146 lines (124 loc) · 4.28 KB
/
dynamic_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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright 2017 Google Inc.
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
https://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 clientset
import (
"fmt"
dynamicdiscovery "github.com/appscode/kutil/dynamic/discovery"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/retry"
)
type Clientset struct {
config rest.Config
resources *dynamicdiscovery.ResourceMap
}
func New(config *rest.Config, resources *dynamicdiscovery.ResourceMap) *Clientset {
return &Clientset{
config: *config,
resources: resources,
}
}
func (cs *Clientset) HasSynced() bool {
return cs.resources.HasSynced()
}
func (cs *Clientset) Resource(apiVersion, resource, namespace string) (*ResourceClient, error) {
// Look up the requested resource in discovery.
apiResource := cs.resources.Get(apiVersion, resource)
if apiResource == nil {
return nil, fmt.Errorf("discovery: can't find resource %s in apiVersion %s", resource, apiVersion)
}
return cs.resource(apiResource, namespace)
}
func (cs *Clientset) Kind(apiVersion, kind, namespace string) (*ResourceClient, error) {
// Look up the requested resource in discovery.
apiResource := cs.resources.GetKind(apiVersion, kind)
if apiResource == nil {
return nil, fmt.Errorf("discovery: can't find kind %s in apiVersion %s", kind, apiVersion)
}
return cs.resource(apiResource, namespace)
}
func (cs *Clientset) resource(apiResource *dynamicdiscovery.APIResource, namespace string) (*ResourceClient, error) {
// Create dynamic client for this apiVersion/resource.
gv := apiResource.GroupVersion()
config := cs.config
config.GroupVersion = &gv
if gv.Group != "" {
config.APIPath = "/apis"
}
client, err := dynamic.NewForConfig(&config)
if err != nil {
return nil, fmt.Errorf("can't create dynamic client for resource %v in apiVersion %v: %v", apiResource.Name, apiResource.APIVersion, err)
}
dc := client.Resource(gv.WithResource(apiResource.Name))
return &ResourceClient{
ResourceInterface: dc.Namespace(namespace),
dc: dc,
gv: gv,
resource: apiResource,
}, nil
}
type ResourceClient struct {
dynamic.ResourceInterface
dc dynamic.NamespaceableResourceInterface
gv schema.GroupVersion
resource *dynamicdiscovery.APIResource
}
func (rc *ResourceClient) WithNamespace(namespace string) *ResourceClient {
// Make a shallow copy of self, then change the namespace.
rc2 := *rc
rc2.ResourceInterface = rc.dc.Namespace(namespace)
return &rc2
}
func (rc *ResourceClient) Kind() string {
return rc.resource.Kind
}
func (rc *ResourceClient) GroupVersion() schema.GroupVersion {
return rc.gv
}
func (rc *ResourceClient) GroupResource() schema.GroupResource {
return schema.GroupResource{
Group: rc.gv.Group,
Resource: rc.resource.Name,
}
}
func (rc *ResourceClient) GroupVersionKind() schema.GroupVersionKind {
return rc.gv.WithKind(rc.resource.Kind)
}
func (rc *ResourceClient) APIResource() *dynamicdiscovery.APIResource {
return rc.resource
}
func (rc *ResourceClient) UpdateWithRetries(orig *unstructured.Unstructured, update func(obj *unstructured.Unstructured) bool) (result *unstructured.Unstructured, err error) {
name := orig.GetName()
err = retry.RetryOnConflict(retry.DefaultBackoff, func() error {
current, err := rc.Get(name, metav1.GetOptions{})
if err != nil {
return err
}
if current.GetUID() != orig.GetUID() {
// The original object was deleted and replaced with a new one.
return apierrors.NewNotFound(rc.GroupResource(), name)
}
if changed := update(current); !changed {
// There's nothing to do.
result = current
return nil
}
result, err = rc.Update(current, metav1.UpdateOptions{})
return err
})
return result, err
}