-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
220 lines (204 loc) · 4.81 KB
/
utils.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
b64 "encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"strings"
"github.com/open-integration/core/pkg/task"
"github.com/open-integration/service-catalog/kubernetes/pkg/endpoints/run"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type (
kubeRunTaskOptions struct {
name string
commands []string
environ []string
wfcontext *workflowcontext
image string
}
)
func buildCommand(cmds []string) string {
command := []string{
"set -e",
}
command = append(command, cmds...)
return strings.Join(command, " ; ")
}
func buildPodString(namespace string, name string, cmd string, environ []string, image string, pvc string) string {
envs := []v1.EnvVar{}
for _, e := range environ {
kv := strings.Split(e, "=")
envs = append(envs, v1.EnvVar{
Name: kv[0],
Value: kv[1],
})
}
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: strings.ReplaceAll(name, " ", "-"),
Namespace: namespace,
},
Spec: v1.PodSpec{
Containers: []v1.Container{
v1.Container{
Name: "container-1",
Image: image,
Command: []string{
"sh",
"-c",
},
Args: []string{
cmd,
},
WorkingDir: "/openintegration",
VolumeMounts: []v1.VolumeMount{
v1.VolumeMount{
MountPath: "/openintegration",
Name: pvc,
},
},
Env: envs,
},
},
Volumes: []v1.Volume{
v1.Volume{
Name: pvc,
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: pvc,
},
},
},
},
},
}
p, _ := json.Marshal(pod)
return string(p)
}
func buildPvcString(namespace string, pvc string) string {
sc := "azurefile"
p, _ := json.Marshal(&v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: pvc,
Namespace: namespace,
},
Spec: v1.PersistentVolumeClaimSpec{
AccessModes: []v1.PersistentVolumeAccessMode{
v1.ReadWriteMany,
},
StorageClassName: &sc,
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
"storage": resource.MustParse("1Gi"),
},
},
},
})
return string(p)
}
func buildKubeRunTask(opt *kubeRunTaskOptions) task.Task {
image := "openintegration/testing"
if opt.image != "" {
image = opt.image
}
return task.Task{
Metadata: task.Metadata{
Name: opt.name,
},
Spec: task.Spec{
Service: "kubernetes",
Endpoint: "run",
Arguments: []task.Argument{
buildAuthTaskArgument(opt.wfcontext),
task.Argument{
Key: "Timeout",
Value: 120,
},
task.Argument{
Key: "Pod",
Value: buildPodString(opt.wfcontext.kube.namespace, opt.name, buildCommand(opt.commands), opt.environ, image, opt.wfcontext.pvc),
},
},
},
}
}
func buildAuthTaskArgument(wfcontext *workflowcontext) task.Argument {
return task.Argument{
Key: "Auth",
Value: &run.Auth{
Type: run.KubernetesServiceAccount,
CRT: &wfcontext.kube.crt,
Host: &wfcontext.kube.host,
Token: &wfcontext.kube.token,
},
}
}
func buildKubeCredentials() (*kubeCredentials, error) {
if os.Getenv("IN_CLUSTER") == "true" {
const (
tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
rootCAFile = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
namespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
)
host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
if len(host) == 0 || len(port) == 0 {
return nil, fmt.Errorf("ErrNotInCluster")
}
token, err := ioutil.ReadFile(tokenFile)
if err != nil {
return nil, err
}
namespace, err := ioutil.ReadFile(namespaceFile)
if err != nil {
return nil, err
}
crt, err := ioutil.ReadFile(rootCAFile)
if err != nil {
return nil, err
}
return &kubeCredentials{
crt: b64.StdEncoding.EncodeToString(crt),
token: string(token),
host: "https://" + net.JoinHostPort(host, port),
namespace: string(namespace),
}, nil
}
return &kubeCredentials{
crt: getEnvOrDie("KUBERNETES_B64_CRT"),
token: getEnvOrDie("KUBERNETES_TOKEN"),
host: getEnvOrDie("KUBERNETES_HOST"),
namespace: getEnvOrDie("KUBERNETES_NAMESPACE"),
}, nil
}
func getEnvOrDie(name string) string {
v := os.Getenv(name)
if v == "" {
dieOnError(fmt.Errorf("Variable \"%s\" is not set, exiting", name))
}
return v
}
func dieOnError(err error) {
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func updateWorkflowContextWithPVC(wfctx *workflowcontext) {
if p := os.Getenv("PVC_NAME"); p != "" {
wfctx.pvc = p
return
}
wfctx.pvc = "core-ci-pvc"
}
func updateWorkflowContextWithLogDirectory(wfctx *workflowcontext) {
if p := os.Getenv("LOG_DIRECTORY"); p != "" {
wfctx.logsdir = p
return
}
wfctx.logsdir = ""
}