github.com/oam-dev/kubevela@v1.9.11/pkg/policy/replication.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 policy
    18  
    19  import (
    20  	"github.com/pkg/errors"
    21  	"k8s.io/kubectl/pkg/util/slice"
    22  
    23  	"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
    24  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1"
    25  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    26  	pkgutils "github.com/oam-dev/kubevela/pkg/utils"
    27  )
    28  
    29  // selectReplicateComponents will replicate the components
    30  func selectReplicateComponents(components []common.ApplicationComponent, selectors []string) ([]common.ApplicationComponent, error) {
    31  	var compToReplicate = make([]common.ApplicationComponent, 0)
    32  	for _, comp := range components {
    33  		if slice.ContainsString(selectors, comp.Name, nil) {
    34  			compToReplicate = append(compToReplicate, comp)
    35  		}
    36  	}
    37  	if len(compToReplicate) == 0 {
    38  		return nil, errors.New("no component selected to replicate")
    39  	}
    40  	return compToReplicate, nil
    41  }
    42  
    43  // ReplicateComponents will filter the components to replicate, return the replication decisions
    44  func ReplicateComponents(policies []v1beta1.AppPolicy, components []common.ApplicationComponent) ([]common.ApplicationComponent, error) {
    45  	var (
    46  		compToRemove = make(map[string]bool)
    47  		compToAdd    = make([]common.ApplicationComponent, 0)
    48  	)
    49  	existReplicationPolicy := false
    50  	for _, policy := range policies {
    51  		if policy.Type == v1alpha1.ReplicationPolicyType {
    52  			existReplicationPolicy = true
    53  			replicateSpec := &v1alpha1.ReplicationPolicySpec{}
    54  			if policy.Properties == nil {
    55  				continue
    56  			}
    57  			if err := pkgutils.StrictUnmarshal(policy.Properties.Raw, replicateSpec); err != nil {
    58  				return nil, errors.Wrapf(err, "failed to parse replicate policy %s", policy.Name)
    59  			}
    60  			compToRep, err := selectReplicateComponents(components, replicateSpec.Selector)
    61  			if err != nil {
    62  				return nil, errors.Wrapf(err, "failed to apply replicate policy %s", policy.Name)
    63  			}
    64  			compToAdd = append(compToAdd, replicateComponents(compToRep, replicateSpec.Keys)...)
    65  			for _, comp := range compToRep {
    66  				compToRemove[comp.Name] = true
    67  			}
    68  
    69  		}
    70  	}
    71  
    72  	if !existReplicationPolicy {
    73  		return components, nil
    74  	}
    75  	compsAfterReplicate := make([]common.ApplicationComponent, 0, len(components))
    76  	for _, comp := range components {
    77  		if !compToRemove[comp.Name] {
    78  			compsAfterReplicate = append(compsAfterReplicate, comp)
    79  		}
    80  	}
    81  	compsAfterReplicate = append(compsAfterReplicate, compToAdd...)
    82  	return compsAfterReplicate, nil
    83  }
    84  
    85  func replicateComponents(comps []common.ApplicationComponent, keys []string) []common.ApplicationComponent {
    86  	compsAfterReplicate := make([]common.ApplicationComponent, 0, len(comps)*len(keys))
    87  	for _, comp := range comps {
    88  		for _, key := range keys {
    89  			compAfterReplicate := comp.DeepCopy()
    90  			compAfterReplicate.ReplicaKey = key
    91  			compsAfterReplicate = append(compsAfterReplicate, *compAfterReplicate)
    92  		}
    93  	}
    94  	return compsAfterReplicate
    95  }