Skip to content

Commit

Permalink
Add metrics server check at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprodan committed Sep 26, 2018
1 parent 1b7210e commit 8115757
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
16 changes: 10 additions & 6 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/stefanprodan/steerer/pkg/logging"
"github.com/stefanprodan/steerer/pkg/server"
"github.com/stefanprodan/steerer/pkg/version"
"go.uber.org/zap"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/cache"
Expand Down Expand Up @@ -73,16 +72,21 @@ func main() {
rolloutInformerFactory := informers.NewSharedInformerFactory(rolloutClient, time.Second*30)
rolloutInformer := rolloutInformerFactory.Apps().V1beta1().Rollouts()

logger.Infof("Starting steerer version %s revision %s", version.VERSION, version.REVISION)

ver, err := kubeClient.Discovery().ServerVersion()
if err != nil {
logger.Fatalf("Error calling Kubernetes API: %v", err)
}

logger.Infow("Starting steerer",
zap.String("version", version.VERSION),
zap.String("revision", version.REVISION),
zap.String("metrics provider", metricsServer),
zap.Any("kubernetes version", ver))
logger.Infof("Connected to Kubernetes API %s", ver)

ok, err := controller.CheckMetricsServer(metricsServer)
if ok {
logger.Infof("Connected to metrics server %s", metricsServer)
} else {
logger.Errorf("Metrics server %s unreachable %v", metricsServer, err)
}

// start HTTP server
go server.ListenAndServe(port, 3*time.Second, logger, stopCh)
Expand Down
1 change: 0 additions & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ func (c *Controller) processNextWorkItem() bool {
// Finally, if no error occurs we Forget this item so it does not
// get queued again until another change happens.
c.workqueue.Forget(obj)
c.logger.Infof("Successfully synced '%s'", key)
return nil
}(obj)

Expand Down
40 changes: 39 additions & 1 deletion pkg/controller/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func (c *Controller) queryMetric(query string) (*VectorQueryResponse, error) {
}

return &values, nil

}

func (c *Controller) getDeploymentMetric(name string, namespace string, counter string, interval string) (float64, error) {
Expand Down Expand Up @@ -97,3 +96,42 @@ func (c *Controller) getDeploymentMetric(name string, namespace string, counter
}
return rate, nil
}

func CheckMetricsServer(address string) (bool, error) {
promURL, err := url.Parse(address)
if err != nil {
return false, err
}

u, err := url.Parse("./api/v1/status/flags")
if err != nil {
return false, err
}

u = promURL.ResolveReference(u)

req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return false, err
}

ctx, cancel := context.WithTimeout(req.Context(), 5*time.Second)
defer cancel()

r, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return false, err
}
defer r.Body.Close()

b, err := ioutil.ReadAll(r.Body)
if err != nil {
return false, fmt.Errorf("error reading body: %s", err.Error())
}

if 400 <= r.StatusCode {
return false, fmt.Errorf("error response: %s", string(b))
}

return true, nil
}

0 comments on commit 8115757

Please sign in to comment.