Skip to content

Commit 09ad351

Browse files
Merge pull request #16761 from smarterclayton/policy_fix
Automatic merge from submit-queue. Image policy should ignore unchanged images on update If an UPDATE policy decision is made, references that are unchanged should not be reprocessed. This ensures that storage migration can continue working even if a restrictive image policy is in place. @bparees @enj
2 parents 3f94f23 + 125b3c9 commit 09ad351

File tree

7 files changed

+908
-94
lines changed

7 files changed

+908
-94
lines changed

pkg/api/meta/builds.go

+70-19
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ package meta
33
import (
44
"k8s.io/apimachinery/pkg/api/errors"
55
"k8s.io/apimachinery/pkg/util/validation/field"
6+
kapi "k8s.io/kubernetes/pkg/api"
67

78
buildapi "github.com/openshift/origin/pkg/build/apis/build"
89
)
910

1011
type buildSpecMutator struct {
11-
spec *buildapi.CommonSpec
12-
path *field.Path
13-
output bool
12+
spec *buildapi.CommonSpec
13+
oldSpec *buildapi.CommonSpec
14+
path *field.Path
15+
output bool
1416
}
1517

1618
// NewBuildMutator returns an ImageReferenceMutator that includes the output field.
@@ -22,41 +24,90 @@ func NewBuildMutator(build *buildapi.Build) ImageReferenceMutator {
2224
}
2325
}
2426

27+
func hasIdenticalImageSourceObjectReference(spec *buildapi.CommonSpec, ref kapi.ObjectReference) bool {
28+
if spec == nil {
29+
return false
30+
}
31+
for i := range spec.Source.Images {
32+
if spec.Source.Images[i].From == ref {
33+
return true
34+
}
35+
}
36+
return false
37+
}
38+
39+
func hasIdenticalStrategyFrom(spec, oldSpec *buildapi.CommonSpec) bool {
40+
if oldSpec == nil {
41+
return false
42+
}
43+
switch {
44+
case spec.Strategy.CustomStrategy != nil:
45+
if oldSpec.Strategy.CustomStrategy != nil {
46+
return spec.Strategy.CustomStrategy.From == oldSpec.Strategy.CustomStrategy.From
47+
}
48+
case spec.Strategy.DockerStrategy != nil:
49+
if oldSpec.Strategy.DockerStrategy != nil {
50+
return hasIdenticalObjectReference(spec.Strategy.DockerStrategy.From, oldSpec.Strategy.DockerStrategy.From)
51+
}
52+
case spec.Strategy.SourceStrategy != nil:
53+
if oldSpec.Strategy.SourceStrategy != nil {
54+
return spec.Strategy.SourceStrategy.From == oldSpec.Strategy.SourceStrategy.From
55+
}
56+
}
57+
return false
58+
}
59+
60+
func hasIdenticalObjectReference(ref, oldRef *kapi.ObjectReference) bool {
61+
if ref == nil || oldRef == nil {
62+
return false
63+
}
64+
return *ref == *oldRef
65+
}
66+
2567
func (m *buildSpecMutator) Mutate(fn ImageReferenceMutateFunc) field.ErrorList {
2668
var errs field.ErrorList
2769
for i := range m.spec.Source.Images {
70+
if hasIdenticalImageSourceObjectReference(m.oldSpec, m.spec.Source.Images[i].From) {
71+
continue
72+
}
2873
if err := fn(&m.spec.Source.Images[i].From); err != nil {
2974
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("source", "images").Index(i).Child("from", "name")))
3075
continue
3176
}
3277
}
33-
if s := m.spec.Strategy.CustomStrategy; s != nil {
34-
if err := fn(&s.From); err != nil {
35-
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("strategy", "customStrategy", "from", "name")))
78+
if !hasIdenticalStrategyFrom(m.spec, m.oldSpec) {
79+
if s := m.spec.Strategy.CustomStrategy; s != nil {
80+
if err := fn(&s.From); err != nil {
81+
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("strategy", "customStrategy", "from", "name")))
82+
}
3683
}
37-
}
38-
if s := m.spec.Strategy.DockerStrategy; s != nil {
39-
if s.From != nil {
40-
if err := fn(s.From); err != nil {
41-
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("strategy", "dockerStrategy", "from", "name")))
84+
if s := m.spec.Strategy.DockerStrategy; s != nil {
85+
if s.From != nil {
86+
if err := fn(s.From); err != nil {
87+
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("strategy", "dockerStrategy", "from", "name")))
88+
}
4289
}
4390
}
44-
}
45-
if s := m.spec.Strategy.SourceStrategy; s != nil {
46-
if err := fn(&s.From); err != nil {
47-
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("strategy", "sourceStrategy", "from", "name")))
91+
if s := m.spec.Strategy.SourceStrategy; s != nil {
92+
if err := fn(&s.From); err != nil {
93+
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("strategy", "sourceStrategy", "from", "name")))
94+
}
4895
}
49-
if s.RuntimeImage != nil {
96+
}
97+
if s := m.spec.Strategy.SourceStrategy; s != nil && s.RuntimeImage != nil {
98+
if m.oldSpec == nil || m.oldSpec.Strategy.SourceStrategy == nil || !hasIdenticalObjectReference(s.RuntimeImage, m.oldSpec.Strategy.SourceStrategy.RuntimeImage) {
5099
if err := fn(s.RuntimeImage); err != nil {
51100
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("strategy", "sourceStrategy", "runtimeImage", "from", "name")))
52101
}
53102
}
54-
55103
}
104+
56105
if m.output {
57106
if s := m.spec.Output.To; s != nil {
58-
if err := fn(s); err != nil {
59-
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("output", "to")))
107+
if m.oldSpec == nil || m.oldSpec.Output.To == nil || !hasIdenticalObjectReference(s, m.oldSpec.Output.To) {
108+
if err := fn(s); err != nil {
109+
errs = append(errs, fieldErrorOrInternal(err, m.path.Child("output", "to")))
110+
}
60111
}
61112
}
62113
}

0 commit comments

Comments
 (0)