/
kubereq.go
183 lines (137 loc) · 3.98 KB
/
kubereq.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package direktiv
import (
"context"
"github.com/vorteil/direktiv/pkg/functions"
igrpc "github.com/vorteil/direktiv/pkg/functions/grpc"
"github.com/vorteil/direktiv/pkg/model"
)
func cancelJob(ctx context.Context, client igrpc.FunctionsServiceClient,
actionID string) {
appLog.Debugf("cancelling job %v", actionID)
cr := igrpc.CancelPodRequest{
ActionID: &actionID,
}
_, err := client.CancelFunctionsPod(ctx, &cr)
if err != nil {
appLog.Errorf("can not cancel job %s: %v", actionID, err)
}
}
func addPodFunction(ctx context.Context,
client igrpc.FunctionsServiceClient, ir *functionRequest) (string, string, error) {
sz := int32(ir.Container.Size)
scale := int32(ir.Container.Scale)
step := int64(ir.Workflow.Step)
cr := igrpc.CreatePodRequest{
Info: &igrpc.BaseInfo{
Name: &ir.Container.ID,
Namespace: &ir.Workflow.Namespace,
Workflow: &ir.Workflow.ID,
Image: &ir.Container.Image,
Cmd: &ir.Container.Cmd,
Size: &sz,
MinScale: &scale,
},
ActionID: &ir.ActionID,
InstanceID: &ir.Workflow.InstanceID,
Step: &step,
}
r, err := client.CreateFunctionsPod(ctx, &cr)
return r.GetHostname(), r.GetIp(), err
}
func isKnativeFunction(client igrpc.FunctionsServiceClient,
name, namespace, workflow string) bool {
// search annotations
a := make(map[string]string)
a[functions.ServiceHeaderName] = name
a[functions.ServiceHeaderNamespace] = namespace
a[functions.ServiceHeaderWorkflow] = workflow
a[functions.ServiceHeaderScope] = functions.PrefixService
appLog.Debugf("knative function search: %v", a)
l, err := client.ListFunctions(context.Background(), &igrpc.ListFunctionsRequest{
Annotations: a,
})
if err != nil {
appLog.Errorf("can not list knative service: %v", err)
return false
}
if len(l.Functions) > 0 {
return true
}
return false
}
func createKnativeFunction(client igrpc.FunctionsServiceClient,
ir *functionRequest) error {
sz := int32(ir.Container.Size)
scale := int32(ir.Container.Scale)
cr := igrpc.CreateFunctionRequest{
Info: &igrpc.BaseInfo{
Name: &ir.Container.ID,
Namespace: &ir.Workflow.Namespace,
Workflow: &ir.Workflow.ID,
Image: &ir.Container.Image,
Cmd: &ir.Container.Cmd,
Size: &sz,
MinScale: &scale,
},
}
_, err := client.CreateFunction(context.Background(), &cr)
return err
}
func createKnativeFunctions(client igrpc.FunctionsServiceClient,
wfm model.Workflow, ns string) error {
for _, f := range wfm.GetFunctions() {
// only build workflow based functions
if f.GetType() != model.ReusableContainerFunctionType {
continue
}
fn := f.(*model.ReusableFunctionDefinition)
// create services async
go func(fd *model.ReusableFunctionDefinition,
model model.Workflow, name, namespace string) {
sz := int32(fd.Size)
scale := int32(fd.Scale)
cr := igrpc.CreateFunctionRequest{
Info: &igrpc.BaseInfo{
Name: &name,
Namespace: &namespace,
Workflow: &model.ID,
Image: &fd.Image,
Cmd: &fd.Cmd,
Size: &sz,
MinScale: &scale,
},
}
_, err := client.CreateFunction(context.Background(), &cr)
if err != nil {
appLog.Errorf("can not create knative service: %v", err)
}
}(fn, wfm, fn.ID, ns)
}
return nil
}
func deleteKnativeFunctions(client igrpc.FunctionsServiceClient,
ns, wf, name string) error {
annotations := make(map[string]string)
scope := functions.PrefixService
if ns != "" {
annotations[functions.ServiceHeaderNamespace] = ns
scope = functions.PrefixNamespace
}
if wf != "" {
annotations[functions.ServiceHeaderWorkflow] = wf
scope = functions.PrefixWorkflow
}
if name != "" {
annotations[functions.ServiceHeaderName] = name
scope = functions.PrefixService
}
annotations[functions.ServiceHeaderScope] = scope
dr := igrpc.ListFunctionsRequest{
Annotations: annotations,
}
_, err := client.DeleteFunctions(context.Background(), &dr)
if err != nil {
appLog.Errorf("can not delete knative service: %v", err)
}
return nil
}