mirror of
https://github.com/onepanelio/onepanel.git
synced 2025-11-02 09:42:32 +08:00
fix stats request
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
sq "github.com/Masterminds/squirrel"
|
sq "github.com/Masterminds/squirrel"
|
||||||
"github.com/ghodss/yaml"
|
"github.com/ghodss/yaml"
|
||||||
"github.com/onepanelio/core/api"
|
|
||||||
"github.com/onepanelio/core/pkg/util/label"
|
"github.com/onepanelio/core/pkg/util/label"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -1232,7 +1231,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 getWorkflowStatisticsExitHandlerTemplate(namespace string, workflowTemplateId *uint64) (statsTemplate *wfv1.Template, err error) {
|
func getWorkflowStatisticsCallerTemplate(namespace, path, templateName string, statistics map[string]interface{}) (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.")
|
||||||
@@ -1243,76 +1242,35 @@ func getWorkflowStatisticsExitHandlerTemplate(namespace string, workflowTemplate
|
|||||||
err = errors.New("ONEPANEL_CORE_SERVICE_PORT is empty.")
|
err = errors.New("ONEPANEL_CORE_SERVICE_PORT is empty.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
curlEndpoint := fmt.Sprintf("http://%s:%s/apis/v1beta1/%s/workflow_executions/{{workflow.name}}/statistics", host, port, namespace)
|
endpoint := fmt.Sprintf("http://%s:%s/apis/v1beta1/%s/workflow_executions/{{workflow.name}}/%v", host, port, namespace, path)
|
||||||
|
|
||||||
jsonRequestStruct := api.Statistics{
|
statisticsBytes, err := json.Marshal(statistics)
|
||||||
WorkflowStatus: "{{workflow.status}}",
|
|
||||||
WorkflowTemplateId: int64(*workflowTemplateId),
|
|
||||||
}
|
|
||||||
jsonRequestBytes, err := json.Marshal(jsonRequestStruct)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
jsonRequestStr := string(jsonRequestBytes)
|
body := fmt.Sprintf("--data '%s'", string(statisticsBytes))
|
||||||
curlJSONBody := fmt.Sprintf("--data '%s'", jsonRequestStr)
|
|
||||||
|
|
||||||
statsTemplate = &wfv1.Template{
|
template = &wfv1.Template{
|
||||||
Name: "sys-send-exit-stats",
|
Name: templateName,
|
||||||
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}' '" + curlEndpoint + "' -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\"'' " +
|
||||||
curlJSONBody + " --compressed",
|
body + " --compressed",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCronWorkflowInitContainerTemplate(namespace string, workflowTemplateId *uint64) (containerTemplate *wfv1.Template, err error) {
|
|
||||||
host := env.GetEnv("ONEPANEL_CORE_SERVICE_HOST", "onepanel-core.onepanel.svc.cluster.local")
|
|
||||||
if host == "" {
|
|
||||||
err = errors.New("ONEPANEL_CORE_SERVICE_HOST is empty.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
port := env.GetEnv("ONEPANEL_CORE_SERVICE_PORT", "80")
|
|
||||||
if port == "" {
|
|
||||||
err = errors.New("ONEPANEL_CORE_SERVICE_PORT is empty.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
curlEndpoint := fmt.Sprintf("http://%s:%s/apis/v1beta1/%s/workflow_executions/{{workflow.name}}/cron_start_statistics/%d", host, port, namespace, workflowTemplateId)
|
|
||||||
|
|
||||||
jsonRequestStruct := api.Statistics{
|
|
||||||
WorkflowStatus: "{{workflow.status}}",
|
|
||||||
WorkflowTemplateId: int64(*workflowTemplateId),
|
|
||||||
}
|
|
||||||
jsonRequestBytes, err := json.Marshal(jsonRequestStruct)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
jsonRequestStr := string(jsonRequestBytes)
|
|
||||||
curlJSONBody := fmt.Sprintf("--data '%s'", jsonRequestStr)
|
|
||||||
|
|
||||||
containerTemplate = &wfv1.Template{
|
|
||||||
Name: "sys-send-init-stats",
|
|
||||||
Container: &corev1.Container{
|
|
||||||
Name: "cron-init-container",
|
|
||||||
Image: "curlimages/curl",
|
|
||||||
Command: []string{"sh", "-c"},
|
|
||||||
Args: []string{
|
|
||||||
"SERVICE_ACCOUNT_TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) && curl -s -o /dev/null -w '%{http_code}' '" + curlEndpoint + "' -H \"Content-Type: application/json\" -H 'Connection: keep-alive' -H 'Accept: application/json' " +
|
|
||||||
"-H 'Authorization: Bearer '\"$SERVICE_ACCOUNT_TOKEN\"'' " +
|
|
||||||
curlJSONBody + " --compressed",
|
|
||||||
},
|
|
||||||
}}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func injectExitHandlerWorkflowExecutionStatistic(wf *wfv1.Workflow, namespace string, workflowTemplateId *uint64) error {
|
func injectExitHandlerWorkflowExecutionStatistic(wf *wfv1.Workflow, namespace string, workflowTemplateId *uint64) error {
|
||||||
statsTemplate, err := getWorkflowStatisticsExitHandlerTemplate(namespace, workflowTemplateId)
|
statistics := map[string]interface{}{
|
||||||
|
"workflowStatus": "{{workflow.status}}",
|
||||||
|
"workflowTemplateId": int64(*workflowTemplateId),
|
||||||
|
}
|
||||||
|
statsTemplate, err := getWorkflowStatisticsCallerTemplate(namespace, "statistics", "sys-send-exit-stats", statistics)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -1350,7 +1308,10 @@ 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 {
|
||||||
containerTemplate, err := getCronWorkflowInitContainerTemplate(namespace, workflowTemplateId)
|
statistics := map[string]interface{}{
|
||||||
|
"workflowTemplateId": int64(*workflowTemplateId),
|
||||||
|
}
|
||||||
|
containerTemplate, err := getWorkflowStatisticsCallerTemplate(namespace, "cron_start_statistics", "sys-send-init-stats", statistics)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user