@@ -3,14 +3,16 @@ package meta
3
3
import (
4
4
"k8s.io/apimachinery/pkg/api/errors"
5
5
"k8s.io/apimachinery/pkg/util/validation/field"
6
+ kapi "k8s.io/kubernetes/pkg/api"
6
7
7
8
buildapi "github.com/openshift/origin/pkg/build/apis/build"
8
9
)
9
10
10
11
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
14
16
}
15
17
16
18
// NewBuildMutator returns an ImageReferenceMutator that includes the output field.
@@ -22,41 +24,90 @@ func NewBuildMutator(build *buildapi.Build) ImageReferenceMutator {
22
24
}
23
25
}
24
26
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
+
25
67
func (m * buildSpecMutator ) Mutate (fn ImageReferenceMutateFunc ) field.ErrorList {
26
68
var errs field.ErrorList
27
69
for i := range m .spec .Source .Images {
70
+ if hasIdenticalImageSourceObjectReference (m .oldSpec , m .spec .Source .Images [i ].From ) {
71
+ continue
72
+ }
28
73
if err := fn (& m .spec .Source .Images [i ].From ); err != nil {
29
74
errs = append (errs , fieldErrorOrInternal (err , m .path .Child ("source" , "images" ).Index (i ).Child ("from" , "name" )))
30
75
continue
31
76
}
32
77
}
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
+ }
36
83
}
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
+ }
42
89
}
43
90
}
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
+ }
48
95
}
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 ) {
50
99
if err := fn (s .RuntimeImage ); err != nil {
51
100
errs = append (errs , fieldErrorOrInternal (err , m .path .Child ("strategy" , "sourceStrategy" , "runtimeImage" , "from" , "name" )))
52
101
}
53
102
}
54
-
55
103
}
104
+
56
105
if m .output {
57
106
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
+ }
60
111
}
61
112
}
62
113
}
0 commit comments