generated from crossplane/function-template-go
-
Notifications
You must be signed in to change notification settings - Fork 37
/
claimconditions.go
88 lines (78 loc) · 2.65 KB
/
claimconditions.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/function-sdk-go/errors"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/response"
corev1 "k8s.io/api/core/v1"
)
// A CompositionTarget is the target of a composition event or condition.
type CompositionTarget string
// A TargetedCondition represents a condition produced by the composition
// process. It can target either the XR only, or both the XR and the claim.
type TargetedCondition struct {
xpv1.Condition `json:",inline"`
Target CompositionTarget `json:"target"`
}
// Composition event and condition targets.
const (
CompositionTargetComposite CompositionTarget = "Composite"
CompositionTargetCompositeAndClaim CompositionTarget = "CompositeAndClaim"
)
// UpdateClaimConditions updates Conditions in the Claim and Composite
func UpdateClaimConditions(rsp *fnv1.RunFunctionResponse, conditions ...TargetedCondition) error {
if rsp == nil {
return nil
}
for _, c := range conditions {
if xpv1.IsSystemConditionType(xpv1.ConditionType(c.Type)) {
response.Fatal(rsp, errors.Errorf("cannot set ClaimCondition type: %s is a reserved Crossplane Condition", c.Type))
return errors.New("error updating response")
}
co := transformCondition(c)
UpdateResponseWithCondition(rsp, co)
}
return nil
}
// transformCondition converts a TargetedCondition to be compatible with the Protobuf SDK
func transformCondition(tc TargetedCondition) *fnv1.Condition {
c := &fnv1.Condition{
Type: string(tc.Condition.Type),
Reason: string(tc.Condition.Reason),
Target: transformTarget(tc.Target),
}
switch tc.Condition.Status {
case corev1.ConditionTrue:
c.Status = fnv1.Status_STATUS_CONDITION_TRUE
case corev1.ConditionFalse:
c.Status = fnv1.Status_STATUS_CONDITION_FALSE
case corev1.ConditionUnknown:
fallthrough
default:
c.Status = fnv1.Status_STATUS_CONDITION_UNKNOWN
}
if tc.Message != "" {
c.Message = &tc.Message
}
return c
}
// transformTarget converts the input into a target Go SDK Enum
// Default to TARGET_COMPOSITE
func transformTarget(ct CompositionTarget) *fnv1.Target {
if ct == CompositionTargetCompositeAndClaim {
return fnv1.Target_TARGET_COMPOSITE_AND_CLAIM.Enum().Enum()
}
return fnv1.Target_TARGET_COMPOSITE.Enum()
}
// UpdateResponseWithCondition updates the RunFunctionResponse with a Condition
func UpdateResponseWithCondition(rsp *fnv1.RunFunctionResponse, c *fnv1.Condition) {
if rsp == nil {
return
}
if rsp.GetConditions() == nil {
rsp.Conditions = make([]*fnv1.Condition, 0, 1)
}
if c != nil {
rsp.Conditions = append(rsp.GetConditions(), c)
}
}