Skip to content

Commit 0b6b512

Browse files
Fix Generate Sample for Getting Started Tutorial
1 parent a7f6e03 commit 0b6b512

File tree

14 files changed

+73
-307
lines changed

14 files changed

+73
-307
lines changed

docs/book/src/getting-started/testdata/project/PROJECT

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# This file is used to track the info used to scaffold your project
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
5-
componentConfig: true
65
domain: example.com
76
layout:
87
- go.kubebuilder.io/v4

docs/book/src/getting-started/testdata/project/api/v1alpha1/memcached_types.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package v1alpha1
1818

1919
import (
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21-
cfg "sigs.k8s.io/controller-runtime/pkg/config/v1alpha1"
2221
)
2322

2423
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
@@ -65,10 +64,6 @@ type Memcached struct {
6564

6665
Spec MemcachedSpec `json:"spec,omitempty"`
6766
Status MemcachedStatus `json:"status,omitempty"`
68-
// ControllerManagerConfigurationSpec returns the configurations for controllers
69-
cfg.ControllerManagerConfigurationSpec `json:",inline"`
70-
71-
ClusterName string `json:"clusterName,omitempty"`
7267
}
7368

7469
//+kubebuilder:object:root=true

docs/book/src/getting-started/testdata/project/api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/book/src/getting-started/testdata/project/cmd/main.go

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"crypto/tls"
2021
"flag"
2122
"os"
2223

@@ -30,6 +31,8 @@ import (
3031
ctrl "sigs.k8s.io/controller-runtime"
3132
"sigs.k8s.io/controller-runtime/pkg/healthz"
3233
"sigs.k8s.io/controller-runtime/pkg/log/zap"
34+
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
35+
"sigs.k8s.io/controller-runtime/pkg/webhook"
3336

3437
cachev1alpha1 "example.com/memcached/api/v1alpha1"
3538
"example.com/memcached/internal/controller"
@@ -49,11 +52,20 @@ func init() {
4952
}
5053

5154
func main() {
52-
var configFile string
53-
flag.StringVar(&configFile, "config", "",
54-
"The controller will load its initial configuration from this file. "+
55-
"Omit this flag to use the default configuration values. "+
56-
"Command-line flags override configuration from this file.")
55+
var metricsAddr string
56+
var enableLeaderElection bool
57+
var probeAddr string
58+
var secureMetrics bool
59+
var enableHTTP2 bool
60+
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
61+
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
62+
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
63+
"Enable leader election for controller manager. "+
64+
"Enabling this will ensure there is only one active controller manager.")
65+
flag.BoolVar(&secureMetrics, "metrics-secure", false,
66+
"If set the metrics endpoint is served securely")
67+
flag.BoolVar(&enableHTTP2, "enable-http2", false,
68+
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
5769
opts := zap.Options{
5870
Development: true,
5971
}
@@ -62,18 +74,49 @@ func main() {
6274

6375
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
6476

65-
var err error
66-
ctrlConfig := cachev1alpha1.Memcached{}
67-
options := ctrl.Options{Scheme: scheme}
68-
if configFile != "" {
69-
options, err = options.AndFrom(ctrl.ConfigFile().AtPath(configFile).OfKind(&ctrlConfig))
70-
if err != nil {
71-
setupLog.Error(err, "unable to load the config file")
72-
os.Exit(1)
73-
}
77+
// if the enable-http2 flag is false (the default), http/2 should be disabled
78+
// due to its vulnerabilities. More specifically, disabling http/2 will
79+
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
80+
// Rapid Reset CVEs. For more information see:
81+
// - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
82+
// - https://github.com/advisories/GHSA-4374-p667-p6c8
83+
disableHTTP2 := func(c *tls.Config) {
84+
setupLog.Info("disabling http/2")
85+
c.NextProtos = []string{"http/1.1"}
7486
}
7587

76-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
88+
tlsOpts := []func(*tls.Config){}
89+
if !enableHTTP2 {
90+
tlsOpts = append(tlsOpts, disableHTTP2)
91+
}
92+
93+
webhookServer := webhook.NewServer(webhook.Options{
94+
TLSOpts: tlsOpts,
95+
})
96+
97+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
98+
Scheme: scheme,
99+
Metrics: metricsserver.Options{
100+
BindAddress: metricsAddr,
101+
SecureServing: secureMetrics,
102+
TLSOpts: tlsOpts,
103+
},
104+
WebhookServer: webhookServer,
105+
HealthProbeBindAddress: probeAddr,
106+
LeaderElection: enableLeaderElection,
107+
LeaderElectionID: "4b13cc52.example.com",
108+
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
109+
// when the Manager ends. This requires the binary to immediately end when the
110+
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
111+
// speeds up voluntary leader transitions as the new leader don't have to wait
112+
// LeaseDuration time first.
113+
//
114+
// In the default scaffold provided, the program ends immediately after
115+
// the manager stops, so would be fine to enable this option. However,
116+
// if you are doing or is intended to do any operation such as perform cleanups
117+
// after the manager stops then its usage might be unsafe.
118+
// LeaderElectionReleaseOnCancel: true,
119+
})
77120
if err != nil {
78121
setupLog.Error(err, "unable to start manager")
79122
os.Exit(1)

docs/book/src/getting-started/testdata/project/config/crd/bases/cache.example.com_memcacheds.yaml

Lines changed: 0 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -26,73 +26,6 @@ spec:
2626
may reject unrecognized values.
2727
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
2828
type: string
29-
cacheNamespace:
30-
description: |-
31-
CacheNamespace if specified restricts the manager's cache to watch objects in
32-
the desired namespace Defaults to all namespaces
33-
34-
35-
Note: If a namespace is specified, controllers can still Watch for a
36-
cluster-scoped resource (e.g Node). For namespaced resources the cache
37-
will only hold objects from the desired namespace.
38-
type: string
39-
clusterName:
40-
type: string
41-
controller:
42-
description: |-
43-
Controller contains global configuration options for controllers
44-
registered within this manager.
45-
properties:
46-
cacheSyncTimeout:
47-
description: |-
48-
CacheSyncTimeout refers to the time limit set to wait for syncing caches.
49-
Defaults to 2 minutes if not set.
50-
format: int64
51-
type: integer
52-
groupKindConcurrency:
53-
additionalProperties:
54-
type: integer
55-
description: |-
56-
GroupKindConcurrency is a map from a Kind to the number of concurrent reconciliation
57-
allowed for that controller.
58-
59-
60-
When a controller is registered within this manager using the builder utilities,
61-
users have to specify the type the controller reconciles in the For(...) call.
62-
If the object's kind passed matches one of the keys in this map, the concurrency
63-
for that controller is set to the number specified.
64-
65-
66-
The key is expected to be consistent in form with GroupKind.String(),
67-
e.g. ReplicaSet in apps group (regardless of version) would be `ReplicaSet.apps`.
68-
type: object
69-
recoverPanic:
70-
description: RecoverPanic indicates if panics should be recovered.
71-
type: boolean
72-
type: object
73-
gracefulShutDown:
74-
description: |-
75-
GracefulShutdownTimeout is the duration given to runnable to stop before the manager actually returns on stop.
76-
To disable graceful shutdown, set to time.Duration(0)
77-
To use graceful shutdown without timeout, set to a negative duration, e.G. time.Duration(-1)
78-
The graceful shutdown is skipped for safety reasons in case the leader election lease is lost.
79-
type: string
80-
health:
81-
description: Health contains the controller health configuration
82-
properties:
83-
healthProbeBindAddress:
84-
description: |-
85-
HealthProbeBindAddress is the TCP address that the controller should bind to
86-
for serving health probes
87-
It can be set to "0" or "" to disable serving the health probe.
88-
type: string
89-
livenessEndpointName:
90-
description: LivenessEndpointName, defaults to "healthz"
91-
type: string
92-
readinessEndpointName:
93-
description: ReadinessEndpointName, defaults to "readyz"
94-
type: string
95-
type: object
9629
kind:
9730
description: |-
9831
Kind is a string value representing the REST resource this object represents.
@@ -101,75 +34,8 @@ spec:
10134
In CamelCase.
10235
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
10336
type: string
104-
leaderElection:
105-
description: |-
106-
LeaderElection is the LeaderElection config to be used when configuring
107-
the manager.Manager leader election
108-
properties:
109-
leaderElect:
110-
description: |-
111-
leaderElect enables a leader election client to gain leadership
112-
before executing the main loop. Enable this when running replicated
113-
components for high availability.
114-
type: boolean
115-
leaseDuration:
116-
description: |-
117-
leaseDuration is the duration that non-leader candidates will wait
118-
after observing a leadership renewal until attempting to acquire
119-
leadership of a led but unrenewed leader slot. This is effectively the
120-
maximum duration that a leader can be stopped before it is replaced
121-
by another candidate. This is only applicable if leader election is
122-
enabled.
123-
type: string
124-
renewDeadline:
125-
description: |-
126-
renewDeadline is the interval between attempts by the acting master to
127-
renew a leadership slot before it stops leading. This must be less
128-
than or equal to the lease duration. This is only applicable if leader
129-
election is enabled.
130-
type: string
131-
resourceLock:
132-
description: |-
133-
resourceLock indicates the resource object type that will be used to lock
134-
during leader election cycles.
135-
type: string
136-
resourceName:
137-
description: |-
138-
resourceName indicates the name of resource object that will be used to lock
139-
during leader election cycles.
140-
type: string
141-
resourceNamespace:
142-
description: |-
143-
resourceName indicates the namespace of resource object that will be used to lock
144-
during leader election cycles.
145-
type: string
146-
retryPeriod:
147-
description: |-
148-
retryPeriod is the duration the clients should wait between attempting
149-
acquisition and renewal of a leadership. This is only applicable if
150-
leader election is enabled.
151-
type: string
152-
required:
153-
- leaderElect
154-
- leaseDuration
155-
- renewDeadline
156-
- resourceLock
157-
- resourceName
158-
- resourceNamespace
159-
- retryPeriod
160-
type: object
16137
metadata:
16238
type: object
163-
metrics:
164-
description: Metrics contains the controller metrics configuration
165-
properties:
166-
bindAddress:
167-
description: |-
168-
BindAddress is the TCP address that the controller should bind to
169-
for serving prometheus metrics.
170-
It can be set to "0" to disable the metrics serving.
171-
type: string
172-
type: object
17339
spec:
17440
description: MemcachedSpec defines the desired state of Memcached
17541
properties:
@@ -261,36 +127,6 @@ spec:
261127
type: object
262128
type: array
263129
type: object
264-
syncPeriod:
265-
description: |-
266-
SyncPeriod determines the minimum frequency at which watched resources are
267-
reconciled. A lower period will correct entropy more quickly, but reduce
268-
responsiveness to change if there are many watched resources. Change this
269-
value only if you know what you are doing. Defaults to 10 hours if unset.
270-
there will a 10 percent jitter between the SyncPeriod of all controllers
271-
so that all controllers will not send list requests simultaneously.
272-
type: string
273-
webhook:
274-
description: Webhook contains the controllers webhook configuration
275-
properties:
276-
certDir:
277-
description: |-
278-
CertDir is the directory that contains the server key and certificate.
279-
if not set, webhook server would look up the server key and certificate in
280-
{TempDir}/k8s-webhook-server/serving-certs. The server key and certificate
281-
must be named tls.key and tls.crt, respectively.
282-
type: string
283-
host:
284-
description: |-
285-
Host is the hostname that the webhook server binds to.
286-
It is used to set webhook.Server.Host.
287-
type: string
288-
port:
289-
description: |-
290-
Port is the port that the webhook server serves at.
291-
It is used to set webhook.Server.Port.
292-
type: integer
293-
type: object
294130
type: object
295131
served: true
296132
storage: true

docs/book/src/getting-started/testdata/project/config/default/kustomization.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ patches:
3232
# endpoint w/o any authn/z, please comment the following line.
3333
- path: manager_auth_proxy_patch.yaml
3434

35-
# Mount the controller config file for loading manager configurations
36-
# through a ComponentConfig type
37-
- path: manager_config_patch.yaml
38-
3935
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
4036
# crd/kustomization.yaml
4137
#- path: manager_webhook_patch.yaml

docs/book/src/getting-started/testdata/project/config/default/manager_auth_proxy_patch.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ spec:
3232
requests:
3333
cpu: 5m
3434
memory: 64Mi
35+
- name: manager
36+
args:
37+
- "--health-probe-bind-address=:8081"
38+
- "--metrics-bind-address=127.0.0.1:8080"
39+
- "--leader-elect"

docs/book/src/getting-started/testdata/project/config/default/manager_config_patch.yaml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,3 @@ spec:
88
spec:
99
containers:
1010
- name: manager
11-
args:
12-
- "--config=controller_manager_config.yaml"
13-
volumeMounts:
14-
- name: manager-config
15-
mountPath: /controller_manager_config.yaml
16-
subPath: controller_manager_config.yaml
17-
volumes:
18-
- name: manager-config
19-
configMap:
20-
name: manager-config

docs/book/src/getting-started/testdata/project/config/manager/controller_manager_config.yaml

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,2 @@
11
resources:
22
- manager.yaml
3-
4-
generatorOptions:
5-
disableNameSuffixHash: true
6-
7-
configMapGenerator:
8-
- name: manager-config
9-
files:
10-
- controller_manager_config.yaml

0 commit comments

Comments
 (0)