Skip to content

Commit f4dc902

Browse files
committed
introduce image change build trigger logic
1 parent 986ac46 commit f4dc902

24 files changed

+841
-70
lines changed

pkg/build/api/types.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,23 @@ type BuildStatus string
4444

4545
// Valid values for BuildStatus.
4646
const (
47-
// BuildNew is automatically assigned to a newly created build.
47+
// BuildStatusNew is automatically assigned to a newly created build.
4848
BuildStatusNew BuildStatus = "New"
4949

50-
// BuildPending indicates that a pod name has been assigned and a build is
50+
// BuildStatusPending indicates that a pod name has been assigned and a build is
5151
// about to start running.
5252
BuildStatusPending BuildStatus = "Pending"
5353

54-
// BuildRunning indicates that a pod has been created and a build is running.
54+
// BuildStatusRunning indicates that a pod has been created and a build is running.
5555
BuildStatusRunning BuildStatus = "Running"
5656

57-
// BuildComplete indicates that a build has been successful.
57+
// BuildStatusComplete indicates that a build has been successful.
5858
BuildStatusComplete BuildStatus = "Complete"
5959

60-
// BuildFailed indicates that a build has executed and failed.
60+
// BuildStatusFailed indicates that a build has executed and failed.
6161
BuildStatusFailed BuildStatus = "Failed"
6262

63-
// BuildError indicates that an error prevented the build from executing.
63+
// BuildStatusError indicates that an error prevented the build from executing.
6464
BuildStatusError BuildStatus = "Error"
6565

6666
// BuildStatusCancelled indicates that a running/pending build was stopped from executing.
@@ -72,7 +72,7 @@ type BuildSourceType string
7272

7373
// Valid values for BuildSourceType.
7474
const (
75-
//BuildGitSource is a Git SCM
75+
//BuildSourceGit is a Git SCM
7676
BuildSourceGit BuildSourceType = "Git"
7777
)
7878

@@ -150,6 +150,15 @@ const (
150150
CustomBuildStrategyType BuildStrategyType = "Custom"
151151
)
152152

153+
const (
154+
// CustomBuildStrategyBaseImageKey is the environment variable that indicates the base image to be used when
155+
// performing a custom build, if needed.
156+
CustomBuildStrategyBaseImageKey = "OPENSHIFT_CUSTOM_BUILD_BASE_IMAGE"
157+
158+
// DefaultImageTag is used when an image tag is needed and the configuration
159+
DefaultImageTag string = "latest"
160+
)
161+
153162
// CustomBuildStrategy defines input parameters specific to Custom build.
154163
type CustomBuildStrategy struct {
155164
// Image is the image required to execute the build. If not specified
@@ -175,6 +184,11 @@ type DockerBuildStrategy struct {
175184
// NoCache if set to true indicates that the docker build must be executed with the
176185
// --no-cache=true flag
177186
NoCache bool `json:"noCache,omitempty" yaml:"noCache,omitempty"`
187+
188+
// BaseImage is optional and indicates the image that the dockerfile for this
189+
// build should "FROM". If present, the build process will substitute this value
190+
// into the FROM line of the dockerfile.
191+
BaseImage string `json:"baseImage,omitempty" yaml:"baseImage,omitempty"`
178192
}
179193

180194
// STIBuildStrategy defines input parameters specific to an STI build.
@@ -222,6 +236,17 @@ type WebHookTrigger struct {
222236
Secret string `json:"secret,omitempty" yaml:"secret,omitempty"`
223237
}
224238

239+
// ImageChangeTrigger allows builds to be triggered when an ImageRepository changes
240+
type ImageChangeTrigger struct {
241+
// Image is used to specify the value in the BuildConfig to replace with the
242+
// immutable image id supplied by the ImageRepository when this trigger fires.
243+
Image string `json:"image" yaml:"image"`
244+
// ImageRepositoryRef a reference to a Docker image repository to watch for changes.
245+
ImageRepositoryRef *kapi.ObjectReference `json:"imageRepositoryRef" yaml:"imageRepositoryRef"`
246+
// Tag is the name of an image repository tag to watch for changes.
247+
Tag string `json:"tag,omitempty" yaml:"tag,omitempty"`
248+
}
249+
225250
// BuildTriggerPolicy describes a policy for a single trigger that results in a new Build.
226251
type BuildTriggerPolicy struct {
227252
// Type is the type of build trigger
@@ -232,6 +257,9 @@ type BuildTriggerPolicy struct {
232257

233258
// GenericWebHook contains the parameters for a Generic webhook type of trigger
234259
GenericWebHook *WebHookTrigger `json:"generic,omitempty" yaml:"generic,omitempty"`
260+
261+
// ImageChange contains parameters for an ImageChange type of trigger
262+
ImageChange *ImageChangeTrigger `json:"imageChange,omitempty" yaml:"imageChange,omitempty"`
235263
}
236264

237265
// BuildTriggerType refers to a specific BuildTriggerPolicy implementation.
@@ -240,11 +268,15 @@ type BuildTriggerType string
240268
const (
241269
// GithubWebHookType represents a trigger that launches builds on
242270
// Github webhook invocations
243-
GithubWebHookType BuildTriggerType = "github"
271+
GithubWebHookBuildTriggerType BuildTriggerType = "github"
244272

245273
// GenericWebHookType represents a trigger that launches builds on
246274
// generic webhook invocations
247-
GenericWebHookType BuildTriggerType = "generic"
275+
GenericWebHookBuildTriggerType BuildTriggerType = "generic"
276+
277+
// ImageChangeType represents a trigger that launches builds on
278+
// availability of a new version of an image
279+
ImageChangeBuildTriggerType BuildTriggerType = "imageChange"
248280
)
249281

250282
// BuildList is a collection of Builds.

pkg/build/api/v1beta1/types.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,23 @@ type BuildStatus string
4444

4545
// Valid values for BuildStatus.
4646
const (
47-
// BuildNew is automatically assigned to a newly created build.
47+
// BuildStatusNew is automatically assigned to a newly created build.
4848
BuildStatusNew BuildStatus = "New"
4949

50-
// BuildPending indicates that a pod name has been assigned and a build is
50+
// BuildStatusPending indicates that a pod name has been assigned and a build is
5151
// about to start running.
5252
BuildStatusPending BuildStatus = "Pending"
5353

54-
// BuildRunning indicates that a pod has been created and a build is running.
54+
// BuildStatusRunning indicates that a pod has been created and a build is running.
5555
BuildStatusRunning BuildStatus = "Running"
5656

57-
// BuildComplete indicates that a build has been successful.
5857
BuildStatusComplete BuildStatus = "Complete"
58+
// BuildStatusComplete indicates that a build has been successful.
5959

60-
// BuildFailed indicates that a build has executed and failed.
60+
// BuildStatusFailed indicates that a build has executed and failed.
6161
BuildStatusFailed BuildStatus = "Failed"
6262

63-
// BuildError indicates that an error prevented the build from executing.
63+
// BuildStatusError indicates that an error prevented the build from executing.
6464
BuildStatusError BuildStatus = "Error"
6565

6666
// BuildStatusCancelled indicates that a running/pending build was stopped from executing.
@@ -72,7 +72,7 @@ type BuildSourceType string
7272

7373
// Valid values for BuildSourceType.
7474
const (
75-
//BuildGitSource is a Git SCM
75+
//BuildSourceGit is a Git SCM
7676
BuildSourceGit BuildSourceType = "Git"
7777
)
7878

@@ -175,6 +175,11 @@ type DockerBuildStrategy struct {
175175
// NoCache if set to true indicates that the docker build must be executed with the
176176
// --no-cache=true flag
177177
NoCache bool `json:"noCache,omitempty" yaml:"noCache,omitempty"`
178+
179+
// BaseImage is optional and indicates the image that the dockerfile for this
180+
// build should "FROM". If present, the build process will substitute this value
181+
// into the FROM line of the dockerfile.
182+
BaseImage string `json:"baseImage,omitempty" yaml:"baseImage,omitempty"`
178183
}
179184

180185
// STIBuildStrategy defines input parameters specific to an STI build.
@@ -226,6 +231,17 @@ type WebHookTrigger struct {
226231
Secret string `json:"secret,omitempty" yaml:"secret,omitempty"`
227232
}
228233

234+
// ImageChangeTrigger allows builds to be triggered when an ImageRepository changes
235+
type ImageChangeTrigger struct {
236+
// Image is used to specify the value in the BuildConfig to replace with the
237+
// immutable image id supplied by the ImageRepository when this trigger fires.
238+
Image string `json:"image" yaml:"image"`
239+
// ImageRepositoryRef a reference to a Docker image repository to watch for changes.
240+
ImageRepositoryRef *kapi.ObjectReference `json:"imageRepositoryRef" yaml:"imageRepositoryRef"`
241+
// Tag is the name of an image repository tag to watch for changes.
242+
Tag string `json:"tag,omitempty" yaml:"tag,omitempty"`
243+
}
244+
229245
// BuildTriggerPolicy describes a policy for a single trigger that results in a new Build.
230246
type BuildTriggerPolicy struct {
231247
// Type is the type of build trigger
@@ -236,6 +252,9 @@ type BuildTriggerPolicy struct {
236252

237253
// GenericWebHook contains the parameters for a Generic webhook type of trigger
238254
GenericWebHook *WebHookTrigger `json:"generic,omitempty" yaml:"generic,omitempty"`
255+
256+
// ImageChange contains parameters for an ImageChange type of trigger
257+
ImageChange *ImageChangeTrigger `json:"imageChange,omitempty" yaml:"imageChange,omitempty"`
239258
}
240259

241260
// BuildTriggerType refers to a specific BuildTriggerPolicy implementation.

pkg/build/api/validation/validation.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,31 @@ func validateTrigger(trigger *buildapi.BuildTriggerPolicy) errs.ValidationErrorL
138138

139139
// Ensure that only parameters for the trigger's type are present
140140
triggerPresence := map[buildapi.BuildTriggerType]bool{
141-
buildapi.GithubWebHookType: trigger.GithubWebHook != nil,
142-
buildapi.GenericWebHookType: trigger.GenericWebHook != nil,
141+
buildapi.GithubWebHookBuildTriggerType: trigger.GithubWebHook != nil,
142+
buildapi.GenericWebHookBuildTriggerType: trigger.GenericWebHook != nil,
143143
}
144144
allErrs = append(allErrs, validateTriggerPresence(triggerPresence, trigger.Type)...)
145145

146146
// Validate each trigger type
147147
switch trigger.Type {
148-
case buildapi.GithubWebHookType:
148+
case buildapi.GithubWebHookBuildTriggerType:
149149
if trigger.GithubWebHook == nil {
150150
allErrs = append(allErrs, errs.NewFieldRequired("github", nil))
151151
} else {
152152
allErrs = append(allErrs, validateWebHook(trigger.GithubWebHook).Prefix("github")...)
153153
}
154-
case buildapi.GenericWebHookType:
154+
case buildapi.GenericWebHookBuildTriggerType:
155155
if trigger.GenericWebHook == nil {
156156
allErrs = append(allErrs, errs.NewFieldRequired("generic", nil))
157157
} else {
158158
allErrs = append(allErrs, validateWebHook(trigger.GenericWebHook).Prefix("generic")...)
159159
}
160+
case buildapi.ImageChangeBuildTriggerType:
161+
if trigger.ImageChange == nil {
162+
allErrs = append(allErrs, errs.NewFieldRequired("imageChange", nil))
163+
} else {
164+
allErrs = append(allErrs, validateImageChange(trigger.ImageChange).Prefix("imageChange")...)
165+
}
160166
}
161167
return allErrs
162168
}
@@ -171,6 +177,21 @@ func validateTriggerPresence(params map[buildapi.BuildTriggerType]bool, t builda
171177
return allErrs
172178
}
173179

180+
func validateImageChange(imageChange *buildapi.ImageChangeTrigger) errs.ValidationErrorList {
181+
allErrs := errs.ValidationErrorList{}
182+
if len(imageChange.Image) == 0 {
183+
allErrs = append(allErrs, errs.NewFieldRequired("image", ""))
184+
}
185+
if imageChange.ImageRepositoryRef == nil {
186+
allErrs = append(allErrs, errs.NewFieldRequired("imageRepositoryRef", ""))
187+
} else if len(imageChange.ImageRepositoryRef.Name) == 0 {
188+
nestedErrs := errs.ValidationErrorList{errs.NewFieldRequired("name", "")}
189+
nestedErrs.Prefix("imageRepositoryRef")
190+
allErrs = append(allErrs, nestedErrs...)
191+
}
192+
return allErrs
193+
}
194+
174195
func validateWebHook(webHook *buildapi.WebHookTrigger) errs.ValidationErrorList {
175196
allErrs := errs.ValidationErrorList{}
176197
if len(webHook.Secret) == 0 {

pkg/build/api/validation/validation_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,39 +204,39 @@ func TestValidateTrigger(t *testing.T) {
204204
expected: []*errs.ValidationError{errs.NewFieldRequired("type", "")},
205205
},
206206
"github type with no github webhook": {
207-
trigger: buildapi.BuildTriggerPolicy{Type: buildapi.GithubWebHookType},
207+
trigger: buildapi.BuildTriggerPolicy{Type: buildapi.GithubWebHookBuildTriggerType},
208208
expected: []*errs.ValidationError{errs.NewFieldRequired("github", "")},
209209
},
210210
"github trigger with no secret": {
211211
trigger: buildapi.BuildTriggerPolicy{
212-
Type: buildapi.GithubWebHookType,
212+
Type: buildapi.GithubWebHookBuildTriggerType,
213213
GithubWebHook: &buildapi.WebHookTrigger{},
214214
},
215215
expected: []*errs.ValidationError{errs.NewFieldRequired("github.secret", "")},
216216
},
217217
"github trigger with generic webhook": {
218218
trigger: buildapi.BuildTriggerPolicy{
219-
Type: buildapi.GithubWebHookType,
219+
Type: buildapi.GithubWebHookBuildTriggerType,
220220
GenericWebHook: &buildapi.WebHookTrigger{
221221
Secret: "secret101",
222222
},
223223
},
224224
expected: []*errs.ValidationError{errs.NewFieldInvalid("generic", "", "long description")},
225225
},
226226
"generic trigger with no generic webhook": {
227-
trigger: buildapi.BuildTriggerPolicy{Type: buildapi.GenericWebHookType},
227+
trigger: buildapi.BuildTriggerPolicy{Type: buildapi.GenericWebHookBuildTriggerType},
228228
expected: []*errs.ValidationError{errs.NewFieldRequired("generic", "")},
229229
},
230230
"generic trigger with no secret": {
231231
trigger: buildapi.BuildTriggerPolicy{
232-
Type: buildapi.GenericWebHookType,
232+
Type: buildapi.GenericWebHookBuildTriggerType,
233233
GenericWebHook: &buildapi.WebHookTrigger{},
234234
},
235235
expected: []*errs.ValidationError{errs.NewFieldRequired("generic.secret", "")},
236236
},
237237
"generic trigger with github webhook": {
238238
trigger: buildapi.BuildTriggerPolicy{
239-
Type: buildapi.GenericWebHookType,
239+
Type: buildapi.GenericWebHookBuildTriggerType,
240240
GithubWebHook: &buildapi.WebHookTrigger{
241241
Secret: "secret101",
242242
},
@@ -245,15 +245,15 @@ func TestValidateTrigger(t *testing.T) {
245245
},
246246
"valid github trigger": {
247247
trigger: buildapi.BuildTriggerPolicy{
248-
Type: buildapi.GithubWebHookType,
248+
Type: buildapi.GithubWebHookBuildTriggerType,
249249
GithubWebHook: &buildapi.WebHookTrigger{
250250
Secret: "secret101",
251251
},
252252
},
253253
},
254254
"valid generic trigger": {
255255
trigger: buildapi.BuildTriggerPolicy{
256-
Type: buildapi.GenericWebHookType,
256+
Type: buildapi.GenericWebHookBuildTriggerType,
257257
GenericWebHook: &buildapi.WebHookTrigger{
258258
Secret: "secret101",
259259
},

0 commit comments

Comments
 (0)