sigs.k8s.io/cluster-api@v1.7.1/util/resource/resource.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes 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 resource implements resource utilites.
    18  package resource
    19  
    20  import (
    21  	"sort"
    22  
    23  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    24  )
    25  
    26  // orderMapping maps resource kinds to their creation order,
    27  // the lower the number, the earlier the resource should be created.
    28  var orderMapping = map[string]int{
    29  	// Namespaces go first because all namespaced resources depend on them.
    30  	"Namespace": -100,
    31  	// Custom Resource Definitions come before Custom Resource so that they can be
    32  	// restored with their corresponding CRD.
    33  	"CustomResourceDefinition": -99,
    34  	// Storage Classes are needed to create PVs and PVCs correctly.
    35  	"StorageClass": -98,
    36  	// PVs go before PVCs because PVCs depend on them.
    37  	"PersistentVolume": -97,
    38  	// PVCs go before pods or controllers so they can be mounted as volumes.
    39  	"PersistentVolumeClaim": -96,
    40  	// Secrets and ConfigMaps go before pods or controllers so they can be mounted as volumes.
    41  	"Secret":    -95,
    42  	"ConfigMap": -94,
    43  	// Service accounts go before pods or controllers so pods can use them.
    44  	"ServiceAccount": -93,
    45  	// Limit ranges go before pods or controllers so pods can use them.
    46  	"LimitRange": -92,
    47  	// Pods go before ReplicaSets
    48  	"Pod": -91,
    49  	// ReplicaSets go before Deployments
    50  	"ReplicaSet": -90,
    51  	// Endpoints go before Services (and everything else)
    52  	"Endpoint": -89,
    53  }
    54  
    55  // priorityLess returns true if o1 should be created before o2.
    56  // To be used in sort.{Slice,SliceStable} to sort manifests in place.
    57  func priorityLess(o1, o2 unstructured.Unstructured) bool {
    58  	p1 := orderMapping[o1.GetKind()]
    59  	p2 := orderMapping[o2.GetKind()]
    60  	return p1 < p2
    61  }
    62  
    63  // SortForCreate sorts objects by creation priority.
    64  func SortForCreate(resources []unstructured.Unstructured) []unstructured.Unstructured {
    65  	ret := make([]unstructured.Unstructured, len(resources))
    66  	copy(ret, resources)
    67  	sort.SliceStable(ret, func(i, j int) bool {
    68  		return priorityLess(ret[i], ret[j])
    69  	})
    70  
    71  	return ret
    72  }