github.com/oam-dev/kubevela@v1.9.11/pkg/workflow/step/dependency.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 step
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/pkg/errors"
    23  	kerrors "k8s.io/apimachinery/pkg/api/errors"
    24  	types2 "k8s.io/apimachinery/pkg/types"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  
    27  	workflowv1alpha1 "github.com/kubevela/workflow/api/v1alpha1"
    28  
    29  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1"
    30  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    31  	"github.com/oam-dev/kubevela/pkg/utils"
    32  )
    33  
    34  // LoadExternalPoliciesForWorkflow detects policies used in workflow steps which are not declared in internal policies
    35  // try to load them from external policy objects in the application's namespace
    36  func LoadExternalPoliciesForWorkflow(ctx context.Context, cli client.Client, appNs string, steps []workflowv1alpha1.WorkflowStep, internalPolicies []v1beta1.AppPolicy) ([]v1beta1.AppPolicy, error) {
    37  	policies := internalPolicies
    38  	policyMap := map[string]struct{}{}
    39  	for _, policy := range policies {
    40  		policyMap[policy.Name] = struct{}{}
    41  	}
    42  	// Load extra used policies declared in the workflow step
    43  	for _, _step := range steps {
    44  		if _step.Type == DeployWorkflowStep && _step.Properties != nil {
    45  			props := DeployWorkflowStepSpec{}
    46  			if err := utils.StrictUnmarshal(_step.Properties.Raw, &props); err != nil {
    47  				return nil, errors.Wrapf(err, "invalid WorkflowStep %s", _step.Name)
    48  			}
    49  			for _, policyName := range props.Policies {
    50  				if _, found := policyMap[policyName]; !found {
    51  					po := &v1alpha1.Policy{}
    52  					if err := cli.Get(ctx, types2.NamespacedName{Namespace: appNs, Name: policyName}, po); err != nil {
    53  						if kerrors.IsNotFound(err) {
    54  							return nil, errors.Errorf("external policy %s not found", policyName)
    55  						}
    56  						return nil, errors.Wrapf(err, "failed to load external policy %s in namespace %s", policyName, appNs)
    57  					}
    58  					policies = append(policies, v1beta1.AppPolicy{Name: policyName, Type: po.Type, Properties: po.Properties})
    59  					policyMap[policyName] = struct{}{}
    60  				}
    61  			}
    62  		}
    63  	}
    64  	return policies, nil
    65  }