• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

operator-framework / operator-sdk / 12323500562

13 Dec 2024 09:44PM UTC coverage: 34.569%. First build
12323500562

Pull #6878

github

acornett21
k8s 1.31 work

Signed-off-by: Adam D. Cornett <adc@redhat.com>
Pull Request #6878: k8s 1.31 work

9 of 22 new or added lines in 15 files covered. (40.91%)

4766 of 13787 relevant lines covered (34.57%)

13.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/internal/helm/flags/flag.go
1
// Copyright 2019 The Operator-SDK Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package flags
16

17
import (
18
        "crypto/tls"
19
        "runtime"
20
        "time"
21

22
        "github.com/spf13/pflag"
23
        "k8s.io/client-go/tools/leaderelection/resourcelock"
24
        "sigs.k8s.io/controller-runtime/pkg/manager"
25
        "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
26
        "sigs.k8s.io/controller-runtime/pkg/webhook"
27
)
28

29
// Flags - Options to be used by a helm operator
30
type Flags struct {
31
        ReconcilePeriod         time.Duration
32
        WatchesFile             string
33
        MetricsBindAddress      string
34
        LeaderElection          bool
35
        LeaderElectionID        string
36
        LeaderElectionNamespace string
37
        MaxConcurrentReconciles int
38
        ProbeAddr               string
39
        SuppressOverrideValues  bool
40
        EnableHTTP2             bool
41
        SecureMetrics           bool
42
        MetricsRequireRBAC      bool
43

44
        // If not nil, used to deduce which flags were set in the CLI.
45
        flagSet *pflag.FlagSet
46
}
47

48
// AddTo - Add the helm operator flags to the flagset
49
func (f *Flags) AddTo(flagSet *pflag.FlagSet) {
4✔
50
        // Store flagset internally to be used for lookups later.
4✔
51
        f.flagSet = flagSet
4✔
52

4✔
53
        // Helm flags.
4✔
54
        flagSet.StringVar(&f.WatchesFile,
4✔
55
                "watches-file",
4✔
56
                "./watches.yaml",
4✔
57
                "Path to the watches file to use",
4✔
58
        )
4✔
59

4✔
60
        // Controller flags.
4✔
61
        flagSet.DurationVar(&f.ReconcilePeriod,
4✔
62
                "reconcile-period",
4✔
63
                time.Minute,
4✔
64
                "Default reconcile period for controllers",
4✔
65
        )
4✔
66
        flagSet.IntVar(&f.MaxConcurrentReconciles,
4✔
67
                "max-concurrent-reconciles",
4✔
68
                runtime.NumCPU(),
4✔
69
                "Maximum number of concurrent reconciles for controllers.",
4✔
70
        )
4✔
71

4✔
72
        _ = flagSet.MarkDeprecated("config",
4✔
73
                `controller-runtime has deprecated the ComponentConfig package 
4✔
74
and as such, the ability to load the configuation from a file. Since the helm operator relies on controller-runtime
4✔
75
this flag will be removed when upgrading to a version of controller-runtime where the ComponentConfig package has been removed.
4✔
76
see https://github.com/kubernetes-sigs/controller-runtime/issues/895 for more information.`)
4✔
77

4✔
78
        // TODO(2.0.0): remove
4✔
79
        flagSet.StringVar(&f.MetricsBindAddress,
4✔
80
                "metrics-addr",
4✔
81
                ":8080",
4✔
82
                "The address the metric endpoint binds to",
4✔
83
        )
4✔
84
        _ = flagSet.MarkDeprecated("metrics-addr", "use --metrics-bind-address instead")
4✔
85
        flagSet.StringVar(&f.MetricsBindAddress,
4✔
86
                "metrics-bind-address",
4✔
87
                ":8080",
4✔
88
                "The address the metric endpoint binds to",
4✔
89
        )
4✔
90
        // TODO(2.0.0): for Go/Helm the port used is: 8081
4✔
91
        // update it to keep the project aligned to the other
4✔
92
        flagSet.StringVar(&f.ProbeAddr,
4✔
93
                "health-probe-bind-address",
4✔
94
                ":8081",
4✔
95
                "The address the probe endpoint binds to.",
4✔
96
        )
4✔
97
        // TODO(2.0.0): remove
4✔
98
        flagSet.BoolVar(&f.LeaderElection,
4✔
99
                "enable-leader-election",
4✔
100
                false,
4✔
101
                "Enable leader election for controller manager. Enabling this will"+
4✔
102
                        " ensure there is only one active controller manager.",
4✔
103
        )
4✔
104
        _ = flagSet.MarkDeprecated("enable-leader-election", "use --leader-elect instead.")
4✔
105
        flagSet.BoolVar(&f.LeaderElection,
4✔
106
                "leader-elect",
4✔
107
                false,
4✔
108
                "Enable leader election for controller manager. Enabling this will"+
4✔
109
                        " ensure there is only one active controller manager.",
4✔
110
        )
4✔
111
        flagSet.StringVar(&f.LeaderElectionID,
4✔
112
                "leader-election-id",
4✔
113
                "",
4✔
114
                "Name of the configmap that is used for holding the leader lock.",
4✔
115
        )
4✔
116
        flagSet.StringVar(&f.LeaderElectionNamespace,
4✔
117
                "leader-election-namespace",
4✔
118
                "",
4✔
119
                "Namespace in which to create the leader election configmap for"+
4✔
120
                        " holding the leader lock (required if running locally with leader"+
4✔
121
                        " election enabled).",
4✔
122
        )
4✔
123
        flagSet.BoolVar(&f.SuppressOverrideValues,
4✔
124
                "suppress-override-values",
4✔
125
                false,
4✔
126
                "Silences the override-value for OverrideValuesInUse events",
4✔
127
        )
4✔
128
        flagSet.BoolVar(&f.EnableHTTP2,
4✔
129
                "enable-http2",
4✔
130
                false,
4✔
131
                "enables HTTP/2 on the webhook and metrics servers",
4✔
132
        )
4✔
133
        flagSet.BoolVar(&f.SecureMetrics,
4✔
134
                "metrics-secure",
4✔
135
                false,
4✔
136
                "enables secure serving of the metrics endpoint",
4✔
137
        )
4✔
138
        flagSet.BoolVar(&f.MetricsRequireRBAC,
4✔
139
                "metrics-require-rbac",
4✔
140
                false,
4✔
141
                "enables protection of the metrics endpoint with RBAC-based authn/authz."+
4✔
142
                        "see https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.4/pkg/metrics/filters#WithAuthenticationAndAuthorization for more info")
4✔
143
}
4✔
144

145
// ToManagerOptions uses the flag set in f to configure options.
146
// Values of options take precedence over flag defaults,
147
// as values are assume to have been explicitly set.
148
func (f *Flags) ToManagerOptions(options manager.Options) manager.Options {
4✔
149
        // Alias FlagSet.Changed so options are still updated when fields are empty.
4✔
150
        changed := func(flagName string) bool {
30✔
151
                return f.flagSet.Changed(flagName)
26✔
152
        }
26✔
153
        if f.flagSet == nil {
4✔
NEW
154
                //nolint:golint
×
NEW
155
                changed = func(_ string) bool { return false }
×
156
        }
157

158
        // TODO(2.0.0): remove metrics-addr
159
        if changed("metrics-bind-address") || changed("metrics-addr") || options.Metrics.BindAddress == "" {
7✔
160
                options.Metrics.BindAddress = f.MetricsBindAddress
3✔
161
        }
3✔
162
        if changed("health-probe-bind-address") || options.HealthProbeBindAddress == "" {
8✔
163
                options.HealthProbeBindAddress = f.ProbeAddr
4✔
164
        }
4✔
165
        // TODO(2.0.0): remove enable-leader-election
166
        if changed("leader-elect") || changed("enable-leader-election") || !options.LeaderElection {
8✔
167
                options.LeaderElection = f.LeaderElection
4✔
168
        }
4✔
169
        if changed("leader-election-id") || options.LeaderElectionID == "" {
8✔
170
                options.LeaderElectionID = f.LeaderElectionID
4✔
171
        }
4✔
172
        if changed("leader-election-namespace") || options.LeaderElectionNamespace == "" {
8✔
173
                options.LeaderElectionNamespace = f.LeaderElectionNamespace
4✔
174
        }
4✔
175
        if options.LeaderElectionResourceLock == "" {
8✔
176
                options.LeaderElectionResourceLock = resourcelock.LeasesResourceLock
4✔
177
        }
4✔
178

179
        disableHTTP2 := func(c *tls.Config) {
4✔
180
                c.NextProtos = []string{"http/1.1"}
×
181
        }
×
182
        if !f.EnableHTTP2 {
8✔
183
                options.WebhookServer = webhook.NewServer(webhook.Options{
4✔
184
                        TLSOpts: []func(*tls.Config){disableHTTP2},
4✔
185
                })
4✔
186
                options.Metrics.TLSOpts = append(options.Metrics.TLSOpts, disableHTTP2)
4✔
187
        }
4✔
188
        options.Metrics.SecureServing = f.SecureMetrics
4✔
189

4✔
190
        if f.MetricsRequireRBAC {
4✔
191
                // FilterProvider is used to protect the metrics endpoint with authn/authz.
×
192
                // These configurations ensure that only authorized users and service accounts
×
193
                // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
×
194
                // https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.4/pkg/metrics/filters#WithAuthenticationAndAuthorization
×
195
                options.Metrics.FilterProvider = filters.WithAuthenticationAndAuthorization
×
196
        }
×
197

198
        return options
4✔
199
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc