forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
136 lines (120 loc) · 4.32 KB
/
config.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright 2018 The Knative Authors
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 gc
import (
"context"
"fmt"
"strconv"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
cm "knative.dev/pkg/configmap"
)
const (
// ConfigName is the name of the config map for garbage collection.
ConfigName = "config-gc"
// Disabled is the value (-1) used by various config map values to indicate
// the setting is disabled.
Disabled = -1
disabled = "disabled"
)
// Config defines the tunable parameters for Garbage Collection.
type Config struct {
// Duration from creation when a Revision should be considered active
// and exempt from GC. Note that GCMaxStaleRevision may override this if set.
// Set Disabled (-1) to disable/ignore duration and always consider active.
RetainSinceCreateTime time.Duration
// Duration from last active when a Revision should be considered active
// and exempt from GC.Note that GCMaxStaleRevision may override this if set.
// Set Disabled (-1) to disable/ignore duration and always consider active.
RetainSinceLastActiveTime time.Duration
// Minimum number of non-active revisions to keep before considering for GC.
MinNonActiveRevisions int64
// Maximum number of non-active revisions to keep before considering for GC.
// regardless of creation or staleness time-bounds.
// Set Disabled (-1) to disable/ignore max.
MaxNonActiveRevisions int64
}
func defaultConfig() *Config {
return &Config{
RetainSinceCreateTime: 48 * time.Hour,
RetainSinceLastActiveTime: 15 * time.Hour,
MinNonActiveRevisions: 20,
MaxNonActiveRevisions: 1000,
}
}
// NewConfigFromConfigMapFunc creates a Config from the supplied ConfigMap func.
func NewConfigFromConfigMapFunc(ctx context.Context) func(configMap *corev1.ConfigMap) (*Config, error) {
return func(configMap *corev1.ConfigMap) (*Config, error) {
c := defaultConfig()
var retainCreate, retainActive, max string
if err := cm.Parse(configMap.Data,
cm.AsString("retain-since-create-time", &retainCreate),
cm.AsString("retain-since-last-active-time", &retainActive),
cm.AsInt64("min-non-active-revisions", &c.MinNonActiveRevisions),
cm.AsString("max-non-active-revisions", &max),
); err != nil {
return nil, fmt.Errorf("failed to parse data: %w", err)
}
// validate V2 settings
if err := parseDisabledOrDuration(retainCreate, &c.RetainSinceCreateTime); err != nil {
return nil, fmt.Errorf("failed to parse retain-since-create-time: %w", err)
}
if err := parseDisabledOrDuration(retainActive, &c.RetainSinceLastActiveTime); err != nil {
return nil, fmt.Errorf("failed to parse retain-since-last-active-time: %w", err)
}
if err := parseDisabledOrInt64(max, &c.MaxNonActiveRevisions); err != nil {
return nil, fmt.Errorf("failed to parse max-non-active-revisions: %w", err)
}
if c.MinNonActiveRevisions < 0 {
return nil, fmt.Errorf("min-non-active-revisions must be non-negative, was: %d", c.MinNonActiveRevisions)
}
if c.MaxNonActiveRevisions >= 0 && c.MinNonActiveRevisions > c.MaxNonActiveRevisions {
return nil, fmt.Errorf("min-non-active-revisions(%d) must be <= max-non-active-revisions(%d)", c.MinNonActiveRevisions, c.MaxNonActiveRevisions)
}
return c, nil
}
}
func parseDisabledOrInt64(val string, toSet *int64) error {
switch {
case val == "":
// keep default value
case strings.EqualFold(val, disabled):
*toSet = Disabled
default:
parsed, err := strconv.ParseUint(val, 10, 64)
if err != nil {
return err
}
*toSet = int64(parsed)
}
return nil
}
func parseDisabledOrDuration(val string, toSet *time.Duration) error {
switch {
case val == "":
// keep default value
case strings.EqualFold(val, disabled):
*toSet = time.Duration(Disabled)
default:
parsed, err := time.ParseDuration(val)
if err != nil {
return err
}
if parsed < 0 {
return fmt.Errorf("must be non-negative")
}
*toSet = parsed
}
return nil
}