github.com/oam-dev/kubevela@v1.9.11/pkg/workflow/template/load.go (about)

     1  /*
     2  Copyright 2021 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 template
    18  
    19  import (
    20  	"context"
    21  	"embed"
    22  	"fmt"
    23  
    24  	"github.com/pkg/errors"
    25  	corev1 "k8s.io/api/core/v1"
    26  	"k8s.io/apimachinery/pkg/api/meta"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  
    29  	"github.com/kubevela/workflow/pkg/tasks/template"
    30  	wfTypes "github.com/kubevela/workflow/pkg/types"
    31  
    32  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    33  	"github.com/oam-dev/kubevela/apis/types"
    34  	"github.com/oam-dev/kubevela/pkg/appfile"
    35  )
    36  
    37  var (
    38  	//go:embed static
    39  	templateFS embed.FS
    40  )
    41  
    42  const (
    43  	templateDir = "static"
    44  )
    45  
    46  // WorkflowStepLoader load workflowStep task definition template.
    47  type WorkflowStepLoader struct {
    48  	loadCapabilityDefinition func(ctx context.Context, capName string) (*appfile.Template, error)
    49  }
    50  
    51  // LoadTemplate gets the workflowStep definition.
    52  func (loader *WorkflowStepLoader) LoadTemplate(ctx context.Context, name string) (string, error) {
    53  	files, err := templateFS.ReadDir(templateDir)
    54  	if err != nil {
    55  		return "", err
    56  	}
    57  	if name == wfTypes.WorkflowStepTypeApplyComponent {
    58  		name = wfTypes.WorkflowStepTypeBuiltinApplyComponent
    59  	}
    60  	staticFilename := name + ".cue"
    61  	for _, file := range files {
    62  		if staticFilename == file.Name() {
    63  			fileName := fmt.Sprintf("%s/%s", templateDir, file.Name())
    64  			content, err := templateFS.ReadFile(fileName)
    65  			return string(content), err
    66  		}
    67  	}
    68  
    69  	templ, err := loader.loadCapabilityDefinition(ctx, name)
    70  	if err != nil {
    71  		return "", err
    72  	}
    73  	schematic := templ.WorkflowStepDefinition.Spec.Schematic
    74  	if schematic != nil && schematic.CUE != nil {
    75  		return schematic.CUE.Template, nil
    76  	}
    77  
    78  	return "", errors.New("custom workflowStep only support cue")
    79  }
    80  
    81  // NewWorkflowStepTemplateLoader create a task template loader.
    82  func NewWorkflowStepTemplateLoader(client client.Client) template.Loader {
    83  	return &WorkflowStepLoader{
    84  		loadCapabilityDefinition: func(ctx context.Context, capName string) (*appfile.Template, error) {
    85  			return appfile.LoadTemplate(ctx, client, capName, types.TypeWorkflowStep)
    86  		},
    87  	}
    88  }
    89  
    90  // NewWorkflowStepTemplateRevisionLoader create a task template loader from ApplicationRevision.
    91  func NewWorkflowStepTemplateRevisionLoader(rev *v1beta1.ApplicationRevision, mapper meta.RESTMapper) template.Loader {
    92  	return &WorkflowStepLoader{
    93  		loadCapabilityDefinition: func(ctx context.Context, capName string) (*appfile.Template, error) {
    94  			return appfile.LoadTemplateFromRevision(capName, types.TypeWorkflowStep, rev, mapper)
    95  		},
    96  	}
    97  }
    98  
    99  // ViewLoader load view task definition template.
   100  type ViewLoader struct {
   101  	client    client.Client
   102  	namespace string
   103  }
   104  
   105  // LoadTemplate gets the workflowStep definition.
   106  func (loader *ViewLoader) LoadTemplate(ctx context.Context, name string) (string, error) {
   107  	cm := new(corev1.ConfigMap)
   108  	cmKey := client.ObjectKey{Name: name, Namespace: loader.namespace}
   109  	if err := loader.client.Get(ctx, cmKey, cm); err != nil {
   110  		return "", errors.Wrapf(err, "fail to get view template %v from configMap", cmKey)
   111  	}
   112  	return cm.Data["template"], nil
   113  }
   114  
   115  // NewViewTemplateLoader create a view task template loader.
   116  func NewViewTemplateLoader(client client.Client, namespace string) template.Loader {
   117  	return &ViewLoader{
   118  		client:    client,
   119  		namespace: namespace,
   120  	}
   121  }
   122  
   123  // EchoLoader will load data from input as it is.
   124  type EchoLoader struct {
   125  }
   126  
   127  // LoadTemplate gets the echo content exactly what it is .
   128  func (ll *EchoLoader) LoadTemplate(_ context.Context, content string) (string, error) {
   129  	return content, nil
   130  }