Files
onepanel/pkg/cron_workflow.go
2020-04-29 11:11:33 -07:00

655 lines
21 KiB
Go

package v1
import (
"fmt"
sq "github.com/Masterminds/squirrel"
wfv1 "github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
argojson "github.com/argoproj/pkg/json"
"github.com/onepanelio/core/pkg/util"
"github.com/onepanelio/core/pkg/util/label"
"github.com/onepanelio/core/pkg/util/pagination"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"regexp"
"strings"
)
func (c *Client) UpdateCronWorkflow(namespace string, name string, cronWorkflow *CronWorkflow) (*CronWorkflow, error) {
workflow := cronWorkflow.WorkflowExecution
workflowTemplate, err := c.GetWorkflowTemplate(namespace, workflow.WorkflowTemplate.UID, workflow.WorkflowTemplate.Version)
if err != nil {
log.WithFields(log.Fields{
"Namespace": namespace,
"CronWorkflow": cronWorkflow,
"Error": err.Error(),
}).Error("Error with getting workflow template.")
return nil, util.NewUserError(codes.NotFound, "Error with getting workflow template.")
}
// TODO: Need to pull system parameters from k8s config/secret here, example: HOST
opts := &WorkflowExecutionOptions{}
re, _ := regexp.Compile(`[^a-zA-Z0-9-]{1,}`)
opts.GenerateName = strings.ToLower(re.ReplaceAllString(workflowTemplate.Name, `-`)) + "-"
for _, param := range workflow.Parameters {
opts.Parameters = append(opts.Parameters, WorkflowExecutionParameter{
Name: param.Name,
Value: param.Value,
})
}
if opts.Labels == nil {
opts.Labels = &map[string]string{}
}
(*opts.Labels)[workflowTemplateUIDLabelKey] = workflowTemplate.UID
(*opts.Labels)[workflowTemplateVersionLabelKey] = fmt.Sprint(workflowTemplate.Version)
var argoCronWorkflow wfv1.CronWorkflow
argoCronWorkflow.Spec.Schedule = cronWorkflow.Schedule
argoCronWorkflow.Spec.Timezone = cronWorkflow.Timezone
argoCronWorkflow.Spec.Suspend = cronWorkflow.Suspend
argoCronWorkflow.Spec.ConcurrencyPolicy = wfv1.ConcurrencyPolicy(cronWorkflow.ConcurrencyPolicy)
argoCronWorkflow.Spec.StartingDeadlineSeconds = cronWorkflow.StartingDeadlineSeconds
argoCronWorkflow.Spec.SuccessfulJobsHistoryLimit = cronWorkflow.SuccessfulJobsHistoryLimit
argoCronWorkflow.Spec.FailedJobsHistoryLimit = cronWorkflow.FailedJobsHistoryLimit
//UX prevents multiple workflows
manifestBytes, err := workflowTemplate.GetWorkflowManifestBytes()
if err != nil {
return nil, err
}
workflows, err := UnmarshalWorkflows(manifestBytes, true)
if err != nil {
log.WithFields(log.Fields{
"Namespace": namespace,
"CronWorkflow": cronWorkflow,
"Error": err.Error(),
}).Error("Error parsing workflow.")
return nil, err
}
for _, wf := range workflows {
argoCronWorkflow.Spec.WorkflowSpec = wf.Spec
argoCreatedCronWorkflow, err := c.updateCronWorkflow(namespace, name, &workflowTemplate.ID, &wf, &argoCronWorkflow, opts)
if err != nil {
log.WithFields(log.Fields{
"Namespace": namespace,
"CronWorkflow": cronWorkflow,
"Error": err.Error(),
}).Error("Error parsing workflow.")
return nil, err
}
cronWorkflow.Name = argoCreatedCronWorkflow.Name
cronWorkflow.CreatedAt = argoCreatedCronWorkflow.CreationTimestamp.UTC()
cronWorkflow.UID = string(argoCreatedCronWorkflow.ObjectMeta.UID)
cronWorkflow.WorkflowExecution.WorkflowTemplate = workflowTemplate
// Manifests could get big, don't return them in this case.
cronWorkflow.WorkflowExecution.WorkflowTemplate.Manifest = ""
workflowSpec, err := yaml.Marshal(argoCreatedCronWorkflow.Spec.WorkflowSpec)
if err != nil {
return nil, err
}
_, err = sb.Update("cron_workflows").
SetMap(sq.Eq{
"schedule": cronWorkflow.Schedule,
"timezone": cronWorkflow.Timezone,
"suspend": cronWorkflow.Suspend,
"concurrency_policy": cronWorkflow.ConcurrencyPolicy,
"starting_deadline_seconds": cronWorkflow.StartingDeadlineSeconds,
"successful_jobs_history_limit": cronWorkflow.SuccessfulJobsHistoryLimit,
"failed_jobs_history_limit": cronWorkflow.FailedJobsHistoryLimit,
"workflow_spec": workflowSpec,
}).
Suffix("RETURNING id").
RunWith(c.DB.DB).
Exec()
if err != nil {
return nil, err
}
return cronWorkflow, nil
}
return nil, nil
}
func (c *Client) CreateCronWorkflow(namespace string, cronWorkflow *CronWorkflow) (*CronWorkflow, error) {
workflow := cronWorkflow.WorkflowExecution
workflowTemplate, err := c.GetWorkflowTemplate(namespace, workflow.WorkflowTemplate.UID, workflow.WorkflowTemplate.Version)
if err != nil {
log.WithFields(log.Fields{
"Namespace": namespace,
"CronWorkflow": cronWorkflow,
"Error": err.Error(),
}).Error("Error with getting workflow template.")
return nil, util.NewUserError(codes.NotFound, "Error with getting workflow template.")
}
// TODO: Need to pull system parameters from k8s config/secret here, example: HOST
opts := &WorkflowExecutionOptions{}
re, _ := regexp.Compile(`[^a-zA-Z0-9-]{1,}`)
opts.GenerateName = strings.ToLower(re.ReplaceAllString(workflowTemplate.Name, `-`)) + "-"
for _, param := range workflow.Parameters {
opts.Parameters = append(opts.Parameters, WorkflowExecutionParameter{
Name: param.Name,
Value: param.Value,
})
}
if opts.Labels == nil {
opts.Labels = &map[string]string{}
}
(*opts.Labels)[workflowTemplateUIDLabelKey] = workflowTemplate.UID
(*opts.Labels)[workflowTemplateVersionLabelKey] = fmt.Sprint(workflowTemplate.Version)
label.MergeLabelsPrefix(*opts.Labels, workflow.Labels, label.TagPrefix)
var argoCronWorkflow wfv1.CronWorkflow
argoCronWorkflow.Spec.Schedule = cronWorkflow.Schedule
argoCronWorkflow.Spec.Timezone = cronWorkflow.Timezone
argoCronWorkflow.Spec.Suspend = cronWorkflow.Suspend
argoCronWorkflow.Spec.ConcurrencyPolicy = wfv1.ConcurrencyPolicy(cronWorkflow.ConcurrencyPolicy)
argoCronWorkflow.Spec.StartingDeadlineSeconds = cronWorkflow.StartingDeadlineSeconds
argoCronWorkflow.Spec.SuccessfulJobsHistoryLimit = cronWorkflow.SuccessfulJobsHistoryLimit
argoCronWorkflow.Spec.FailedJobsHistoryLimit = cronWorkflow.FailedJobsHistoryLimit
//UX prevents multiple workflows
manifestBytes, err := workflowTemplate.GetWorkflowManifestBytes()
if err != nil {
return nil, err
}
workflows, err := UnmarshalWorkflows(manifestBytes, true)
if err != nil {
log.WithFields(log.Fields{
"Namespace": namespace,
"CronWorkflow": cronWorkflow,
"Error": err.Error(),
}).Error("Error parsing workflow.")
return nil, err
}
for _, wf := range workflows {
argoCronWorkflow.Spec.WorkflowSpec = wf.Spec
argoCreatedCronWorkflow, err := c.createCronWorkflow(namespace, &workflowTemplate.ID, &wf, &argoCronWorkflow, opts)
if err != nil {
log.WithFields(log.Fields{
"Namespace": namespace,
"CronWorkflow": cronWorkflow,
"Error": err.Error(),
}).Error("Error parsing workflow.")
return nil, err
}
cronWorkflow.Name = argoCreatedCronWorkflow.Name
cronWorkflow.CreatedAt = argoCreatedCronWorkflow.CreationTimestamp.UTC()
cronWorkflow.UID = string(argoCreatedCronWorkflow.ObjectMeta.UID)
cronWorkflow.WorkflowExecution.WorkflowTemplate = workflowTemplate
// Manifests could get big, don't return them in this case.
cronWorkflow.WorkflowExecution.WorkflowTemplate.Manifest = ""
workflowSpec, err := yaml.Marshal(argoCreatedCronWorkflow.Spec.WorkflowSpec)
if err != nil {
return nil, err
}
_, err = sb.Insert("cron_workflows").
SetMap(sq.Eq{
"uid": cronWorkflow.UID,
"name": cronWorkflow.Name,
"workflow_template_version_id": workflowTemplate.WorkflowTemplateVersionId,
"schedule": cronWorkflow.Schedule,
"timezone": cronWorkflow.Timezone,
"suspend": cronWorkflow.Suspend,
"concurrency_policy": cronWorkflow.ConcurrencyPolicy,
"starting_deadline_seconds": cronWorkflow.StartingDeadlineSeconds,
"successful_jobs_history_limit": cronWorkflow.SuccessfulJobsHistoryLimit,
"failed_jobs_history_limit": cronWorkflow.FailedJobsHistoryLimit,
"workflow_spec": workflowSpec,
}).
Suffix("RETURNING id").
RunWith(c.DB.DB).
Exec()
if err != nil {
return nil, err
}
return cronWorkflow, nil
}
return nil, nil
}
func (c *Client) GetCronWorkflow(namespace, name string) (cronWorkflow *CronWorkflow, err error) {
cwf, err := c.ArgoprojV1alpha1().CronWorkflows(namespace).Get(name, metav1.GetOptions{})
if err != nil {
log.WithFields(log.Fields{
"Namespace": namespace,
"Name": name,
"Error": err.Error(),
}).Error("CronWorkflow not found.")
return nil, util.NewUserError(codes.NotFound, "CronWorkflow not found.")
}
cronWorkflow = &CronWorkflow{
CreatedAt: cwf.CreationTimestamp.UTC(),
UID: string(cwf.UID),
Name: cwf.Name,
Schedule: cwf.Spec.Schedule,
Timezone: cwf.Spec.Timezone,
Suspend: cwf.Spec.Suspend,
ConcurrencyPolicy: string(cwf.Spec.ConcurrencyPolicy),
StartingDeadlineSeconds: cwf.Spec.StartingDeadlineSeconds,
SuccessfulJobsHistoryLimit: cwf.Spec.SuccessfulJobsHistoryLimit,
FailedJobsHistoryLimit: cwf.Spec.FailedJobsHistoryLimit,
WorkflowExecution: nil,
}
return
}
// prefix is the label prefix.
// e.g. prefix/my-label-key: my-label-value
func (c *Client) GetCronWorkflowLabels(namespace, name, prefix string) (labels map[string]string, err error) {
cwf, err := c.ArgoprojV1alpha1().CronWorkflows(namespace).Get(name, metav1.GetOptions{})
if err != nil {
log.WithFields(log.Fields{
"Namespace": namespace,
"Name": name,
"Error": err.Error(),
}).Error("CronWorkflow not found.")
return nil, util.NewUserError(codes.NotFound, "CronWorkflow not found.")
}
labels = label.FilterByPrefix(prefix, cwf.Labels)
labels = label.RemovePrefix(prefix, labels)
return
}
// prefix is the label prefix.
// we delete all labels with that prefix and set the new ones
// e.g. prefix/my-label-key: my-label-value
func (c *Client) SetCronWorkflowLabels(namespace, name, prefix string, keyValues map[string]string, deleteOld bool) (workflowLabels map[string]string, err error) {
cwf, err := c.ArgoprojV1alpha1().CronWorkflows(namespace).Get(name, metav1.GetOptions{})
if err != nil {
log.WithFields(log.Fields{
"Namespace": namespace,
"Name": name,
"Error": err.Error(),
}).Error("CronWorkflow not found.")
return nil, util.NewUserError(codes.NotFound, "CronWorkflow not found.")
}
if deleteOld {
label.DeleteWithPrefix(cwf.Labels, prefix)
}
label.MergeLabelsPrefix(cwf.Labels, keyValues, prefix)
cwf, err = c.ArgoprojV1alpha1().CronWorkflows(namespace).Update(cwf)
if err != nil {
return nil, err
}
filteredMap := label.FilterByPrefix(prefix, cwf.Labels)
filteredMap = label.RemovePrefix(prefix, filteredMap)
return filteredMap, nil
}
func (c *Client) DeleteCronWorkflowLabel(namespace, name string, keysToDelete ...string) (labels map[string]string, err error) {
wf, err := c.ArgoprojV1alpha1().CronWorkflows(namespace).Get(name, metav1.GetOptions{})
if err != nil {
log.WithFields(log.Fields{
"Namespace": namespace,
"Name": name,
"Error": err.Error(),
}).Error("CronWorkflow not found.")
return nil, util.NewUserError(codes.NotFound, "CronWorkflow not found.")
}
label.Delete(wf.Labels, keysToDelete...)
return wf.Labels, nil
}
func (c *Client) ListCronWorkflows(namespace, workflowTemplateUID string, pagination *pagination.PaginationRequest) (cronWorkflows []*CronWorkflow, err error) {
sb := c.cronWorkflowSelectBuilder(namespace, workflowTemplateUID).
OrderBy("cw.created_at DESC")
sb = *pagination.ApplyToSelect(&sb)
query, args, err := sb.ToSql()
if err != nil {
return nil, err
}
if err := c.DB.Select(&cronWorkflows, query, args...); err != nil {
return nil, err
}
for _, cwf := range cronWorkflows {
parameters, err := cwf.GetParametersFromWorkflowSpec()
if err != nil {
continue
}
cwf.WorkflowExecution = &WorkflowExecution{
Parameters: parameters,
}
}
return
}
func (c *Client) CountCronWorkflows(namespace, workflowTemplateUID string) (count int, err error) {
err = c.cronWorkflowSelectBuilderNoColumns(namespace, workflowTemplateUID).
Columns("COUNT(*)").
RunWith(c.DB.DB).
QueryRow().
Scan(&count)
return
}
func (c *Client) updateCronWorkflow(namespace string, name string, workflowTemplateId *uint64, wf *wfv1.Workflow, cwf *wfv1.CronWorkflow, opts *WorkflowExecutionOptions) (updatedCronWorkflow *wfv1.CronWorkflow, err error) {
//Make sure the CronWorkflow exists before we edit it
toUpdateCWF, err := c.ArgoprojV1alpha1().CronWorkflows(namespace).Get(name, metav1.GetOptions{})
if err != nil {
log.WithFields(log.Fields{
"Namespace": namespace,
"Name": name,
"Error": err.Error(),
}).Error("CronWorkflow not found.")
return nil, util.NewUserError(codes.NotFound, "CronWorkflow not found.")
}
if opts == nil {
opts = &WorkflowExecutionOptions{}
}
if opts.Name != "" {
cwf.ObjectMeta.Name = opts.Name
}
if opts.GenerateName != "" {
cwf.ObjectMeta.GenerateName = opts.GenerateName
}
if opts.Entrypoint != "" {
cwf.Spec.WorkflowSpec.Entrypoint = opts.Entrypoint
}
if opts.ServiceAccount != "" {
cwf.Spec.WorkflowSpec.ServiceAccountName = opts.ServiceAccount
}
if len(opts.Parameters) > 0 {
newParams := make([]wfv1.Parameter, 0)
passedParams := make(map[string]bool)
for _, param := range opts.Parameters {
newParams = append(newParams, wfv1.Parameter{
Name: param.Name,
Value: param.Value,
})
passedParams[param.Name] = true
}
for _, param := range cwf.Spec.WorkflowSpec.Arguments.Parameters {
if _, ok := passedParams[param.Name]; ok {
// this parameter was overridden via command line
continue
}
newParams = append(newParams, param)
}
cwf.Spec.WorkflowSpec.Arguments.Parameters = newParams
}
if opts.Labels != nil {
cwf.ObjectMeta.Labels = *opts.Labels
}
err = injectExitHandlerWorkflowExecutionStatistic(wf, namespace, workflowTemplateId)
if err != nil {
return nil, err
}
err = injectInitHandlerWorkflowExecutionStatistic(wf, namespace, workflowTemplateId)
if err != nil {
return nil, err
}
if err = c.injectAutomatedFields(namespace, wf, opts); err != nil {
return nil, err
}
cwf.Spec.WorkflowSpec = wf.Spec
cwf.Spec.WorkflowMetadata = &wf.ObjectMeta
//merge the labels
mergedLabels := wf.ObjectMeta.Labels
if mergedLabels == nil {
mergedLabels = make(map[string]string)
}
for k, v := range *opts.Labels {
mergedLabels[k] = v
}
cwf.Spec.WorkflowMetadata.Labels = mergedLabels
cwf.Name = name
cwf.ResourceVersion = toUpdateCWF.ResourceVersion
updatedCronWorkflow, err = c.ArgoprojV1alpha1().CronWorkflows(namespace).Update(cwf)
if err != nil {
return nil, err
}
return
}
func (c *Client) createCronWorkflow(namespace string, workflowTemplateId *uint64, wf *wfv1.Workflow, cwf *wfv1.CronWorkflow, opts *WorkflowExecutionOptions) (createdCronWorkflow *wfv1.CronWorkflow, err error) {
if opts == nil {
opts = &WorkflowExecutionOptions{}
}
if opts.Name != "" {
cwf.ObjectMeta.Name = opts.Name
}
if opts.GenerateName != "" {
cwf.ObjectMeta.GenerateName = opts.GenerateName
}
if opts.Entrypoint != "" {
cwf.Spec.WorkflowSpec.Entrypoint = opts.Entrypoint
}
if opts.ServiceAccount != "" {
cwf.Spec.WorkflowSpec.ServiceAccountName = opts.ServiceAccount
}
if len(opts.Parameters) > 0 {
newParams := make([]wfv1.Parameter, 0)
passedParams := make(map[string]bool)
for _, param := range opts.Parameters {
newParams = append(newParams, wfv1.Parameter{
Name: param.Name,
Value: param.Value,
})
passedParams[param.Name] = true
}
for _, param := range cwf.Spec.WorkflowSpec.Arguments.Parameters {
if _, ok := passedParams[param.Name]; ok {
// this parameter was overridden via command line
continue
}
newParams = append(newParams, param)
}
cwf.Spec.WorkflowSpec.Arguments.Parameters = newParams
wf.Spec.Arguments.Parameters = newParams
}
if opts.Labels != nil {
cwf.ObjectMeta.Labels = *opts.Labels
}
err = injectExitHandlerWorkflowExecutionStatistic(wf, namespace, workflowTemplateId)
if err != nil {
return nil, err
}
err = injectInitHandlerWorkflowExecutionStatistic(wf, namespace, workflowTemplateId)
if err != nil {
return nil, err
}
if err = c.injectAutomatedFields(namespace, wf, opts); err != nil {
return nil, err
}
cwf.Spec.WorkflowSpec = wf.Spec
cwf.Spec.WorkflowMetadata = &wf.ObjectMeta
//merge the labels
mergedLabels := wf.ObjectMeta.Labels
if mergedLabels == nil {
mergedLabels = make(map[string]string)
}
for k, v := range *opts.Labels {
mergedLabels[k] = v
}
cwf.Spec.WorkflowMetadata.Labels = mergedLabels
createdCronWorkflow, err = c.ArgoprojV1alpha1().CronWorkflows(namespace).Create(cwf)
if err != nil {
return nil, err
}
return
}
func (c *Client) TerminateCronWorkflow(namespace, name string) (err error) {
query, args, err := sb.Select().
Columns("cw.id", "cw.created_at", "cw.uid", "cw.name", "cw.workflow_template_version_id").
Columns("cw.schedule", "cw.timezone", "cw.suspend", "cw.concurrency_policy", "cw.starting_deadline_seconds").
Columns("cw.successful_jobs_history_limit", "cw.failed_jobs_history_limit", "cw.workflow_spec", "wtv.version").
From("cron_workflows cw").
Join("workflow_template_versions wtv ON wtv.id = cw.workflow_template_version_id").
Join("workflow_templates wt ON wt.id = wtv.workflow_template_id").
Where(sq.Eq{
"wt.namespace": namespace,
"cw.name": name,
}).
ToSql()
cronWorkflow := &CronWorkflow{}
if err := c.DB.Get(cronWorkflow, query, args...); err != nil {
return err
}
query = `DELETE FROM workflow_executions
WHERE cron_workflow_id = $1`
if _, err := c.DB.Exec(query, cronWorkflow.ID); err != nil {
return err
}
query = `DELETE FROM cron_workflows
USING workflow_template_versions, workflow_templates
WHERE cron_workflows.workflow_template_version_id = workflow_template_versions.id
AND workflow_template_versions.workflow_template_id = workflow_templates.id
AND workflow_templates.namespace = $1
AND cron_workflows.name = $2`
if _, err := c.DB.Exec(query, namespace, name); err != nil {
return err
}
err = c.ArgoprojV1alpha1().CronWorkflows(namespace).Delete(name, nil)
if err != nil && strings.Contains(err.Error(), "not found") {
err = nil
}
return
}
func unmarshalCronWorkflows(cwfBytes []byte, strict bool) (cwfs wfv1.CronWorkflow, err error) {
var cwf wfv1.CronWorkflow
var jsonOpts []argojson.JSONOpt
if strict {
jsonOpts = append(jsonOpts, argojson.DisallowUnknownFields)
}
err = argojson.Unmarshal(cwfBytes, &cwf, jsonOpts...)
if err == nil {
return cwf, nil
}
return
}
func (c *Client) cronWorkflowSelectBuilder(namespace string, workflowTemplateUid string) sq.SelectBuilder {
sb := c.cronWorkflowSelectBuilderNoColumns(namespace, workflowTemplateUid).
Columns("cw.id", "cw.created_at", "cw.uid", "cw.name", "cw.workflow_template_version_id").
Columns("cw.schedule", "cw.timezone", "cw.suspend", "cw.concurrency_policy", "cw.starting_deadline_seconds").
Columns("cw.successful_jobs_history_limit", "cw.failed_jobs_history_limit", "cw.workflow_spec", "wtv.version")
return sb
}
func (c *Client) cronWorkflowSelectBuilderNoColumns(namespace string, workflowTemplateUid string) sq.SelectBuilder {
sb := sb.Select().
From("cron_workflows cw").
Join("workflow_template_versions wtv ON wtv.id = cw.workflow_template_version_id").
Join("workflow_templates wt ON wt.id = wtv.workflow_template_id").
Where(sq.Eq{
"wt.namespace": namespace,
"wt.uid": workflowTemplateUid,
})
return sb
}
func (c *Client) GetCronWorkflowStatisticsForTemplates(workflowTemplates ...*WorkflowTemplate) (err error) {
if len(workflowTemplates) == 0 {
return nil
}
tx, err := c.DB.Begin()
if err != nil {
return err
}
whereIn := "wtv.workflow_template_id IN (?"
for i := range workflowTemplates {
if i == 0 {
continue
}
whereIn += ",?"
}
whereIn += ")"
ids := make([]interface{}, len(workflowTemplates))
for i, workflowTemplate := range workflowTemplates {
ids[i] = workflowTemplate.ID
}
defer tx.Rollback()
statsSelect := `
workflow_template_id,
COUNT(*) total`
query, args, err := sb.Select(statsSelect).
From("cron_workflows cw").
Join("workflow_template_versions wtv ON wtv.id = cw.workflow_template_version_id").
Where(whereIn, ids...).
GroupBy("wtv.workflow_template_id").
ToSql()
if err != nil {
return err
}
result := make([]*CronWorkflowStatisticReport, 0)
err = c.DB.Select(&result, query, args...)
if err != nil {
return err
}
resultMapping := make(map[uint64]*CronWorkflowStatisticReport)
for i := range result {
report := result[i]
resultMapping[report.WorkflowTemplateId] = report
}
for _, workflowTemplate := range workflowTemplates {
resultMap, ok := resultMapping[workflowTemplate.ID]
if ok {
workflowTemplate.CronWorkflowsStatisticsReport = resultMap
}
}
return
}