Better logging for teams api calls

This commit is contained in:
Murat Kabilov 2017-04-10 19:22:37 +02:00
parent 5b66d0adba
commit 101dc06acb
5 changed files with 28 additions and 9 deletions

View File

@ -11,7 +11,6 @@ import (
"github.bus.zalan.do/acid/postgres-operator/pkg/controller"
"github.bus.zalan.do/acid/postgres-operator/pkg/util/config"
"github.bus.zalan.do/acid/postgres-operator/pkg/util/k8sutil"
"github.bus.zalan.do/acid/postgres-operator/pkg/util/teams"
)
var (
@ -48,12 +47,10 @@ func ControllerConfig() *controller.Config {
restClient, err := k8sutil.KubernetesRestClient(restConfig)
teamsApi := teams.NewTeamsAPI(cfg.TeamsAPIUrl)
return &controller.Config{
PodNamespace: podNamespace,
PodNamespace: podNamespace, //TODO: move to config.Config
KubeClient: client,
RestClient: restClient,
TeamsAPIClient: teamsApi,
}
}

View File

@ -137,11 +137,14 @@ func (c *Cluster) logPodChanges(pod *v1.Pod, statefulset *v1beta1.StatefulSet, r
}
func (c *Cluster) getTeamMembers() ([]string, error) {
if c.Spec.TeamId == "" {
return nil, fmt.Errorf("No teamId specified")
}
teamInfo, err := c.TeamsAPIClient.TeamInfo(c.Spec.TeamId)
if err != nil {
return nil, fmt.Errorf("Can't get team info: %s", err)
}
c.logger.Debugf("Got from the Team API: %v", teamInfo)
c.logger.Debugf("Got from the Team API: %+v", *teamInfo)
return teamInfo.Members, nil
}

View File

@ -45,6 +45,7 @@ func New(controllerConfig *Config, operatorConfig *config.Config) *Controller {
logger.Level = logrus.DebugLevel
}
controllerConfig.TeamsAPIClient = teams.NewTeamsAPI(operatorConfig.TeamsAPIUrl, logger)
return &Controller{
Config: *controllerConfig,
opConfig: operatorConfig,

View File

@ -27,6 +27,7 @@ func (c *Controller) getOAuthToken() (string, error) {
credentialsSecret, err := c.KubeClient.Secrets(api.NamespaceDefault).Get(c.opConfig.OAuthTokenSecretName)
if err != nil {
c.logger.Debugf("Oauth token secret name: %s", c.opConfig.OAuthTokenSecretName)
return "", fmt.Errorf("Can't get credentials Secret: %s", err)
}
data := credentialsSecret.Data

View File

@ -1,10 +1,12 @@
package teams
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"encoding/json"
"net/http"
"github.com/Sirupsen/logrus"
)
type InfrastructureAccount struct {
@ -38,13 +40,15 @@ type Team struct {
type TeamsAPI struct {
url string
httpClient *http.Client
logger *logrus.Entry
RefreshTokenAction func() (string, error)
}
func NewTeamsAPI(url string) *TeamsAPI {
func NewTeamsAPI(url string, log *logrus.Logger) *TeamsAPI {
t := TeamsAPI{
url: strings.TrimRight(url, "/"),
httpClient: &http.Client{},
logger: log.WithField("pkg", "teamsapi"),
}
return &t
@ -57,6 +61,7 @@ func (t *TeamsAPI) TeamInfo(teamId string) (*Team, error) {
return nil, err
}
url := fmt.Sprintf("%s/teams/%s", t.url, teamId)
t.logger.Debugf("Request url: %s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
@ -67,9 +72,21 @@ func (t *TeamsAPI) TeamInfo(teamId string) (*Team, error) {
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
var raw map[string]json.RawMessage
d := json.NewDecoder(resp.Body)
err = d.Decode(&raw)
if err != nil {
return nil, err
}
if errMessage, ok := raw["error"]; ok {
return nil, fmt.Errorf("Team API query failed with status code %d and message: '%s'", resp.StatusCode, string(errMessage))
} else {
return nil, fmt.Errorf("Team API query failed with status code %d", resp.StatusCode)
}
}
teamInfo := &Team{}
d := json.NewDecoder(resp.Body)
err = d.Decode(teamInfo)