Skip to content

Commit 565a1c0

Browse files
author
OpenShift Bot
authored
Merge pull request #15118 from smarterclayton/dont_resolve_jobs
Merged by openshift-bot
2 parents fe7173b + f779a73 commit 565a1c0

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

pkg/image/admission/imagepolicy/accept.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func accept(accepter rules.Accepter, policy imageResolutionPolicy, resolver imag
6767
// if we resolved properly, assign the attributes and rewrite the pull spec if we need to
6868
decision.attrs = resolvedAttrs
6969

70-
if policy.RewriteImagePullSpec(resolvedAttrs, gr) {
70+
if policy.RewriteImagePullSpec(resolvedAttrs, attr.GetOperation() == admission.Update, gr) {
7171
ref.Namespace = ""
7272
ref.Name = decision.attrs.Name.Exact()
7373
ref.Kind = "DockerImage"

pkg/image/admission/imagepolicy/imagepolicy.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ type imageResolutionPolicy interface {
8383
// FailOnResolutionFailure returns true if you should fail when resolution fails
8484
FailOnResolutionFailure(schema.GroupResource) bool
8585
// RewriteImagePullSpec returns true if you should rewrite image pull specs when resolution succeeds
86-
RewriteImagePullSpec(*rules.ImagePolicyAttributes, schema.GroupResource) bool
86+
RewriteImagePullSpec(attr *rules.ImagePolicyAttributes, isUpdate bool, gr schema.GroupResource) bool
8787
}
8888

8989
// imagePolicyPlugin returns an admission controller for pods that controls what images are allowed to run on the
@@ -439,8 +439,24 @@ func (config resolutionConfig) FailOnResolutionFailure(gr schema.GroupResource)
439439
return api.FailOnResolutionFailure(config.config.ResolveImages)
440440
}
441441

442+
var skipImageRewriteOnUpdate = map[schema.GroupResource]struct{}{
443+
// Job template specs are immutable, they cannot be updated.
444+
{Group: "extensions", Resource: "jobs"}: {},
445+
{Group: "batch", Resource: "jobs"}: {},
446+
// Build specs are immutable, they cannot be updated.
447+
{Group: "", Resource: "builds"}: {},
448+
{Group: "build.openshift.io", Resource: "builds"}: {},
449+
// TODO: remove when statefulsets allow spec.template updates in 3.7
450+
{Group: "apps", Resource: "statefulsets"}: {},
451+
}
452+
442453
// RewriteImagePullSpec applies to implicit rewrite attributes and local resources as well as if the policy requires it.
443-
func (config resolutionConfig) RewriteImagePullSpec(attr *rules.ImagePolicyAttributes, gr schema.GroupResource) bool {
454+
func (config resolutionConfig) RewriteImagePullSpec(attr *rules.ImagePolicyAttributes, isUpdate bool, gr schema.GroupResource) bool {
455+
if isUpdate {
456+
if _, ok := skipImageRewriteOnUpdate[gr]; ok {
457+
return false
458+
}
459+
}
444460
if api.RequestsResolution(config.config.ResolveImages) {
445461
return true
446462
}

pkg/image/admission/imagepolicy/imagepolicy_test.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ func TestResolutionConfig(t *testing.T) {
970970
config *api.ImagePolicyConfig
971971
resource schema.GroupResource
972972
attrs rules.ImagePolicyAttributes
973+
update bool
973974

974975
resolve bool
975976
fail bool
@@ -1042,6 +1043,63 @@ func TestResolutionConfig(t *testing.T) {
10421043
resolve: true,
10431044
rewrite: true,
10441045
},
1046+
// resource match skips on job update
1047+
{
1048+
attrs: rules.ImagePolicyAttributes{LocalRewrite: true},
1049+
config: &api.ImagePolicyConfig{
1050+
ResolveImages: api.DoNotAttempt,
1051+
ResolutionRules: []api.ImageResolutionPolicyRule{
1052+
{LocalNames: true, TargetResource: metav1.GroupResource{Group: "extensions", Resource: "jobs"}},
1053+
},
1054+
},
1055+
resource: schema.GroupResource{Group: "extensions", Resource: "jobs"},
1056+
update: true,
1057+
resolve: true,
1058+
rewrite: false,
1059+
},
1060+
// resource match succeeds on job create
1061+
{
1062+
attrs: rules.ImagePolicyAttributes{LocalRewrite: true},
1063+
config: &api.ImagePolicyConfig{
1064+
ResolveImages: api.DoNotAttempt,
1065+
ResolutionRules: []api.ImageResolutionPolicyRule{
1066+
{LocalNames: true, TargetResource: metav1.GroupResource{Group: "extensions", Resource: "jobs"}},
1067+
},
1068+
},
1069+
resource: schema.GroupResource{Group: "extensions", Resource: "jobs"},
1070+
update: false,
1071+
resolve: true,
1072+
rewrite: true,
1073+
},
1074+
// resource match skips on build update
1075+
{
1076+
attrs: rules.ImagePolicyAttributes{LocalRewrite: true},
1077+
config: &api.ImagePolicyConfig{
1078+
ResolveImages: api.DoNotAttempt,
1079+
ResolutionRules: []api.ImageResolutionPolicyRule{
1080+
{LocalNames: true, TargetResource: metav1.GroupResource{Group: "build.openshift.io", Resource: "builds"}},
1081+
},
1082+
},
1083+
resource: schema.GroupResource{Group: "build.openshift.io", Resource: "builds"},
1084+
update: true,
1085+
resolve: true,
1086+
rewrite: false,
1087+
},
1088+
// resource match skips on statefulset update
1089+
// TODO: remove in 3.7
1090+
{
1091+
attrs: rules.ImagePolicyAttributes{LocalRewrite: true},
1092+
config: &api.ImagePolicyConfig{
1093+
ResolveImages: api.DoNotAttempt,
1094+
ResolutionRules: []api.ImageResolutionPolicyRule{
1095+
{LocalNames: true, TargetResource: metav1.GroupResource{Group: "apps", Resource: "statefulsets"}},
1096+
},
1097+
},
1098+
resource: schema.GroupResource{Group: "apps", Resource: "statefulsets"},
1099+
update: true,
1100+
resolve: true,
1101+
rewrite: false,
1102+
},
10451103
}
10461104

10471105
for i, test := range testCases {
@@ -1052,7 +1110,7 @@ func TestResolutionConfig(t *testing.T) {
10521110
if c.FailOnResolutionFailure(test.resource) != test.fail {
10531111
t.Errorf("%d: resolution failure != %t", i, test.fail)
10541112
}
1055-
if c.RewriteImagePullSpec(&test.attrs, test.resource) != test.rewrite {
1113+
if c.RewriteImagePullSpec(&test.attrs, test.update, test.resource) != test.rewrite {
10561114
t.Errorf("%d: rewrite != %t", i, test.rewrite)
10571115
}
10581116
}

0 commit comments

Comments
 (0)