github.com/oam-dev/kubevela@v1.9.11/pkg/resourcekeeper/utils.go (about)

     1  /*
     2  Copyright 2022 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 resourcekeeper
    18  
    19  import (
    20  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    21  	"k8s.io/utils/strings/slices"
    22  
    23  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha1"
    24  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    25  	"github.com/oam-dev/kubevela/pkg/oam"
    26  	"github.com/oam-dev/kubevela/pkg/utils"
    27  )
    28  
    29  // ClearNamespaceForClusterScopedResources clear namespace for cluster scoped resources
    30  func (h *resourceKeeper) ClearNamespaceForClusterScopedResources(manifests []*unstructured.Unstructured) {
    31  	for _, manifest := range manifests {
    32  		if ok, err := utils.IsClusterScope(manifest.GroupVersionKind(), h.Client.RESTMapper()); err == nil && ok {
    33  			manifest.SetNamespace("")
    34  		}
    35  	}
    36  }
    37  
    38  func (h *resourceKeeper) isShared(manifest *unstructured.Unstructured) bool {
    39  	if h.sharedResourcePolicy == nil {
    40  		return false
    41  	}
    42  	return h.sharedResourcePolicy.FindStrategy(manifest)
    43  }
    44  
    45  func (h *resourceKeeper) canTakeOver(manifest *unstructured.Unstructured) bool {
    46  	if h.takeOverPolicy == nil {
    47  		return false
    48  	}
    49  	return h.takeOverPolicy.FindStrategy(manifest)
    50  }
    51  
    52  func (h *resourceKeeper) isReadOnly(manifest *unstructured.Unstructured) bool {
    53  	if h.readOnlyPolicy == nil {
    54  		return false
    55  	}
    56  	return h.readOnlyPolicy.FindStrategy(manifest)
    57  }
    58  
    59  func (h *resourceKeeper) getUpdateStrategy(manifest *unstructured.Unstructured) *v1alpha1.ResourceUpdateStrategy {
    60  	if h.resourceUpdatePolicy == nil {
    61  		return nil
    62  	}
    63  	return h.resourceUpdatePolicy.FindStrategy(manifest)
    64  }
    65  
    66  // hasOrphanFinalizer checks if the target application should orphan child resources
    67  func hasOrphanFinalizer(app *v1beta1.Application) bool {
    68  	return slices.Contains(app.GetFinalizers(), oam.FinalizerOrphanResource)
    69  }