Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: kubernetes-sigs/kubebuilder
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 732a5ee446a1473e1e785475b734e7d7e7cc0187
Choose a base ref
..
head repository: kubernetes-sigs/kubebuilder
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 169ac6598cf1627385cc31fffe92d1d8b7c978cb
Choose a head ref
Showing with 527 additions and 242 deletions.
  1. +9 −9 .github/workflows/test-e2e-samples.yml
  2. +2 −2 Makefile
  3. +41 −4 docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go
  4. +2 −0 docs/book/src/cronjob-tutorial/testdata/project/config/default/kustomization.yaml
  5. +32 −26 docs/book/src/cronjob-tutorial/testdata/project/config/default/manager_webhook_patch.yaml
  6. +3 −17 docs/book/src/cronjob-tutorial/testdata/project/dist/install.yaml
  7. +41 −4 docs/book/src/getting-started/testdata/project/cmd/main.go
  8. +2 −0 docs/book/src/getting-started/testdata/project/config/default/kustomization.yaml
  9. +41 −4 docs/book/src/multiversion-tutorial/testdata/project/cmd/main.go
  10. +2 −0 docs/book/src/multiversion-tutorial/testdata/project/config/default/kustomization.yaml
  11. +32 −26 docs/book/src/multiversion-tutorial/testdata/project/config/default/manager_webhook_patch.yaml
  12. +3 −17 docs/book/src/multiversion-tutorial/testdata/project/dist/install.yaml
  13. +2 −0 pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/kdefault/kustomization.go
  14. +33 −26 ...plugins/common/kustomize/v2/scaffolds/internal/templates/config/kdefault/webhook_manager_patch.go
  15. +3 −1 pkg/plugins/common/kustomize/v2/scaffolds/webhook.go
  16. +45 −7 pkg/plugins/golang/v4/scaffolds/internal/templates/cmd/main.go
  17. +41 −4 testdata/project-v4-multigroup/cmd/main.go
  18. +2 −0 testdata/project-v4-multigroup/config/default/kustomization.yaml
  19. +32 −26 testdata/project-v4-multigroup/config/default/manager_webhook_patch.yaml
  20. +3 −3 testdata/project-v4-multigroup/dist/install.yaml
  21. +41 −4 testdata/project-v4-with-plugins/cmd/main.go
  22. +2 −0 testdata/project-v4-with-plugins/config/default/kustomization.yaml
  23. +32 −26 testdata/project-v4-with-plugins/config/default/manager_webhook_patch.yaml
  24. +3 −3 testdata/project-v4-with-plugins/dist/install.yaml
  25. +41 −4 testdata/project-v4/cmd/main.go
  26. +2 −0 testdata/project-v4/config/default/kustomization.yaml
  27. +32 −26 testdata/project-v4/config/default/manager_webhook_patch.yaml
  28. +3 −3 testdata/project-v4/dist/install.yaml
18 changes: 9 additions & 9 deletions .github/workflows/test-e2e-samples.yml
Original file line number Diff line number Diff line change
@@ -43,8 +43,8 @@ jobs:
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '47,49s/^#//' $KUSTOMIZATION_FILE_PATH
# Uncomment all cert-manager injections
sed -i '57,210s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '212,227s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '59,212s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '214,229s/^#//' $KUSTOMIZATION_FILE_PATH
cd testdata/project-v4/
go mod tidy
@@ -86,11 +86,11 @@ jobs:
# Uncomment only ValidatingWebhookConfiguration
# from cert-manager replaces; we are leaving defaulting uncommented
# since this sample has no defaulting webhooks
sed -i '57,57s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '133,162s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '59,59s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '135,164s/^#//' $KUSTOMIZATION_FILE_PATH
# Uncomment only --conversion webhooks CA injection
sed -i '195,210s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '212,227s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '197,212s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '214,229s/^#//' $KUSTOMIZATION_FILE_PATH
cd testdata/project-v4-with-plugins/
go mod tidy
@@ -130,9 +130,9 @@ jobs:
KUSTOMIZATION_FILE_PATH="testdata/project-v4-multigroup/config/default/kustomization.yaml"
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
# Uncomment all cert-manager injections for webhooks only
sed -i '57,57s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '96,210s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '212,227s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '59,59s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '98,212s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '214,229s/^#//' $KUSTOMIZATION_FILE_PATH
cd testdata/project-v4-multigroup
go mod tidy
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -89,9 +89,9 @@ generate-docs: ## Update/generate the docs
./hack/docs/generate.sh

.PHONY: generate-charts
generate-charts: ## Re-generate the helm chart testdata only
generate-charts: build ## Re-generate the helm chart testdata only
rm -rf testdata/project-v4-with-plugins/dist/chart
(cd testdata/project-v4-with-plugins && kubebuilder edit --plugins=helm/v1-alpha)
(cd testdata/project-v4-with-plugins && ../../bin/kubebuilder edit --plugins=helm/v1-alpha)

.PHONY: check-docs
check-docs: ## Run the script to ensure that the docs are updated
45 changes: 41 additions & 4 deletions docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@ func main() {
*/
var metricsAddr string
var metricsCertPath, metricsCertName, metricsCertKey string
var webhookCertPath, webhookCertName, webhookCertKey string
var enableLeaderElection bool
var probeAddr string
var secureMetrics bool
@@ -90,6 +91,9 @@ func main() {
"Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&secureMetrics, "metrics-secure", true,
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
flag.StringVar(&webhookCertPath, "webhook-cert-path", "", "The directory that contains the webhook certificate.")
flag.StringVar(&webhookCertName, "webhook-cert-name", "tls.crt", "The name of the webhook certificate file.")
flag.StringVar(&webhookCertKey, "webhook-cert-key", "tls.key", "The name of the webhook key file.")
flag.StringVar(&metricsCertPath, "metrics-cert-path", "", "The directory that contains the metrics server certificate.")
flag.StringVar(&metricsCertName, "metrics-cert-name", "tls.crt", "The name of the metrics server certificate file.")
flag.StringVar(&metricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.")
@@ -118,11 +122,33 @@ func main() {
tlsOpts = append(tlsOpts, disableHTTP2)
}

// Create watchers for metrics certificates
var metricsCertWatcher *certwatcher.CertWatcher
// Create watchers for metrics and webhooks certificates
var metricsCertWatcher, webhookCertWatcher *certwatcher.CertWatcher

// Initial webhook TLS options
webhookTLSOpts := append([]func(*tls.Config){}, tlsOpts...)

if len(webhookCertPath) > 0 {
setupLog.Info("Initializing webhook certificate watcher using provided certificates",
"webhook-cert-path", webhookCertPath, "webhook-cert-name", webhookCertName, "webhook-cert-key", webhookCertKey)

var err error
webhookCertWatcher, err = certwatcher.New(
filepath.Join(webhookCertPath, webhookCertName),
filepath.Join(webhookCertPath, webhookCertKey),
)
if err != nil {
setupLog.Error(err, "Failed to initialize webhook certificate watcher")
os.Exit(1)
}

webhookTLSOpts = append(webhookTLSOpts, func(config *tls.Config) {
config.GetCertificate = webhookCertWatcher.GetCertificate
})
}

webhookServer := webhook.NewServer(webhook.Options{
TLSOpts: tlsOpts,
TLSOpts: webhookTLSOpts,
})

// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
@@ -133,11 +159,14 @@ func main() {
BindAddress: metricsAddr,
SecureServing: secureMetrics,
TLSOpts: tlsOpts,
}

if secureMetrics {
// FilterProvider is used to protect the metrics endpoint with authn/authz.
// These configurations ensure that only authorized users and service accounts
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
// https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.1/pkg/metrics/filters#WithAuthenticationAndAuthorization
FilterProvider: filters.WithAuthenticationAndAuthorization,
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
}

// If the certificate is not specified, controller-runtime will automatically
@@ -226,6 +255,14 @@ func main() {
}
}

if webhookCertWatcher != nil {
setupLog.Info("Adding webhook certificate watcher to manager")
if err := mgr.Add(webhookCertWatcher); err != nil {
setupLog.Error(err, "unable to add webhook certificate watcher to manager")
os.Exit(1)
}
}

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
Original file line number Diff line number Diff line change
@@ -51,6 +51,8 @@ patches:
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
- path: manager_webhook_patch.yaml
target:
kind: Deployment

# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.
# Uncomment the following replacements to add the cert-manager CA injection annotations
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
labels:
app.kubernetes.io/name: project
app.kubernetes.io/managed-by: kustomize
spec:
template:
spec:
containers:
- name: manager
ports:
- containerPort: 9443
name: webhook-server
protocol: TCP
volumeMounts:
- mountPath: /tmp/k8s-webhook-server/serving-certs
name: cert
readOnly: true
volumes:
- name: cert
secret:
defaultMode: 420
secretName: webhook-server-cert
# This patch ensures the webhook certificates are properly mounted in the manager container.
# It configures the necessary volume, volume mounts, and container ports.
- op: add
path: /spec/template/spec/containers/0/args/-
value: --webhook-cert-path=/tmp/k8s-webhook-server/serving-certs
- op: add
path: /spec/template/spec/containers/0/volumeMounts
value: []
- op: add
path: /spec/template/spec/containers/0/volumeMounts/-
value:
mountPath: /tmp/k8s-webhook-server/serving-certs
name: webhook-certs
readOnly: true
- op: add
path: /spec/template/spec/containers/0/ports
value: []
- op: add
path: /spec/template/spec/containers/0/ports/-
value:
containerPort: 9443
name: webhook-server
protocol: TCP
- op: add
path: /spec/template/spec/volumes
value: []
- op: add
path: /spec/template/spec/volumes/-
value:
name: webhook-certs
secret:
secretName: webhook-server-cert
20 changes: 3 additions & 17 deletions docs/book/src/cronjob-tutorial/testdata/project/dist/install.yaml
Original file line number Diff line number Diff line change
@@ -4119,6 +4119,7 @@ spec:
- --leader-elect
- --health-probe-bind-address=:8081
- --metrics-cert-path=/tmp/k8s-metrics-server/metrics-certs
- --webhook-cert-path=/tmp/k8s-webhook-server/serving-certs
command:
- /manager
image: controller:latest
@@ -4153,10 +4154,7 @@ spec:
- ALL
volumeMounts:
- mountPath: /tmp/k8s-webhook-server/serving-certs
name: cert
readOnly: true
- mountPath: /tmp/k8s-metrics-server/metrics-certs
name: metrics-certs
name: webhook-certs
readOnly: true
securityContext:
runAsNonRoot: true
@@ -4165,21 +4163,9 @@ spec:
serviceAccountName: project-controller-manager
terminationGracePeriodSeconds: 10
volumes:
- name: cert
- name: webhook-certs
secret:
defaultMode: 420
secretName: webhook-server-cert
- name: metrics-certs
secret:
items:
- key: ca.crt
path: ca.crt
- key: tls.crt
path: tls.crt
- key: tls.key
path: tls.key
optional: false
secretName: metrics-server-cert
---
apiVersion: cert-manager.io/v1
kind: Certificate
45 changes: 41 additions & 4 deletions docs/book/src/getting-started/testdata/project/cmd/main.go
Original file line number Diff line number Diff line change
@@ -57,6 +57,7 @@ func init() {
func main() {
var metricsAddr string
var metricsCertPath, metricsCertName, metricsCertKey string
var webhookCertPath, webhookCertName, webhookCertKey string
var enableLeaderElection bool
var probeAddr string
var secureMetrics bool
@@ -70,6 +71,9 @@ func main() {
"Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&secureMetrics, "metrics-secure", true,
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
flag.StringVar(&webhookCertPath, "webhook-cert-path", "", "The directory that contains the webhook certificate.")
flag.StringVar(&webhookCertName, "webhook-cert-name", "tls.crt", "The name of the webhook certificate file.")
flag.StringVar(&webhookCertKey, "webhook-cert-key", "tls.key", "The name of the webhook key file.")
flag.StringVar(&metricsCertPath, "metrics-cert-path", "", "The directory that contains the metrics server certificate.")
flag.StringVar(&metricsCertName, "metrics-cert-name", "tls.crt", "The name of the metrics server certificate file.")
flag.StringVar(&metricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.")
@@ -98,11 +102,33 @@ func main() {
tlsOpts = append(tlsOpts, disableHTTP2)
}

// Create watchers for metrics certificates
var metricsCertWatcher *certwatcher.CertWatcher
// Create watchers for metrics and webhooks certificates
var metricsCertWatcher, webhookCertWatcher *certwatcher.CertWatcher

// Initial webhook TLS options
webhookTLSOpts := append([]func(*tls.Config){}, tlsOpts...)

if len(webhookCertPath) > 0 {
setupLog.Info("Initializing webhook certificate watcher using provided certificates",
"webhook-cert-path", webhookCertPath, "webhook-cert-name", webhookCertName, "webhook-cert-key", webhookCertKey)

var err error
webhookCertWatcher, err = certwatcher.New(
filepath.Join(webhookCertPath, webhookCertName),
filepath.Join(webhookCertPath, webhookCertKey),
)
if err != nil {
setupLog.Error(err, "Failed to initialize webhook certificate watcher")
os.Exit(1)
}

webhookTLSOpts = append(webhookTLSOpts, func(config *tls.Config) {
config.GetCertificate = webhookCertWatcher.GetCertificate
})
}

webhookServer := webhook.NewServer(webhook.Options{
TLSOpts: tlsOpts,
TLSOpts: webhookTLSOpts,
})

// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
@@ -113,11 +139,14 @@ func main() {
BindAddress: metricsAddr,
SecureServing: secureMetrics,
TLSOpts: tlsOpts,
}

if secureMetrics {
// FilterProvider is used to protect the metrics endpoint with authn/authz.
// These configurations ensure that only authorized users and service accounts
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
// https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.19.1/pkg/metrics/filters#WithAuthenticationAndAuthorization
FilterProvider: filters.WithAuthenticationAndAuthorization,
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
}

// If the certificate is not specified, controller-runtime will automatically
@@ -188,6 +217,14 @@ func main() {
}
}

if webhookCertWatcher != nil {
setupLog.Info("Adding webhook certificate watcher to manager")
if err := mgr.Add(webhookCertWatcher); err != nil {
setupLog.Error(err, "unable to add webhook certificate watcher to manager")
os.Exit(1)
}
}

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
Original file line number Diff line number Diff line change
@@ -51,6 +51,8 @@ patches:
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- path: manager_webhook_patch.yaml
# target:
# kind: Deployment

# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.
# Uncomment the following replacements to add the cert-manager CA injection annotations
Loading