@@ -22,9 +22,10 @@ import (
22
22
"reflect"
23
23
24
24
"k8s.io/apimachinery/pkg/api/meta"
25
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25
26
"k8s.io/apimachinery/pkg/runtime"
26
- "k8s.io/apimachinery/pkg/runtime/schema"
27
27
"k8s.io/apimachinery/pkg/runtime/serializer"
28
+ "k8s.io/client-go/dynamic"
28
29
"k8s.io/client-go/kubernetes/scheme"
29
30
"k8s.io/client-go/rest"
30
31
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
@@ -59,16 +60,26 @@ func New(config *rest.Config, options Options) (Client, error) {
59
60
}
60
61
}
61
62
63
+ dynamicClient , err := dynamic .NewForConfig (config )
64
+ if err != nil {
65
+ return nil , err
66
+ }
67
+
62
68
c := & client {
63
- cache : clientCache {
64
- config : config ,
65
- scheme : options .Scheme ,
66
- mapper : options .Mapper ,
67
- codecs : serializer .NewCodecFactory (options .Scheme ),
68
- resourceByType : make (map [reflect.Type ]* resourceMeta ),
69
- unstructuredResourceByGVK : make (map [schema.GroupVersionKind ]* resourceMeta ),
69
+ typedClient : typedClient {
70
+ cache : clientCache {
71
+ config : config ,
72
+ scheme : options .Scheme ,
73
+ mapper : options .Mapper ,
74
+ codecs : serializer .NewCodecFactory (options .Scheme ),
75
+ resourceByType : make (map [reflect.Type ]* resourceMeta ),
76
+ },
77
+ paramCodec : runtime .NewParameterCodec (options .Scheme ),
78
+ },
79
+ unstructuredClient : unstructuredClient {
80
+ client : dynamicClient ,
81
+ restMapper : options .Mapper ,
70
82
},
71
- paramCodec : runtime .NewParameterCodec (options .Scheme ),
72
83
}
73
84
74
85
return c , nil
@@ -79,85 +90,53 @@ var _ Client = &client{}
79
90
// client is a client.Client that reads and writes directly from/to an API server. It lazily initializes
80
91
// new clients at the time they are used, and caches the client.
81
92
type client struct {
82
- cache clientCache
83
- paramCodec runtime. ParameterCodec
93
+ typedClient typedClient
94
+ unstructuredClient unstructuredClient
84
95
}
85
96
86
97
// Create implements client.Client
87
98
func (c * client ) Create (ctx context.Context , obj runtime.Object ) error {
88
- o , err := c . cache . getObjMeta ( obj )
89
- if err != nil {
90
- return err
99
+ _ , ok := obj .( * unstructured. Unstructured )
100
+ if ok {
101
+ return c . unstructuredClient . Create ( ctx , obj )
91
102
}
92
- return o .Post ().
93
- NamespaceIfScoped (o .GetNamespace (), o .isNamespaced ()).
94
- Resource (o .resource ()).
95
- Body (obj ).
96
- Do ().
97
- Into (obj )
103
+ return c .typedClient .Create (ctx , obj )
98
104
}
99
105
100
106
// Update implements client.Client
101
107
func (c * client ) Update (ctx context.Context , obj runtime.Object ) error {
102
- o , err := c . cache . getObjMeta ( obj )
103
- if err != nil {
104
- return err
108
+ _ , ok := obj .( * unstructured. Unstructured )
109
+ if ok {
110
+ return c . unstructuredClient . Update ( ctx , obj )
105
111
}
106
- return o .Put ().
107
- NamespaceIfScoped (o .GetNamespace (), o .isNamespaced ()).
108
- Resource (o .resource ()).
109
- Name (o .GetName ()).
110
- Body (obj ).
111
- Do ().
112
- Into (obj )
112
+ return c .typedClient .Update (ctx , obj )
113
113
}
114
114
115
115
// Delete implements client.Client
116
116
func (c * client ) Delete (ctx context.Context , obj runtime.Object , opts ... DeleteOptionFunc ) error {
117
- o , err := c . cache . getObjMeta ( obj )
118
- if err != nil {
119
- return err
117
+ _ , ok := obj .( * unstructured. Unstructured )
118
+ if ok {
119
+ return c . unstructuredClient . Delete ( ctx , obj , opts ... )
120
120
}
121
-
122
- deleteOpts := DeleteOptions {}
123
- return o .Delete ().
124
- NamespaceIfScoped (o .GetNamespace (), o .isNamespaced ()).
125
- Resource (o .resource ()).
126
- Name (o .GetName ()).
127
- Body (deleteOpts .ApplyOptions (opts ).AsDeleteOptions ()).
128
- Do ().
129
- Error ()
121
+ return c .typedClient .Delete (ctx , obj , opts ... )
130
122
}
131
123
132
124
// Get implements client.Client
133
125
func (c * client ) Get (ctx context.Context , key ObjectKey , obj runtime.Object ) error {
134
- r , err := c . cache . getResource ( obj )
135
- if err != nil {
136
- return err
126
+ _ , ok := obj .( * unstructured. Unstructured )
127
+ if ok {
128
+ return c . unstructuredClient . Get ( ctx , key , obj )
137
129
}
138
- return r .Get ().
139
- NamespaceIfScoped (key .Namespace , r .isNamespaced ()).
140
- Resource (r .resource ()).
141
- Name (key .Name ).Do ().Into (obj )
130
+ return c .typedClient .Get (ctx , key , obj )
142
131
}
143
132
144
133
// List implements client.Client
145
134
func (c * client ) List (ctx context.Context , opts * ListOptions , obj runtime.Object ) error {
146
- r , err := c . cache . getResource ( obj )
147
- if err != nil {
148
- return err
135
+ _ , ok := obj .( * unstructured. UnstructuredList )
136
+ if ok {
137
+ return c . unstructuredClient . List ( ctx , opts , obj )
149
138
}
150
- namespace := ""
151
- if opts != nil {
152
- namespace = opts .Namespace
153
- }
154
- return r .Get ().
155
- NamespaceIfScoped (namespace , r .isNamespaced ()).
156
- Resource (r .resource ()).
157
- Body (obj ).
158
- VersionedParams (opts .AsListOptions (), c .paramCodec ).
159
- Do ().
160
- Into (obj )
139
+ return c .typedClient .List (ctx , opts , obj )
161
140
}
162
141
163
142
// Status implements client.StatusClient
@@ -175,7 +154,7 @@ var _ StatusWriter = &statusWriter{}
175
154
176
155
// Update implements client.StatusWriter
177
156
func (sw * statusWriter ) Update (_ context.Context , obj runtime.Object ) error {
178
- o , err := sw .client .cache .getObjMeta (obj )
157
+ o , err := sw .client .typedClient . cache .getObjMeta (obj )
179
158
if err != nil {
180
159
return err
181
160
}
0 commit comments