forked from grafana/xk6-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake.go
59 lines (51 loc) · 2.15 KB
/
fake.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
package testutils
import (
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
dynamicfake "k8s.io/client-go/dynamic/fake"
"k8s.io/client-go/kubernetes/fake"
)
// NewFakeDynamic creates a new instance of a fake dynamic client with a default scheme
func NewFakeDynamic(objs ...runtime.Object) (*dynamicfake.FakeDynamicClient, error) {
scheme := runtime.NewScheme()
err := fake.AddToScheme(scheme)
if err != nil {
return nil, err
}
return dynamicfake.NewSimpleDynamicClient(scheme, objs...), nil
}
type FakeRESTMapper struct {
meta.RESTMapper
}
func (f *FakeRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
kindMapping := map[string]schema.GroupVersionResource{
"ConfigMap": {Group: "", Version: "v1", Resource: "configmaps"},
"Deployment": {Group: "apps", Version: "v1", Resource: "deployments"},
"Endpoint": {Group: "", Version: "v1", Resource: "endpoints"},
"Ingress": {Group: "networking.k8s.io", Version: "v1", Resource: "ingresses"},
"Job": {Group: "batch", Version: "v1", Resource: "jobs"},
"PersistentVolume": {Group: "", Version: "v1", Resource: "persistentvolumes"},
"PersistentVolumeClaim": {Group: "", Version: "v1", Resource: "persistentvolumeclaims"},
"Pod": {Group: "", Version: "v1", Resource: "pods"},
"Namespace": {Group: "", Version: "v1", Resource: "namespaces"},
"Node": {Group: "", Version: "v1", Resource: "nodes"},
"Secret": {Group: "", Version: "v1", Resource: "secrets"},
"Service": {Group: "", Version: "v1", Resource: "services"},
"StatefulSet": {Group: "apps", Version: "v1", Resource: "statefulsets"},
}
gvr, found := kindMapping[gk.Kind]
if !found {
return nil, fmt.Errorf("unknown kind: '%s'", gk.Kind)
}
scope := meta.RESTScopeNamespace
if gk.Kind == "Namespace" {
scope = meta.RESTScopeRoot
}
return &meta.RESTMapping{
Resource: gvr,
GroupVersionKind: gvr.GroupVersion().WithKind(gk.Kind),
Scope: scope,
}, nil
}