From 88a025fc04889f25b702f79030c6220c3ec48f9b Mon Sep 17 00:00:00 2001 From: lillecarl Date: Wed, 18 Feb 2026 00:16:22 +0000 Subject: [PATCH] use Helm apply order when rendering manifest lists see lillecarl/nix-csi#3 --- internal.nix | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/internal.nix b/internal.nix index f84f077..eb081ea 100644 --- a/internal.nix +++ b/internal.nix @@ -9,6 +9,63 @@ type = lib.types.anything; }; config.internal = rec { + # https://github.com/helm/helm/blob/4a91f3ad5cc0c1521f6d4dcb5681e2da4baaa157/pkg/release/v1/util/kind_sorter.go#L31 + helmOrder = [ + "PriorityClass" + "Namespace" + "NetworkPolicy" + "ResourceQuota" + "LimitRange" + "PodSecurityPolicy" + "PodDisruptionBudget" + "ServiceAccount" + "Secret" + "SecretList" + "ConfigMap" + "StorageClass" + "PersistentVolume" + "PersistentVolumeClaim" + "CustomResourceDefinition" + "ClusterRole" + "ClusterRoleList" + "ClusterRoleBinding" + "ClusterRoleBindingList" + "Role" + "RoleList" + "RoleBinding" + "RoleBindingList" + "Service" + "DaemonSet" + "Pod" + "ReplicationController" + "ReplicaSet" + "Deployment" + "HorizontalPodAutoscaler" + "StatefulSet" + "Job" + "CronJob" + "IngressClass" + "Ingress" + "APIService" + "MutatingWebhookConfiguration" + "ValidatingWebhookConfiguration" + ]; + + # Map kind -> priority (index in helmOrder) + applyPriorities = lib.listToAttrs ( + lib.imap0 (i: kind: { + name = kind; + value = i; + }) helmOrder + ); + + # Get priority for a kind, default to end if not in list + getApplyPriority = kind: applyPriorities.${kind} or (lib.length helmOrder); + + generatedOrdered = lib.sort ( + a: b: (getApplyPriority a.kind) < (getApplyPriority b.kind) + ) config.kubernetes.generated; + # Makes a valid YAML string, supports multiple documents or single attrsets, # documents will be JSON formatted since Nix can't render YAML. toYAMLStr = @@ -40,7 +97,7 @@ manifestAttrs = { apiVersion = "v1"; kind = "List"; - items = config.kubernetes.generated; + items = generatedOrdered; }; manifestJSON = builtins.toJSON manifestAttrs; manifestJSONFile = pkgs.writeText "manifest.json" manifestJSON; @@ -52,6 +109,6 @@ ${lib.getExe pkgs.yq} --yaml-output '.' ${manifestJSONFile} > $out ''; manifestYAML = builtins.readFile manifestYAMLFile; - manifestYAMLFile = toYAMLFile "manifest.yaml" config.kubernetes.generated; + manifestYAMLFile = toYAMLFile "nix-csi.yaml" generatedOrdered; }; }