Can't update the image of CronJob with python library ( patch_namespaced_cron_job )

12/27/2019

What happened:

I'm creating a deploy in AWS with lambda for set the images in Kubernetes Cluster.

I want set image in CronJob:

Cron:

NAME                                SCHEDULE      SUSPEND   ACTIVE  
LAST SCHEDULE   AGE cronjob.batch/analytics-staging-0   */5 * * * *  
False     0        4m23s           144m Describe cronjob:

 Name:                          analytics-staging-0 Namespace:         
 staging-app Labels:                        app=analytics-staging-0
 Annotations:                   <none> Schedule:                     
 */5 * * * * Concurrency Policy:            Replace Suspend:                       False Successful Job History Limit:  824641908604 Failed Job History
 Limit:      1 Starting Deadline Seconds:     <unset> Selector:        
 <unset> Parallelism:                   1 Completions:                 
 1 Pod Template:   Labels:  <none>   Containers:   
 analytics-staging-0:
     Image:      accountid.dkr.ecr.eu-west-1.amazonaws.com/app:latest
     Port:       <none>
     Host Port:  <none>
     Command:
       /opt/app/analytics
     Environment:
       AWS_REGION:    eu-west-1
     Mounts:          <none>   Volumes:           <none> Last Schedule Time:  Tue, 24 Dec 2019 15:20:00 +0100 Active Jobs:        
 analytics-staging-0-1577197200

Python Code:

 def set_image_job(image, namespace, name, conn):
     api_instance = conn.BatchV1beta1Api()

     body = {
         "spec": {
             "template": {
                 "spec": {
                     "containers": [
                         {
                             "image": image
                         }
                     ]
                 }
             }
         }
     }

     try:
         response = api_instance.patch_namespaced_cron_job(name, namespace, body, pretty=True)
         print(response)
     except Exception as e:
         print("Exception when calling BatchV2alpha1Api->patch_namespaced_cron_job: %s\n" % e)
if __name__ == "__main__":
     conn = get_connection_eks()
     eks_resource = EKSResources('staging-app', conn)
     jobs = eks_resource.get_cron_jobs()
     for item in jobs:
         set_image_job('accountid.dkr.ecr.eu-west-1.amazonaws.com/app:latest',
 'staging-app', item, conn)

Note: item = analytics-staging-0

The response is ok, but don't change the image.

Can you help me?

Thanks!

Environment:

Kubernetes version (kubectl version):

Client Version: version.Info{Major:"1", Minor:"14",
GitVersion:"v1.14.0",
GitCommit:"641856db18352033a0d96dbc99153fa3b27298e5",
GitTreeState:"clean", BuildDate:"2019-03-25T15:53:57Z",
GoVersion:"go1.12.1", Compiler:"gc", Platform:"darwin/amd64"} Server
Version: version.Info{Major:"1", Minor:"14+",
GitVersion:"v1.14.9-eks-c0eccc",
GitCommit:"c0eccca51d7500bb03b2f163dd8d534ffeb2f7a2",
GitTreeState:"clean", BuildDate:"2019-12-22T23:14:11Z",
GoVersion:"go1.12.12", Compiler:"gc", Platform:"linux/amd64"}

Python version (python --version)

Python 3.7

-- ricarzd
aws-eks
kubernetes
python

0 Answers