github.com/oam-dev/kubevela@v1.9.11/pkg/policy/topology.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  	"context"
    21  	"fmt"
    22  
    23  	pkgmulticluster "github.com/kubevela/pkg/multicluster"
    24  	"github.com/pkg/errors"
    25  	utilfeature "k8s.io/apiserver/pkg/util/feature"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1"
    29  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    30  	"github.com/oam-dev/kubevela/pkg/features"
    31  	"github.com/oam-dev/kubevela/pkg/multicluster"
    32  	"github.com/oam-dev/kubevela/pkg/utils"
    33  )
    34  
    35  // GetClusterLabelSelectorInTopology get cluster label selector in topology policy spec
    36  func GetClusterLabelSelectorInTopology(topology *v1alpha1.TopologyPolicySpec) map[string]string {
    37  	if topology.ClusterLabelSelector != nil {
    38  		return topology.ClusterLabelSelector
    39  	}
    40  	if utilfeature.DefaultMutableFeatureGate.Enabled(features.DeprecatedPolicySpec) {
    41  		return topology.DeprecatedClusterSelector
    42  	}
    43  	return nil
    44  }
    45  
    46  // GetPlacementsFromTopologyPolicies get placements from topology policies with provided client
    47  func GetPlacementsFromTopologyPolicies(ctx context.Context, cli client.Client, appNs string, policies []v1beta1.AppPolicy, allowCrossNamespace bool) ([]v1alpha1.PlacementDecision, error) {
    48  	placements := make([]v1alpha1.PlacementDecision, 0)
    49  	placementMap := map[string]struct{}{}
    50  	addCluster := func(cluster string, ns string, validateCluster bool) error {
    51  		if validateCluster {
    52  			if _, e := multicluster.NewClusterClient(cli).Get(ctx, cluster); e != nil {
    53  				return errors.Wrapf(e, "failed to get cluster %s", cluster)
    54  			}
    55  		}
    56  		if !allowCrossNamespace && (ns != appNs && ns != "") {
    57  			return errors.Errorf("cannot cross namespace")
    58  		}
    59  		placement := v1alpha1.PlacementDecision{Cluster: cluster, Namespace: ns}
    60  		name := placement.String()
    61  		if _, found := placementMap[name]; !found {
    62  			placementMap[name] = struct{}{}
    63  			placements = append(placements, placement)
    64  		}
    65  		return nil
    66  	}
    67  	hasTopologyPolicy := false
    68  	for _, policy := range policies {
    69  		if policy.Type == v1alpha1.TopologyPolicyType {
    70  			if policy.Properties == nil {
    71  				return nil, fmt.Errorf("topology policy %s must not have empty properties", policy.Name)
    72  			}
    73  			hasTopologyPolicy = true
    74  			topologySpec := &v1alpha1.TopologyPolicySpec{}
    75  			if err := utils.StrictUnmarshal(policy.Properties.Raw, topologySpec); err != nil {
    76  				return nil, errors.Wrapf(err, "failed to parse topology policy %s", policy.Name)
    77  			}
    78  			clusterLabelSelector := GetClusterLabelSelectorInTopology(topologySpec)
    79  			switch {
    80  			case topologySpec.Clusters != nil:
    81  				for _, cluster := range topologySpec.Clusters {
    82  					if err := addCluster(cluster, topologySpec.Namespace, true); err != nil {
    83  						return nil, err
    84  					}
    85  				}
    86  			case clusterLabelSelector != nil:
    87  				clusterList, err := multicluster.NewClusterClient(cli).List(ctx, client.MatchingLabels(clusterLabelSelector))
    88  				if err != nil {
    89  					return nil, errors.Wrapf(err, "failed to find clusters in topology %s", policy.Name)
    90  				}
    91  				if len(clusterList.Items) == 0 && !topologySpec.AllowEmpty {
    92  					return nil, errors.New("failed to find any cluster matches given labels")
    93  				}
    94  				for _, cluster := range clusterList.Items {
    95  					if err = addCluster(cluster.Name, topologySpec.Namespace, false); err != nil {
    96  						return nil, err
    97  					}
    98  				}
    99  			default:
   100  				if err := addCluster(pkgmulticluster.Local, topologySpec.Namespace, false); err != nil {
   101  					return nil, err
   102  				}
   103  			}
   104  		}
   105  	}
   106  	if !hasTopologyPolicy {
   107  		placements = []v1alpha1.PlacementDecision{{Cluster: multicluster.ClusterLocalName}}
   108  	}
   109  	return placements, nil
   110  }