k8s/gke/gcr - scope an image to a namespace

7/28/2017

I have a gke cluster with a number of different namespaces. I would like to be able to in effect namespace my images the same way my other resources are namespaced. That is, I would like pods in different namespaces to be able to reference an image using the same name but for them to get different images depending on which namespace they are in. One way to achieve this (if it were supported) might be to substitute the name of the namespace into the image name in the yml, eg:

containers:
- image: eu.gcr.io/myproject/$(NAMESPACE)-myimage
  name: myimage

Then I could push eu.gcr.io/myproject/mynamespace-myimage to make my image available to namespace mynamespace.

Is there any tidy way to achieve this kind of thing. If not, and since I've been unable to find anybody else asking similar questions, is there some way in which this is a bad thing to want to do?

-- Tom
google-container-registry
google-kubernetes-engine
kubernetes

1 Answer

7/28/2017

I don't think this is possible. Kubernetes supports expansion on fields like command and args. This lets you use substitutions with a variable set in container's env field, which can come from configmap/secret. Example: https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#use-environment-variables-to-define-arguments

However I don't think variable expansion works for the image field. :(

What you're trying to do does not seem like a great idea: Having different images across multiple environments defeats the purpose of having test/staging environments.

Instead you should probably should use the same image to test on all platforms, by changing env vars, configMaps etc.

-- AhmetB - Google
Source: StackOverflow