-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
lucasrod16
wants to merge
1
commit into
GoogleContainerTools:main
Choose a base branch
from
lucasrod16:6992-transform-imagePullPolicy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+166
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package manifest | ||
|
||
import apimachinery "k8s.io/apimachinery/pkg/runtime/schema" | ||
|
||
// resourceSelectorImagePullPolicy selects PodSpecs for transforming the imagePullPolicy field | ||
// based on allowlist and denylist rules for their GroupKind and navigation path. | ||
type resourceSelectorImagePullPolicy struct{} | ||
|
||
func NewResourceSelectorImagePullPolicy() ResourceSelector { | ||
return &resourceSelectorImagePullPolicy{} | ||
} | ||
|
||
// allowByGroupKind checks if a GroupKind is allowed for transformation. | ||
// It allows GroupKinds in the allowlist unless they are in the denylist with ".*" in PodSpec paths, which blocks all PodSpecs. | ||
func (rs *resourceSelectorImagePullPolicy) allowByGroupKind(gk apimachinery.GroupKind) bool { | ||
if _, allowed := TransformAllowlist[gk]; allowed { | ||
if rf, disallowed := TransformDenylist[gk]; disallowed { | ||
for _, s := range rf.PodSpec { | ||
if s == ".*" { | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// allowByNavpath checks if a GroupKind's PodSpec path (navpath) allows transformation. | ||
// It blocks transformation if the path matches a denylist entry or ".*". | ||
// If not denied, it permits transformation if the path matches an allowlist entry or ".*". | ||
func (rs *resourceSelectorImagePullPolicy) allowByNavpath(gk apimachinery.GroupKind, navpath string, k string) (string, bool) { | ||
if rf, ok := TransformDenylist[gk]; ok { | ||
for _, denypath := range rf.PodSpec { | ||
if denypath == ".*" || navpath == denypath { | ||
return "", false | ||
} | ||
} | ||
} | ||
if rf, ok := TransformAllowlist[gk]; ok { | ||
for _, allowpath := range rf.PodSpec { | ||
if allowpath == ".*" || navpath == allowpath { | ||
return "", true | ||
} | ||
} | ||
} | ||
return "", false | ||
} | ||
|
||
func (l *ManifestList) ReplaceImagePullPolicy(rs ResourceSelector) (ManifestList, error) { | ||
r := &imagePullPolicyReplacer{} | ||
return l.Visit(r, rs) | ||
} | ||
|
||
// imagePullPolicyReplacer implements FieldVisitor and modifies the "imagePullPolicy" field in Kubernetes manifests. | ||
type imagePullPolicyReplacer struct{} | ||
|
||
// Visit sets the value of the "imagePullPolicy" field in a Kubernetes manifest to "Never". | ||
func (i *imagePullPolicyReplacer) Visit(gk apimachinery.GroupKind, navpath string, o map[string]interface{}, k string, v interface{}, rs ResourceSelector) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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". |
||
const imagePullPolicyField = "imagePullPolicy" | ||
if _, allowed := rs.allowByNavpath(gk, navpath, k); !allowed { | ||
return true | ||
} | ||
if k != imagePullPolicyField { | ||
return true | ||
} | ||
if _, ok := v.(string); !ok { | ||
return true | ||
} | ||
o[imagePullPolicyField] = "Never" | ||
return false | ||
} |
67 changes: 67 additions & 0 deletions
67
pkg/skaffold/kubernetes/manifest/image_pull_policy_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2019 The Skaffold Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package manifest | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/skaffold/v2/testutil" | ||
) | ||
|
||
func TestReplaceImagePullPolicy(t *testing.T) { | ||
manifests := ManifestList{[]byte(` | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: replace-imagePullPolicy | ||
spec: | ||
containers: | ||
- image: gcr.io/k8s-skaffold/example@sha256:81daf011d63b68cfa514ddab7741a1adddd59d3264118dfb0fd9266328bb8883 | ||
name: if-not-present | ||
imagePullPolicy: IfNotPresent | ||
- image: gcr.io/k8s-skaffold/example@sha256:81daf011d63b68cfa514ddab7741a1adddd59d3264118dfb0fd9266328bb8883 | ||
name: always | ||
imagePullPolicy: Always | ||
- image: gcr.io/k8s-skaffold/example@sha256:81daf011d63b68cfa514ddab7741a1adddd59d3264118dfb0fd9266328bb8883 | ||
name: never | ||
imagePullPolicy: Never | ||
`)} | ||
|
||
expected := ManifestList{[]byte(` | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: replace-imagePullPolicy | ||
spec: | ||
containers: | ||
- image: gcr.io/k8s-skaffold/example@sha256:81daf011d63b68cfa514ddab7741a1adddd59d3264118dfb0fd9266328bb8883 | ||
name: if-not-present | ||
imagePullPolicy: Never | ||
- image: gcr.io/k8s-skaffold/example@sha256:81daf011d63b68cfa514ddab7741a1adddd59d3264118dfb0fd9266328bb8883 | ||
name: always | ||
imagePullPolicy: Never | ||
- image: gcr.io/k8s-skaffold/example@sha256:81daf011d63b68cfa514ddab7741a1adddd59d3264118dfb0fd9266328bb8883 | ||
name: never | ||
imagePullPolicy: Never | ||
`)} | ||
|
||
testutil.Run(t, "", func(t *testutil.T) { | ||
resultManifest, err := manifests.ReplaceImagePullPolicy(NewResourceSelectorImagePullPolicy()) | ||
t.CheckNoError(err) | ||
t.CheckDeepEqual(expected.String(), resultManifest.String(), testutil.YamlObj(t.T)) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.