github.com/oam-dev/kubevela@v1.9.11/pkg/policy/common.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  	"encoding/json"
    21  	"fmt"
    22  
    23  	"github.com/kubevela/pkg/util/slices"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  
    26  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    27  )
    28  
    29  type typer[T any] interface {
    30  	*T
    31  	Type() string
    32  }
    33  
    34  // ParsePolicy parse policy for the given type
    35  func ParsePolicy[T any, P typer[T]](app *v1beta1.Application) (*T, error) {
    36  	base := new(T)
    37  	policies := slices.Filter(app.Spec.Policies, func(policy v1beta1.AppPolicy) bool {
    38  		return policy.Type == P(base).Type() && policy.Properties != nil && policy.Properties.Raw != nil
    39  	})
    40  	if len(policies) == 0 {
    41  		return nil, nil
    42  	}
    43  	props := slices.Map(policies, func(policy v1beta1.AppPolicy) *runtime.RawExtension { return policy.Properties })
    44  	arr := make([]map[string]interface{}, len(props))
    45  	if err := convertType(props, &arr); err != nil {
    46  		return nil, err
    47  	}
    48  	obj := slices.Reduce(arr[1:], mergePolicies, arr[0])
    49  	if err := convertType(obj, base); err != nil {
    50  		return nil, err
    51  	}
    52  	return base, nil
    53  }
    54  
    55  // mergePolicies merge two policy spec in place
    56  // 1. for array, concat them
    57  // 2. for bool, return if any of them is true
    58  // 3. otherwise, return base value
    59  func mergePolicies(base, patch map[string]interface{}) map[string]interface{} {
    60  	for k, v := range patch {
    61  		old, found := base[k]
    62  		if !found {
    63  			base[k] = v
    64  			continue
    65  		}
    66  		arr1, ok1 := old.([]interface{})
    67  		arr2, ok2 := v.([]interface{})
    68  		if ok1 && ok2 {
    69  			base[k] = append(arr1, arr2...)
    70  			continue
    71  		}
    72  		m1, ok1 := old.(map[string]interface{})
    73  		m2, ok2 := v.(map[string]interface{})
    74  		if ok1 && ok2 {
    75  			base[k] = mergePolicies(m1, m2)
    76  			continue
    77  		}
    78  		if old == false && v == true {
    79  			base[k] = true
    80  		}
    81  	}
    82  	return base
    83  }
    84  
    85  func convertType(src, dest interface{}) error {
    86  	bs, err := json.Marshal(src)
    87  	if err != nil {
    88  		return fmt.Errorf("failed to marshal %T: %w", src, err)
    89  	}
    90  	if err = json.Unmarshal(bs, dest); err != nil {
    91  		return fmt.Errorf("failed to unmarshal %T: %w", dest, err)
    92  	}
    93  	return nil
    94  }