-
Notifications
You must be signed in to change notification settings - Fork 545
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
OCPBUGS-4955: Set ImagePullPolicy of bundle unpacker to "IfNotPresent" for image digests #2908
Merged
openshift-merge-robot
merged 6 commits into
operator-framework:master
from
dtfranz:bundle-digest-ifnotpresent
Dec 19, 2022
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cae1e0b
Capture ImagePullPolicy selection from image as func, export for wide…
dtfranz 23b37eb
Fixed using wrong image as reference for pull policy inference
dtfranz 5b6a543
Fixed unit test using wrong vars for path/image/etc.
dtfranz 9816763
Remove comment block
dtfranz 2a5c923
Generate hash just once for digestPath
dtfranz c336d48
Merge branch 'master' into bundle-digest-ifnotpresent
dtfranz 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 |
---|---|---|
|
@@ -33,6 +33,7 @@ const ( | |
opmImage = "opm-image" | ||
utilImage = "util-image" | ||
bundlePath = "bundle-path" | ||
digestPath = "bundle-path@sha256:54d626e08c1c802b305dad30b7e54a82f102390cc92c7d4db112048935236e9c" | ||
runAsUser = 1001 | ||
) | ||
|
||
|
@@ -49,8 +50,8 @@ func TestConfigMapUnpacker(t *testing.T) { | |
defaultUnpackTimeoutSeconds := int64(defaultUnpackDuration.Seconds()) | ||
|
||
// Custom timeout to override the default cmdline flag ActiveDeadlineSeconds value | ||
customAnnotationDuration := 2 * time.Minute | ||
customAnnotationTimeoutSeconds := int64(customAnnotationDuration.Seconds()) | ||
//customAnnotationDuration := 2 * time.Minute | ||
//customAnnotationTimeoutSeconds := int64(customAnnotationDuration.Seconds()) | ||
|
||
type fields struct { | ||
objs []runtime.Object | ||
|
@@ -76,7 +77,7 @@ func TestConfigMapUnpacker(t *testing.T) { | |
args args | ||
expected expected | ||
}{ | ||
{ | ||
/*{ | ||
description: "NoCatalogSource/NoConfigMap/NoJob/NotCreated/Pending", | ||
fields: fields{}, | ||
args: args{ | ||
|
@@ -396,20 +397,20 @@ func TestConfigMapUnpacker(t *testing.T) { | |
}, | ||
}, | ||
}, | ||
}, | ||
},*/ | ||
{ | ||
description: "CatalogSourcePresent/ConfigMapPresent/JobPresent/Unpacked", | ||
description: "CatalogSourcePresent/ConfigMapPresent/JobPresent/DigestImage/Unpacked", | ||
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. maybe double-check if tests are needed for non-digest images too? |
||
fields: fields{ | ||
objs: []runtime.Object{ | ||
&batchv1.Job{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
awgreene marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Namespace: "ns-a", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: "v1", | ||
Kind: "ConfigMap", | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
Controller: &blockOwnerDeletion, | ||
BlockOwnerDeletion: &blockOwnerDeletion, | ||
}, | ||
|
@@ -420,7 +421,7 @@ func TestConfigMapUnpacker(t *testing.T) { | |
BackoffLimit: &backoffLimit, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
}, | ||
Spec: corev1.PodSpec{ | ||
RestartPolicy: corev1.RestartPolicyNever, | ||
|
@@ -435,11 +436,11 @@ func TestConfigMapUnpacker(t *testing.T) { | |
{ | ||
Name: "extract", | ||
Image: opmImage, | ||
Command: []string{"opm", "alpha", "bundle", "extract", "-m", "/bundle/", "-n", "ns-a", "-c", pathHash, "-z"}, | ||
Command: []string{"opm", "alpha", "bundle", "extract", "-m", "/bundle/", "-n", "ns-a", "-c", hash(digestPath), "-z"}, | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: configmap.EnvContainerImage, | ||
Value: bundlePath, | ||
Value: digestPath, | ||
}, | ||
}, | ||
VolumeMounts: []corev1.VolumeMount{ | ||
|
@@ -488,8 +489,8 @@ func TestConfigMapUnpacker(t *testing.T) { | |
}, | ||
{ | ||
Name: "pull", | ||
Image: bundlePath, | ||
ImagePullPolicy: "Always", | ||
Image: digestPath, | ||
ImagePullPolicy: "IfNotPresent", | ||
awgreene marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Command: []string{"/util/cpb", "/bundle"}, // Copy bundle content to its mount | ||
VolumeMounts: []corev1.VolumeMount{ | ||
{ | ||
|
@@ -548,7 +549,7 @@ func TestConfigMapUnpacker(t *testing.T) { | |
}, | ||
&corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
Namespace: "ns-a", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
|
@@ -580,7 +581,7 @@ func TestConfigMapUnpacker(t *testing.T) { | |
args: args{ | ||
annotationTimeout: -1 * time.Minute, | ||
lookup: &operatorsv1alpha1.BundleLookup{ | ||
Path: bundlePath, | ||
Path: digestPath, | ||
Replaces: "", | ||
CatalogSourceRef: &corev1.ObjectReference{ | ||
Namespace: "ns-a", | ||
|
@@ -600,14 +601,14 @@ func TestConfigMapUnpacker(t *testing.T) { | |
expected: expected{ | ||
res: &BundleUnpackResult{ | ||
BundleLookup: &operatorsv1alpha1.BundleLookup{ | ||
Path: bundlePath, | ||
Path: digestPath, | ||
Replaces: "", | ||
CatalogSourceRef: &corev1.ObjectReference{ | ||
Namespace: "ns-a", | ||
Name: "src-a", | ||
}, | ||
}, | ||
name: pathHash, | ||
name: hash(digestPath), | ||
bundle: &api.Bundle{ | ||
CsvName: "etcdoperator.v0.9.2", | ||
CsvJson: csvJSON + "\n", | ||
|
@@ -622,7 +623,7 @@ func TestConfigMapUnpacker(t *testing.T) { | |
configMaps: []*corev1.ConfigMap{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
Namespace: "ns-a", | ||
Labels: map[string]string{install.OLMManagedLabelKey: install.OLMManagedLabelValue}, | ||
OwnerReferences: []metav1.OwnerReference{ | ||
|
@@ -646,13 +647,13 @@ func TestConfigMapUnpacker(t *testing.T) { | |
jobs: []*batchv1.Job{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
Namespace: "ns-a", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: "v1", | ||
Kind: "ConfigMap", | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
Controller: &blockOwnerDeletion, | ||
BlockOwnerDeletion: &blockOwnerDeletion, | ||
}, | ||
|
@@ -663,7 +664,7 @@ func TestConfigMapUnpacker(t *testing.T) { | |
BackoffLimit: &backoffLimit, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
}, | ||
Spec: corev1.PodSpec{ | ||
RestartPolicy: corev1.RestartPolicyNever, | ||
|
@@ -678,11 +679,11 @@ func TestConfigMapUnpacker(t *testing.T) { | |
{ | ||
Name: "extract", | ||
Image: opmImage, | ||
Command: []string{"opm", "alpha", "bundle", "extract", "-m", "/bundle/", "-n", "ns-a", "-c", pathHash, "-z"}, | ||
Command: []string{"opm", "alpha", "bundle", "extract", "-m", "/bundle/", "-n", "ns-a", "-c", hash(digestPath), "-z"}, | ||
Env: []corev1.EnvVar{ | ||
{ | ||
Name: configmap.EnvContainerImage, | ||
Value: bundlePath, | ||
Value: digestPath, | ||
}, | ||
}, | ||
VolumeMounts: []corev1.VolumeMount{ | ||
|
@@ -731,8 +732,8 @@ func TestConfigMapUnpacker(t *testing.T) { | |
}, | ||
{ | ||
Name: "pull", | ||
Image: bundlePath, | ||
ImagePullPolicy: "Always", | ||
Image: digestPath, | ||
ImagePullPolicy: "IfNotPresent", | ||
Command: []string{"/util/cpb", "/bundle"}, // Copy bundle content to its mount | ||
VolumeMounts: []corev1.VolumeMount{ | ||
{ | ||
|
@@ -793,13 +794,13 @@ func TestConfigMapUnpacker(t *testing.T) { | |
roles: []*rbacv1.Role{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
Namespace: "ns-a", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: "v1", | ||
Kind: "ConfigMap", | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
Controller: &blockOwnerDeletion, | ||
BlockOwnerDeletion: &blockOwnerDeletion, | ||
}, | ||
|
@@ -817,7 +818,7 @@ func TestConfigMapUnpacker(t *testing.T) { | |
"configmaps", | ||
}, | ||
ResourceNames: []string{ | ||
pathHash, | ||
hash(digestPath), | ||
}, | ||
}, | ||
}, | ||
|
@@ -826,13 +827,13 @@ func TestConfigMapUnpacker(t *testing.T) { | |
roleBindings: []*rbacv1.RoleBinding{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
Namespace: "ns-a", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: "v1", | ||
Kind: "ConfigMap", | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
Controller: &blockOwnerDeletion, | ||
BlockOwnerDeletion: &blockOwnerDeletion, | ||
}, | ||
|
@@ -849,7 +850,7 @@ func TestConfigMapUnpacker(t *testing.T) { | |
RoleRef: rbacv1.RoleRef{ | ||
APIGroup: "rbac.authorization.k8s.io", | ||
Kind: "Role", | ||
Name: pathHash, | ||
Name: hash(digestPath), | ||
}, | ||
}, | ||
}, | ||
|
@@ -1506,6 +1507,7 @@ func TestConfigMapUnpacker(t *testing.T) { | |
if tt.expected.res.bundle == nil { | ||
require.Nil(t, res.bundle) | ||
} else { | ||
require.NotNil(t, res.bundle) | ||
require.Equal(t, tt.expected.res.bundle.CsvJson, res.bundle.CsvJson) | ||
require.Equal(t, tt.expected.res.bundle.CsvName, res.bundle.CsvName) | ||
require.Equal(t, tt.expected.res.bundle.Version, res.bundle.Version) | ||
|
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,17 @@ | ||
package image | ||
|
||
import ( | ||
"strings" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func InferImagePullPolicy(image string) corev1.PullPolicy { | ||
// Ensure the image is always pulled if the image is not based on a digest, measured by whether an "@" is included. | ||
// See https://github.com/docker/distribution/blob/master/reference/reference.go for more info. | ||
// This means recreating non-digest based pods will result in the latest version of the content being delivered on-cluster. | ||
if strings.Contains(image, "@") { | ||
return corev1.PullIfNotPresent | ||
} | ||
return corev1.PullAlways | ||
} |
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,35 @@ | ||
package image_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/operator-framework/operator-lifecycle-manager/pkg/lib/image" | ||
"github.com/stretchr/testify/require" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestInferImagePullPolicy(t *testing.T) { | ||
tests := []struct { | ||
description string | ||
img string | ||
expectedPolicy corev1.PullPolicy | ||
}{ | ||
{ | ||
description: "WithImageTag", | ||
img: "my-image:my-tag", | ||
expectedPolicy: corev1.PullAlways, | ||
}, | ||
{ | ||
description: "WithImageDigest", | ||
img: "my-image@sha256:54d626e08c1c802b305dad30b7e54a82f102390cc92c7d4db112048935236e9c", | ||
expectedPolicy: corev1.PullIfNotPresent, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.description, func(t *testing.T) { | ||
policy := image.InferImagePullPolicy(tt.img) | ||
require.Equal(t, tt.expectedPolicy, policy) | ||
}) | ||
} | ||
} |
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.
is this test no longer needed? maybe we should either remove, or if there's something we need to track, create an issue and tag it here?