Skip to content

Commit a309fdb

Browse files
authored
chore: re-scaffold project (#539)
## Issue #536 ## Description This PR re-scaffolds the project using `kubebuilder alpha generate`. The 2 major changes are: - Remove all usage of `kube-rbac-proxy` and replace with [WithAuthenticationAndAuthorization ](https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/metrics/filters#WithAuthenticationAndAuthorization) due to `kube-rbac-proxy` being deprecated - Initialize e2e tests. For now, we are only testing basic functionality. The tests should be expanded to cover validator-specific use-cases. --------- Signed-off-by: Artur Shad Nik <arturshadnik@gmail.com>
1 parent 41e8b90 commit a309fdb

32 files changed

Lines changed: 1029 additions & 256 deletions

.golangci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ issues:
66
# don't skip warning about doc comments
77
# don't exclude the default set of lint
88
exclude-use-default: false
9+
# restore some of the defaults
10+
# (fill in the rest as needed)
11+
exclude-dirs:
12+
- tests
913
exclude-files:
1014
- ".*_test\\.go"
1115

@@ -14,6 +18,7 @@ linters:
1418
enable:
1519
- dupl
1620
- errcheck
21+
- copyloopvar
1722
- ginkgolinter
1823
- goconst
1924
- gocyclo
@@ -31,3 +36,8 @@ linters:
3136
- unconvert
3237
- unparam
3338
- unused
39+
40+
linters-settings:
41+
revive:
42+
rules:
43+
- name: comment-spacings

api/v1alpha1/awsvalidator_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ func (r *TagRule) SetName(name string) {
311311
// AwsValidatorStatus defines the observed state of AwsValidator
312312
type AwsValidatorStatus struct{}
313313

314-
//+kubebuilder:object:root=true
315-
//+kubebuilder:subresource:status
314+
// +kubebuilder:object:root=true
315+
// +kubebuilder:subresource:status
316316

317317
// AwsValidator is the Schema for the awsvalidators API
318318
type AwsValidator struct {
@@ -338,7 +338,7 @@ func (v AwsValidator) ResultCount() int {
338338
return v.Spec.ResultCount()
339339
}
340340

341-
//+kubebuilder:object:root=true
341+
// +kubebuilder:object:root=true
342342

343343
// AwsValidatorList contains a list of AwsValidator
344344
type AwsValidatorList struct {

chart/validator-plugin-aws/README.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,7 @@ The following table lists the configurable parameters of the Validator-plugin-aw
1111

1212
| Parameter | Description | Default |
1313
| ------------------------ | ----------------------- | -------------- |
14-
| `controllerManager.kubeRbacProxy.args` | | `["--secure-listen-address=0.0.0.0:8443", "--upstream=http://127.0.0.1:8080/", "--logtostderr=true", "--v=0"]` |
15-
| `controllerManager.kubeRbacProxy.containerSecurityContext.allowPrivilegeEscalation` | | `false` |
16-
| `controllerManager.kubeRbacProxy.containerSecurityContext.capabilities.drop` | | `["ALL"]` |
17-
| `controllerManager.kubeRbacProxy.image.repository` | | `"gcr.io/kubebuilder/kube-rbac-proxy"` |
18-
| `controllerManager.kubeRbacProxy.image.tag` | | `"v0.16.0"` |
19-
| `controllerManager.kubeRbacProxy.resources.limits.cpu` | | `"500m"` |
20-
| `controllerManager.kubeRbacProxy.resources.limits.memory` | | `"128Mi"` |
21-
| `controllerManager.kubeRbacProxy.resources.requests.cpu` | | `"5m"` |
22-
| `controllerManager.kubeRbacProxy.resources.requests.memory` | | `"64Mi"` |
23-
| `controllerManager.manager.args` | | `["--health-probe-bind-address=:8081", "--leader-elect"]` |
14+
| `controllerManager.manager.args` | | `["--health-probe-bind-address=:8081", "--metrics-bind-address=:8443", "--leader-elect"]` |
2415
| `controllerManager.manager.containerSecurityContext.allowPrivilegeEscalation` | | `false` |
2516
| `controllerManager.manager.containerSecurityContext.capabilities.drop` | | `["ALL"]` |
2617
| `controllerManager.manager.image.repository` | | `"quay.io/validator-labs/validator-plugin-aws"` |

chart/validator-plugin-aws/templates/deployment.yaml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ spec:
2323
kubectl.kubernetes.io/default-container: manager
2424
spec:
2525
containers:
26-
- args: {{- toYaml .Values.controllerManager.kubeRbacProxy.args | nindent 8 }}
27-
env:
28-
- name: KUBERNETES_CLUSTER_DOMAIN
29-
value: {{ quote .Values.kubernetesClusterDomain }}
30-
image: {{ .Values.controllerManager.kubeRbacProxy.image.repository }}:{{ .Values.controllerManager.kubeRbacProxy.image.tag | default .Chart.AppVersion }}
31-
name: kube-rbac-proxy
32-
ports:
33-
- containerPort: 8443
34-
name: https
35-
protocol: TCP
36-
resources: {{- toYaml .Values.controllerManager.kubeRbacProxy.resources | nindent 10 }}
37-
securityContext: {{- toYaml .Values.controllerManager.kubeRbacProxy.containerSecurityContext | nindent 10 }}
3826
- args: {{- toYaml .Values.controllerManager.manager.args | nindent 8 }}
3927
command:
4028
- /manager

chart/validator-plugin-aws/templates/proxy-rbac.yaml renamed to chart/validator-plugin-aws/templates/metrics-auth-rbac.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
apiVersion: rbac.authorization.k8s.io/v1
22
kind: ClusterRole
33
metadata:
4-
name: {{ include "chart.fullname" . }}-proxy-role
4+
name: {{ include "chart.fullname" . }}-metrics-auth-role
55
labels:
6-
app.kubernetes.io/component: kube-rbac-proxy
6+
app.kubernetes.io/component: rbac
77
app.kubernetes.io/created-by: validator-plugin-aws
88
app.kubernetes.io/part-of: validator-plugin-aws
99
{{- include "chart.labels" . | nindent 4 }}
@@ -24,16 +24,16 @@ rules:
2424
apiVersion: rbac.authorization.k8s.io/v1
2525
kind: ClusterRoleBinding
2626
metadata:
27-
name: {{ include "chart.fullname" . }}-proxy-rolebinding
27+
name: {{ include "chart.fullname" . }}-metrics-auth-rolebinding
2828
labels:
29-
app.kubernetes.io/component: kube-rbac-proxy
29+
app.kubernetes.io/component: rbac
3030
app.kubernetes.io/created-by: validator-plugin-aws
3131
app.kubernetes.io/part-of: validator-plugin-aws
3232
{{- include "chart.labels" . | nindent 4 }}
3333
roleRef:
3434
apiGroup: rbac.authorization.k8s.io
3535
kind: ClusterRole
36-
name: '{{ include "chart.fullname" . }}-proxy-role'
36+
name: '{{ include "chart.fullname" . }}-metrics-auth-role'
3737
subjects:
3838
- kind: ServiceAccount
3939
name: '{{ include "chart.fullname" . }}-controller-manager'

chart/validator-plugin-aws/templates/metrics-reader-rbac.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ kind: ClusterRole
33
metadata:
44
name: {{ include "chart.fullname" . }}-metrics-reader
55
labels:
6-
app.kubernetes.io/component: kube-rbac-proxy
76
app.kubernetes.io/created-by: validator-plugin-aws
87
app.kubernetes.io/part-of: validator-plugin-aws
98
{{- include "chart.labels" . | nindent 4 }}

chart/validator-plugin-aws/templates/metrics-service.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ kind: Service
33
metadata:
44
name: {{ include "chart.fullname" . }}-controller-manager-metrics-service
55
labels:
6-
app.kubernetes.io/component: kube-rbac-proxy
76
app.kubernetes.io/created-by: validator-plugin-aws
87
app.kubernetes.io/part-of: validator-plugin-aws
98
control-plane: controller-manager

chart/validator-plugin-aws/values.yaml

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,8 @@
11
controllerManager:
2-
kubeRbacProxy:
3-
args:
4-
- --secure-listen-address=0.0.0.0:8443
5-
- --upstream=http://127.0.0.1:8080/
6-
- --logtostderr=true
7-
- --v=0
8-
containerSecurityContext:
9-
allowPrivilegeEscalation: false
10-
capabilities:
11-
drop:
12-
- ALL
13-
image:
14-
repository: gcr.io/kubebuilder/kube-rbac-proxy
15-
tag: v0.16.0
16-
resources:
17-
limits:
18-
cpu: 500m
19-
memory: 128Mi
20-
requests:
21-
cpu: 5m
22-
memory: 64Mi
232
manager:
243
args:
254
- --health-probe-bind-address=:8081
5+
- --metrics-bind-address=:8443
266
- --leader-elect
277
containerSecurityContext:
288
allowPrivilegeEscalation: false

cmd/main.go

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2023.
2+
Copyright 2024.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -14,10 +14,10 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
// Package main initializes an AWSValidator controller.
1817
package main
1918

2019
import (
20+
"crypto/tls"
2121
"flag"
2222
"os"
2323

@@ -31,11 +31,14 @@ import (
3131
ctrl "sigs.k8s.io/controller-runtime"
3232
"sigs.k8s.io/controller-runtime/pkg/healthz"
3333
"sigs.k8s.io/controller-runtime/pkg/log/zap"
34+
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
35+
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
36+
"sigs.k8s.io/controller-runtime/pkg/webhook"
3437

3538
validationv1alpha1 "github.com/validator-labs/validator-plugin-aws/api/v1alpha1"
3639
"github.com/validator-labs/validator-plugin-aws/internal/controller"
3740
validatorv1alpha1 "github.com/validator-labs/validator/api/v1alpha1"
38-
//+kubebuilder:scaffold:imports
41+
// +kubebuilder:scaffold:imports
3942
)
4043

4144
var (
@@ -47,16 +50,26 @@ func init() {
4750
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
4851
utilruntime.Must(validatorv1alpha1.AddToScheme(scheme))
4952
utilruntime.Must(validationv1alpha1.AddToScheme(scheme))
50-
//+kubebuilder:scaffold:scheme
53+
// +kubebuilder:scaffold:scheme
5154
}
5255

5356
func main() {
57+
var metricsAddr string
5458
var enableLeaderElection bool
5559
var probeAddr string
60+
var secureMetrics bool
61+
var enableHTTP2 bool
62+
var tlsOpts []func(*tls.Config)
63+
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
64+
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
5665
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
5766
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
5867
"Enable leader election for controller manager. "+
5968
"Enabling this will ensure there is only one active controller manager.")
69+
flag.BoolVar(&secureMetrics, "metrics-secure", true,
70+
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
71+
flag.BoolVar(&enableHTTP2, "enable-http2", false,
72+
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
6073
opts := zap.Options{
6174
Development: true,
6275
}
@@ -65,11 +78,61 @@ func main() {
6578

6679
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
6780

81+
// if the enable-http2 flag is false (the default), http/2 should be disabled
82+
// due to its vulnerabilities. More specifically, disabling http/2 will
83+
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
84+
// Rapid Reset CVEs. For more information see:
85+
// - https://github.com/advisories/GHSA-qppj-fm5r-hxr3
86+
// - https://github.com/advisories/GHSA-4374-p667-p6c8
87+
disableHTTP2 := func(c *tls.Config) {
88+
setupLog.Info("disabling http/2")
89+
c.NextProtos = []string{"http/1.1"}
90+
}
91+
92+
if !enableHTTP2 {
93+
tlsOpts = append(tlsOpts, disableHTTP2)
94+
}
95+
96+
webhookServer := webhook.NewServer(webhook.Options{
97+
TLSOpts: tlsOpts,
98+
})
99+
100+
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
101+
// More info:
102+
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.1/pkg/metrics/server
103+
// - https://book.kubebuilder.io/reference/metrics.html
104+
metricsServerOptions := metricsserver.Options{
105+
BindAddress: metricsAddr,
106+
SecureServing: secureMetrics,
107+
TLSOpts: tlsOpts,
108+
}
109+
110+
if secureMetrics {
111+
// FilterProvider is used to protect the metrics endpoint with authn/authz.
112+
// These configurations ensure that only authorized users and service accounts
113+
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
114+
// https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.1/pkg/metrics/filters#WithAuthenticationAndAuthorization
115+
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
116+
117+
// TODO(user): If CertDir, CertName, and KeyName are not specified, controller-runtime will automatically
118+
// generate self-signed certificates for the metrics server. While convenient for development and testing,
119+
// this setup is not recommended for production.
120+
121+
// TODO(user): If cert-manager is enabled in config/default/kustomization.yaml,
122+
// you can uncomment the following lines to use the certificate managed by cert-manager.
123+
124+
// metricsServerOptions.CertDir = "/tmp/k8s-metrics-server/metrics-certs"
125+
// metricsServerOptions.CertName = "tls.crt"
126+
// metricsServerOptions.KeyName = "tls.key"
127+
}
128+
68129
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
69130
Scheme: scheme,
131+
Metrics: metricsServerOptions,
132+
WebhookServer: webhookServer,
70133
HealthProbeBindAddress: probeAddr,
71134
LeaderElection: enableLeaderElection,
72-
LeaderElectionID: "1f172fb1.spectrocloud.labs",
135+
LeaderElectionID: "52db6862.spectrocloud.labs",
73136
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
74137
// when the Manager ends. This requires the binary to immediately end when the
75138
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
@@ -95,7 +158,7 @@ func main() {
95158
setupLog.Error(err, "unable to create controller", "controller", "AwsValidator")
96159
os.Exit(1)
97160
}
98-
//+kubebuilder:scaffold:builder
161+
// +kubebuilder:scaffold:builder
99162

100163
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
101164
setupLog.Error(err, "unable to set up health check")

0 commit comments

Comments
 (0)