aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

[cloudformation-diff] cdk diff does not show diff with v1.60.0 or later if there is "DependsOn" key

Open atakidev opened this issue 6 years ago • 0 comments

cdk diff does not show diff with v1.60.0 or later version if there is "DependsOn" key because of the following change:

  • cloudformation-diff: DependsOn singleton arrays aren't equal to string values (#9814) (49cdb47) ( see 1.60.0 in https://github.com/aws/aws-cdk/blob/master/CHANGELOG.md#1600-2020-08-19 )

I confirmed that the change added dependsOnEqual for 'DependsOn' key in packages/@aws-cdk/cloudformation-diff/lib/diff/util.ts . However, the code ignores remained keys if there is DependsOn key because of return dependsOnEqual(... snip ...). So, I think this is a regression issue by the above change.

Reproduction Steps

  1. Setup an app.

$ cdk init app --language=typescript

  1. Create like the following code in lib.
import * as cdk from '@aws-cdk/core';
import * as ecs from '@aws-cdk/aws-ecs';

export class CdkTestStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const taskdef = new ecs.TaskDefinition(this, 'CDKTaskDefinition', {
      "compatibility": ecs.Compatibility.EC2,
      "cpu": "1024",
      "memoryMiB":"1024",
    });

    const contdef = taskdef.addContainer('my-app', {
      "image": ecs.ContainerImage.fromRegistry("busybox"),
      "essential": true,
      "environment": {
        "ENV_KEY": "10"
      },
      "memoryLimitMiB": 256
    });

    const contdef2 = taskdef.addContainer('my-app-2', {
      "image": ecs.ContainerImage.fromRegistry("busybox"),
      "essential": true,
      "memoryLimitMiB": 256
    });

    contdef.addContainerDependencies(
      {
        container: contdef2,
        condition: ecs.ContainerDependencyCondition.START
      }
    );
  }
}

If you comment out contdef.addContainerDependencies, then the issue doesn't occur because DependsOn doesn't exist in a generated template.

  1. Deploy the code.

$ cdk deploy

  1. After deploy, change ENV_KEY value from "10" to "15".

  2. Check cdk diff.

If you use CDK v1.59.0, then you can see diff like the following one.

e.g.

$ cdk diff
Stack CdkTestStack
Resources
[~] AWS::ECS::TaskDefinition CDKTaskDefinition CDKTaskDefinitionXXXXXXX replace
 └─ [~] ContainerDefinitions (requires replacement)
     └─ @@ -9,7 +9,7 @@
        [ ] "Environment": [
        [ ]   {
        [ ]     "Name": "ENV_KEY",
        [-]     "Value": "10"
        [+]     "Value": "15"
        [ ]   }
        [ ] ],
        [ ] "Essential": true,
  1. However, if you use v1.60.0 or later, you only see "There were no differences"

e.g.

$ cdk diff
Stack CdkTestStack
There were no differences

What did you expect to happen?

cdk diff should show the difference between my local code and a created stack.

What actually happened?

cdk diff returns "There were no differences" even if there is a difference in the code.

Environment

  • CLI Version :1.60.0
  • Framework Version:
  • Node.js Version:v14.7.0
  • OS :Linux
  • Language (Version):TypeScript

Other

I think the following change relates this this issue.

cloudformation-diff: DependsOn singleton arrays aren't equal to string values (#9814) (49cdb47)

The fix added the following 'DependsOn' check to fix the issue.

    for (const key of keys) {
      if (!rvalue.hasOwnProperty(key)) { return false; }
      if (key === 'DependsOn') { return dependsOnEqual(lvalue[key], rvalue[key]); }
      if (!deepEqual(lvalue[key], rvalue[key])) { return false; }
    }

However, if there is 'DependsOn', the above loop is exit and returns the function. It means that remained keys are not checked any more. I guess it is a problem. For example, I think the issue can fix like the following code.

    for (const key of keys) {
        if (!rvalue.hasOwnProperty(key)) { return false; }
        if (key === 'DependsOn') {
            if (!dependsOnEqual(lvalue[key], rvalue[key])) {
                return false;
            }
        } else if (!deepEqual(lvalue[key], rvalue[key])) {
            return false;
        }
    }

This is :bug: Bug Report

atakidev avatar Sep 12 '20 14:09 atakidev