Skip to content

Commit

Permalink
Issue fabric8-services#143 Improving handling of OpenShift events
Browse files Browse the repository at this point in the history
  • Loading branch information
hferentschik committed Mar 2, 2018
1 parent 7bd6cd9 commit 4625c2a
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 30 deletions.
10 changes: 3 additions & 7 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ type IdlerAPI interface {
// If an error occurs a response with the HTTP status 500 is returned.
IsIdle(w http.ResponseWriter, r *http.Request, ps httprouter.Params)

// User writes a JSON representation of the User struct to the HTTP response.
// If no namespace parameter is specified all Users are included into the response. If the namespace
// parameter is set only the user with the specified namespace gets added to the response.
//
// NOTE: This endpoint is for debugging purposes and will be removed at some stage.
User(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
// Info writes a JSON representation of internal state of the specified namespace to the response writer.
Info(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
}

type idler struct {
Expand Down Expand Up @@ -87,7 +83,7 @@ func (api *idler) IsIdle(w http.ResponseWriter, r *http.Request, ps httprouter.P
json.NewEncoder(w).Encode(s)
}

func (api *idler) User(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
func (api *idler) Info(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json")
ns := ps.ByName("namespace")

Expand Down
4 changes: 2 additions & 2 deletions internal/idler/user_idler.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ func (idler *UserIdler) Run(wg *sync.WaitGroup, ctx context.Context, cancel cont
cancel()
return
case idler.user = <-idler.userChan:
idler.logger.WithField("data", idler.user.String()).Debug("Received user data.")
idler.logger.WithField("state", idler.user.String()).Debug("Received user data.")

err := idler.checkIdle()
if err != nil {
idler.logger.WithField("error", err.Error()).Warn("Error during idle check.")
}
case <-ticker:
idler.logger.Info("Time based idle check.")
idler.logger.WithField("state", idler.user.String()).Info("Time based idle check.")
idler.resetCounters()
err := idler.checkIdle()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/model/openshift_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (s DCStatus) GetByType(t string) (Condition, error) {
}
}

return Condition{}, fmt.Errorf("Could not find condition '%s'", t)
return Condition{}, fmt.Errorf("could not find condition '%s'", t)
}

var Phases = map[string]int{
Expand Down
7 changes: 0 additions & 7 deletions internal/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@ type User struct {
ID string
ActiveBuild Build
DoneBuild Build
JenkinsStateList []JenkinsState
JenkinsLastUpdate time.Time
}

type JenkinsState struct {
Running bool
Time time.Time
Message string
}

func NewUser(id string, name string) User {
return User{
ID: id,
Expand Down
4 changes: 2 additions & 2 deletions internal/openshift/client/openshift_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (o OpenShift) WatchBuilds(namespace string, buildType string, callback func
logger.WithField("namespace", o.Object.Metadata.Namespace).Debugf("Skipping build %s (type: %s)", o.Object.Metadata.Name, o.Object.Spec.Strategy.Type)
continue
}
logger.WithField("namespace", o.Object.Metadata.Namespace).Debug("Handling Build change event")
logger.WithFields(log.Fields{"namespace": o.Object.Metadata.Namespace, "data": o}).Debug("Handling Build change event")
err = callback(o)
if err != nil {
logger.Errorf("Error from callback: %s", err)
Expand Down Expand Up @@ -402,7 +402,7 @@ func (o OpenShift) WatchDeploymentConfigs(namespace string, nsSuffix string, cal
continue
}

logger.WithField("namespace", o.Object.Metadata.Namespace).Debug("Handling DC change event")
logger.WithFields(log.Fields{"namespace": o.Object.Metadata.Namespace, "data": o}).Debug("Handling DC change event")
err = callback(o)
if err != nil {
logger.Errorf("Error from DC callback: %s", err)
Expand Down
6 changes: 1 addition & 5 deletions internal/openshift/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ func (oc *OpenShiftController) GetUser(ns string) model.User {
// just does couple comparisons and returns
func (oc *OpenShiftController) HandleBuild(o model.Object) error {
ns := o.Object.Metadata.Namespace
logger.WithField("ns", ns).Infof("Processing build event '%s'", o.Object.Metadata.Name)

err := oc.createIfNotExist(o.Object.Metadata.Namespace)
err := oc.createIfNotExist(ns)
if err != nil {
return err
}
Expand Down Expand Up @@ -112,8 +110,6 @@ func (oc *OpenShiftController) HandleBuild(o model.Object) error {
// of ConfigChange or manual intervention.
func (oc *OpenShiftController) HandleDeploymentConfig(dc model.DCObject) error {
ns := dc.Object.Metadata.Namespace[:len(dc.Object.Metadata.Namespace)-len(jenkinsNamespaceSuffix)]
logger.WithField("ns", ns).Infof("Processing deployment config change event '%s'", dc.Object.Metadata.Name)

err := oc.createIfNotExist(ns)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (r *Router) Shutdown() {
func CreateAPIRouter(api api.IdlerAPI) *httprouter.Router {
router := httprouter.New()

router.GET("/iapi/idler/builds/:namespace", api.User)
router.GET("/iapi/idler/builds/:namespace/", api.User)
router.GET("/iapi/idler/info/:namespace", api.Info)
router.GET("/iapi/idler/info/:namespace/", api.Info)

router.GET("/iapi/idler/idle/:namespace", api.Idle)
router.GET("/iapi/idler/idle/:namespace/", api.Idle)
Expand Down
8 changes: 4 additions & 4 deletions scripts/setupLocalIdler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ setTokens() {
# None
###############################################################################
login() {
[ -z "${DSAAS_PREVIEW_TOKEN}" ] && echo "DSAAS_PREVIEW_TOKEN needs to be exported." && printHelp && exit 1
[ -z "${DSAAS_PREVIEW_TOKEN}" ] && echo "DSAAS_PREVIEW_TOKEN needs to be exported." && exit 1

loc login https://api.rh-idev.openshift.com -n dsaas-preview --token=${DSAAS_PREVIEW_TOKEN} >/dev/null
}
Expand All @@ -202,7 +202,7 @@ login() {
# None
###############################################################################
start() {
[ -z "${DSAAS_PREVIEW_TOKEN}" ] && echo "DSAAS_PREVIEW_TOKEN needs to be exported." && printHelp && exit 1
[ -z "${DSAAS_PREVIEW_TOKEN}" ] && echo "DSAAS_PREVIEW_TOKEN needs to be exported." && exit 1

login
forwardProxyService &
Expand All @@ -220,8 +220,8 @@ start() {
# None
###############################################################################
env() {
[ -z "${DSAAS_PREVIEW_TOKEN}" ] && echo "DSAAS_PREVIEW_TOKEN needs to be exported." && printHelp && exit 1
[ -z "${JC_FIXED_UUIDS}" ] && echo "JC_FIXED_UUID needs to be exported." && printHelp && exit 1
[ -z "${DSAAS_PREVIEW_TOKEN}" ] && echo "DSAAS_PREVIEW_TOKEN needs to be exported." && exit 1
[ -z "${JC_FIXED_UUIDS}" ] && echo "JC_FIXED_UUIDS needs to be exported." && exit 1

login
setTokens
Expand Down

0 comments on commit 4625c2a

Please sign in to comment.