-
Notifications
You must be signed in to change notification settings - Fork 114
/
freight_helpers.go
66 lines (62 loc) · 1.66 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
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
}
// IsFreightAvailable answers whether the specified Freight is available to the
// specified Stage having the specified upstream stages. Freight is available
// if:
//
// 1. No upstreamStages are specified
// OR
// 2. The Freight has has been verified in ANY of the specified upstream stages
// OR
// 3. The Freight is approved for the specified stage
//
// Note: The rationale for returning true when no upstream stages are specified
// is that some Stages have no upstream Stages (e.g. a Stage that subscribes to
// a Warehouse), so ANY Freight is available to such a Stage.
func IsFreightAvailable(
freight *Freight,
stage string,
upstreamStages []string,
) bool {
if len(upstreamStages) == 0 {
return true
}
for _, stage := range upstreamStages {
if _, ok := freight.Status.VerifiedIn[stage]; ok {
return true
}
}
if stage != "" {
if _, ok := freight.Status.ApprovedFor[stage]; ok {
return true
}
}
return false
}