Skip to content

Commit cbcef29

Browse files
authored
Merge pull request #18 from jaypipes/no-retry-fail
ensure no retry for create/apply/delete
2 parents 3e24bea + f198f96 commit cbcef29

15 files changed

+263
-235
lines changed

action.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"os"
1414
"strings"
1515

16+
"github.com/gdt-dev/gdt/api"
1617
"github.com/gdt-dev/gdt/debug"
17-
gdterrors "github.com/gdt-dev/gdt/errors"
1818
"github.com/gdt-dev/gdt/parse"
1919
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -226,7 +226,7 @@ func (a *Action) create(
226226
if err != nil {
227227
// This should never happen because we check during parse time
228228
// whether the file can be opened.
229-
rterr := fmt.Errorf("%w: %s", gdterrors.RuntimeError, err)
229+
rterr := fmt.Errorf("%w: %s", api.RuntimeError, err)
230230
return rterr
231231
}
232232
defer f.Close()
@@ -245,7 +245,7 @@ func (a *Action) create(
245245

246246
objs, err := unstructuredFromReader(r)
247247
if err != nil {
248-
rterr := fmt.Errorf("%w: %s", gdterrors.RuntimeError, err)
248+
rterr := fmt.Errorf("%w: %s", api.RuntimeError, err)
249249
return rterr
250250
}
251251
for _, obj := range objs {
@@ -290,7 +290,7 @@ func (a *Action) apply(
290290
if err != nil {
291291
// This should never happen because we check during parse time
292292
// whether the file can be opened.
293-
rterr := fmt.Errorf("%w: %s", gdterrors.RuntimeError, err)
293+
rterr := fmt.Errorf("%w: %s", api.RuntimeError, err)
294294
return rterr
295295
}
296296
defer f.Close()
@@ -309,7 +309,7 @@ func (a *Action) apply(
309309

310310
objs, err := unstructuredFromReader(r)
311311
if err != nil {
312-
rterr := fmt.Errorf("%w: %s", gdterrors.RuntimeError, err)
312+
rterr := fmt.Errorf("%w: %s", api.RuntimeError, err)
313313
return rterr
314314
}
315315
for _, obj := range objs {
@@ -358,13 +358,13 @@ func (a *Action) delete(
358358
if err != nil {
359359
// This should never happen because we check during parse time
360360
// whether the file can be opened.
361-
rterr := fmt.Errorf("%w: %s", gdterrors.RuntimeError, err)
361+
rterr := fmt.Errorf("%w: %s", api.RuntimeError, err)
362362
return rterr
363363
}
364364
defer f.Close()
365365
objs, err := unstructuredFromReader(f)
366366
if err != nil {
367-
rterr := fmt.Errorf("%w: %s", gdterrors.RuntimeError, err)
367+
rterr := fmt.Errorf("%w: %s", api.RuntimeError, err)
368368
return rterr
369369
}
370370
for _, obj := range objs {
@@ -378,7 +378,7 @@ func (a *Action) delete(
378378
if ons == "" {
379379
ons = ns
380380
}
381-
if err = a.doDelete(ctx, c, res, name, ns); err != nil {
381+
if err = a.doDelete(ctx, c, res, name, ons); err != nil {
382382
return err
383383
}
384384
}

assertions.go

+14-45
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ import (
1212
"net/http"
1313
"strings"
1414

15+
"github.com/gdt-dev/gdt/api"
1516
gdtjson "github.com/gdt-dev/gdt/assertion/json"
16-
gdterrors "github.com/gdt-dev/gdt/errors"
17-
gdttypes "github.com/gdt-dev/gdt/types"
1817
"gopkg.in/yaml.v3"
1918
apierrors "k8s.io/apimachinery/pkg/api/errors"
2019
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -166,8 +165,8 @@ type Expect struct {
166165
// conditionMatch is a struct with fields that we will match a resource's
167166
// `Condition` against.
168167
type conditionMatch struct {
169-
Status *gdttypes.FlexStrings `yaml:"status,omitempty"`
170-
Reason string `yaml:"reason,omitempty"`
168+
Status *api.FlexStrings `yaml:"status,omitempty"`
169+
Reason string `yaml:"reason,omitempty"`
171170
}
172171

173172
// ConditionMatch can be a string (the ConditionStatus to match), a slice of
@@ -182,7 +181,7 @@ type ConditionMatch struct {
182181
// ConditionMatch can be either a string, a slice of strings, or an object with .
183182
func (m *ConditionMatch) UnmarshalYAML(node *yaml.Node) error {
184183
if node.Kind == yaml.ScalarNode || node.Kind == yaml.SequenceNode {
185-
var fs gdttypes.FlexStrings
184+
var fs api.FlexStrings
186185
if err := node.Decode(&fs); err != nil {
187186
return ConditionMatchInvalid(node, err)
188187
}
@@ -203,10 +202,10 @@ func (m *ConditionMatch) UnmarshalYAML(node *yaml.Node) error {
203202
type PlacementAssertion struct {
204203
// Spread contains zero or more topology keys that gdt-kube will assert an
205204
// even spread across.
206-
Spread *gdttypes.FlexStrings `yaml:"spread,omitempty"`
205+
Spread *api.FlexStrings `yaml:"spread,omitempty"`
207206
// Pack contains zero or more topology keys that gdt-kube will assert
208207
// bin-packing of resources within.
209-
Pack *gdttypes.FlexStrings `yaml:"pack,omitempty"`
208+
Pack *api.FlexStrings `yaml:"pack,omitempty"`
210209
}
211210

212211
// assertions contains all assertions made for the exec test
@@ -246,7 +245,7 @@ func (a *assertions) OK(ctx context.Context) bool {
246245
exp := a.exp
247246
if exp == nil {
248247
if a.err != nil {
249-
a.Fail(gdterrors.UnexpectedError(a.err))
248+
a.Fail(api.UnexpectedError(a.err))
250249
return false
251250
}
252251
return true
@@ -309,16 +308,16 @@ func (a *assertions) errorOK() bool {
309308
}
310309
if exp.Error != "" && a.r != nil {
311310
if a.err == nil {
312-
a.Fail(gdterrors.UnexpectedError(a.err))
311+
a.Fail(api.UnexpectedError(a.err))
313312
return false
314313
}
315314
if !strings.Contains(a.err.Error(), exp.Error) {
316-
a.Fail(gdterrors.NotIn(a.err.Error(), exp.Error))
315+
a.Fail(api.NotIn(a.err.Error(), exp.Error))
317316
return false
318317
}
319318
}
320319
if a.err != nil {
321-
a.Fail(gdterrors.UnexpectedError(a.err))
320+
a.Fail(api.UnexpectedError(a.err))
322321
return false
323322
}
324323
return true
@@ -329,36 +328,6 @@ func (a *assertions) expectsNotFound() bool {
329328
return (exp.Len != nil && *exp.Len == 0) || exp.NotFound
330329
}
331330

332-
// notFoundOK returns true if the supplied error and response matches the
333-
// NotFound condition and the Len==0 condition, false otherwise
334-
func (a *assertions) notFoundOK() bool {
335-
if a.expectsNotFound() {
336-
// First check if the error is like one returned from Get or Delete
337-
// that has a 404 ErrStatus.Code in it
338-
apierr, ok := a.err.(*apierrors.StatusError)
339-
if ok {
340-
if http.StatusNotFound != int(apierr.ErrStatus.Code) {
341-
msg := fmt.Sprintf("got status code %d", apierr.ErrStatus.Code)
342-
a.Fail(ExpectedNotFound(msg))
343-
return false
344-
}
345-
return true
346-
}
347-
// Next check to see if the supplied resp is a list of objects returned
348-
// by the dynamic client and if so, is that an empty list.
349-
list, ok := a.r.(*unstructured.UnstructuredList)
350-
if ok {
351-
if len(list.Items) != 0 {
352-
msg := fmt.Sprintf("got %d items", len(list.Items))
353-
a.Fail(ExpectedNotFound(msg))
354-
return false
355-
}
356-
return true
357-
}
358-
}
359-
return true
360-
}
361-
362331
// lenOK returns true if the subject matches the Len condition, false otherwise
363332
func (a *assertions) lenOK() bool {
364333
exp := a.exp
@@ -368,7 +337,7 @@ func (a *assertions) lenOK() bool {
368337
list, ok := a.r.(*unstructured.UnstructuredList)
369338
if ok && list != nil {
370339
if len(list.Items) != *exp.Len {
371-
a.Fail(gdterrors.NotEqualLength(*exp.Len, len(list.Items)))
340+
a.Fail(api.NotEqualLength(*exp.Len, len(list.Items)))
372341
return false
373342
}
374343
}
@@ -402,7 +371,7 @@ func (a *assertions) matchesOK() bool {
402371
// for _, obj := range list.Items {
403372
// diff := compareResourceToMatchObject(obj, matchObj)
404373
//
405-
// a.Fail(gdterrors.NotEqualLength(*exp.Len, len(list.Items)))
374+
// a.Fail(api.NotEqualLength(*exp.Len, len(list.Items)))
406375
// return false
407376
// }
408377
//}
@@ -435,7 +404,7 @@ func (a *assertions) conditionsOK() bool {
435404
// for _, obj := range list.Items {
436405
// diff := compareResourceToMatchObject(obj, matchObj)
437406
//
438-
// a.Fail(gdterrors.NotEqualLength(*exp.Len, len(list.Items)))
407+
// a.Fail(api.NotEqualLength(*exp.Len, len(list.Items)))
439408
// return false
440409
// }
441410
//}
@@ -511,7 +480,7 @@ func newAssertions(
511480
exp *Expect,
512481
err error,
513482
r interface{},
514-
) gdttypes.Assertions {
483+
) api.Assertions {
515484
return &assertions{
516485
c: c,
517486
failures: []error{},

compare.go

+21-23
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package kube
66

77
import (
88
"fmt"
9-
"io/ioutil"
9+
"os"
1010
"reflect"
1111
"strconv"
1212
"strings"
@@ -42,7 +42,7 @@ func compareConditions(
4242
panic(msg)
4343
}
4444
if (!found || len(conds) == 0) && len(expected) != 0 {
45-
for condType, _ := range expected {
45+
for condType := range expected {
4646
d.Add(fmt.Sprintf("no condition with type %q found", condType))
4747
}
4848
return d
@@ -130,13 +130,13 @@ func compareConditions(
130130
// map[string]interface{} is the collection of resource fields that we will
131131
// match against.
132132
func matchObjectFromAny(m interface{}) map[string]interface{} {
133-
switch m.(type) {
133+
switch m := m.(type) {
134134
case string:
135135
var err error
136136
var b []byte
137-
v := m.(string)
137+
v := m
138138
if probablyFilePath(v) {
139-
b, err = ioutil.ReadFile(v)
139+
b, err = os.ReadFile(v)
140140
if err != nil {
141141
// NOTE(jaypipes): We already validated that the file exists at
142142
// parse time. If we get an error here, just panic cuz there's
@@ -155,7 +155,7 @@ func matchObjectFromAny(m interface{}) map[string]interface{} {
155155
}
156156
return obj
157157
case map[string]interface{}:
158-
return m.(map[string]interface{})
158+
return m
159159
}
160160
return map[string]interface{}{}
161161
}
@@ -238,7 +238,7 @@ func collectFieldDifferences(
238238
}
239239
return
240240
case int, int8, int16, int32, int64:
241-
switch subject.(type) {
241+
switch subject := subject.(type) {
242242
case int, int8, int16, int32, int64:
243243
mv := toInt64(match)
244244
sv := toInt64(subject)
@@ -261,8 +261,7 @@ func collectFieldDifferences(
261261
}
262262
case string:
263263
mv := toInt64(match)
264-
ss := subject.(string)
265-
sv, err := strconv.Atoi(ss)
264+
sv, err := strconv.Atoi(subject)
266265
if err != nil {
267266
diff := fmt.Sprintf(
268267
"%s had different values. expected %v but found %v",
@@ -296,8 +295,7 @@ func collectFieldDifferences(
296295
}
297296
case string:
298297
mv, _ := match.(string)
299-
sv, _ := subject.(string)
300-
if mv != sv {
298+
if mv != subject {
301299
diff := fmt.Sprintf(
302300
"%s had different values. expected %v but found %v",
303301
fp, match, subject,
@@ -362,34 +360,34 @@ func typesComparable(a, b interface{}) bool {
362360

363361
// toUint64 takes an interface and returns a uint64
364362
func toUint64(v interface{}) uint64 {
365-
switch v.(type) {
363+
switch v := v.(type) {
366364
case uint64:
367-
return v.(uint64)
365+
return v
368366
case uint8:
369-
return uint64(v.(uint8))
367+
return uint64(v)
370368
case uint16:
371-
return uint64(v.(uint16))
369+
return uint64(v)
372370
case uint32:
373-
return uint64(v.(uint32))
371+
return uint64(v)
374372
case uint:
375-
return uint64(v.(uint))
373+
return uint64(v)
376374
}
377375
return 0
378376
}
379377

380378
// toInt64 takes an interface and returns an int64
381379
func toInt64(v interface{}) int64 {
382-
switch v.(type) {
380+
switch v := v.(type) {
383381
case int64:
384-
return v.(int64)
382+
return v
385383
case int8:
386-
return int64(v.(int8))
384+
return int64(v)
387385
case int16:
388-
return int64(v.(int16))
386+
return int64(v)
389387
case int32:
390-
return int64(v.(int32))
388+
return int64(v)
391389
case int:
392-
return int64(v.(int))
390+
return int64(v)
393391
}
394392
return 0
395393
}

defaults.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ package kube
77
import (
88
"os"
99

10-
"github.com/gdt-dev/gdt/errors"
11-
gdttypes "github.com/gdt-dev/gdt/types"
10+
"github.com/gdt-dev/gdt/api"
1211
"gopkg.in/yaml.v3"
1312
)
1413

@@ -39,21 +38,21 @@ type Defaults struct {
3938

4039
func (d *Defaults) UnmarshalYAML(node *yaml.Node) error {
4140
if node.Kind != yaml.MappingNode {
42-
return errors.ExpectedMapAt(node)
41+
return api.ExpectedMapAt(node)
4342
}
4443
// maps/structs are stored in a top-level Node.Content field which is a
4544
// concatenated slice of Node pointers in pairs of key/values.
4645
for i := 0; i < len(node.Content); i += 2 {
4746
keyNode := node.Content[i]
4847
if keyNode.Kind != yaml.ScalarNode {
49-
return errors.ExpectedScalarAt(keyNode)
48+
return api.ExpectedScalarAt(keyNode)
5049
}
5150
key := keyNode.Value
5251
valNode := node.Content[i+1]
5352
switch key {
5453
case "kube":
5554
if valNode.Kind != yaml.MappingNode {
56-
return errors.ExpectedMapAt(valNode)
55+
return api.ExpectedMapAt(valNode)
5756
}
5857
hd := kubeDefaults{}
5958
if err := valNode.Decode(&hd); err != nil {
@@ -86,7 +85,7 @@ func (d *Defaults) validate() error {
8685
}
8786

8887
// fromBaseDefaults returns an gdt-kube plugin-specific Defaults from a Spec
89-
func fromBaseDefaults(base *gdttypes.Defaults) *Defaults {
88+
func fromBaseDefaults(base *api.Defaults) *Defaults {
9089
if base == nil {
9190
return nil
9291
}

0 commit comments

Comments
 (0)