forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_controller.go
81 lines (69 loc) · 2.31 KB
/
config_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package controller
import (
"fmt"
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
kerrors "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/util"
buildapi "github.com/openshift/origin/pkg/build/api"
buildclient "github.com/openshift/origin/pkg/build/client"
buildgenerator "github.com/openshift/origin/pkg/build/generator"
)
// ConfigControllerFatalError represents a fatal error while generating a build.
// An operation that fails because of a fatal error should not be retried.
type ConfigControllerFatalError struct {
// Reason the fatal error occurred
Reason string
}
// Error returns the error string for this fatal error
func (e ConfigControllerFatalError) Error() string {
return fmt.Sprintf("fatal error processing BuildConfig: %s", e.Reason)
}
// IsFatal returns true if err is a fatal error
func IsFatal(err error) bool {
_, isFatal := err.(ConfigControllerFatalError)
return isFatal
}
type BuildConfigController struct {
BuildConfigInstantiator buildclient.BuildConfigInstantiator
}
func (c *BuildConfigController) HandleBuildConfig(bc *buildapi.BuildConfig) error {
glog.V(4).Infof("Handling BuildConfig %s/%s", bc.Namespace, bc.Name)
hasChangeTrigger := false
for _, trigger := range bc.Spec.Triggers {
if trigger.Type == buildapi.ConfigChangeBuildTriggerType {
hasChangeTrigger = true
break
}
}
if !hasChangeTrigger {
return nil
}
if bc.Status.LastVersion > 0 {
return nil
}
glog.V(4).Infof("Running build for BuildConfig %s/%s", bc.Namespace, bc.Name)
// instantiate new build
lastVersion := 0
request := &buildapi.BuildRequest{
ObjectMeta: kapi.ObjectMeta{
Name: bc.Name,
Namespace: bc.Namespace,
},
LastVersion: &lastVersion,
}
if _, err := c.BuildConfigInstantiator.Instantiate(bc.Namespace, request); err != nil {
var instantiateErr error
if kerrors.IsConflict(err) {
instantiateErr = fmt.Errorf("unable to instantiate Build for BuildConfig %s/%s due to a conflicting update: %v", bc.Namespace, bc.Name, err)
util.HandleError(instantiateErr)
} else if buildgenerator.IsFatal(err) {
return &ConfigControllerFatalError{err.Error()}
} else {
instantiateErr = fmt.Errorf("error instantiating Build from BuildConfig %s/%s: %v", bc.Namespace, bc.Name, err)
util.HandleError(instantiateErr)
}
return instantiateErr
}
return nil
}