github.com/oam-dev/kubevela@v1.9.11/references/docgen/convert.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 docgen
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/api/meta"
    23  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  
    26  	"github.com/kubevela/workflow/pkg/cue/packages"
    27  
    28  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    29  	"github.com/oam-dev/kubevela/apis/types"
    30  	"github.com/oam-dev/kubevela/pkg/oam/util"
    31  )
    32  
    33  // ParseCapabilityFromUnstructured will convert Unstructured to Capability
    34  func ParseCapabilityFromUnstructured(mapper meta.RESTMapper, pd *packages.PackageDiscover, obj unstructured.Unstructured) (types.Capability, error) {
    35  	var err error
    36  	switch obj.GetKind() {
    37  	case "ComponentDefinition":
    38  		var cd v1beta1.ComponentDefinition
    39  		err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &cd)
    40  		if err != nil {
    41  			return types.Capability{}, err
    42  		}
    43  		var workloadDefinitionRef string
    44  		if cd.Spec.Workload.Type != "" {
    45  			workloadDefinitionRef = cd.Spec.Workload.Type
    46  		} else if mapper != nil {
    47  			ref, err := util.ConvertWorkloadGVK2Definition(mapper, cd.Spec.Workload.Definition)
    48  			if err != nil {
    49  				return types.Capability{}, err
    50  			}
    51  			workloadDefinitionRef = ref.Name
    52  		}
    53  		return HandleDefinition(cd.Name, workloadDefinitionRef, cd.Annotations, cd.Labels, cd.Spec.Extension, types.TypeComponentDefinition, nil, cd.Spec.Schematic, pd)
    54  	case "TraitDefinition":
    55  		var td v1beta1.TraitDefinition
    56  		err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &td)
    57  		if err != nil {
    58  			return types.Capability{}, err
    59  		}
    60  		return HandleDefinition(td.Name, td.Spec.Reference.Name, td.Annotations, td.Labels, td.Spec.Extension, types.TypeTrait, td.Spec.AppliesToWorkloads, td.Spec.Schematic, pd)
    61  	case "PolicyDefinition":
    62  		var plcd v1beta1.PolicyDefinition
    63  		err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &plcd)
    64  		if err != nil {
    65  			return types.Capability{}, err
    66  		}
    67  		return HandleDefinition(plcd.Name, plcd.Spec.Reference.Name, plcd.Annotations, plcd.Labels, nil, types.TypePolicy, nil, plcd.Spec.Schematic, pd)
    68  	case "WorkflowStepDefinition":
    69  		var wfd v1beta1.WorkflowStepDefinition
    70  		err = runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &wfd)
    71  		if err != nil {
    72  			return types.Capability{}, err
    73  		}
    74  		return HandleDefinition(wfd.Name, wfd.Spec.Reference.Name, wfd.Annotations, wfd.Labels, nil, types.TypeWorkflowStep, nil, wfd.Spec.Schematic, pd)
    75  	}
    76  	return types.Capability{}, fmt.Errorf("unknown definition Type %s", obj.GetKind())
    77  }