From 4b9d5c98afaacff200b5bfcfa9e63663264e24d7 Mon Sep 17 00:00:00 2001 From: Lasse Gaardsholt Date: Mon, 30 Oct 2023 09:25:37 +0100 Subject: [PATCH] Proper handle empty string + error handling (#86) * empty strings should be ignored Signed-off-by: Lasse Gaardsholt * better error handling Signed-off-by: Lasse Gaardsholt * oops Signed-off-by: Lasse Gaardsholt --------- Signed-off-by: Lasse Gaardsholt --- pubsub/listen.go | 6 ++++-- pubsub/sendNamespaces.go | 22 ++++++++++++++++++---- timeutil/timeutil.go | 21 +++++++++++++-------- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/pubsub/listen.go b/pubsub/listen.go index 54bf081..f46d467 100644 --- a/pubsub/listen.go +++ b/pubsub/listen.go @@ -42,7 +42,8 @@ func Listen() error { // Unmarshal message var m PubSubMsg if err := json.Unmarshal(msg.Data, &m); err != nil { - logger.Log.Fatal().Err(err).Msg("Failed to unmarshal message") + logger.Log.Error().Err(err).Msg("Failed to unmarshal message") + return } if m.Cluster != config.Config.ClusterName { @@ -53,7 +54,8 @@ func Listen() error { logger.Log.Info().Msgf("Scaling %s namespace %s up", m.Cluster, m.Namespace) convertIntToTimeDuration, err := time.ParseDuration(fmt.Sprintf("%dh", m.Duration)) if err != nil { - logger.Log.Fatal().Err(err).Msg("Failed to parse duration") + logger.Log.Error().Err(err).Msg("Failed to parse duration") + return } logger.Log.Debug().Msgf("Duration: %d, Duration in time.Duration: %s", m.Duration, convertIntToTimeDuration) diff --git a/pubsub/sendNamespaces.go b/pubsub/sendNamespaces.go index 049934e..2180428 100644 --- a/pubsub/sendNamespaces.go +++ b/pubsub/sendNamespaces.go @@ -7,28 +7,42 @@ import ( "github.com/BESTSELLER/nightscaler/k8s" "github.com/BESTSELLER/nightscaler/logger" "github.com/BESTSELLER/nightscaler/timeutil" + v1 "k8s.io/api/core/v1" ) func SendNamespaces() { namespaces, err := k8s.List() if err != nil { - logger.Log.Fatal().Err(err).Msg("failed to list namespaces") + logger.Log.Error().Err(err).Msg("failed to list namespaces") } + tempNamespaces := []v1.Namespace{} + for _, ns := range namespaces { s := ns.Annotations[config.NAMESPACE_ANNOTATION] - startTime, endTime := timeutil.GetUptimes(s) + if s == "" { + continue + } + + startTime, endTime, err := timeutil.GetUptimes(s) + if err != nil { + logger.Log.Error().Err(err).Msg("failed to parse uptime") + return + } ns.Annotations["nightscaler/uptime-start"] = strconv.FormatInt(startTime.UnixMilli(), 10) ns.Annotations["nightscaler/uptime-end"] = strconv.FormatInt(endTime.UnixMilli(), 10) + + tempNamespaces = append(tempNamespaces, ns) } - err = Publish(namespaces, Attributes{ + err = Publish(tempNamespaces, Attributes{ Entity: "namespace", Action: "create", }) if err != nil { - logger.Log.Fatal().Err(err).Msg("failed to send Pub/Sub message") + logger.Log.Error().Err(err).Msg("failed to send Pub/Sub message") + return } } diff --git a/timeutil/timeutil.go b/timeutil/timeutil.go index f58a1d3..f03a762 100644 --- a/timeutil/timeutil.go +++ b/timeutil/timeutil.go @@ -24,36 +24,41 @@ var WeekDay = map[string]Day{ "Sun": {"Sunday", 6}, } -func GetUptimes(s string) (time.Time, time.Time) { - _, startTime, endTime := isUptimeNow(s) +func GetUptimes(s string) (time.Time, time.Time, error) { + _, startTime, endTime, err := isUptimeNow(s) - return startTime, endTime + return startTime, endTime, err } -func isUptimeNow(s string) (bool, time.Time, time.Time) { +func isUptimeNow(s string) (bool, time.Time, time.Time, error) { now := time.Now() uptimeSplit := strings.Split(s, " ") + + if len(uptimeSplit) != 3 { + return false, time.Time{}, time.Time{}, fmt.Errorf("'%s' is an invalid uptime format", s) + } + weekDays := uptimeSplit[0] timeRange := uptimeSplit[1] timeZone := uptimeSplit[2] days, err := getWeekDays(weekDays) if err != nil { - panic(err) + return false, time.Time{}, time.Time{}, err } isIt := isCurrentDay(days, now) if !isIt { - return false, time.Time{}, time.Time{} + return false, time.Time{}, time.Time{}, nil } startTime, endTime, err := getTimeBoundary(timeRange, timeZone, now) if err != nil { - panic(err) + return false, time.Time{}, time.Time{}, err } - return now.After(startTime) && now.Before(endTime), startTime, endTime + return now.After(startTime) && now.Before(endTime), startTime, endTime, nil } func getWeekDays(weekDays string) ([]Day, error) {