github.com/oam-dev/kubevela@v1.9.11/references/docgen/local.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 docgen
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/oam-dev/kubevela/apis/types"
    24  	"github.com/oam-dev/kubevela/pkg/utils/common"
    25  )
    26  
    27  // LoadCapabilityByName will load capability from local by name
    28  func LoadCapabilityByName(name string, userNamespace string, c common.Args) (types.Capability, error) {
    29  	caps, err := LoadAllInstalledCapability(userNamespace, c)
    30  	if err != nil {
    31  		return types.Capability{}, err
    32  	}
    33  	for _, c := range caps {
    34  		if c.Name == name {
    35  			return c, nil
    36  		}
    37  	}
    38  	return types.Capability{}, fmt.Errorf("%s not found", name)
    39  }
    40  
    41  // LoadAllInstalledCapability will list all capability
    42  func LoadAllInstalledCapability(userNamespace string, c common.Args) ([]types.Capability, error) {
    43  	caps, err := GetCapabilitiesFromCluster(context.TODO(), userNamespace, c, nil)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	systemCaps, err := GetCapabilitiesFromCluster(context.TODO(), types.DefaultKubeVelaNS, c, nil)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	caps = append(caps, systemCaps...)
    52  	return caps, nil
    53  }
    54  
    55  // LoadInstalledCapabilityWithType will load cap list by type
    56  func LoadInstalledCapabilityWithType(userNamespace string, c common.Args, capT types.CapType) ([]types.Capability, error) {
    57  	switch capT {
    58  	case types.TypeComponentDefinition:
    59  		caps, _, err := GetComponentsFromCluster(context.TODO(), userNamespace, c, nil)
    60  		if err != nil {
    61  			return nil, err
    62  		}
    63  		if userNamespace != types.DefaultKubeVelaNS {
    64  			systemCaps, _, err := GetComponentsFromCluster(context.TODO(), types.DefaultKubeVelaNS, c, nil)
    65  			if err != nil {
    66  				return nil, err
    67  			}
    68  			caps = append(caps, systemCaps...)
    69  		}
    70  		return caps, nil
    71  	case types.TypeTrait:
    72  		caps, _, err := GetTraitsFromCluster(context.TODO(), userNamespace, c, nil)
    73  		if err != nil {
    74  			return nil, err
    75  		}
    76  		if userNamespace != types.DefaultKubeVelaNS {
    77  			systemCaps, _, err := GetTraitsFromCluster(context.TODO(), types.DefaultKubeVelaNS, c, nil)
    78  			if err != nil {
    79  				return nil, err
    80  			}
    81  			caps = append(caps, systemCaps...)
    82  		}
    83  		return caps, nil
    84  	case types.TypeWorkload:
    85  	default:
    86  	}
    87  	return nil, nil
    88  }