...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package curl
17
18 import (
19 "fmt"
20 "net/http"
21
22 corev1 "k8s.io/api/core/v1"
23
24 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
25 )
26
27 const image = "curlimages/curl:7.78.0"
28
29 const nameSuffix = "-rendered-http-request"
30
31 func renderCommands(request CommandFlags) (Commands, error) {
32
33 result := []string{"curl", "-i", "-s"}
34
35
36 if request.FollowLocation {
37 result = append(result, "-L")
38 }
39
40 if request.Method != http.MethodGet {
41 result = append(result, "-X", request.Method)
42
43 if len(request.Body) > 0 {
44 result = append(result, "-d", request.Body)
45 }
46 }
47
48 renderedHeaders := http.Header{}
49 for k, v := range request.Header {
50 renderedHeaders[k] = v
51 }
52 if request.JsonContent {
53 if request.Header == nil {
54 request.Header = Header{}
55 }
56 renderedHeaders[HeaderContentType] = []string{ApplicationJson}
57 }
58
59 for key, values := range renderedHeaders {
60 for _, value := range values {
61 result = append(result, "-H", fmt.Sprintf("%s: %s", key, value))
62 }
63 }
64
65 result = append(result, request.URL)
66
67 return result, nil
68 }
69
70 func RenderWorkflowTaskTemplate(request RequestForm) (*v1alpha1.Template, error) {
71 commands, err := renderCommands(request.CommandFlags)
72 if err != nil {
73 return nil, err
74 }
75 containerName := fmt.Sprintf("%s%s", request.Name, nameSuffix)
76 return &v1alpha1.Template{
77 Name: request.Name,
78 Type: v1alpha1.TypeTask,
79 Task: &v1alpha1.Task{
80 Container: &corev1.Container{
81 Name: containerName,
82 Image: image,
83 Command: commands,
84 },
85 },
86 ConditionalBranches: nil,
87 }, nil
88 }
89