-
Notifications
You must be signed in to change notification settings - Fork 47
/
miscellaneous.go
98 lines (85 loc) · 3.32 KB
/
miscellaneous.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package utils
import (
"fmt"
"time"
"github.com/robfig/cron/v3"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// MergeStringMaps merges the content of the newMaps with the oldMap. If a key already exists then
// it gets overwritten by the last value with the same key.
func MergeStringMaps(oldMap map[string]string, newMaps ...map[string]string) map[string]string {
var out map[string]string
if oldMap != nil {
out = make(map[string]string)
}
for k, v := range oldMap {
out[k] = v
}
for _, newMap := range newMaps {
if newMap != nil && out == nil {
out = make(map[string]string)
}
for k, v := range newMap {
out[k] = v
}
}
return out
}
func nameAndNamespace(namespaceOrName string, nameOpt ...string) (namespace, name string) {
if len(nameOpt) > 1 {
panic(fmt.Sprintf("more than name/namespace for key specified: %s/%v", namespaceOrName, nameOpt))
}
if len(nameOpt) == 0 {
name = namespaceOrName
return
}
namespace = namespaceOrName
name = nameOpt[0]
return
}
// Key creates a new client.ObjectKey from the given parameters.
// There are only two ways to call this function:
// - If only namespaceOrName is set, then a client.ObjectKey with name set to namespaceOrName is returned.
// - If namespaceOrName and one nameOpt is given, then a client.ObjectKey with namespace set to namespaceOrName
// and name set to nameOpt[0] is returned.
//
// For all other cases, this method panics.
func Key(namespaceOrName string, nameOpt ...string) client.ObjectKey {
namespace, name := nameAndNamespace(namespaceOrName, nameOpt...)
return client.ObjectKey{Namespace: namespace, Name: name}
}
// Max returns the larger of x or y.
func Max(x, y int) int {
if y > x {
return y
}
return x
}
// ComputeScheduleInterval computes the interval between two activations for the given cron schedule.
// Assumes that every cron activation is at equal intervals apart, based on cron schedules such as
// "once every X hours", "once every Y days", "at 1:00pm on every Tuesday", etc.
// TODO: write a new function to accurately compute the previous activation time from the cron schedule
// in order to compute when the previous activation of the cron schedule was supposed to have occurred,
// instead of relying on the assumption that all the cron activations are evenly spaced.
func ComputeScheduleInterval(cronSchedule string) (time.Duration, error) {
schedule, err := cron.ParseStandard(cronSchedule)
if err != nil {
return 0, err
}
nextScheduledTime := schedule.Next(time.Now())
nextNextScheduledTime := schedule.Next(nextScheduledTime)
return nextNextScheduledTime.Sub(nextScheduledTime), nil
}