github.com/oam-dev/kubevela@v1.9.11/references/common/registry.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 common
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"strings"
    23  
    24  	"github.com/pkg/errors"
    25  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  	"sigs.k8s.io/yaml"
    29  
    30  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    31  	"github.com/oam-dev/kubevela/apis/types"
    32  	cmdutil "github.com/oam-dev/kubevela/pkg/utils/util"
    33  )
    34  
    35  // InstallComponentDefinition will add a component into K8s cluster and install its controller
    36  func InstallComponentDefinition(client client.Client, componentData []byte, ioStreams cmdutil.IOStreams) error {
    37  	var cd v1beta1.ComponentDefinition
    38  	var err error
    39  	if componentData == nil {
    40  		return errors.New("componentData is nil")
    41  	}
    42  	if err = yaml.Unmarshal(componentData, &cd); err != nil {
    43  		return err
    44  	}
    45  	cd.Namespace = types.DefaultKubeVelaNS
    46  	ioStreams.Info("Installing component: " + cd.Name)
    47  	if err = client.Create(context.Background(), &cd); err != nil && !apierrors.IsAlreadyExists(err) {
    48  		return err
    49  	}
    50  	return nil
    51  }
    52  
    53  // InstallTraitDefinition will add a trait into K8s cluster and install it's controller
    54  func InstallTraitDefinition(client client.Client, traitdata []byte, ioStreams cmdutil.IOStreams) error {
    55  	var td v1beta1.TraitDefinition
    56  	var err error
    57  	if err = yaml.Unmarshal(traitdata, &td); err != nil {
    58  		return err
    59  	}
    60  	td.Namespace = types.DefaultKubeVelaNS
    61  	ioStreams.Info("Installing trait " + td.Name)
    62  	if err = client.Create(context.Background(), &td); err != nil && !apierrors.IsAlreadyExists(err) {
    63  		return err
    64  	}
    65  	return nil
    66  }
    67  
    68  func addSourceIntoExtension(in *runtime.RawExtension, source *types.Source) error {
    69  	var extension map[string]interface{}
    70  	err := json.Unmarshal(in.Raw, &extension)
    71  	if err != nil {
    72  		return err
    73  	}
    74  	extension["source"] = source
    75  	data, err := json.Marshal(extension)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	in.Raw = data
    80  	return nil
    81  }
    82  
    83  // CheckLabelExistence checks whether a label `key=value` exists in definition labels
    84  func CheckLabelExistence(labels map[string]string, label string) bool {
    85  	splitLabel := strings.Split(label, "=")
    86  	if len(splitLabel) < 2 {
    87  		return false
    88  	}
    89  	k, v := splitLabel[0], splitLabel[1]
    90  	if labelValue, ok := labels[k]; ok {
    91  		if labelValue == v {
    92  			return true
    93  		}
    94  	}
    95  	return false
    96  }