From 6a63daab73127efc99db24532082884f79daf95e Mon Sep 17 00:00:00 2001 From: Tobias Trabelsi Date: Fri, 10 Nov 2023 21:14:49 +0100 Subject: [PATCH] error handling in agent parsing --- internal/hetzner/hetzneragent.go | 8 ++++---- internal/utils/utils.go | 14 +++++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/internal/hetzner/hetzneragent.go b/internal/hetzner/hetzneragent.go index 264362b..33f9537 100644 --- a/internal/hetzner/hetzneragent.go +++ b/internal/hetzner/hetzneragent.go @@ -82,17 +82,17 @@ func CreateNewAgent(cfg *config.Config) (*hcloud.Server, error) { keys = append(keys, key) } img, _, err := client.Image.GetByNameAndArchitecture(context.Background(), "docker-ce", "x86") + utils.CheckError(err, "GetImageByNameAndArchitecture") loc, _, err := client.Location.GetByName(context.Background(), cfg.HcloudRegion) + utils.CheckError(err, "GetRegionByName") pln, _, err := client.ServerType.GetByName(context.Background(), cfg.HcloudInstanceType) + utils.CheckError(err, "GetServerTypeByName") dc, _, err := client.Datacenter.GetByName(context.Background(), cfg.HcloudDatacenter) + utils.CheckError(err, "GetDatacenterByName") labels := map[string]string{} labels["Role"] = "WoodpeckerAgent" labels["ControledBy"] = "WoodpeckerAutoscaler" - if err != nil { - return nil, errors.New(fmt.Sprintf("Could not parse agent spec: %s", err.Error())) - } - networkConf := hcloud.ServerCreatePublicNet{ EnableIPv4: !cfg.HcloudIPv6Only, EnableIPv6: true, diff --git a/internal/utils/utils.go b/internal/utils/utils.go index b2103f4..3cb1904 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -1,6 +1,10 @@ package utils -import "math/rand" +import ( + "math/rand" + + log "github.com/sirupsen/logrus" +) const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" @@ -15,3 +19,11 @@ func RandStringBytes(n int) string { func BoolPointer(b bool) *bool { return &b } + +func CheckError(err error, caller string) { + if err != nil { + log.WithFields(log.Fields{ + "Caller": caller, + }).Warnf("Error from hetzner API: %s", err.Error()) + } +}