1
0
Fork 0
mirror of https://github.com/prometheus-operator/prometheus-operator.git synced 2025-04-21 03:38:43 +00:00

Merge pull request from jimmidyson/restclient-tpr-check

Fixes error handling when waiting for TPRs
This commit is contained in:
Frederic Branczyk 2016-11-28 13:41:51 +01:00 committed by GitHub
commit 6f03e93bd0
3 changed files with 35 additions and 13 deletions
pkg
alertmanager
k8sutil
prometheus

View file

@ -112,6 +112,7 @@ func (c *Operator) Run(stopc <-chan struct{}) error {
if err != nil {
return err
}
c.logger.Log("msg", "TPR API endpoints ready")
case <-stopc:
return nil
}

View file

@ -19,6 +19,7 @@ import (
"net/http"
"time"
"k8s.io/client-go/1.5/pkg/api/errors"
"k8s.io/client-go/1.5/pkg/api/v1"
"k8s.io/client-go/1.5/pkg/util/wait"
"k8s.io/client-go/1.5/rest"
@ -31,19 +32,23 @@ func WaitForTPRReady(restClient *rest.RESTClient, tprGroup, tprVersion, tprName
res := restClient.Get().AbsPath("apis", tprGroup, tprVersion, tprName).Do()
err := res.Error()
if err != nil {
// RESTClient returns *errors.StatusError for any status codes < 200 or > 206
// and http.Client.Do errors are returned directly.
if se, ok := err.(*errors.StatusError); ok {
if se.Status().Code == http.StatusNotFound {
return false, nil
}
}
return false, err
}
var statusCode int
res.StatusCode(&statusCode)
switch statusCode {
case http.StatusOK:
return true, nil
case http.StatusNotFound: // not set up yet. wait.
return false, nil
default:
if statusCode != http.StatusOK {
return false, fmt.Errorf("invalid status code: %d", statusCode)
}
return true, nil
})
}

View file

@ -164,14 +164,30 @@ func New(conf Config, logger log.Logger) (*Operator, error) {
func (c *Operator) Run(stopc <-chan struct{}) error {
defer c.queue.ShutDown()
v, err := c.kclient.Discovery().ServerVersion()
if err != nil {
return fmt.Errorf("communicating with server failed: %s", err)
}
c.logger.Log("msg", "connection established", "cluster-version", v)
errChan := make(chan error)
go func() {
v, err := c.kclient.Discovery().ServerVersion()
if err != nil {
errChan <- fmt.Errorf("communicating with server failed: %s", err)
return
}
c.logger.Log("msg", "connection established", "cluster-version", v)
if err := c.createTPRs(); err != nil {
return err
if err := c.createTPRs(); err != nil {
errChan <- err
return
}
errChan <- nil
}()
select {
case err := <-errChan:
if err != nil {
return err
}
c.logger.Log("msg", "TPR API endpoints ready")
case <-stopc:
return nil
}
go c.worker()