GoogleCloudPlatform / deploymentmanager-samples

Deployment Manager samples and templates.
Apache License 2.0
935 stars 716 forks source link

The template produces different results with Python3 and Python2 #519

Open davidebelloni opened 4 years ago

davidebelloni commented 4 years ago

Hi, I'm trying to apply this simple template Jinja:

{% set LIST_LABELS = env['project'].split('-') %}
{% set BASE_LABELS = dict() %}
{% set x=BASE_LABELS.__setitem__('application',LIST_LABELS[0].encode('utf-8')) %}
{% set x=BASE_LABELS.__setitem__('landscape',LIST_LABELS[1].encode('utf-8')) %}

- name: test
  type: gcp-types/pubsub-v1:projects.topics
  properties:
    topic: test
    labels: {{ BASE_LABELS }}

but gcloud command return the following error:

code: NOT_CRITICAL_ERROR
message: |-
  The template produces different results with Python3 and Python2. This could be because of the non-determinism of your configuration, or a potential incompatibility with Python3. Make sure that your templates are compatible with Python3.
  https://cloud.google.com/deployment-manager/docs/migrate-to-python3

How can I check the different results and resolve them?

OS/gcloud info:

$ system_profiler SPSoftwareDataType
Software:

    System Software Overview:

      System Version: macOS 10.14.6 (18G2022)
      Kernel Version: Darwin 18.7.0
      ...

$ gcloud version
Google Cloud SDK 274.0.1
alpha 2019.05.17
app-engine-python 1.9.87
beta 2019.05.17
bq 2.0.51
cloud-datastore-emulator 2.1.0
cloud_sql_proxy 
core 2019.12.27
datalab 20190610
docker-credential-gcr 
gcloud 
gsutil 4.46
kubectl 2019.11.04

Thanks you

davidebelloni commented 4 years ago

Solved with

{% set LIST_LABELS = env['project'].split('-') %}
{% set BASE_LABELS = dict() %}
{% set x=BASE_LABELS.__setitem__('application',LIST_LABELS[0]) %}
{% set x=BASE_LABELS.__setitem__('landscape',LIST_LABELS[1]) %}

- name: test
  type: gcp-types/pubsub-v1:projects.topics
  properties:
    topic: test
    labels: {{ BASE_LABELS|tojson }}
davidebelloni commented 4 years ago

Now I've the same problem with the cloud_function.py template ... I've converted the line

cmd = "echo '%s' | base64 -d > /function/function.zip;" % (content)

in

cmd = "echo '%s' | base64 -d > /function/function.zip;" % (content.decode('ascii'))

but is not a definitive solution

andudo commented 4 years ago

Both are good fixes. It performs the same in both Python2 and Python3. But it would be better to use 'utf-8'.

Feel free to send a PR for cloud_function template. Or I'll later update it together with others that might have the same issue.

dinvlad commented 4 years ago

Same issue here - how can we decipher The template produces different results with Python3 and Python2 message in a general case? It can be quite hard to understand what's different between those, esp. if the templates are using jinja..