-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_lifecycle.go
142 lines (115 loc) · 3.99 KB
/
function_lifecycle.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package v1alpha1
import (
"context"
"strings"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
"github.com/zeiss/typhoon/pkg/apis/common/v1alpha1"
)
// GetGroupVersionKind implements kmeta.OwnerRefable.
func (*Function) GetGroupVersionKind() schema.GroupVersionKind {
return SchemeGroupVersion.WithKind("Function")
}
// GetConditionSet implements duckv1.KRShaped.
func (f *Function) GetConditionSet() apis.ConditionSet {
if f.Spec.Sink.Ref != nil || f.Spec.Sink.URI != nil {
return funcSenderConditionSet
}
return functionConditionSet
}
// GetStatus implements duckv1.KRShaped.
func (f *Function) GetStatus() *duckv1.Status {
return &f.Status.Status.Status
}
// GetStatusManager implements Reconcilable.
func (f *Function) GetStatusManager() *v1alpha1.StatusManager {
return &v1alpha1.StatusManager{
ConditionSet: f.GetConditionSet(),
Status: &f.Status.Status,
}
}
// GetEventTypes implements EventSource.
func (f *Function) GetEventTypes() []string {
if f.Spec.CloudEventOverrides == nil || len(f.Spec.CloudEventOverrides.Extensions) == 0 {
return []string{defaultCEType(f)}
}
if typ := f.Spec.CloudEventOverrides.Extensions["type"]; typ != "" {
return []string{typ}
}
return []string{defaultCEType(f)}
}
func defaultCEType(f *Function) string {
const ceDefaultTypePrefix = "com.zeiss.function."
return ceDefaultTypePrefix + f.Spec.Runtime
}
// AsEventSource implements EventSource.
func (f *Function) AsEventSource() string {
if f.Spec.CloudEventOverrides == nil || len(f.Spec.CloudEventOverrides.Extensions) == 0 {
return defaultCESource(f)
}
if source := f.Spec.CloudEventOverrides.Extensions["source"]; source != "" {
return source
}
return defaultCESource(f)
}
func defaultCESource(f *Function) string {
kind := strings.ToLower(f.GetGroupVersionKind().Kind)
return "com.zeiss." + kind + "." + f.Namespace + "." + f.Name
}
// GetSink implements EventSender.
func (f *Function) GetSink() *duckv1.Destination {
return &f.Spec.Sink
}
// GetAdapterOverrides implements AdapterConfigurable.
func (f *Function) GetAdapterOverrides() *v1alpha1.AdapterOverrides {
return f.Spec.AdapterOverrides
}
// Status conditions
const (
// FunctionConditionConfigMapReady has status True when the ConfigMap
// containing the code of the Function was successfully reconciled.
FunctionConditionConfigMapReady apis.ConditionType = "ConfigMapReady"
)
// Reasons for status conditions
const (
// FunctionReasonFailedSync encompasses any type of error occurring while synchronizing a Kubernetes API object.
// It is meant to be set on the ConfigMapReady condition.
FunctionReasonFailedSync = "FailedSync"
)
// ConditionSets
var (
// functionConditionSet is used when the component instance is
// configured without a sink (reply mode).
functionConditionSet = v1alpha1.NewConditionSet(
FunctionConditionConfigMapReady,
)
// funcSenderConditionSet is used when the component instance is
// configured with a sink (send mode).
funcSenderConditionSet = v1alpha1.NewConditionSet(
FunctionConditionConfigMapReady,
v1alpha1.ConditionSinkProvided,
)
)
// MarkConfigMapAvailable sets the ConfigMapReady condition to True and reports
// the name and resource version of the code ConfigMap.
func (s *FunctionStatus) MarkConfigMapAvailable(cmapName, resourceVersion string) {
s.ConfigMap = &FunctionConfigMapIdentity{
Name: cmapName,
ResourceVersion: resourceVersion,
}
functionConditionSet.Manage(s).MarkTrue(FunctionConditionConfigMapReady)
}
// MarkConfigMapUnavailable sets the ConfigMapReady condition to False with the
// given reason and associated message.
func (s *FunctionStatus) MarkConfigMapUnavailable(reason, msg string) {
functionConditionSet.Manage(s).MarkFalse(FunctionConditionConfigMapReady,
reason, msg)
}
// SetDefaults implements apis.Defaultable
func (f *Function) SetDefaults(ctx context.Context) {
}
// Validate implements apis.Validatable
func (f *Function) Validate(ctx context.Context) *apis.FieldError {
return nil
}