mirror of
https://github.com/onepanelio/onepanel.git
synced 2025-10-30 08:16:23 +08:00
feat: added label support for workflow executions and templates.
Also updated workflow templates to create a workflow template k8 resource so we can easily work with labels.
This commit is contained in:
@@ -65,6 +65,20 @@ func apiWorkflowTemplate(wft *v1.WorkflowTemplate) *api.WorkflowTemplate {
|
||||
}
|
||||
}
|
||||
|
||||
func mapToKeyValue(input map[string]string) []*api.KeyValue {
|
||||
var result []*api.KeyValue
|
||||
for key, value := range input {
|
||||
keyValue := &api.KeyValue{
|
||||
Key: key,
|
||||
Value: value,
|
||||
}
|
||||
|
||||
result = append(result, keyValue)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *WorkflowServer) CreateWorkflowExecution(ctx context.Context, req *api.CreateWorkflowExecutionRequest) (*api.WorkflowExecution, error) {
|
||||
client := ctx.Value("kubeClient").(*v1.Client)
|
||||
allowed, err := auth.IsAuthorized(client, req.Namespace, "create", "argoproj.io", "workflows", "")
|
||||
@@ -87,9 +101,7 @@ func (s *WorkflowServer) CreateWorkflowExecution(ctx context.Context, req *api.C
|
||||
|
||||
wf, err := client.CreateWorkflowExecution(req.Namespace, workflow)
|
||||
if err != nil {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return apiWorkflowExecution(wf), nil
|
||||
@@ -482,3 +494,183 @@ func (s *WorkflowServer) ListFiles(ctx context.Context, req *api.ListFilesReques
|
||||
ParentPath: parentPath,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *WorkflowServer) GetWorkflowExecutionLabels(ctx context.Context, req *api.GetLabelsRequest) (*api.GetLabelsResponse, error) {
|
||||
client := ctx.Value("kubeClient").(*v1.Client)
|
||||
allowed, err := auth.IsAuthorized(client, req.Namespace, "create", "argoproj.io", "workflows", "")
|
||||
if err != nil || !allowed {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
labels, err := client.GetWorkflowExecutionLabels(req.Namespace, req.Name, "tags.onepanel.io/")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &api.GetLabelsResponse{
|
||||
Labels: mapToKeyValue(labels),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Adds any labels that are not yet associated to the workflow execution.
|
||||
// If the label already exists, overwrites it.
|
||||
func (s *WorkflowServer) AddWorkflowExecutionLabels(ctx context.Context, req *api.AddLabelsRequest) (*api.GetLabelsResponse, error) {
|
||||
client := ctx.Value("kubeClient").(*v1.Client)
|
||||
allowed, err := auth.IsAuthorized(client, req.Namespace, "create", "argoproj.io", "workflows", "")
|
||||
if err != nil || !allowed {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keyValues := make(map[string]string)
|
||||
for _, item := range req.Labels.Items {
|
||||
keyValues[item.Key] = item.Value
|
||||
}
|
||||
|
||||
labels, err := client.SetWorkflowExecutionLabels(req.Namespace, req.Name, "tags.onepanel.io", keyValues, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &api.GetLabelsResponse{
|
||||
Labels: mapToKeyValue(labels),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Deletes all of the old labels and adds the new ones.
|
||||
func (s *WorkflowServer) ReplaceWorkflowExecutionLabels(ctx context.Context, req *api.ReplaceLabelsRequest) (*api.GetLabelsResponse, error) {
|
||||
client := ctx.Value("kubeClient").(*v1.Client)
|
||||
allowed, err := auth.IsAuthorized(client, req.Namespace, "create", "argoproj.io", "workflows", "")
|
||||
if err != nil || !allowed {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keyValues := make(map[string]string)
|
||||
for _, item := range req.Labels.Items {
|
||||
keyValues[item.Key] = item.Value
|
||||
}
|
||||
|
||||
labels, err := client.SetWorkflowExecutionLabels(req.Namespace, req.Name, "tags.onepanel.io", keyValues, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &api.GetLabelsResponse{
|
||||
Labels: mapToKeyValue(labels),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *WorkflowServer) DeleteWorkflowExecutionLabel(ctx context.Context, req *api.DeleteLabelRequest) (*api.GetLabelsResponse, error) {
|
||||
client := ctx.Value("kubeClient").(*v1.Client)
|
||||
allowed, err := auth.IsAuthorized(client, req.Namespace, "delete", "argoproj.io", "workflows", "")
|
||||
if err != nil || !allowed {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keyToDelete := "tags.onepanel.io/" + req.Key
|
||||
labels, err := client.DeleteWorkflowExecutionLabel(req.Namespace, req.Name, keyToDelete)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &api.GetLabelsResponse{
|
||||
Labels: mapToKeyValue(labels),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *WorkflowServer) GetWorkflowTemplateLabels(ctx context.Context, req *api.GetLabelsRequest) (*api.GetLabelsResponse, error) {
|
||||
client := ctx.Value("kubeClient").(*v1.Client)
|
||||
allowed, err := auth.IsAuthorized(client, req.Namespace, "create", "argoproj.io", "workflows", "")
|
||||
if err != nil || !allowed {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
labels, err := client.GetWorkflowTemplateLabels(req.Namespace, req.Name, "tags.onepanel.io/")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &api.GetLabelsResponse{
|
||||
Labels: mapToKeyValue(labels),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Adds any labels that are not yet associated to the workflow execution.
|
||||
// If the label already exists, overwrites it.
|
||||
func (s *WorkflowServer) AddWorkflowTemplateLabels(ctx context.Context, req *api.AddLabelsRequest) (*api.GetLabelsResponse, error) {
|
||||
client := ctx.Value("kubeClient").(*v1.Client)
|
||||
allowed, err := auth.IsAuthorized(client, req.Namespace, "create", "argoproj.io", "workflows", "")
|
||||
if err != nil || !allowed {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keyValues := make(map[string]string)
|
||||
for _, item := range req.Labels.Items {
|
||||
keyValues[item.Key] = item.Value
|
||||
}
|
||||
|
||||
labels, err := client.SetWorkflowTemplateLabels(req.Namespace, req.Name, "tags.onepanel.io", keyValues, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &api.GetLabelsResponse{
|
||||
Labels: mapToKeyValue(labels),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// Deletes all of the old labels and adds the new ones.
|
||||
func (s *WorkflowServer) ReplaceWorkflowTemplateLabels(ctx context.Context, req *api.ReplaceLabelsRequest) (*api.GetLabelsResponse, error) {
|
||||
client := ctx.Value("kubeClient").(*v1.Client)
|
||||
allowed, err := auth.IsAuthorized(client, req.Namespace, "create", "argoproj.io", "workflows", "")
|
||||
if err != nil || !allowed {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keyValues := make(map[string]string)
|
||||
for _, item := range req.Labels.Items {
|
||||
keyValues[item.Key] = item.Value
|
||||
}
|
||||
|
||||
labels, err := client.SetWorkflowTemplateLabels(req.Namespace, req.Name, "tags.onepanel.io", keyValues, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &api.GetLabelsResponse{
|
||||
Labels: mapToKeyValue(labels),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *WorkflowServer) DeleteWorkflowTemplateLabel(ctx context.Context, req *api.DeleteLabelRequest) (*api.GetLabelsResponse, error) {
|
||||
client := ctx.Value("kubeClient").(*v1.Client)
|
||||
allowed, err := auth.IsAuthorized(client, req.Namespace, "delete", "argoproj.io", "workflows", "")
|
||||
if err != nil || !allowed {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keyToDelete := "tags.onepanel.io/" + req.Key
|
||||
labels, err := client.DeleteWorkflowTemplateLabel(req.Namespace, req.Name, keyToDelete)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp := &api.GetLabelsResponse{
|
||||
Labels: mapToKeyValue(labels),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user