Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only substitute manifest image when there's a full match #10643

Merged

Conversation

DJLemkes
Copy link
Contributor

While using this task, I encountered some strange behavior. Using a deployment file (snippet) like:

    spec:
      containers:
      - name: nginx
         image: nginx:1.7.9
      - name: edgecase
         image: nginx-image:1.7.9
      initContainers:
      - name: nginx-init-container
         image: nginx-init:0.1.0 

And a containers element in my task definition like:

- task: KubernetesManifest@0
   displayName: Deploy application
   inputs:
     action: deploy
     manifests: kubernetes/deployment.yaml
     containers: |
       nginx:42
       nginx-image:42.1
       nginx-init:42.1.2

I got the following output which is incorrect and potentially dangerous because of unexpected containers being deployed

    spec:
      containers:
      - name: nginx
         image: nginx:42
      - name: edgecase
         image: nginx:42
      initContainers:
      - name: nginx-init-container
         image: nginx:42

This PR fixes that issue and only matches when there is a full match on the target image name. The solution is still doing raw string processing. Personally I would prefer using a Yaml lib (or even Kubernetes Yaml model) for parsing the incoming Yaml file but there are probably reasons for not doing this.

While writing test cases, I encountered runtime errors because of a circular import between utilities.ts and constants.ts. Because of that I moved the string equality utility to a separate file. This resolved the issue.

Note that I'm completely new to TypeScript (did some JS before) so please forgive possible style and convention mistakes.

@DJLemkes DJLemkes requested a review from bansalaseem as a code owner June 12, 2019 21:33
@msftclas
Copy link

msftclas commented Jun 12, 2019

CLA assistant check
All CLA requirements met.

Copy link
Contributor

@thesattiraju thesattiraju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DJLemkes Thanks for the contribution.
It's almost there; you need to bump up the task patch version to allow these changes to be shipped.

@@ -8,7 +8,8 @@ import * as yaml from 'js-yaml';
import * as TaskInputParameters from '../models/TaskInputParameters';
import * as fileHelper from '../utils/FileHelper';
import * as helper from './KubernetesObjectUtility';
import { KubernetesWorkload } from '../models/constants';
import { KubernetesWorkload } from '../models/constants';
import {StringComparer, isEqual} from '../utils/StringComparison';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: spacing

@@ -4,16 +4,16 @@ import * as tl from 'vsts-task-lib/task';
import * as yaml from 'js-yaml';
import { Resource } from 'kubernetes-common/kubectl-object-model';
import { KubernetesWorkload, recognizedWorkloadTypes } from '../models/constants';
import {StringComparer, isEqual} from '../utils/StringComparison';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: spacing

@@ -3,13 +3,14 @@
import { Kubectl } from 'kubernetes-common/kubectl-object-model';
import * as utils from '../utils/utilities';
import * as TaskInputParameters from '../models/TaskInputParameters';
import {StringComparer, isEqual} from '../utils/StringComparison';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Spacing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing

@@ -1,7 +1,7 @@
'use strict';

import * as tl from 'vsts-task-lib/task';
import * as utils from '../utils/utilities';
import {StringComparer, isEqual} from '../utils/StringComparison';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Spacing

@@ -7,7 +7,7 @@
"resolved": "https://registry.npmjs.org/@types/del/-/del-2.2.33.tgz",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo theses changes

@thesattiraju
Copy link
Contributor

@DJLemkes If you build after the task version bump up, that would bump it up in task.loc.json file.
Also, there seem to be conflicts, you may have to rebase your upstream.

@DJLemkes
Copy link
Contributor Author

Ok, I see. Sorry about that. Will do a build, add the task.loc.json and rebase.

When building, the package-lock.json of multiple tasks seem to be updated as well. Do you want me to commit them? I reverted the package-lock of the KubernetesManifest task but I added js-yaml for easier testing. That should be the reason why at least the package-lock of KubernetesManifest task is being updated, right?

@thesattiraju
Copy link
Contributor

If you notice the changes correctly in the generated package-lock.json file, the majority of the changes would be a conversion of some static version to either * or ^<some-version>; which we don't want.

You can ignore this for now, as js-yaml is already being brought in by one of the dependencies, and it's already added in the package-lock.json file.

DJLemkes added 3 commits June 14, 2019 08:19
Previously, the line `image: nginx-init` would already match
an 'nginx:42' container in the task definition resulting in
`image: nginx:42`. This leads to unexpected images being deployed.
@DJLemkes DJLemkes force-pushed the better-k8-manifest-image-substitution branch from c74e7b8 to 68e70ab Compare June 14, 2019 06:29
@DJLemkes
Copy link
Contributor Author

Did a rebase and left the package-lock untouched when they were caused by my changes.

Copy link
Contributor

@thesattiraju thesattiraju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@@ -3,13 +3,14 @@
import { Kubectl } from 'kubernetes-common/kubectl-object-model';
import * as utils from '../utils/utilities';
import * as TaskInputParameters from '../models/TaskInputParameters';
import {StringComparer, isEqual} from '../utils/StringComparison';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is missing

@sabbour
Copy link
Member

sabbour commented Jul 16, 2019

This introduced a regression bug. The use case below was working, and now it isn't.

Given the spec below with an untagged image, the task should be able to correctly apply the tag at deploy time.

spec:
      containers:
      - name: myapp
         image: myrepo.azurecr.io/myapp
- task: KubernetesManifest@0
   displayName: Deploy application
   inputs:
     action: deploy
     manifests: kubernetes/deployment.yaml
     containers: 'myrepo.azurecr.io/myapp:$(Build.BuildID)'

This is currently not working anymore, unless you do something like dummytag:

spec:
      containers:
      - name: myapp
         image: myrepo.azurecr.io/myapp:dummytag

@sabbour
Copy link
Member

sabbour commented Jul 16, 2019

@bansalaseem @DS-MS PTAL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants