generated from argoproj-labs/argo-workflows-hello-executor-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
plugin.go
68 lines (57 loc) · 2.04 KB
/
plugin.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
package plugin
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"k8s.io/client-go/kubernetes"
// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
executor "github.com/argoproj/argo-workflows/v3/pkg/plugins/executor"
)
var (
ErrWrongContentType = errors.New("Content-Type header is not set to 'appliaction/json'")
ErrReadingBody = errors.New("Couldn't read request body")
ErrMarshallingBody = errors.New("Couldn't unmrashal request body")
ErrExecutingPlugin = errors.New("Error occured while executing plugin")
)
// The plugin's logic
type PluginExecutor interface {
// Executes commands based on the args provided from the workflow
Execute(args executor.ExecuteTemplateArgs) (executor.ExecuteTemplateResponse, error)
}
func ArgocdPlugin(plugin PluginExecutor, kubeClient kubernetes.Interface, namespace string) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
if header := req.Header.Get("Content-Type"); header == "" || header != "application/json" {
log.Print(ErrWrongContentType)
http.Error(w, ErrWrongContentType.Error(), http.StatusBadRequest)
return
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("%v: %v", ErrReadingBody.Error(), err)
http.Error(w, ErrReadingBody.Error(), http.StatusBadRequest)
return
}
args := executor.ExecuteTemplateArgs{}
if err := json.Unmarshal(body, &args); err != nil || args.Workflow == nil || args.Template == nil {
log.Printf("%v: %v", ErrMarshallingBody.Error(), err)
http.Error(w, ErrMarshallingBody.Error(), http.StatusBadRequest)
return
}
resp, err := plugin.Execute(args)
if err != nil {
log.Printf("%v: %v", ErrExecutingPlugin.Error(), err)
http.Error(w, ErrExecutingPlugin.Error(), http.StatusInternalServerError)
return
}
jsonResp, err := json.Marshal(resp)
if err != nil {
log.Printf("Error marshalling result: %v", err)
http.Error(w, "something went wrong", http.StatusBadRequest)
}
w.WriteHeader(http.StatusOK)
w.Write(jsonResp)
return
}
}