Commit 6cfcd3c7 authored by superherointj's avatar superherointj
Browse files

k3s: format with nixfmt-rfc-style

parent 9defb411
Loading
Loading
Loading
Loading
+31 −13
Original line number Diff line number Diff line
{ config, lib, pkgs, ... }:
{
  config,
  lib,
  pkgs,
  ...
}:

with lib;
let
  cfg = config.services.k3s;
  removeOption = config: instruction:
    lib.mkRemovedOptionModule ([ "services" "k3s" ] ++ config) instruction;
  removeOption =
    config: instruction:
    lib.mkRemovedOptionModule (
      [
        "services"
        "k3s"
      ]
      ++ config
    ) instruction;
in
{
  imports = [
    (removeOption [ "docker" ] "k3s docker option is no longer supported.")
  ];
  imports = [ (removeOption [ "docker" ] "k3s docker option is no longer supported.") ];

  # interface
  options.services.k3s = {
@@ -33,7 +43,10 @@ in
        - `serverAddr` is required.
      '';
      default = "server";
      type = types.enum [ "server" "agent" ];
      type = types.enum [
        "server"
        "agent"
      ];
    };

    serverAddr = mkOption {
@@ -125,7 +138,8 @@ in
        message = "serverAddr or configPath (with 'server' key) should be set if role is 'agent'";
      }
      {
        assertion = cfg.role == "agent" -> cfg.configPath != null || cfg.tokenFile != null || cfg.token != "";
        assertion =
          cfg.role == "agent" -> cfg.configPath != null || cfg.tokenFile != null || cfg.token != "";
        message = "token or tokenFile or configPath (with 'token' or 'token-file' keys) should be set if role is 'agent'";
      }
      {
@@ -142,8 +156,14 @@ in

    systemd.services.k3s = {
      description = "k3s service";
      after = [ "firewall.service" "network-online.target" ];
      wants = [ "firewall.service" "network-online.target" ];
      after = [
        "firewall.service"
        "network-online.target"
      ];
      wants = [
        "firewall.service"
        "network-online.target"
      ];
      wantedBy = [ "multi-user.target" ];
      path = optional config.boot.zfs.enabled config.boot.zfs.package;
      serviceConfig = {
@@ -159,9 +179,7 @@ in
        TasksMax = "infinity";
        EnvironmentFile = cfg.environmentFile;
        ExecStart = concatStringsSep " \\\n " (
          [
            "${cfg.package}/bin/k3s ${cfg.role}"
          ]
          [ "${cfg.package}/bin/k3s ${cfg.role}" ]
          ++ (optional cfg.clusterInit "--cluster-init")
          ++ (optional cfg.disableAgent "--disable-agent")
          ++ (optional (cfg.serverAddr != "") "--server ${cfg.serverAddr}")
+11 −7
Original line number Diff line number Diff line
{ system ? builtins.currentSystem
, pkgs ? import ../../.. { inherit system; }
, lib ? pkgs.lib
{
  system ? builtins.currentSystem,
  pkgs ? import ../../.. { inherit system; },
  lib ? pkgs.lib,
}:
let
  allK3s = lib.filterAttrs (n: _: lib.strings.hasPrefix "k3s_" n) pkgs;
in
{
  # Testing K3s with Etcd backend
  etcd = lib.mapAttrs (_: k3s: import ./etcd.nix {
  etcd = lib.mapAttrs (
    _: k3s:
    import ./etcd.nix {
      inherit system pkgs k3s;
      inherit (pkgs) etcd;
  }) allK3s;
    }
  ) allK3s;
  # Run a single node k3s cluster and verify a pod can run
  single-node = lib.mapAttrs (_: k3s: import ./single-node.nix { inherit system pkgs k3s; }) allK3s;
  # Run a multi-node k3s cluster and verify pod networking works across nodes
+115 −85
Original line number Diff line number Diff line
import ../make-test-python.nix ({ pkgs, lib, k3s, etcd, ... }:
import ../make-test-python.nix (
  {
    pkgs,
    lib,
    k3s,
    etcd,
    ...
  }:

  {
    name = "${k3s.name}-etcd";

    nodes = {

    etcd = { ... }: {
      etcd =
        { ... }:
        {
          services.etcd = {
            enable = true;
            openFirewall = true;
        listenClientUrls = [ "http://192.168.1.1:2379" "http://127.0.0.1:2379" ];
            listenClientUrls = [
              "http://192.168.1.1:2379"
              "http://127.0.0.1:2379"
            ];
            listenPeerUrls = [ "http://192.168.1.1:2380" ];
            initialAdvertisePeerUrls = [ "http://192.168.1.1:2380" ];
            initialCluster = [ "etcd=http://192.168.1.1:2380" ];
@@ -18,12 +30,17 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, etcd, ... }:
            useDHCP = false;
            defaultGateway = "192.168.1.1";
            interfaces.eth1.ipv4.addresses = pkgs.lib.mkForce [
          { address = "192.168.1.1"; prefixLength = 24; }
              {
                address = "192.168.1.1";
                prefixLength = 24;
              }
            ];
          };
        };

    k3s = { pkgs, ... }: {
      k3s =
        { pkgs, ... }:
        {
          environment.systemPackages = with pkgs; [ jq ];
          # k3s uses enough resources the default vm fails.
          virtualisation.memorySize = 1536;
@@ -34,28 +51,40 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, etcd, ... }:
            role = "server";
            extraFlags = builtins.toString [
              "--datastore-endpoint=\"http://192.168.1.1:2379\""
          "--disable" "coredns"
          "--disable" "local-storage"
          "--disable" "metrics-server"
          "--disable" "servicelb"
          "--disable" "traefik"
          "--node-ip" "192.168.1.2"
              "--disable"
              "coredns"
              "--disable"
              "local-storage"
              "--disable"
              "metrics-server"
              "--disable"
              "servicelb"
              "--disable"
              "traefik"
              "--node-ip"
              "192.168.1.2"
            ];
          };

          networking = {
            firewall = {
          allowedTCPPorts = [ 2379 2380 6443 ];
              allowedTCPPorts = [
                2379
                2380
                6443
              ];
              allowedUDPPorts = [ 8472 ];
            };
            useDHCP = false;
            defaultGateway = "192.168.1.2";
            interfaces.eth1.ipv4.addresses = pkgs.lib.mkForce [
          { address = "192.168.1.2"; prefixLength = 24; }
              {
                address = "192.168.1.2";
                prefixLength = 24;
              }
            ];
          };
        };

    };

    testScript = ''
@@ -97,4 +126,5 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, etcd, ... }:
    '';

    meta.maintainers = etcd.meta.maintainers ++ k3s.meta.maintainers;
})
  }
)
+144 −82
Original line number Diff line number Diff line
import ../make-test-python.nix ({ pkgs, lib, k3s, ... }:
import ../make-test-python.nix (
  {
    pkgs,
    lib,
    k3s,
    ...
  }:
  let
    imageEnv = pkgs.buildEnv {
      name = "k3s-pause-image-env";
      paths = with pkgs; [ tini bashInteractive coreutils socat ];
      paths = with pkgs; [
        tini
        bashInteractive
        coreutils
        socat
      ];
    };
    pauseImage = pkgs.dockerTools.streamLayeredImage {
      name = "test.local/pause";
      tag = "local";
      contents = imageEnv;
      config.Entrypoint = [ "/bin/tini" "--" "/bin/sleep" "inf" ];
      config.Entrypoint = [
        "/bin/tini"
        "--"
        "/bin/sleep"
        "inf"
      ];
    };
    # A daemonset that responds 'server' on port 8000
    networkTestDaemonset = pkgs.writeText "test.yml" ''
@@ -42,8 +58,13 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, ... }:
    name = "${k3s.name}-multi-node";

    nodes = {
      server = { pkgs, ... }: {
        environment.systemPackages = with pkgs; [ gzip jq ];
      server =
        { pkgs, ... }:
        {
          environment.systemPackages = with pkgs; [
            gzip
            jq
          ];
          # k3s uses enough resources the default vm fails.
          virtualisation.memorySize = 1536;
          virtualisation.diskSize = 4096;
@@ -55,27 +76,46 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, ... }:
            package = k3s;
            clusterInit = true;
            extraFlags = builtins.toString [
            "--disable" "coredns"
            "--disable" "local-storage"
            "--disable" "metrics-server"
            "--disable" "servicelb"
            "--disable" "traefik"
            "--node-ip" "192.168.1.1"
            "--pause-image" "test.local/pause:local"
              "--disable"
              "coredns"
              "--disable"
              "local-storage"
              "--disable"
              "metrics-server"
              "--disable"
              "servicelb"
              "--disable"
              "traefik"
              "--node-ip"
              "192.168.1.1"
              "--pause-image"
              "test.local/pause:local"
            ];
          };
        networking.firewall.allowedTCPPorts = [ 2379 2380 6443 ];
          networking.firewall.allowedTCPPorts = [
            2379
            2380
            6443
          ];
          networking.firewall.allowedUDPPorts = [ 8472 ];
          networking.firewall.trustedInterfaces = [ "flannel.1" ];
          networking.useDHCP = false;
          networking.defaultGateway = "192.168.1.1";
          networking.interfaces.eth1.ipv4.addresses = pkgs.lib.mkForce [
          { address = "192.168.1.1"; prefixLength = 24; }
            {
              address = "192.168.1.1";
              prefixLength = 24;
            }
          ];
        };

      server2 = { pkgs, ... }: {
        environment.systemPackages = with pkgs; [ gzip jq ];
      server2 =
        { pkgs, ... }:
        {
          environment.systemPackages = with pkgs; [
            gzip
            jq
          ];
          virtualisation.memorySize = 1536;
          virtualisation.diskSize = 4096;

@@ -85,26 +125,42 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, ... }:
            serverAddr = "https://192.168.1.1:6443";
            clusterInit = false;
            extraFlags = builtins.toString [
            "--disable" "coredns"
            "--disable" "local-storage"
            "--disable" "metrics-server"
            "--disable" "servicelb"
            "--disable" "traefik"
            "--node-ip" "192.168.1.3"
            "--pause-image" "test.local/pause:local"
              "--disable"
              "coredns"
              "--disable"
              "local-storage"
              "--disable"
              "metrics-server"
              "--disable"
              "servicelb"
              "--disable"
              "traefik"
              "--node-ip"
              "192.168.1.3"
              "--pause-image"
              "test.local/pause:local"
            ];
          };
        networking.firewall.allowedTCPPorts = [ 2379 2380 6443 ];
          networking.firewall.allowedTCPPorts = [
            2379
            2380
            6443
          ];
          networking.firewall.allowedUDPPorts = [ 8472 ];
          networking.firewall.trustedInterfaces = [ "flannel.1" ];
          networking.useDHCP = false;
          networking.defaultGateway = "192.168.1.3";
          networking.interfaces.eth1.ipv4.addresses = pkgs.lib.mkForce [
          { address = "192.168.1.3"; prefixLength = 24; }
            {
              address = "192.168.1.3";
              prefixLength = 24;
            }
          ];
        };

      agent = { pkgs, ... }: {
      agent =
        { pkgs, ... }:
        {
          virtualisation.memorySize = 1024;
          virtualisation.diskSize = 2048;
          services.k3s = {
@@ -113,8 +169,10 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, ... }:
            role = "agent";
            serverAddr = "https://192.168.1.3:6443";
            extraFlags = lib.concatStringsSep " " [
            "--pause-image" "test.local/pause:local"
            "--node-ip" "192.168.1.2"
              "--pause-image"
              "test.local/pause:local"
              "--node-ip"
              "192.168.1.2"
            ];
          };
          networking.firewall.allowedTCPPorts = [ 6443 ];
@@ -123,7 +181,10 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, ... }:
          networking.useDHCP = false;
          networking.defaultGateway = "192.168.1.2";
          networking.interfaces.eth1.ipv4.addresses = pkgs.lib.mkForce [
          { address = "192.168.1.2"; prefixLength = 24; }
            {
              address = "192.168.1.2";
              prefixLength = 24;
            }
          ];
        };
    };
@@ -178,4 +239,5 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, ... }:
      for m in machines:
          m.shutdown()
    '';
  })
  }
)
+84 −55
Original line number Diff line number Diff line
import ../make-test-python.nix ({ pkgs, lib, k3s, ... }:
import ../make-test-python.nix (
  {
    pkgs,
    lib,
    k3s,
    ...
  }:
  let
    imageEnv = pkgs.buildEnv {
      name = "k3s-pause-image-env";
      paths = with pkgs; [ tini (hiPrio coreutils) busybox ];
      paths = with pkgs; [
        tini
        (hiPrio coreutils)
        busybox
      ];
    };
    pauseImage = pkgs.dockerTools.streamLayeredImage {
      name = "test.local/pause";
      tag = "local";
      contents = imageEnv;
      config.Entrypoint = [ "/bin/tini" "--" "/bin/sleep" "inf" ];
      config.Entrypoint = [
        "/bin/tini"
        "--"
        "/bin/sleep"
        "inf"
      ];
    };
    testPodYaml = pkgs.writeText "test.yml" ''
      apiVersion: v1
@@ -27,8 +42,13 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, ... }:
    name = "${k3s.name}-single-node";
    meta.maintainers = k3s.meta.maintainers;

    nodes.machine = { pkgs, ... }: {
      environment.systemPackages = with pkgs; [ k3s gzip ];
    nodes.machine =
      { pkgs, ... }:
      {
        environment.systemPackages = with pkgs; [
          k3s
          gzip
        ];

        # k3s uses enough resources the default vm fails.
        virtualisation.memorySize = 1536;
@@ -39,12 +59,18 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, ... }:
        services.k3s.package = k3s;
        # Slightly reduce resource usage
        services.k3s.extraFlags = builtins.toString [
        "--disable" "coredns"
        "--disable" "local-storage"
        "--disable" "metrics-server"
        "--disable" "servicelb"
        "--disable" "traefik"
        "--pause-image" "test.local/pause:local"
          "--disable"
          "coredns"
          "--disable"
          "local-storage"
          "--disable"
          "metrics-server"
          "--disable"
          "servicelb"
          "--disable"
          "traefik"
          "--pause-image"
          "test.local/pause:local"
        ];

        users.users = {
@@ -56,14 +82,16 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, ... }:
        };
      };

    testScript = ''
    testScript =
      ''
        start_all()

        machine.wait_for_unit("k3s")
        machine.succeed("kubectl cluster-info")
        machine.fail("sudo -u noprivs kubectl cluster-info")
      '' # Fix-Me: Tests fail for 'aarch64-linux' as: "CONFIG_CGROUP_FREEZER: missing (fail)"
      + lib.optionalString (!pkgs.stdenv.isAarch64) ''machine.succeed("k3s check-config")'' + ''
      + lib.optionalString (!pkgs.stdenv.isAarch64) ''machine.succeed("k3s check-config")''
      + ''

        machine.succeed(
            "${pauseImage} | ctr image import -"
@@ -92,4 +120,5 @@ import ../make-test-python.nix ({ pkgs, lib, k3s, ... }:

        machine.shutdown()
      '';
  })
  }
)
Loading