git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #37871 from Ma27/docker-registry-enhancements

nixos/docker-registry: add configuration options to support image deletions

commitb28d151ac9b945f165b207357c69bc27bc71cfcd authorRobin Gloster <mail@glob.in> dateWed 2 May 2018, 13:15 (9 years ago) parent78f09c9fe9096e
3 files changed+117−9
nixos/doc/manual/release-notes/rl-1809.xml+7 nixos/modules/services/misc/docker-registry.nix+89−8 nixos/tests/docker-registry.nix+21−1
nixos/doc/manual/release-notes/rl-1809.xml+7
@@ -94,6 +94,13 @@ $ nix-instantiate -E '(import &lt;nixpkgsunstable&gt; {}).gitFull'
9494 accepted by the nc command.
9595 </para>
9696 </listitem>
97+ <listitem>
98+ <para>
99+ The <varname>services.docker-registry.extraConfig</varname> object doesn't contain
100+ environment variables anymore. Instead it needs to provide an object structure
101+ that can be mapped onto the YAML configuration defined in <link xlink:href="https://github.com/docker/distribution/blob/v2.6.2/docs/configuration.md">the <varname>docker/distribution</varname> docs</link>.
102+ </para>
103+ </listitem>
97104 </itemizedlist>
98105 </section>
99106
nixos/modules/services/misc/docker-registry.nix+89−8
@@ -5,6 +5,45 @@ with lib;
55let
66 cfg = config.services.dockerRegistry;
77
8+ blobCache = if cfg.enableRedisCache
9+ then "redis"
10+ else "inmemory";
11+
12+ registryConfig = {
13+ version = "0.1";
14+ log.fields.service = "registry";
15+ storage = {
16+ cache.blobdescriptor = blobCache;
17+ filesystem.rootdirectory = cfg.storagePath;
18+ delete.enabled = cfg.enableDelete;
19+ };
20+ http = {
21+ addr = ":${builtins.toString cfg.port}";
22+ headers.X-Content-Type-Options = ["nosniff"];
23+ };
24+ health.storagedriver = {
25+ enabled = true;
26+ interval = "10s";
27+ threshold = 3;
28+ };
29+ };
30+
31+ registryConfig.redis = mkIf cfg.enableRedisCache {
32+ addr = "${cfg.redisUrl}";
33+ password = "${cfg.redisPassword}";
34+ db = 0;
35+ dialtimeout = "10ms";
36+ readtimeout = "10ms";
37+ writetimeout = "10ms";
38+ pool = {
39+ maxidle = 16;
40+ maxactive = 64;
41+ idletimeout = "300s";
42+ };
43+ };
44+
45+ configFile = pkgs.writeText "docker-registry-config.yml" (builtins.toJSON (registryConfig // cfg.extraConfig));
46+
847in {
948 options.services.dockerRegistry = {
1049 enable = mkEnableOption "Docker Registry";
@@ -27,6 +66,26 @@ in {
2766 description = "Docker registry storage path.";
2867 };
2968
69+ enableDelete = mkOption {
70+ type = types.bool;
71+ default = false;
72+ description = "Enable delete for manifests and blobs.";
73+ };
74+
75+ enableRedisCache = mkEnableOption "redis as blob cache";
76+
77+ redisUrl = mkOption {
78+ type = types.str;
79+ default = "localhost:6379";
80+ description = "Set redis host and port.";
81+ };
82+
83+ redisPassword = mkOption {
84+ type = types.str;
85+ default = "";
86+ description = "Set redis password.";
87+ };
88+
3089 extraConfig = mkOption {
3190 description = ''
3291 Docker extra registry configuration via environment variables.
@@ -34,6 +93,19 @@ in {
3493 default = {};
3594 type = types.attrsOf types.str;
3695 };
96+
97+ enableGarbageCollect = mkEnableOption "garbage collect";
98+
99+ garbageCollectDates = mkOption {
100+ default = "daily";
101+ type = types.str;
102+ description = ''
103+ Specification (in the format described by
104+ <citerefentry><refentrytitle>systemd.time</refentrytitle>
105+ <manvolnum>7</manvolnum></citerefentry>) of the time at
106+ which the garbage collect will occur.
107+ '';
108+ };
37109 };
38110
39111 config = mkIf cfg.enable {
@@ -41,15 +113,8 @@ in {
41113 description = "Docker Container Registry";
42114 wantedBy = [ "multi-user.target" ];
43115 after = [ "network.target" ];
44−
45− environment = {
46− REGISTRY_HTTP_ADDR = "${cfg.listenAddress}:${toString cfg.port}";
47− REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY = cfg.storagePath;
48− } // cfg.extraConfig;
49−
50116 script = ''
51− ${pkgs.docker-distribution}/bin/registry serve \
52− ${pkgs.docker-distribution.out}/share/go/src/github.com/docker/distribution/cmd/registry/config-example.yml
117+ ${pkgs.docker-distribution}/bin/registry serve ${configFile}
53118 '';
54119
55120 serviceConfig = {
@@ -58,6 +123,22 @@ in {
58123 };
59124 };
60125
126+ systemd.services.docker-registry-garbage-collect = {
127+ description = "Run Garbage Collection for docker registry";
128+
129+ restartIfChanged = false;
130+ unitConfig.X-StopOnRemoval = false;
131+
132+ serviceConfig.Type = "oneshot";
133+
134+ script = ''
135+ ${pkgs.docker-distribution}/bin/registry garbage-collect ${configFile}
136+ ${pkgs.systemd}/bin/systemctl restart docker-registry.service
137+ '';
138+
139+ startAt = optional cfg.enableGarbageCollect cfg.garbageCollectDates;
140+ };
141+
61142 users.extraUsers.docker-registry = {
62143 createHome = true;
63144 home = cfg.storagePath;
nixos/tests/docker-registry.nix+21−1
@@ -3,14 +3,16 @@
33import ./make-test.nix ({ pkgs, ...} : {
44 name = "docker-registry";
55 meta = with pkgs.stdenv.lib.maintainers; {
6− maintainers = [ globin ];
6+ maintainers = [ globin ma27 ironpinguin ];
77 };
88
99 nodes = {
1010 registry = { config, pkgs, ... }: {
1111 services.dockerRegistry.enable = true;
12+ services.dockerRegistry.enableDelete = true;
1213 services.dockerRegistry.port = 8080;
1314 services.dockerRegistry.listenAddress = "0.0.0.0";
15+ services.dockerRegistry.enableGarbageCollect = true;
1416 networking.firewall.allowedTCPPorts = [ 8080 ];
1517 };
1618
@@ -33,11 +35,29 @@ import ./make-test.nix ({ pkgs, ...} : {
3335
3436 $registry->start();
3537 $registry->waitForUnit("docker-registry.service");
38+ $registry->waitForOpenPort("8080");
3639 $client1->succeed("docker push registry:8080/scratch");
3740
3841 $client2->start();
3942 $client2->waitForUnit("docker.service");
4043 $client2->succeed("docker pull registry:8080/scratch");
4144 $client2->succeed("docker images | grep scratch");
45+
46+ $client2->succeed(
47+ 'curl -fsS -X DELETE registry:8080/v2/scratch/manifests/$(curl -fsS -I -H"Accept: application/vnd.docker.distribution.manifest.v2+json" registry:8080/v2/scratch/manifests/latest | grep Docker-Content-Digest | sed -e \'s/Docker-Content-Digest: //\' | tr -d \'\r\')'
48+ );
49+
50+ $registry->systemctl("start docker-registry-garbage-collect.service");
51+ $registry->waitUntilFails("systemctl status docker-registry-garbage-collect.service");
52+ $registry->waitForUnit("docker-registry.service");
53+
54+ $registry->fail(
55+ 'ls -l /var/lib/docker-registry/docker/registry/v2/blobs/sha256/*/*/data'
56+ );
57+
58+ $client1->succeed("docker push registry:8080/scratch");
59+ $registry->succeed(
60+ 'ls -l /var/lib/docker-registry/docker/registry/v2/blobs/sha256/*/*/data'
61+ );
4262 '';
4363})