2
votes

I have a deployment comprising a managed instance group and two instance templates (A and B). The deployment was initially created with the instance group referencing instance template A.

I tried updating the sourceImage in instance template B using deployment manager (gcloud beta deployment-manager deployments update my-deployment --template ...), but got the following error:

ERROR: (gcloud.beta.deployment-manager.deployments.update) Error in 
Operation [operation-1538798895713-57787898f4ae9-8b478716-0bb72a09]: 
errors:
- code: NO_METHOD_TO_UPDATE_FIELD
  message: No method found to update field 'properties' on 
  resource 'fwp-app-preprod-instance-template-a' of type 
  'compute.v1.instanceTemplate'. The resource may need to be
  recreated with the new field.

I should make it clear that the only change I made from the original deployment is the instance template's sourceImage.

Is it possible to perform an update of an instance template via deployment manager so that it references an updated sourceImage?

The error states clearly that the resource (instance template) may need to be recreated, and I'm happy for deployment manager to do that. But I have no idea how to instruct/force deployment manager to take that action.

I don't doubt it can be done outside of deployment manager, but I want to avoid configuration drift.

My app.jinja.schema:

imports:
- path: instance-group.jinja
- path: instance-template.jinja

My app.jinja:

resources:
- name: instance-template-a
  type: instance-template.jinja
  properties:
    name: {{ env["deployment"] }}-instance-template-a
    machineType: g1-small
    sourceImage: "projects/my-project/global/images/my-image"
    diskSizeGb: '30'

- name: instance-template-b
  type: instance-template.jinja
  properties:
    name: {{ env["deployment"] }}-instance-template-b
    machineType: g1-small
    sourceImage: "projects/my-project/global/images/my-image"
    diskSizeGb: '30'

- name: fwp-instance-group
  type: instance-group.jinja

My instance-group.jinja:

resources:
- name: 'instance-group-{{ env["deployment"] }}'
  type: compute.v1.regionInstanceGroupManager
  properties:
    baseInstanceName: ig-instance-{{ env["deployment"] }}
    instanceTemplate: '$(ref.{{ env["deployment"] }}-instance-template-a.selfLink)'
    targetSize: 1
    region: australia-southeast1

- name: 'autoscaler-{{ env["deployment"] }}'
  type: compute.v1.regionAutoscalers
  properties:
    autoscalingPolicy:
      coolDownPeriodSec: 60
      loadBalancingUtilization:
        utilizationTarget: 0.9
      maxNumReplicas: 10
      minNumReplicas: 2
    target: $(ref.instance-group-{{ env["deployment"] }}.selfLink)
    region: australia-southeast1

And my instance-template.jinja

resources:
- name: {{ properties["name"] }}
  type: compute.v1.instanceTemplate
  properties:
    name: {{ properties["name"] }}
    description: ''
    properties:
      machineType: {{ properties["machineType"] }}
      tags:
        items:
        - no-ip
        - web-server
        - http-server
        - https-server
      disks:
      - type: 'PERSISTENT'
        boot: true
        mode: 'READ_WRITE'
        autoDelete: true
        deviceName: instance-device
        initializeParams:
          sourceImage: {{ properties["sourceImage"] }}
          diskType: 'pd-standard'
          diskSizeGb: {{ properties["diskSizeGb"] }}
      canIpForward: false
      networkInterfaces:
      - network: projects/my-project/global/networks/vpc-fwp-nonprod
        subnetwork: projects/my-project/regions/australia-southeast1/subnetworks/subnet-private-fwp-nonprod
        aliasIpRanges: []
      labels: { environment: {{ env["deployment"] }}, tenancy: "fwp-nonprod" }
      scheduling:
        preemptible: false
        onHostMaintenance: MIGRATE
        automaticRestart: true
        nodeAffinities: []
      serviceAccounts:
      - email: [email protected]
        scopes:
        - https://www.googleapis.com/auth/cloud-platform
1
can you provide the config file (with private info redacted)Patrick W
Originally thought it was too much code, but after stripping out the irrelevant front-end stuff (load balancer etc), it's not so bad. ThanksPoo Bah
so to confirm, the only change you made is to the sourceImage field of your app.jinja file, correct?Patrick W
I suspect that might be the issue. Instance templates are immutable once created, I suspect that your deployment might be interacting with the compute API to update the template and this you are getting that error messagePatrick W
Ok I understand that now. So let's say I do a rolling update using the managed instance group command-line to update a sourceImage, and that results in a new instance template being created. If I subsequently want to update my deployment to (for example) to tweak the autoscaler configuration, I'm guessing DM will try to revert to my previous template - which is not what I want. And that is exactly the configuration drift I wanted to avoid. Thanks again Patrick for your help.Poo Bah

1 Answers

1
votes

To recap the comments:

The DM config includes an instance template for the managed instance group. The change of source image is attempting to change the image used in the template.

Unfortunately, instance templates are immutable once created

"So it is not possible to update an existing instance template or change an instance template after it has been created."

This explains the error message returned. The proper way to change the image you want to use for a Managed Instance Group is to create a new template and perform a rolling update on the group and using the new instance template.