mirror of
https://github.com/onepanelio/onepanel.git
synced 2025-10-17 19:20:36 +08:00
clean/fix: fixed issues with creating workflow template versions and labels.
Removed most of the logic from updating a workflow template version as it is currently not used.
This commit is contained in:
@@ -60,7 +60,7 @@ type WorkflowTemplate struct {
|
|||||||
Name string
|
Name string
|
||||||
Manifest string
|
Manifest string
|
||||||
Version int32
|
Version int32
|
||||||
IsLatest bool `db:"is_latest"`
|
IsLatest bool
|
||||||
IsArchived bool `db:"is_archived"`
|
IsArchived bool `db:"is_archived"`
|
||||||
LatestArgo *wfv1.WorkflowTemplate
|
LatestArgo *wfv1.WorkflowTemplate
|
||||||
Labels map[string]string
|
Labels map[string]string
|
||||||
|
@@ -955,25 +955,6 @@ func (c *Client) GetWorkflowExecutionLabels(namespace, name, prefix string) (lab
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// prefix is the label prefix.
|
|
||||||
// e.g. prefix/my-label-key: my-label-value
|
|
||||||
func (c *Client) GetWorkflowTemplateLabels(namespace, name, prefix string) (labels map[string]string, err error) {
|
|
||||||
wf, err := c.getArgoWorkflowTemplate(namespace, name, "latest")
|
|
||||||
if err != nil {
|
|
||||||
log.WithFields(log.Fields{
|
|
||||||
"Namespace": namespace,
|
|
||||||
"Name": name,
|
|
||||||
"Error": err.Error(),
|
|
||||||
}).Error("Workflow Template not found.")
|
|
||||||
return nil, util.NewUserError(codes.NotFound, "Workflow Template not found.")
|
|
||||||
}
|
|
||||||
|
|
||||||
labels = label.FilterByPrefix(prefix, wf.Labels)
|
|
||||||
labels = label.RemovePrefix(prefix, labels)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) DeleteWorkflowExecutionLabel(namespace, name string, keysToDelete ...string) (labels map[string]string, err error) {
|
func (c *Client) DeleteWorkflowExecutionLabel(namespace, name string, keysToDelete ...string) (labels map[string]string, err error) {
|
||||||
wf, err := c.ArgoprojV1alpha1().Workflows(namespace).Get(name, metav1.GetOptions{})
|
wf, err := c.ArgoprojV1alpha1().Workflows(namespace).Get(name, metav1.GetOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@@ -4,39 +4,22 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
sq "github.com/Masterminds/squirrel"
|
||||||
"github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
|
"github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
|
||||||
argojson "github.com/argoproj/pkg/json"
|
argojson "github.com/argoproj/pkg/json"
|
||||||
"github.com/ghodss/yaml"
|
"github.com/ghodss/yaml"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/onepanelio/core/pkg/util"
|
||||||
"github.com/onepanelio/core/pkg/util/label"
|
"github.com/onepanelio/core/pkg/util/label"
|
||||||
"github.com/onepanelio/core/pkg/util/number"
|
"github.com/onepanelio/core/pkg/util/number"
|
||||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
sq "github.com/Masterminds/squirrel"
|
|
||||||
"github.com/onepanelio/core/pkg/util"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var sb = sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
|
var sb = sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
|
||||||
|
|
||||||
// Deprecated
|
|
||||||
func (c *Client) insertWorkflowTemplateVersion(workflowTemplate *WorkflowTemplate, runner sq.BaseRunner) (err error) {
|
|
||||||
err = sb.Insert("workflow_template_versions").
|
|
||||||
SetMap(sq.Eq{
|
|
||||||
"workflow_template_id": workflowTemplate.ID,
|
|
||||||
"version": int32(time.Now().Unix()),
|
|
||||||
"is_latest": workflowTemplate.IsLatest,
|
|
||||||
}).
|
|
||||||
Suffix("RETURNING version").
|
|
||||||
RunWith(runner).
|
|
||||||
QueryRow().Scan(&workflowTemplate.Version)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) createWorkflowTemplate(namespace string, workflowTemplate *WorkflowTemplate) (*WorkflowTemplate, error) {
|
func (c *Client) createWorkflowTemplate(namespace string, workflowTemplate *WorkflowTemplate) (*WorkflowTemplate, error) {
|
||||||
uid, err := workflowTemplate.GenerateUID()
|
uid, err := workflowTemplate.GenerateUID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -78,68 +61,6 @@ func (c *Client) createWorkflowTemplate(namespace string, workflowTemplate *Work
|
|||||||
return workflowTemplate, nil
|
return workflowTemplate, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) removeIsLatestFromWorkflowTemplateVersions(workflowTemplate *WorkflowTemplate) error {
|
|
||||||
query, args, err := sb.Update("workflow_template_versions").
|
|
||||||
Set("is_latest", true).
|
|
||||||
Where(sq.Eq{
|
|
||||||
"workflow_template_id": workflowTemplate.ID,
|
|
||||||
"is_latest": false,
|
|
||||||
}).
|
|
||||||
ToSql()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := c.DB.Exec(query, args...); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) createWorkflowTemplateVersion(namespace string, workflowTemplate *WorkflowTemplate) (*WorkflowTemplate, error) {
|
|
||||||
query, args, err := sb.Select("id, name").
|
|
||||||
From("workflow_templates").
|
|
||||||
Where(sq.Eq{
|
|
||||||
"namespace": namespace,
|
|
||||||
"uid": workflowTemplate.UID,
|
|
||||||
}).
|
|
||||||
Limit(1).ToSql()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = c.DB.Get(workflowTemplate, query, args...); err == sql.ErrNoRows {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = c.insertWorkflowTemplateVersion(workflowTemplate, c.DB); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return workflowTemplate, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) updateWorkflowTemplateVersion(workflowTemplate *WorkflowTemplate) (*WorkflowTemplate, error) {
|
|
||||||
query, args, err := sb.Update("workflow_template_versions").
|
|
||||||
Set("manifest", workflowTemplate.Manifest).
|
|
||||||
Where(sq.Eq{
|
|
||||||
"workflow_template_id": workflowTemplate.ID,
|
|
||||||
"version": workflowTemplate.Version,
|
|
||||||
}).
|
|
||||||
ToSql()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := c.DB.Exec(query, args...); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return workflowTemplate, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) workflowTemplatesSelectBuilder(namespace string) sq.SelectBuilder {
|
func (c *Client) workflowTemplatesSelectBuilder(namespace string) sq.SelectBuilder {
|
||||||
sb := sb.Select("wt.id", "wt.created_at", "wt.uid", "wt.name", "wt.is_archived").
|
sb := sb.Select("wt.id", "wt.created_at", "wt.uid", "wt.name", "wt.is_archived").
|
||||||
From("workflow_templates wt").
|
From("workflow_templates wt").
|
||||||
@@ -230,22 +151,27 @@ func (c *Client) listWorkflowTemplateVersions(namespace, uid string) (workflowTe
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, argoTemplate := range *argoTemplates {
|
for _, argoTemplate := range *argoTemplates {
|
||||||
version, versionErr := strconv.Atoi(argoTemplate.Labels["onepanel.io/version"])
|
version, versionErr := strconv.Atoi(argoTemplate.Labels[label.Version])
|
||||||
if versionErr != nil {
|
if versionErr != nil {
|
||||||
return nil, versionErr
|
return nil, versionErr
|
||||||
}
|
}
|
||||||
|
|
||||||
isLatest := false
|
isLatest := false
|
||||||
if _, ok := argoTemplate.Labels["onepanel.io/version_latest"]; ok {
|
if _, ok := argoTemplate.Labels[label.VersionLatest]; ok {
|
||||||
isLatest = true
|
isLatest = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
manifest, err := yaml.Marshal(argoTemplate)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
newItem := WorkflowTemplate{
|
newItem := WorkflowTemplate{
|
||||||
ID: template.ID,
|
ID: template.ID,
|
||||||
CreatedAt: template.CreatedAt,
|
CreatedAt: template.CreatedAt,
|
||||||
UID: template.UID,
|
UID: template.UID,
|
||||||
Name: template.Name,
|
Name: template.Name,
|
||||||
Manifest: template.Manifest,
|
Manifest: string(manifest),
|
||||||
Version: int32(version),
|
Version: int32(version),
|
||||||
IsLatest: isLatest,
|
IsLatest: isLatest,
|
||||||
IsArchived: template.IsArchived,
|
IsArchived: template.IsArchived,
|
||||||
@@ -351,9 +277,9 @@ func (c *Client) CreateWorkflowTemplateVersion(namespace string, workflowTemplat
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
incrementedVersion, err := number.IncrementStringInt(latest.Labels["onepanel.io/version"])
|
incrementedVersion, err := number.IncrementStringInt(latest.Labels[label.Version])
|
||||||
|
|
||||||
delete(latest.Labels, "onepanel.io/version_latest")
|
delete(latest.Labels, label.VersionLatest)
|
||||||
|
|
||||||
if _, err := c.ArgoprojV1alpha1().WorkflowTemplates(namespace).Update(latest); err != nil {
|
if _, err := c.ArgoprojV1alpha1().WorkflowTemplates(namespace).Update(latest); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -368,63 +294,10 @@ func (c *Client) CreateWorkflowTemplateVersion(namespace string, workflowTemplat
|
|||||||
updatedTemplate.ObjectMeta.ResourceVersion = ""
|
updatedTemplate.ObjectMeta.ResourceVersion = ""
|
||||||
updatedTemplate.ObjectMeta.SetSelfLink("")
|
updatedTemplate.ObjectMeta.SetSelfLink("")
|
||||||
|
|
||||||
// todo - all the error messages
|
|
||||||
if _, err := c.ArgoprojV1alpha1().WorkflowTemplates(namespace).Create(updatedTemplate); err != nil {
|
if _, err := c.ArgoprojV1alpha1().WorkflowTemplates(namespace).Create(updatedTemplate); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
//workflowTemplate, err := c.createWorkflowTemplateVersion(namespace, workflowTemplate)
|
|
||||||
//if err != nil {
|
|
||||||
// log.WithFields(log.Fields{
|
|
||||||
// "Namespace": namespace,
|
|
||||||
// "WorkflowTemplate": workflowTemplate,
|
|
||||||
// "Error": err.Error(),
|
|
||||||
// }).Error("Could not create workflow template version.")
|
|
||||||
// return nil, util.NewUserErrorWrap(err, "Workflow template")
|
|
||||||
//}
|
|
||||||
//if workflowTemplate == nil {
|
|
||||||
// return nil, util.NewUserError(codes.NotFound, "Workflow template not found.")
|
|
||||||
//}
|
|
||||||
|
|
||||||
return workflowTemplate, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) UpdateWorkflowTemplateVersion(namespace string, workflowTemplate *WorkflowTemplate) (*WorkflowTemplate, error) {
|
|
||||||
// validate workflow template
|
|
||||||
if err := c.ValidateWorkflowExecution(namespace, workflowTemplate.GetManifestBytes()); err != nil {
|
|
||||||
log.WithFields(log.Fields{
|
|
||||||
"Namespace": namespace,
|
|
||||||
"WorkflowTemplate": workflowTemplate,
|
|
||||||
"Error": err.Error(),
|
|
||||||
}).Error("Workflow could not be validated.")
|
|
||||||
return nil, util.NewUserError(codes.InvalidArgument, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
originalWorkflowTemplate, err := c.getWorkflowTemplate(namespace, workflowTemplate.UID, workflowTemplate.Version)
|
|
||||||
if err != nil {
|
|
||||||
log.WithFields(log.Fields{
|
|
||||||
"Namespace": namespace,
|
|
||||||
"WorkflowTemplate": workflowTemplate,
|
|
||||||
"Error": err.Error(),
|
|
||||||
}).Error("Could not get workflow template.")
|
|
||||||
return nil, util.NewUserError(codes.Unknown, "Could not update workflow template version.")
|
|
||||||
}
|
|
||||||
|
|
||||||
workflowTemplate.ID = originalWorkflowTemplate.ID
|
|
||||||
workflowTemplate, err = c.updateWorkflowTemplateVersion(workflowTemplate)
|
|
||||||
if err != nil {
|
|
||||||
log.WithFields(log.Fields{
|
|
||||||
"Namespace": namespace,
|
|
||||||
"WorkflowTemplate": workflowTemplate,
|
|
||||||
"Error": err.Error(),
|
|
||||||
}).Error("Could not update workflow template version.")
|
|
||||||
return nil, util.NewUserErrorWrap(err, "Workflow template")
|
|
||||||
}
|
|
||||||
if workflowTemplate == nil {
|
|
||||||
return nil, util.NewUserError(codes.NotFound, "Workflow template not found.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return workflowTemplate, nil
|
return workflowTemplate, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -570,6 +443,10 @@ func (c *Client) getArgoWorkflowTemplate(namespace, workflowTemplateUid, version
|
|||||||
}
|
}
|
||||||
|
|
||||||
templates := workflowTemplates.Items
|
templates := workflowTemplates.Items
|
||||||
|
if templates.Len() == 0 {
|
||||||
|
return nil, errors.New("not found")
|
||||||
|
}
|
||||||
|
|
||||||
if templates.Len() > 1 {
|
if templates.Len() > 1 {
|
||||||
return nil, errors.New("not unique result")
|
return nil, errors.New("not unique result")
|
||||||
}
|
}
|
||||||
@@ -591,3 +468,22 @@ func (c *Client) listArgoWorkflowTemplates(namespace, workflowTemplateUid string
|
|||||||
|
|
||||||
return &templates, nil
|
return &templates, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prefix is the label prefix.
|
||||||
|
// e.g. prefix/my-label-key: my-label-value
|
||||||
|
func (c *Client) GetWorkflowTemplateLabels(namespace, name, prefix string) (labels map[string]string, err error) {
|
||||||
|
wf, err := c.getArgoWorkflowTemplate(namespace, name, "latest")
|
||||||
|
if err != nil {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"Namespace": namespace,
|
||||||
|
"Name": name,
|
||||||
|
"Error": err.Error(),
|
||||||
|
}).Error("Workflow Template not found.")
|
||||||
|
return nil, util.NewUserError(codes.NotFound, "Workflow Template not found.")
|
||||||
|
}
|
||||||
|
|
||||||
|
labels = label.FilterByPrefix(prefix, wf.Labels)
|
||||||
|
labels = label.RemovePrefix(prefix, labels)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"github.com/onepanelio/core/api"
|
"github.com/onepanelio/core/api"
|
||||||
v1 "github.com/onepanelio/core/pkg"
|
v1 "github.com/onepanelio/core/pkg"
|
||||||
|
"github.com/onepanelio/core/pkg/util/label"
|
||||||
"github.com/onepanelio/core/server/auth"
|
"github.com/onepanelio/core/server/auth"
|
||||||
"github.com/onepanelio/core/server/converter"
|
"github.com/onepanelio/core/server/converter"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -85,6 +86,7 @@ func (s *WorkflowTemplateServer) CreateWorkflowTemplateVersion(ctx context.Conte
|
|||||||
UID: req.WorkflowTemplate.Uid,
|
UID: req.WorkflowTemplate.Uid,
|
||||||
Name: req.WorkflowTemplate.Name,
|
Name: req.WorkflowTemplate.Name,
|
||||||
Manifest: req.WorkflowTemplate.Manifest,
|
Manifest: req.WorkflowTemplate.Manifest,
|
||||||
|
Labels: converter.APIKeyValueToLabel(req.WorkflowTemplate.Labels),
|
||||||
}
|
}
|
||||||
|
|
||||||
workflowTemplate, err = client.CreateWorkflowTemplateVersion(req.Namespace, workflowTemplate)
|
workflowTemplate, err = client.CreateWorkflowTemplateVersion(req.Namespace, workflowTemplate)
|
||||||
@@ -111,10 +113,7 @@ func (s *WorkflowTemplateServer) UpdateWorkflowTemplateVersion(ctx context.Conte
|
|||||||
Manifest: req.WorkflowTemplate.Manifest,
|
Manifest: req.WorkflowTemplate.Manifest,
|
||||||
Version: req.WorkflowTemplate.Version,
|
Version: req.WorkflowTemplate.Version,
|
||||||
}
|
}
|
||||||
workflowTemplate, err = client.UpdateWorkflowTemplateVersion(req.Namespace, workflowTemplate)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.WorkflowTemplate.Uid = workflowTemplate.UID
|
req.WorkflowTemplate.Uid = workflowTemplate.UID
|
||||||
req.WorkflowTemplate.Name = workflowTemplate.Name
|
req.WorkflowTemplate.Name = workflowTemplate.Name
|
||||||
req.WorkflowTemplate.Version = workflowTemplate.Version
|
req.WorkflowTemplate.Version = workflowTemplate.Version
|
||||||
@@ -251,7 +250,7 @@ func (s *WorkflowTemplateServer) GetWorkflowTemplateLabels(ctx context.Context,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
labels, err := client.GetWorkflowTemplateLabels(req.Namespace, req.Name, "tags.onepanel.io/")
|
labels, err := client.GetWorkflowTemplateLabels(req.Namespace, req.Name, label.TagPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user