# build/nix/k8s-manifests/default.nix
#
# Standalone Kubernetes manifest generator for PCP DaemonSet deployment.
# Produces namespace and daemonset YAML files that can be applied directly
# with kubectl, without needing minikube or the full test suite.
#
# Manifests are validated with kube-linter at build time.
# See kube-linter.nix for the full rationale on suppressed checks.
#
# Usage in flake.nix:
#   k8sManifests = import (nixDir + "/k8s-manifests") { inherit pkgs lib; };
#
# With namespace override (for test suites):
#   k8sManifests = import (nixDir + "/k8s-manifests") {
#     inherit pkgs lib;
#     namespaceOverride = "pcp-test";
#   };
#
# Generated outputs:
#   k8sManifests.manifestFile              - Combined YAML file
#   k8sManifests.dir                       - Directory with individual files
#   k8sManifests.packages.pcp-k8s-manifests          - Directory derivation
#   k8sManifests.packages.pcp-k8s-manifest-namespace - Namespace YAML
#   k8sManifests.packages.pcp-k8s-manifest-daemonset - DaemonSet YAML
#
{ pkgs, lib, namespaceOverride ? null }:
let
  baseConstants = import ./constants.nix { };

  # Apply namespace override if provided (used by test suites)
  constants = baseConstants // {
    k8s = baseConstants.k8s // lib.optionalAttrs (namespaceOverride != null) {
      namespace = namespaceOverride;
    };
  };

  namespace = import ./namespace.nix { inherit pkgs constants; };
  daemonset = import ./daemonset.nix { inherit pkgs constants; };

  # Combined manifest (namespace + daemonset, separated by ---)
  combinedYaml = namespace.yaml + "---\n" + daemonset.yaml;
  manifestFile = pkgs.writeText "pcp-daemonset.yaml" combinedYaml;

  # Lint validation — fails the build if manifests have unexpected errors
  # (see kube-linter.nix for rationale on suppressed checks)
  lintResult = import ./kube-linter.nix {
    inherit pkgs;
    manifestFiles = [ namespace.file daemonset.file ];
  };

  readme = ''
    PCP Kubernetes Manifests
    ========================

    These manifests deploy PCP as a privileged DaemonSet for full node
    monitoring including BPF metrics.

    Namespace: ${constants.k8s.namespace}

    Quick start:

      # Apply all manifests at once
      kubectl apply -f combined.yaml

      # Or apply individually
      kubectl apply -f namespace.yaml
      kubectl apply -f daemonset.yaml

      # Verify deployment
      kubectl get pods -n ${constants.k8s.namespace}

      # Query metrics from a pod
      kubectl exec -n ${constants.k8s.namespace} <pod> -- pminfo -f kernel.all.load

      # Cleanup
      kubectl delete namespace ${constants.k8s.namespace}

    Generated by: nix build .#pcp-k8s-manifests
  '';

  # Directory derivation with all manifests (depends on lint passing)
  dir = pkgs.runCommand "pcp-k8s-manifests" {
    inherit lintResult;
  } ''
    mkdir -p $out
    cp ${namespace.file} $out/namespace.yaml
    cp ${daemonset.file} $out/daemonset.yaml
    cp ${manifestFile} $out/combined.yaml
    cp ${pkgs.writeText "README.md" readme} $out/README.md
  '';

in
{
  # Backwards-compatible: combined manifest as a single file
  inherit manifestFile;

  # Individual manifest components
  inherit namespace daemonset;

  # Directory with all files
  inherit dir;

  # Lint validation result
  inherit lintResult;

  # Constants (for consumers that need deployment settings)
  inherit constants;

  # Packages for flake integration
  packages = {
    pcp-k8s-manifests = dir;
    pcp-k8s-manifest-namespace = namespace.file;
    pcp-k8s-manifest-daemonset = daemonset.file;
    pcp-k8s-manifests-lint = lintResult;
  };
}
