github.com/kubevela/workflow@v0.6.0/pkg/debug/context.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package debug
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	corev1 "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/api/errors"
    25  	"k8s.io/apimachinery/pkg/types"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	"github.com/kubevela/workflow/pkg/cue/model/value"
    29  	wfTypes "github.com/kubevela/workflow/pkg/types"
    30  )
    31  
    32  // ContextImpl is workflow debug context interface
    33  type ContextImpl interface {
    34  	Set(v *value.Value) error
    35  }
    36  
    37  // Context is debug context.
    38  type Context struct {
    39  	cli      client.Client
    40  	instance *wfTypes.WorkflowInstance
    41  	id       string
    42  }
    43  
    44  // Set sets debug content into context
    45  func (d *Context) Set(v *value.Value) error {
    46  	data, err := v.String()
    47  	if err != nil {
    48  		return err
    49  	}
    50  	err = setStore(context.Background(), d.cli, d.instance, d.id, data)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  func setStore(ctx context.Context, cli client.Client, instance *wfTypes.WorkflowInstance, id, data string) error {
    59  	cm := &corev1.ConfigMap{}
    60  	if err := cli.Get(ctx, types.NamespacedName{
    61  		Namespace: instance.Namespace,
    62  		Name:      GenerateContextName(instance.Name, id, string(instance.UID)),
    63  	}, cm); err != nil {
    64  		if errors.IsNotFound(err) {
    65  			cm.Name = GenerateContextName(instance.Name, id, string(instance.UID))
    66  			cm.Namespace = instance.Namespace
    67  			cm.Data = map[string]string{
    68  				"debug": data,
    69  			}
    70  			cm.Labels = map[string]string{}
    71  			cm.SetOwnerReferences(instance.ChildOwnerReferences)
    72  			if err := cli.Create(ctx, cm); err != nil {
    73  				return err
    74  			}
    75  			return nil
    76  		}
    77  		return err
    78  	}
    79  	cm.Data = map[string]string{
    80  		"debug": data,
    81  	}
    82  	if err := cli.Update(ctx, cm); err != nil {
    83  		return err
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  // NewContext new workflow context without initialize data.
    90  func NewContext(cli client.Client, instance *wfTypes.WorkflowInstance, id string) ContextImpl {
    91  	return &Context{
    92  		cli:      cli,
    93  		instance: instance,
    94  		id:       id,
    95  	}
    96  }
    97  
    98  // GenerateContextName generate context name
    99  func GenerateContextName(name, id, suffix string) string {
   100  	if len(suffix) > 5 {
   101  		suffix = suffix[len(suffix)-5:]
   102  	}
   103  	return fmt.Sprintf("%s-%s-debug-%s", name, id, suffix)
   104  }