@@ -17,23 +17,13 @@ package test
17
17
import (
18
18
"bytes"
19
19
goctx "context"
20
- "errors"
21
20
"fmt"
22
21
"io/ioutil"
23
- "strings"
24
22
25
- y2j "github.com/ghodss/yaml"
26
23
yaml "gopkg.in/yaml.v2"
27
24
core "k8s.io/api/core/v1"
28
- crd "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
29
- extensions_scheme "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme"
30
25
apierrors "k8s.io/apimachinery/pkg/api/errors"
31
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32
- "k8s.io/apimachinery/pkg/runtime/schema"
33
- "k8s.io/apimachinery/pkg/runtime/serializer"
34
- "k8s.io/apimachinery/pkg/types"
35
- "k8s.io/client-go/kubernetes/scheme"
36
- "k8s.io/client-go/rest"
37
27
)
38
28
39
29
func (ctx * TestCtx ) GetNamespace () (string , error ) {
@@ -55,119 +45,6 @@ func (ctx *TestCtx) GetNamespace() (string, error) {
55
45
return ctx .Namespace , nil
56
46
}
57
47
58
- func (ctx * TestCtx ) GetCRClient (yamlCR []byte ) (* rest.RESTClient , error ) {
59
- if ctx .CRClient != nil {
60
- return ctx .CRClient , nil
61
- }
62
- // a user may pass nil if they expect the CRClient to already exist
63
- if yamlCR == nil {
64
- return nil , errors .New ("ctx.CRClient does not exist; yamlCR cannot be nil" )
65
- }
66
- // get new RESTClient for custom resources
67
- crConfig := Global .KubeConfig
68
- yamlMap := make (map [interface {}]interface {})
69
- err := yaml .Unmarshal (yamlCR , & yamlMap )
70
- if err != nil {
71
- return nil , err
72
- }
73
- groupVersion := strings .Split (yamlMap ["apiVersion" ].(string ), "/" )
74
- crGV := schema.GroupVersion {Group : groupVersion [0 ], Version : groupVersion [1 ]}
75
- crConfig .GroupVersion = & crGV
76
- crConfig .APIPath = "/apis"
77
- crConfig .NegotiatedSerializer = serializer.DirectCodecFactory {CodecFactory : scheme .Codecs }
78
-
79
- if crConfig .UserAgent == "" {
80
- crConfig .UserAgent = rest .DefaultKubernetesUserAgent ()
81
- }
82
- ctx .CRClient , err = rest .RESTClientFor (crConfig )
83
- return ctx .CRClient , err
84
- }
85
-
86
- // TODO: Implement a way for a user to add their own scheme to us the dynamic
87
- // client to eliminate the need for the UpdateCR function
88
-
89
- // UpdateCR takes the name of a resource, the resource plural name,
90
- // the path of the field that need to be updated (e.g. /spec/size),
91
- // and the new value to that field and patches the resource with
92
- // that change
93
- func (ctx * TestCtx ) UpdateCR (name , resourceName , path , value string ) error {
94
- crClient , err := ctx .GetCRClient (nil )
95
- if err != nil {
96
- return err
97
- }
98
- namespace , err := ctx .GetNamespace ()
99
- if err != nil {
100
- return err
101
- }
102
- return crClient .Patch (types .JSONPatchType ).
103
- Namespace (namespace ).
104
- Resource (resourceName ).
105
- Name (name ).
106
- Body ([]byte ("[{\" op\" : \" replace\" , \" path\" : \" " + path + "\" , \" value\" : " + value + "}]" )).
107
- Do ().
108
- Error ()
109
- }
110
-
111
- func (ctx * TestCtx ) createCRFromYAML (yamlFile []byte , resourceName string ) error {
112
- client , err := ctx .GetCRClient (yamlFile )
113
- if err != nil {
114
- return err
115
- }
116
- namespace , err := ctx .GetNamespace ()
117
- if err != nil {
118
- return err
119
- }
120
- yamlMap := make (map [interface {}]interface {})
121
- err = yaml .Unmarshal (yamlFile , & yamlMap )
122
- if err != nil {
123
- return err
124
- }
125
- // TODO: handle failure of this line without segfault
126
- name := yamlMap ["metadata" ].(map [interface {}]interface {})["name" ].(string )
127
- jsonDat , err := y2j .YAMLToJSON (yamlFile )
128
- err = client .Post ().
129
- Namespace (namespace ).
130
- Resource (resourceName ).
131
- Body (jsonDat ).
132
- Do ().
133
- Error ()
134
- ctx .AddFinalizerFn (func () error {
135
- return client .Delete ().
136
- Namespace (namespace ).
137
- Resource (resourceName ).
138
- Name (name ).
139
- Body (metav1 .NewDeleteOptions (0 )).
140
- Do ().
141
- Error ()
142
- })
143
- return err
144
- }
145
-
146
- func (ctx * TestCtx ) createCRDFromYAML (yamlFile []byte ) error {
147
- decode := extensions_scheme .Codecs .UniversalDeserializer ().Decode
148
- obj , _ , err := decode (yamlFile , nil , nil )
149
- if err != nil {
150
- return err
151
- }
152
- switch o := obj .(type ) {
153
- case * crd.CustomResourceDefinition :
154
- _ , err = Global .ExtensionsClient .ApiextensionsV1beta1 ().CustomResourceDefinitions ().Create (o )
155
- ctx .AddFinalizerFn (func () error {
156
- err = Global .ExtensionsClient .ApiextensionsV1beta1 ().CustomResourceDefinitions ().Delete (o .Name , metav1 .NewDeleteOptions (0 ))
157
- if err != nil && ! apierrors .IsNotFound (err ) {
158
- return err
159
- }
160
- return nil
161
- })
162
- if apierrors .IsAlreadyExists (err ) {
163
- return nil
164
- }
165
- return err
166
- default :
167
- return errors .New ("non-CRD resource in createCRDFromYAML function" )
168
- }
169
- }
170
-
171
48
func setNamespaceYAML (yamlFile []byte , namespace string ) ([]byte , error ) {
172
49
yamlMap := make (map [interface {}]interface {})
173
50
err := yaml .Unmarshal (yamlFile , & yamlMap )
@@ -192,17 +69,7 @@ func (ctx *TestCtx) CreateFromYAML(yamlFile []byte) error {
192
69
193
70
obj , _ , err := Global .DynamicDecoder .Decode (yamlSpec , nil , nil )
194
71
if err != nil {
195
- yamlMap := make (map [interface {}]interface {})
196
- err = yaml .Unmarshal (yamlSpec , & yamlMap )
197
- if err != nil {
198
- return err
199
- }
200
- kind := yamlMap ["kind" ].(string )
201
- err = ctx .createCRFromYAML (yamlSpec , strings .ToLower (kind )+ "s" )
202
- if err != nil {
203
- return err
204
- }
205
- continue
72
+ return err
206
73
}
207
74
208
75
err = Global .DynamicClient .Create (goctx .TODO (), obj )
0 commit comments