...

Source file src/sigs.k8s.io/kustomize/kyaml/yaml/order.go

Documentation: sigs.k8s.io/kustomize/kyaml/yaml

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package yaml
     5  
     6  // fieldSortOrder contains the relative ordering of fields when formatting an
     7  // object.
     8  var fieldSortOrder = []string{
     9  	// top-level metadata
    10  	"name", "generateName", "namespace", "clusterName",
    11  	"apiVersion", "kind", "metadata", "type",
    12  	"labels", "annotations",
    13  	"spec", "status",
    14  
    15  	// secret and configmap
    16  	"stringData", "data", "binaryData",
    17  
    18  	// cronjobspec,  daemonsetspec, deploymentspec, statefulsetspec,
    19  	// jobspec fields
    20  	"parallelism", "completions", "activeDeadlineSeconds", "backoffLimit",
    21  	"replicas", "selector", "manualSelector", "template",
    22  	"ttlSecondsAfterFinished", "volumeClaimTemplates", "service", "serviceName",
    23  	"podManagementPolicy", "updateStrategy", "strategy", "minReadySeconds",
    24  	"revision", "revisionHistoryLimit", "paused", "progressDeadlineSeconds",
    25  
    26  	// podspec
    27  	// podspec scalars
    28  	"restartPolicy", "terminationGracePeriodSeconds",
    29  	"activeDeadlineSeconds", "dnsPolicy", "serviceAccountName",
    30  	"serviceAccount", "automountServiceAccountToken", "nodeName",
    31  	"hostNetwork", "hostPID", "hostIPC", "shareProcessNamespace", "hostname",
    32  	"subdomain", "schedulerName", "priorityClassName", "priority",
    33  	"runtimeClassName", "enableServiceLinks",
    34  
    35  	// podspec lists and maps
    36  	"nodeSelector", "hostAliases",
    37  
    38  	// podspec objects
    39  	"initContainers", "containers", "volumes", "securityContext",
    40  	"imagePullSecrets", "affinity", "tolerations", "dnsConfig",
    41  	"readinessGates",
    42  
    43  	// containers
    44  	"image", "command", "args", "workingDir", "ports", "envFrom", "env",
    45  	"resources", "volumeMounts", "volumeDevices", "livenessProbe",
    46  	"readinessProbe", "lifecycle", "terminationMessagePath",
    47  	"terminationMessagePolicy", "imagePullPolicy", "securityContext",
    48  	"stdin", "stdinOnce", "tty",
    49  
    50  	// service
    51  	"clusterIP", "externalIPs", "loadBalancerIP", "loadBalancerSourceRanges",
    52  	"externalName", "externalTrafficPolicy", "sessionAffinity",
    53  
    54  	// ports
    55  	"protocol", "port", "targetPort", "hostPort", "containerPort", "hostIP",
    56  
    57  	// volumemount
    58  	"readOnly", "mountPath", "subPath", "subPathExpr", "mountPropagation",
    59  
    60  	// envvar + envvarsource
    61  	"value", "valueFrom", "fieldRef", "resourceFieldRef", "configMapKeyRef",
    62  	"secretKeyRef", "prefix", "configMapRef", "secretRef",
    63  }
    64  
    65  type set map[string]interface{}
    66  
    67  func newSet(values ...string) set {
    68  	m := map[string]interface{}{}
    69  	for _, value := range values {
    70  		m[value] = nil
    71  	}
    72  	return m
    73  }
    74  
    75  func (s set) Has(key string) bool {
    76  	_, found := s[key]
    77  	return found
    78  }
    79  
    80  // WhitelistedListSortKinds contains the set of kinds that are whitelisted
    81  // for sorting list field elements
    82  var WhitelistedListSortKinds = newSet(
    83  	"CronJob", "DaemonSet", "Deployment", "Job", "ReplicaSet", "StatefulSet",
    84  	"ValidatingWebhookConfiguration")
    85  
    86  // WhitelistedListSortApis contains the set of apis that are whitelisted for
    87  // sorting list field elements
    88  var WhitelistedListSortApis = newSet(
    89  	"apps/v1", "apps/v1beta1", "apps/v1beta2", "batch/v1", "batch/v1beta1",
    90  	"extensions/v1beta1", "v1", "admissionregistration.k8s.io/v1")
    91  
    92  // WhitelistedListSortFields contains json paths to list fields that should
    93  // be sorted, and the field they should be sorted by
    94  var WhitelistedListSortFields = map[string]string{
    95  	".spec.template.spec.containers": "name",
    96  	".webhooks.rules.operations":     "",
    97  }
    98  
    99  // FieldOrder indexes fields and maps them to relative precedence
   100  var FieldOrder = func() map[string]int {
   101  	// create an index of field orderings
   102  	fo := map[string]int{}
   103  	for i, f := range fieldSortOrder {
   104  		fo[f] = i + 1
   105  	}
   106  	return fo
   107  }()
   108  

View as plain text