kube: remove postgres_v, add versionedNames to postgres

Change-Id: Ia9a20efa1f8c7279cff836440c2d9214a749f5ba
Reviewed-on: https://gerrit.hackerspace.pl/c/hscloud/+/1789
Reviewed-by: informatic <informatic@hackerspace.pl>
Reviewed-by: radex <radex@hackerspace.pl>
diff --git a/kube/postgres.libsonnet b/kube/postgres.libsonnet
index 40f43dc..9ae1ccf 100644
--- a/kube/postgres.libsonnet
+++ b/kube/postgres.libsonnet
@@ -11,7 +11,9 @@
         storageClassName: "waw-hdd-paranoid-2",
         prefix: "", # if set, should be 'foo-'
 
-        image: "postgres:10.4",
+        version: "10.4", # valid version tag for https://hub.docker.com/_/postgres/
+        image: "postgres:" + self.version,
+
         database: error "database must be set",
         username: error "username must be set",
         # not literal, instead ref for env (like { secretKeyRef: ... })
@@ -53,16 +55,23 @@
             #   postgres:1.2-suffix, postgres:1-suffix, postgres:1.2, postgres:1
             to: std.native('regexSubst')("^[^:]+:([^.]+).*$", cfg.image, "${1}"),
         },
+
+        # If set to true, resources will be suffixed with postgres version (and will have versioned labels)
+        # This exists solely for backwards compatibility with old postgres_v libsonnet
+        # and should not be used in new deployments
+        versionedNames: false,
     },
 
-    makeName(suffix):: cfg.prefix + suffix,
+    safeVersion:: std.strReplace(cfg.version, ".", "-"),
+    makeName(suffix):: cfg.prefix + suffix + (if cfg.versionedNames then postgres.safeVersion else ""),
 
     metadata:: {
         namespace: cfg.namespace,
         labels: {
             "app.kubernetes.io/name": cfg.appName,
             "app.kubernetes.io/managed-by": "kubecfg",
-            "app.kubernetes.io/component": "postgres",
+            "app.kubernetes.io/component": if cfg.versionedNames then "postgres_v" else "postgres",
+            [if cfg.versionedNames then "hswaw.net/postgres-version" else null]: postgres.safeVersion,
         },
     },