This repository has been archived by the owner on May 27, 2022. It is now read-only.
/
get.go
68 lines (60 loc) · 1.89 KB
/
get.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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2021 Intel Corporation
package client
import (
"context"
"encoding/json"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// ReadResource is structure used for reading resource
type ReadResource struct {
Gvk schema.GroupVersionKind `json:"GVK,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
}
// Get gets the resource from the remote cluster
func (c *Client) Get(gvkRes []byte, namespace string) ([]byte, error) {
var g ReadResource
var ns string
err := json.Unmarshal(gvkRes, &g)
if err != nil {
return nil, fmt.Errorf("Invalid read resource %v", err)
}
if namespace == "default" && g.Namespace != "" {
ns = g.Namespace
} else {
ns = namespace
}
// Create a mapper for the GVK
mapping, err := c.RestMapper.RESTMapping(schema.GroupKind{
Group: g.Gvk.Group,
Kind: g.Gvk.Kind,
}, g.Gvk.Version)
if err != nil {
return nil, fmt.Errorf("RESTMapping for GVK failed %v", err)
}
gvr := mapping.Resource
opts := metav1.GetOptions{}
var unstruct *unstructured.Unstructured
// Handle based on namespace scopped GVK or not
switch mapping.Scope.Name() {
case meta.RESTScopeNameNamespace:
unstruct, err = c.DynamicClient.Resource(gvr).Namespace(ns).Get(context.TODO(), g.Name, opts)
case meta.RESTScopeNameRoot:
unstruct, err = c.DynamicClient.Resource(gvr).Get(context.TODO(), g.Name, opts)
default:
return nil, fmt.Errorf("RESTScopeName for GVK failed %v, %s", err, g.Gvk.String())
}
if err != nil {
return nil, fmt.Errorf("Getting getting RESTScopeName %v", err)
}
b, err := unstruct.MarshalJSON()
if err != nil {
return nil, fmt.Errorf("Failed unstruct MarshalJSON %v", err)
}
return b, nil
}