-
Notifications
You must be signed in to change notification settings - Fork 114
/
freight_helpers.go
69 lines (65 loc) · 1.95 KB
/
freight_helpers.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
69
package v1alpha1
import (
"context"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// GetFreight returns a pointer to the Freight resource specified by the
// namespacedName argument. If no such resource is found, nil is returned
// instead.
func GetFreight(
ctx context.Context,
c client.Client,
namespacedName types.NamespacedName,
) (*Freight, error) {
freight := Freight{}
if err := c.Get(ctx, namespacedName, &freight); err != nil {
if err = client.IgnoreNotFound(err); err == nil {
return nil, nil
}
return nil, errors.Wrapf(
err,
"error getting Freight %q in namespace %q",
namespacedName.Name,
namespacedName.Namespace,
)
}
return &freight, nil
}
// GetQualifiedFreight returns a pointer to the Freight resource specified by
// the namespacedName argument if it is found and EITHER no Stages were
// specified in the function call OR the Freight has qualified for ANY of the
// specified Stages. If all other cases, nil is returned instead.
//
// Note: The rationale for returning the found Freight (if any) instead of nil
// when no Stages are specified is that the Stages provided are typically the
// names of Stages UPSTREAM from some other Stage. i.e. The typical use for this
// function is to answer whether a piece of Freight has qualified for any of a
// given Stage's UPSTREAM Stages. Some Stages have no upstream Stages, so any
// Freight that is found is implicitly qualified.
func GetQualifiedFreight(
ctx context.Context,
c client.Client,
namespacedName types.NamespacedName,
stages []string,
) (*Freight, error) {
freight, err := GetFreight(ctx, c, namespacedName)
if err != nil {
return nil, err
}
if freight == nil {
return nil, nil
}
if len(stages) == 0 {
return freight, nil
}
for qualifiedStage := range freight.Status.Qualifications {
for _, stage := range stages {
if qualifiedStage == stage {
return freight, nil
}
}
}
return nil, nil
}