Skip to content

Commit ef470d4

Browse files
authored
Merge pull request #76 from pablochacin/fix-namespace-resources
Use non-namespaced resource for namespaces
2 parents e201063 + 3702ed0 commit ef470d4

File tree

2 files changed

+287
-87
lines changed

2 files changed

+287
-87
lines changed

pkg/resources/resources.go

+42-54
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ func NewFromClient(ctx context.Context, dynamic dynamic.Interface) *Client {
7777
}
7878
}
7979

80-
// maps kinds to api resources
81-
func knownKinds(kind string) (schema.GroupVersionResource, error) {
80+
// getResource maps kinds to api resources
81+
func (c *Client) getResource(kind string, namespace string) (dynamic.ResourceInterface, error) {
8282
kindMapping := map[string]schema.GroupVersionResource{
8383
"ConfigMap": {Group: "", Version: "v1", Resource: "configmaps"},
8484
"Deployment": {Group: "apps", Version: "v1", Resource: "deployments"},
@@ -94,11 +94,19 @@ func knownKinds(kind string) (schema.GroupVersionResource, error) {
9494
"StatefulSet": {Group: "apps", Version: "v1", Resource: "statefulsets"},
9595
}
9696

97-
gvk, found := kindMapping[kind]
97+
gvr, found := kindMapping[kind]
9898
if !found {
99-
return schema.GroupVersionResource{}, fmt.Errorf("unknown kind: '%s'", kind)
99+
return nil, fmt.Errorf("unknown kind: '%s'", kind)
100100
}
101-
return gvk, nil
101+
102+
var resource dynamic.ResourceInterface
103+
if kind == "Namespace" {
104+
resource = c.dynamic.Resource(gvr)
105+
} else {
106+
resource = c.dynamic.Resource(gvr).Namespace(namespace)
107+
}
108+
109+
return resource, nil
102110
}
103111

104112
// Apply creates a resource in a kubernetes cluster from a YAML manifest
@@ -108,23 +116,21 @@ func (c *Client) Apply(manifest string) error {
108116
if err != nil {
109117
return err
110118
}
111-
resource, err := knownKinds(gvk.Kind)
112-
if err != nil {
113-
return err
114-
}
115-
116119
namespace := uObj.GetNamespace()
117120
if namespace == "" {
118121
namespace = "default"
119122
}
120123

121-
_, err = c.dynamic.Resource(resource).
122-
Namespace(namespace).
123-
Create(
124-
c.ctx,
125-
uObj,
126-
metav1.CreateOptions{},
127-
)
124+
resource, err := c.getResource(gvk.Kind, namespace)
125+
if err != nil {
126+
return err
127+
}
128+
129+
_, err = resource.Create(
130+
c.ctx,
131+
uObj,
132+
metav1.CreateOptions{},
133+
)
128134
return err
129135
}
130136

@@ -139,19 +145,12 @@ func (c *Client) Create(obj map[string]interface{}) (map[string]interface{}, err
139145
if namespace == "" {
140146
namespace = "default"
141147
}
142-
gvr, err := knownKinds(gvk.Kind)
148+
149+
resource, err := c.getResource(gvk.Kind, namespace)
143150
if err != nil {
144151
return nil, err
145152
}
146153

147-
// Namesapces cannot be created in a namespaced resource interface, handle as special case
148-
var resource dynamic.ResourceInterface
149-
if gvk.Kind == "Namespace" {
150-
resource = c.dynamic.Resource(gvr)
151-
} else {
152-
resource = c.dynamic.Resource(gvr).Namespace(namespace)
153-
}
154-
155154
resp, err := resource.Create(
156155
c.ctx,
157156
uObj,
@@ -165,19 +164,16 @@ func (c *Client) Create(obj map[string]interface{}) (map[string]interface{}, err
165164

166165
// Get returns an object given its kind, name and namespace
167166
func (c *Client) Get(kind string, name string, namespace string) (map[string]interface{}, error) {
168-
resource, err := knownKinds(kind)
167+
resource, err := c.getResource(kind, namespace)
169168
if err != nil {
170169
return nil, err
171170
}
172171

173-
resp, err := c.dynamic.
174-
Resource(resource).
175-
Namespace(namespace).
176-
Get(
177-
c.ctx,
178-
name,
179-
metav1.GetOptions{},
180-
)
172+
resp, err := resource.Get(
173+
c.ctx,
174+
name,
175+
metav1.GetOptions{},
176+
)
181177
if err != nil {
182178
return nil, err
183179
}
@@ -186,14 +182,12 @@ func (c *Client) Get(kind string, name string, namespace string) (map[string]int
186182

187183
// List returns a list of objects given its kind and namespace
188184
func (c *Client) List(kind string, namespace string) ([]map[string]interface{}, error) {
189-
resource, err := knownKinds(kind)
185+
resource, err := c.getResource(kind, namespace)
190186
if err != nil {
191187
return nil, err
192188
}
193-
resp, err := c.dynamic.
194-
Resource(resource).
195-
Namespace(namespace).
196-
List(c.ctx, metav1.ListOptions{})
189+
190+
resp, err := resource.List(c.ctx, metav1.ListOptions{})
197191
if err != nil {
198192
return nil, err
199193
}
@@ -207,15 +201,11 @@ func (c *Client) List(kind string, namespace string) ([]map[string]interface{},
207201

208202
// Delete deletes an object given its kind, name and namespace
209203
func (c *Client) Delete(kind string, name string, namespace string) error {
210-
resource, err := knownKinds(kind)
204+
resource, err := c.getResource(kind, namespace)
211205
if err != nil {
212206
return err
213207
}
214-
215-
err = c.dynamic.
216-
Resource(resource).
217-
Namespace(namespace).
218-
Delete(c.ctx, name, metav1.DeleteOptions{})
208+
err = resource.Delete(c.ctx, name, metav1.DeleteOptions{})
219209

220210
return err
221211
}
@@ -231,18 +221,16 @@ func (c *Client) Update(obj map[string]interface{}) (map[string]interface{}, err
231221
if namespace == "" {
232222
namespace = "default"
233223
}
234-
resource, err := knownKinds(gvk.Kind)
224+
resource, err := c.getResource(gvk.Kind, namespace)
235225
if err != nil {
236226
return nil, err
237227
}
238228

239-
resp, err := c.dynamic.Resource(resource).
240-
Namespace(namespace).
241-
Update(
242-
c.ctx,
243-
uObj,
244-
metav1.UpdateOptions{},
245-
)
229+
resp, err := resource.Update(
230+
c.ctx,
231+
uObj,
232+
metav1.UpdateOptions{},
233+
)
246234
if err != nil {
247235
return nil, err
248236
}

0 commit comments

Comments
 (0)