forked from kcp-dev/init-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
115 lines (88 loc) · 3.69 KB
/
options.go
File metadata and controls
115 lines (88 loc) · 3.69 KB
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
/*
Copyright 2026 The kcp 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 main
import (
"errors"
"fmt"
"github.com/spf13/pflag"
"github.com/kcp-dev/init-agent/internal/log"
"k8s.io/apimachinery/pkg/labels"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
)
type Options struct {
// NB: Not actually defined here, as ctrl-runtime registers its
// own --kubeconfig flag that is required to make its GetConfigOrDie()
// work.
// KubeconfigFile string
// ConfigWorkspace is the kcp workspace (either a path or a cluster name)
// where the InitTarget and InitTemplate objects live that should be processed
// by this init-agent.
ConfigWorkspace string
// Whether or not to perform leader election (requires permissions to
// manage coordination/v1 leases)
EnableLeaderElection bool
// LeaderElectionNamespace is the Kubernetes namespace in which the leader
// election lease will be created.
LeaderElectionNamespace string
InitTargetSelectorString string
InitTargetSelector labels.Selector
LogOptions log.Options
MetricsAddr string
HealthAddr string
}
func NewOptions() *Options {
return &Options{
LogOptions: log.NewDefaultOptions(),
InitTargetSelector: labels.Everything(),
MetricsAddr: "127.0.0.1:8085",
}
}
func (o *Options) AddFlags(flags *pflag.FlagSet) {
o.LogOptions.AddPFlags(flags)
flags.StringVar(&o.ConfigWorkspace, "config-workspace", o.ConfigWorkspace, "kcp workspace or cluster where the InitTargets live that should be processed")
flags.StringVar(&o.InitTargetSelectorString, "init-target-selector", o.InitTargetSelectorString, "restrict to only process InitTargets matching this label selector (optional)")
flags.BoolVar(&o.EnableLeaderElection, "enable-leader-election", o.EnableLeaderElection, "whether to perform leader election")
flags.StringVar(&o.LeaderElectionNamespace, "leader-election-namespace", o.LeaderElectionNamespace, "Kubernetes namespace for the leader election lease")
flags.StringVar(&o.MetricsAddr, "metrics-address", o.MetricsAddr, "host and port to serve Prometheus metrics via /metrics (HTTP)")
flags.StringVar(&o.HealthAddr, "health-address", o.HealthAddr, "host and port to serve probes via /readyz and /healthz (HTTP)")
}
func (o *Options) Validate() error {
errs := []error{}
if err := o.LogOptions.Validate(); err != nil {
errs = append(errs, err)
}
if len(o.ConfigWorkspace) == 0 {
errs = append(errs, errors.New("--config-workspace is required"))
}
if o.EnableLeaderElection && len(o.LeaderElectionNamespace) == 0 {
errs = append(errs, errors.New("--leader-election-namespace is required when --enable-leader-election is true"))
}
if s := o.InitTargetSelectorString; len(s) > 0 {
if _, err := labels.Parse(s); err != nil {
errs = append(errs, fmt.Errorf("invalid --init-target-selector %q: %w", s, err))
}
}
return utilerrors.NewAggregate(errs)
}
func (o *Options) Complete() error {
errs := []error{}
if s := o.InitTargetSelectorString; len(s) > 0 {
selector, err := labels.Parse(s)
if err != nil {
// should never happen since it's caught by Validate()
errs = append(errs, fmt.Errorf("invalid --init-target-selector %q: %w", s, err))
}
o.InitTargetSelector = selector
}
return utilerrors.NewAggregate(errs)
}