'Adding time stamp to Kubernetes deployment with latest tag

I am trying to implement Kubernetes resource deployment. For this I created deployment.yaml and service.yaml as my Kubernetes reources. And refering the dockerhub registry image with latest tag in my deployment.yaml. When I am doing like this, latest image is not pulling withlatest` tag.

So I added the time stamp in deployment like following:

template:
 metadata:
  labels:
    app: test-kube-deployment
    date: date "+%H:%M:%S   %d/%m/%y"
spec:
  imagePullSecrets:
    - name: "regcred"
  containers:
   - name: test-kube-deployment-container
     image: spacestudymilletech010/spacestudykubernetes:latest
     imagePullPolicy: Always
     ports:
        - name: http
          containerPort: 8085
          protocol: TCP

When I added time here , I am getting the error like the following,

The Deployment "test-kube-deployment" is invalid: spec.template.labels: Invalid value: "date \"+%H:%M:%S   %d/%m/%y\"": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character 

Here what modification that I need to do to define my time stamp for uniquely identify my Docker image?



Solution 1:[1]

Don't add it as a label, but rather as an annotation.

template: 
  metadata: 
    labels: 
      app: test-kube-deployment 
    annotations: 
      date: "+%H:%M:%S %d/%m/%y"

PD.- You are getting the error because labels don't accept the +, % and : characters.

Solution 2:[2]

I've faced same issue and solved it using annotation. As label not accept the special characters.

kubectl patch deployment deploy_name --patch '{"spec": {"template": {"metadata": {"annotations": {"last_update": '"\"$(date '+%Y%m%d%H%M%S')\""'}}}}}';

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rodrigo Loza
Solution 2 Andrew