refactor to make curl node reusable

This commit is contained in:
rushtehrani
2020-05-05 11:28:56 -07:00
parent c5b34248b3
commit 58d6029d45
2 changed files with 18 additions and 15 deletions

View File

@@ -1277,7 +1277,7 @@ func (c *Client) GetWorkflowExecutionStatisticsForTemplates(workflowTemplates ..
Will build a template that makes a CURL request to the onepanel-core API, Will build a template that makes a CURL request to the onepanel-core API,
with statistics about the workflow that was just executed. with statistics about the workflow that was just executed.
*/ */
func getWorkflowStatisticsCallerTemplate(namespace, path, templateName string, statistics map[string]interface{}) (template *wfv1.Template, err error) { func getCURLNodeTemplate(name, curlPath, curlBody string) (template *wfv1.Template, err error) {
host := env.GetEnv("ONEPANEL_CORE_SERVICE_HOST", "onepanel-core.onepanel.svc.cluster.local") host := env.GetEnv("ONEPANEL_CORE_SERVICE_HOST", "onepanel-core.onepanel.svc.cluster.local")
if host == "" { if host == "" {
err = errors.New("ONEPANEL_CORE_SERVICE_HOST is empty.") err = errors.New("ONEPANEL_CORE_SERVICE_HOST is empty.")
@@ -1288,23 +1288,16 @@ func getWorkflowStatisticsCallerTemplate(namespace, path, templateName string, s
err = errors.New("ONEPANEL_CORE_SERVICE_PORT is empty.") err = errors.New("ONEPANEL_CORE_SERVICE_PORT is empty.")
return return
} }
endpoint := fmt.Sprintf("http://%s:%s/apis/v1beta1/%s/workflow_executions/{{workflow.name}}/%v", host, port, namespace, path) endpoint := fmt.Sprintf("http://%s:%s/%s", host, port, curlPath)
statisticsBytes, err := json.Marshal(statistics)
if err != nil {
return nil, err
}
body := fmt.Sprintf("--data '%s'", string(statisticsBytes))
template = &wfv1.Template{ template = &wfv1.Template{
Name: templateName, Name: name,
Container: &corev1.Container{ Container: &corev1.Container{
Image: "curlimages/curl", Image: "curlimages/curl",
Command: []string{"sh", "-c"}, Command: []string{"sh", "-c"},
Args: []string{ Args: []string{
"SERVICE_ACCOUNT_TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) && curl -s -o /dev/null -w '%{http_code}' '" + endpoint + "' -H \"Content-Type: application/json\" -H 'Connection: keep-alive' -H 'Accept: application/json' " + "SERVICE_ACCOUNT_TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) && curl -s -o /dev/null -w '%{http_code}' '" + endpoint + "' -H \"Content-Type: application/json\" -H 'Connection: keep-alive' -H 'Accept: application/json' " +
"-H 'Authorization: Bearer '\"$SERVICE_ACCOUNT_TOKEN\"'' " + "-H 'Authorization: Bearer '\"$SERVICE_ACCOUNT_TOKEN\"'' " +
body + " --compressed", "--data '" + curlBody + "' --compressed",
}, },
}, },
} }
@@ -1312,11 +1305,16 @@ func getWorkflowStatisticsCallerTemplate(namespace, path, templateName string, s
} }
func injectExitHandlerWorkflowExecutionStatistic(wf *wfv1.Workflow, namespace string, workflowTemplateId *uint64) error { func injectExitHandlerWorkflowExecutionStatistic(wf *wfv1.Workflow, namespace string, workflowTemplateId *uint64) error {
curlPath := fmt.Sprintf("apis/v1beta1/%s/workflow_executions/{{workflow.name}}/statistics", namespace)
statistics := map[string]interface{}{ statistics := map[string]interface{}{
"workflowStatus": "{{workflow.status}}", "workflowStatus": "{{workflow.status}}",
"workflowTemplateId": int64(*workflowTemplateId), "workflowTemplateId": int64(*workflowTemplateId),
} }
statsTemplate, err := getWorkflowStatisticsCallerTemplate(namespace, "statistics", "sys-send-exit-stats", statistics) statisticsBytes, err := json.Marshal(statistics)
if err != nil {
return err
}
statsTemplate, err := getCURLNodeTemplate("sys-send-exit-stats", curlPath, string(statisticsBytes))
if err != nil { if err != nil {
return err return err
} }
@@ -1354,10 +1352,15 @@ func injectExitHandlerWorkflowExecutionStatistic(wf *wfv1.Workflow, namespace st
} }
func injectInitHandlerWorkflowExecutionStatistic(wf *wfv1.Workflow, namespace string, workflowTemplateId *uint64) error { func injectInitHandlerWorkflowExecutionStatistic(wf *wfv1.Workflow, namespace string, workflowTemplateId *uint64) error {
curlPath := fmt.Sprintf("apis/v1beta1/%s/workflow_executions/{{workflow.name}}/cron_start_statistics", namespace)
statistics := map[string]interface{}{ statistics := map[string]interface{}{
"workflowTemplateId": int64(*workflowTemplateId), "workflowTemplateId": int64(*workflowTemplateId),
} }
containerTemplate, err := getWorkflowStatisticsCallerTemplate(namespace, "cron_start_statistics", "sys-send-init-stats", statistics) statisticsBytes, err := json.Marshal(statistics)
if err != nil {
return err
}
containerTemplate, err := getCURLNodeTemplate("sys-send-init-stats", curlPath, string(statisticsBytes))
if err != nil { if err != nil {
return err return err
} }

View File

@@ -55,13 +55,13 @@ func generateArguments(spec *WorkspaceSpec, config map[string]string) (err error
// Workspace action // Workspace action
spec.Arguments.Parameters = append(spec.Arguments.Parameters, Parameter{ spec.Arguments.Parameters = append(spec.Arguments.Parameters, Parameter{
Name: "sys-workspace-action", Name: "sys-workspace-action",
Value: ptr.String(config["ONEPANEL_DOMAIN"]), Value: ptr.String("create"),
Type: "input.hidden", Type: "input.hidden",
}) })
// Host // Host
spec.Arguments.Parameters = append(spec.Arguments.Parameters, Parameter{ spec.Arguments.Parameters = append(spec.Arguments.Parameters, Parameter{
Name: "sys-host", Name: "sys-host",
Value: ptr.String("create"), Value: ptr.String(config["ONEPANEL_DOMAIN"]),
Type: "input.hidden", Type: "input.hidden",
}) })