From 101dc06acb62827e33d89dbe1bfb013009a7b59b Mon Sep 17 00:00:00 2001 From: Murat Kabilov Date: Mon, 10 Apr 2017 19:22:37 +0200 Subject: [PATCH] Better logging for teams api calls --- cmd/main.go | 5 +---- pkg/cluster/util.go | 5 ++++- pkg/controller/controller.go | 1 + pkg/controller/util.go | 1 + pkg/util/teams/teams.go | 25 +++++++++++++++++++++---- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 3eca2b66a..c3fe633b0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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, } } diff --git a/pkg/cluster/util.go b/pkg/cluster/util.go index be13a27d2..507a8fa48 100644 --- a/pkg/cluster/util.go +++ b/pkg/cluster/util.go @@ -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 } diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index b5cf9dafb..e1366f06f 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -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, diff --git a/pkg/controller/util.go b/pkg/controller/util.go index 570695562..3b9469354 100644 --- a/pkg/controller/util.go +++ b/pkg/controller/util.go @@ -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 diff --git a/pkg/util/teams/teams.go b/pkg/util/teams/teams.go index 0f8913809..788cdd372 100644 --- a/pkg/util/teams/teams.go +++ b/pkg/util/teams/teams.go @@ -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)