Skip to content

Commit

Permalink
feat: improve logging (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao authored Nov 25, 2021
1 parent c753e71 commit 3a0cdb5
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 41 deletions.
18 changes: 9 additions & 9 deletions actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Config struct {
}

type Service interface {
Run(ctx context.Context)
Run(ctx context.Context) error
}

type ActionHandler interface {
Expand Down Expand Up @@ -58,29 +58,29 @@ type service struct {
actionHandlers map[reflect.Type]ActionHandler
}

func (s *service) Run(ctx context.Context) {
func (s *service) Run(ctx context.Context) error {
for {
select {
case <-time.After(s.cfg.PollWaitInterval):
err := s.doWork(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
s.log.Debug("actions service stopped")
return
s.log.Info("service stopped")
return nil
}

s.log.Errorf("cycle failed: %v", err)
continue
}
case <-ctx.Done():
s.log.Debug("actions service stopped")
return
s.log.Info("service stopped")
return nil
}
}
}

func (s *service) doWork(ctx context.Context) error {
s.log.Debug("polling actions")
s.log.Info("polling actions")
start := time.Now()
actions, err := s.pollActions(ctx)
if err != nil {
Expand All @@ -89,11 +89,11 @@ func (s *service) doWork(ctx context.Context) error {

pollDuration := time.Now().Sub(start)
if len(actions) == 0 {
s.log.Debugf("no actions returned in %s", pollDuration)
s.log.Infof("no actions returned in %s", pollDuration)
return nil
}

s.log.Debugf("received %d actions in %s", len(actions), pollDuration)
s.log.Infof("received %d actions in %s", len(actions), pollDuration)
if err := s.handleActions(ctx, actions); err != nil {
return fmt.Errorf("handling actions: %w", err)
}
Expand Down
7 changes: 5 additions & 2 deletions castai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"fmt"
"net/http"
"time"

"github.com/castai/cluster-controller/config"
"github.com/go-resty/resty/v2"
"github.com/sirupsen/logrus"

"github.com/castai/cluster-controller/config"
)

const (
Expand All @@ -30,9 +32,10 @@ func NewClient(log *logrus.Logger, rest *resty.Client, clusterID string) Client
}

// NewDefaultClient configures a default instance of the resty.Client used to do HTTP requests.
func NewDefaultClient(url, key string, level logrus.Level, binVersion *config.AgentActionsVersion) *resty.Client {
func NewDefaultClient(url, key string, level logrus.Level, binVersion *config.ClusterControllerVersion) *resty.Client {
client := resty.New()
client.SetHostURL(url)
client.SetTimeout(5 * time.Minute) // Hard timeout for any request.
client.Header.Set(http.CanonicalHeaderKey(headerAPIKey), key)
client.Header.Set(http.CanonicalHeaderKey(headerUserAgent), "castai-cluster-controller/"+binVersion.Version)
if level == logrus.TraceLevel {
Expand Down
4 changes: 2 additions & 2 deletions config/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package config

import "fmt"

type AgentActionsVersion struct {
type ClusterControllerVersion struct {
GitCommit, GitRef, Version string
}

func (a *AgentActionsVersion) String() string {
func (a *ClusterControllerVersion) String() string {
return fmt.Sprintf("GitCommit=%q GitRef=%q Version=%q", a.GitCommit, a.GitRef, a.Version)
}
7 changes: 4 additions & 3 deletions hack/remote/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ cd "$(git rev-parse --show-toplevel)"
GOOS=linux go build -o bin/castai-cluster-controller .

DOCKER_IMAGE_REPO=europe-west3-docker.pkg.dev/ci-master-mo3d/tilt/$USER/castai-cluster-controller
docker build -t $DOCKER_IMAGE_REPO:latest .
docker push $DOCKER_IMAGE_REPO:latest
IMAGE_TAG=latest
docker build -t $DOCKER_IMAGE_REPO:$IMAGE_TAG .
docker push $DOCKER_IMAGE_REPO:$IMAGE_TAG

helm template cluster-controller castai-helm/castai-cluster-controller \
-f ./hack/remote/values.yaml \
Expand All @@ -33,4 +34,4 @@ helm template cluster-controller castai-helm/castai-cluster-controller \
--set apiURL="$API_URL" \
--set clusterID="$CLUSTER_ID" | kubectl apply -f - -n castai-agent

kubectl rollout restart deployment castai-cluster-controller -n castai-agent
kubectl rollout restart deployment castai-cluster-controller -n castai-agent
2 changes: 1 addition & 1 deletion hack/remote/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ helm repo add castai-helm https://castai.github.io/helm-charts
helm repo update

$DOCKER_SECRET_TMPL_PATH castai-agent | kubectl apply -f - -n castai-agent
kubectl patch serviceaccount castai-cluster-controller -p '{"imagePullSecrets": [{"name": "artifact-registry"}]}'
kubectl patch serviceaccount castai-cluster-controller -p '{"imagePullSecrets": [{"name": "artifact-registry"}]}' -n castai-agent
43 changes: 25 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,14 @@ var (
func main() {
cfg := config.Get()

binVersion := &config.AgentActionsVersion{
binVersion := &config.ClusterControllerVersion{
GitCommit: GitCommit,
GitRef: GitRef,
Version: Version,
}

logger := logrus.New()
logger.SetLevel(logrus.Level(cfg.Log.Level))
logger.WithFields(logrus.Fields{
"version": binVersion.Version,
})
logger.Infof("running castai-cluster-controller version %v", binVersion)

client := castai.NewClient(
logger,
Expand All @@ -51,7 +47,7 @@ func main() {
)

log := logrus.WithFields(logrus.Fields{})
if err := run(signals.SetupSignalHandler(), logger, client, logger, cfg); err != nil {
if err := run(signals.SetupSignalHandler(), client, logger, cfg, binVersion); err != nil {
logErr := &logContextErr{}
if errors.As(err, &logErr) {
log = logger.WithFields(logErr.fields)
Expand All @@ -60,14 +56,19 @@ func main() {
}
}

func run(ctx context.Context, log logrus.FieldLogger, client castai.Client, logger *logrus.Logger, cfg config.Config) (reterr error) {
func run(
ctx context.Context,
client castai.Client,
logger *logrus.Logger,
cfg config.Config,
binVersion *config.ClusterControllerVersion,
) (reterr error) {
fields := logrus.Fields{}

defer func() {
if reterr == nil {
return
}

reterr = &logContextErr{
err: reterr,
fields: fields,
Expand All @@ -78,7 +79,7 @@ func run(ctx context.Context, log logrus.FieldLogger, client castai.Client, logg
logger.AddHook(e)
logrus.RegisterExitHandler(e.Wait)

restconfig, err := retrieveKubeConfig(log)
restconfig, err := retrieveKubeConfig(logger)
if err != nil {
return err
}
Expand All @@ -88,6 +89,17 @@ func run(ctx context.Context, log logrus.FieldLogger, client castai.Client, logg
return err
}

k8sVersion, err := version.Get(clientset)
if err != nil {
panic(fmt.Errorf("failed getting kubernetes version: %v", err))
}

log := logger.WithFields(logrus.Fields{
"version": binVersion.Version,
"k8s_version": k8sVersion.Full(),
})
log.Infof("running castai-cluster-controller version %v", binVersion)

if cfg.PprofPort != 0 {
go func() {
addr := fmt.Sprintf(":%d", cfg.PprofPort)
Expand All @@ -98,14 +110,6 @@ func run(ctx context.Context, log logrus.FieldLogger, client castai.Client, logg
}()
}

v, err := version.Get(log, clientset)
if err != nil {
panic(fmt.Errorf("failed getting kubernetes version: %v", err))
}

fields["k8s_version"] = v.Full()
log = log.WithFields(fields)

actionsConfig := actions.Config{
PollWaitInterval: 5 * time.Second,
PollTimeout: 5 * time.Minute,
Expand All @@ -115,8 +119,11 @@ func run(ctx context.Context, log logrus.FieldLogger, client castai.Client, logg
ClusterID: cfg.ClusterID,
}
svc := actions.NewService(log, actionsConfig, clientset, client)
svc.Run(ctx)

// Run action service. Blocks.
if err := svc.Run(ctx); err != nil && !errors.Is(err, context.Canceled) {
return err
}
return nil
}

Expand Down
5 changes: 1 addition & 4 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"regexp"
"strconv"

"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/kubernetes"
)
Expand All @@ -16,7 +15,7 @@ type Interface interface {
MinorInt() int
}

func Get(log logrus.FieldLogger, clientset kubernetes.Interface) (Interface, error) {
func Get(clientset kubernetes.Interface) (Interface, error) {
cs, ok := clientset.(*kubernetes.Clientset)
if !ok {
return nil, fmt.Errorf("expected clientset to be of type *kubernetes.Clientset but was %T", clientset)
Expand All @@ -27,8 +26,6 @@ func Get(log logrus.FieldLogger, clientset kubernetes.Interface) (Interface, err
return nil, fmt.Errorf("getting server version: %w", err)
}

log.Infof("kubernetes version %s.%s", sv.Major, sv.Minor)

m, err := strconv.Atoi(regexp.MustCompile(`^(\d+)`).FindString(sv.Minor))
if err != nil {
return nil, fmt.Errorf("parsing minor version: %w", err)
Expand Down
3 changes: 1 addition & 2 deletions version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http/httptest"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/kubernetes"
Expand All @@ -33,7 +32,7 @@ func Test(t *testing.T) {
defer s.Close()
client := kubernetes.NewForConfigOrDie(&rest.Config{Host: s.URL})

got, err := Get(logrus.New(), client)
got, err := Get(client)
if err != nil {
return
}
Expand Down

0 comments on commit 3a0cdb5

Please sign in to comment.