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

feat: transform imagePullPolicy when using local cluster #9495

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lucasrod16
Copy link

@lucasrod16 lucasrod16 commented Aug 12, 2024

Fixes: #6992

Description

Introduces a ReplaceImagePullPolicy function to update the imagePullPolicy field in Kubernetes manifests to Never when running on a local cluster. This prevents deployment errors that occur when imagePullPolicy is set to Always, as detailed in #6992.

User facing changes

Files to reproduce:

  • Dockerfile

    FROM nginx:alpine
  • pod.yaml

    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        imagePullPolicy: Always
  • skaffold.yaml

    apiVersion: skaffold/v4beta11
    kind: Config
    metadata:
      name: nginx
    build:
      artifacts:
        - image: nginx
          context: .
          docker:
            dockerfile: Dockerfile
    manifests:
      rawYaml:
       - ./pod.yaml

Before:
failed

After:
success

Copy link

google-cla bot commented Aug 12, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@lucasrod16
Copy link
Author

Hi @ericzzzzzzz,

I noticed that this pull request has been pending for a while. Is there anything needed from me? Please let me know if there are any updates required.

Thanks!

@ericzzzzzzz
Copy link
Contributor

Hi, @lucasrod16 Thank you for the pr!

I think we should only do this transformation when using skaffold dev, skaffold run, skaffold debug as we know that the transformed manifests will be applied to the connected local cluster, but for skaffold render command, we're not sure how the manifests will be used, the rendered results may be used in other places even without skaffold.

Also, this pr does the requested change when kubectl deployer is used, it needs to do the similar thing when helm deployer is used.

@lucasrod16
Copy link
Author

Hi, @lucasrod16 Thank you for the pr!

I think we should only do this transformation when using skaffold dev, skaffold run, skaffold debug as we know that the transformed manifests will be applied to the connected local cluster, but for skaffold render command, we're not sure how the manifests will be used, the rendered results may be used in other places even without skaffold.

Also, this pr does the requested change when kubectl deployer is used, it needs to do the similar thing when helm deployer is used.

@ericzzzzzzz That makes sense, thanks for the feedback!

I moved the transform logic from BaseTransform to kubectl Deploy and verified it works locally with dev, run, and debug.

I will work on adding the transform logic to the helm deployer as well

Copy link
Contributor

@alphanota alphanota left a comment

Choose a reason for hiding this comment

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

Hello! Thank you for sending this PR! Please see my comments on it, thanks!.

Also for the PR description, since this PR doesn't have the helm changes needed to fully address the issue, I don't think we should close the bug when this PR is merged.


import apimachinery "k8s.io/apimachinery/pkg/runtime/schema"

type ResourceSelectorImagePullPolicy struct{}
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be unexported, since this new struct is only used as an implementor of the ResourceSelector interface.

I would also add a comment about what this ResourceSelector does/ selects. For example
resourceSelectorImagePullPolicy is an implementation of ResourceSelector to select X and do Z.


type ResourceSelectorImagePullPolicy struct{}

func NewResourceSelectorImagePullPolicy() *ResourceSelectorImagePullPolicy {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should return the interface ResourceSelector instead of *ResourceSelectorImagePullPolicy.

return &ResourceSelectorImagePullPolicy{}
}

func (rs *ResourceSelectorImagePullPolicy) allowByGroupKind(gk apimachinery.GroupKind) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

A short comment explaining the goal of the method and the criteria used would be helpful.

return false
}

func (rs *ResourceSelectorImagePullPolicy) allowByNavpath(gk apimachinery.GroupKind, navpath string, k string) (string, bool) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Here too, a description of the goal would help.

return l.Visit(r, rs)
}

type imagePullPolicyReplacer struct{}
Copy link
Contributor

Choose a reason for hiding this comment

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

A comment explaining the purpose of this struct. ie imagePullPolicyReplacer implements FieldVisitor and is used to edit an entry in a YAML document.


type imagePullPolicyReplacer struct{}

func (i *imagePullPolicyReplacer) Visit(gk apimachinery.GroupKind, navpath string, o map[string]interface{}, k string, v interface{}, rs ResourceSelector) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a comment explaining this method also. For example Visit sets the value of the "imagePullPolicy" field in a Kubernetes manifest to "Never".

@alphanota alphanota self-assigned this Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Skaffold should rewrite imagePullPolicy when using a local cluster
3 participants