From 466cbe50b0a6f407a2a5b12ddcca0015967eacb6 Mon Sep 17 00:00:00 2001
From: Damien Lespiau <damien@weave.works>
Date: Mon, 1 Oct 2018 17:06:16 +0100
Subject: [PATCH] contrib/kube-prometheus: Add a script to sync images to an
 internal registry

Crazy at it sounds, some Kubernetes installations don't have access to Internet
and source all their images from an internal registry.

sync-to-internal-registry.jsonnet is a jsonnet snippet that helps with the task
of pushing upstream images used by the prometheus operator to an internal
registry by printing the right docker pull/tag/push commands.

$ jsonnet -J vendor -S --tla-str repository=internal-registry.com/organization sync-to-internal-registry.jsonnet
docker pull quay.io/coreos/addon-resizer:1.0
docker tag quay.io/coreos/addon-resizer:1.0 internal-registry.com/organization/addon-resizer:1.0
docker push internal-registry.com/organization/addon-resizer:1.0
docker pull quay.io/prometheus/alertmanager:v0.15.2
docker tag quay.io/prometheus/alertmanager:v0.15.2 internal-registry.com/organization/alertmanager:v0.15.2
docker push internal-registry.com/organization/alertmanager:v0.15.2
...
---
 jsonnet/kube-prometheus/lib/image.libsonnet | 21 +++++++++++++++
 jsonnet/kube-prometheus/lib/lib.libsonnet   |  1 +
 sync-to-internal-registry.jsonnet           | 30 +++++++++++++++++++++
 3 files changed, 52 insertions(+)
 create mode 100644 jsonnet/kube-prometheus/lib/image.libsonnet
 create mode 100644 jsonnet/kube-prometheus/lib/lib.libsonnet
 create mode 100644 sync-to-internal-registry.jsonnet

diff --git a/jsonnet/kube-prometheus/lib/image.libsonnet b/jsonnet/kube-prometheus/lib/image.libsonnet
new file mode 100644
index 00000000..0561e33c
--- /dev/null
+++ b/jsonnet/kube-prometheus/lib/image.libsonnet
@@ -0,0 +1,21 @@
+// imageName extracts the image name from a fully qualified image string. eg.
+// quay.io/coreos/addon-resizer -> addon-resizer
+// grafana/grafana -> grafana
+local imageName(image) =
+  local parts = std.split(image, '/');
+  local len = std.length(parts);
+  if len == 3 then
+    # registry.com/org/image
+    parts[2]
+  else if len == 2 then
+    # org/image
+    parts[1]
+  else if len == 1 then
+    # image, ie. busybox
+    parts[0]
+  else
+      error 'unknown image format: ' + image;
+
+{
+  imageName:: imageName,
+}
diff --git a/jsonnet/kube-prometheus/lib/lib.libsonnet b/jsonnet/kube-prometheus/lib/lib.libsonnet
new file mode 100644
index 00000000..c30f976f
--- /dev/null
+++ b/jsonnet/kube-prometheus/lib/lib.libsonnet
@@ -0,0 +1 @@
+(import 'image.libsonnet')
diff --git a/sync-to-internal-registry.jsonnet b/sync-to-internal-registry.jsonnet
new file mode 100644
index 00000000..f0cf35ae
--- /dev/null
+++ b/sync-to-internal-registry.jsonnet
@@ -0,0 +1,30 @@
+local kp = import 'kube-prometheus/kube-prometheus.libsonnet';
+local l = import 'kube-prometheus/lib/lib.libsonnet';
+local config = kp._config;
+
+local makeImages(config) = [
+    {
+        name: config.imageRepos[image],
+        tag: config.versions[image],
+    }
+    for image in std.objectFields(config.imageRepos)
+];
+
+local upstreamImage(image) = '%s:%s' % [image.name, image.tag];
+local downstreamImage(registry, image) = '%s/%s:%s' % [registry, l.imageName(image.name), image.tag];
+
+local pullPush(image, newRegistry) = [
+    'docker pull %s' % upstreamImage(image),
+    'docker tag %s %s' % [upstreamImage(image), downstreamImage(newRegistry, image)],
+    'docker push %s' % downstreamImage(newRegistry, image),
+];
+
+local images = makeImages(config);
+
+local output(repository) = std.flattenArrays([
+  pullPush(image, repository)
+  for image in images
+]);
+
+function(repository="my-registry.com/repository")
+    std.join('\n', output(repository))
-- 
GitLab