github.com/oam-dev/kubevela@v1.9.11/pkg/cue/process/handle.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 process
    18  
    19  import (
    20  	"context"
    21  	"strconv"
    22  	"strings"
    23  
    24  	"github.com/kubevela/workflow/pkg/cue/process"
    25  
    26  	"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
    27  	"github.com/oam-dev/kubevela/apis/types"
    28  	"github.com/oam-dev/kubevela/pkg/oam/util"
    29  )
    30  
    31  // ContextData is the core data of process context
    32  type ContextData struct {
    33  	Namespace       string
    34  	Cluster         string
    35  	AppName         string
    36  	CompName        string
    37  	StepName        string
    38  	CompRevision    string
    39  	AppRevisionName string
    40  	WorkflowName    string
    41  	PublishVersion  string
    42  	ReplicaKey      string
    43  
    44  	Ctx            context.Context
    45  	BaseHooks      []process.BaseHook
    46  	AuxiliaryHooks []process.AuxiliaryHook
    47  	Components     []common.ApplicationComponent
    48  
    49  	AppLabels      map[string]string
    50  	AppAnnotations map[string]string
    51  
    52  	ClusterVersion types.ClusterVersion
    53  }
    54  
    55  // NewContext creates a new process context
    56  func NewContext(data ContextData) process.Context {
    57  	ctx := process.NewContext(process.ContextData{
    58  		Namespace:      data.Namespace,
    59  		Name:           data.CompName,
    60  		StepName:       data.StepName,
    61  		WorkflowName:   data.WorkflowName,
    62  		PublishVersion: data.PublishVersion,
    63  		Ctx:            data.Ctx,
    64  		BaseHooks:      data.BaseHooks,
    65  		AuxiliaryHooks: data.AuxiliaryHooks,
    66  	})
    67  	ctx.PushData(ContextAppName, data.AppName)
    68  	ctx.PushData(ContextAppRevision, data.AppRevisionName)
    69  	ctx.PushData(ContextCompRevisionName, data.CompRevision)
    70  	ctx.PushData(ContextComponents, data.Components)
    71  	ctx.PushData(ContextAppLabels, data.AppLabels)
    72  	ctx.PushData(ContextAppAnnotations, data.AppAnnotations)
    73  	ctx.PushData(ContextReplicaKey, data.ReplicaKey)
    74  	revNum, _ := util.ExtractRevisionNum(data.AppRevisionName, "-")
    75  	ctx.PushData(ContextAppRevisionNum, revNum)
    76  	ctx.PushData(ContextCluster, data.Cluster)
    77  	ctx.PushData(ContextClusterVersion, parseClusterVersion(data.ClusterVersion))
    78  	return ctx
    79  }
    80  
    81  func parseClusterVersion(cv types.ClusterVersion) map[string]interface{} {
    82  	// no minor found, use control plane cluster version instead.
    83  	if cv.Minor == "" {
    84  		cv = types.ControlPlaneClusterVersion
    85  	}
    86  	minorS := strings.TrimSpace(cv.Minor)
    87  	minorS = strings.TrimRight(minorS, ".+-/?!")
    88  	minor, _ := strconv.ParseInt(minorS, 10, 64)
    89  	return map[string]interface{}{
    90  		"major":      cv.Major,
    91  		"gitVersion": cv.GitVersion,
    92  		"platform":   cv.Platform,
    93  		"minor":      minor,
    94  	}
    95  }