Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Automate Uncommenting Webhook Blocks in kustomize.yaml When Scaffolding Webhooks #4422 #4565

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions .github/workflows/test-e2e-samples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ jobs:

- name: Prepare project-v4
run: |
KUSTOMIZATION_FILE_PATH="testdata/project-v4/config/default/kustomization.yaml"
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '47,49s/^#//' $KUSTOMIZATION_FILE_PATH
# Uncomment all cert-manager injections
sed -i '59,234s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '236,251s/^#//' $KUSTOMIZATION_FILE_PATH
cd testdata/project-v4/
go mod tidy

Expand Down Expand Up @@ -81,17 +75,6 @@ jobs:

- name: Prepare project-v4-with-plugins
run: |
KUSTOMIZATION_FILE_PATH="testdata/project-v4-with-plugins/config/default/kustomization.yaml"
sed -i '25s/^#//' $KUSTOMIZATION_FILE_PATH
# Uncomment only ValidatingWebhookConfiguration
# from cert-manager replaces; we are leaving defaulting uncommented
# since this sample has no defaulting webhooks
sed -i '59,77s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '90,107s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '120,186s/^#//' $KUSTOMIZATION_FILE_PATH
# Uncomment only --conversion webhooks CA injection
sed -i '219,234s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '236,251s/^#//' $KUSTOMIZATION_FILE_PATH
cd testdata/project-v4-with-plugins/
go mod tidy

Expand Down Expand Up @@ -128,13 +111,6 @@ jobs:

- name: Prepare project-v4-multigroup
run: |
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 '59,77s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '90,107s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '120,234s/^#//' $KUSTOMIZATION_FILE_PATH
sed -i '236,251s/^#//' $KUSTOMIZATION_FILE_PATH
cd testdata/project-v4-multigroup
go mod tidy

Expand Down
238 changes: 238 additions & 0 deletions pkg/plugins/golang/v4/scaffolds/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,243 @@
}
}
}

kustomizeFilePath := "config/default/kustomization.yaml"
webhookConfig := []string{
"#- ../webhook",
"#- ../certmanager",
webhookTarget,
}
for _, config := range webhookConfig {
if err := pluginutil.UncommentCode(kustomizeFilePath, config, "#"); err != nil {
log.Errorf("Unable to uncomment %s in the file %s.", config, kustomizeFilePath)
}
}

prometheusKustomizeFilePath := "config/prometheus/kustomization.yaml"
if err := pluginutil.UncommentCode(prometheusKustomizeFilePath, prometheusTarget, "#"); err != nil {
log.Errorf("Unable to uncomment resources: in the file %s.", prometheusKustomizeFilePath)
}

if doDefaulting {
if err := pluginutil.UncommentCode(kustomizeFilePath, defaultingWebhookTarget, "#"); err != nil {
if err := pluginutil.UncommentCode(kustomizeFilePath, defaultingWebhookTarget, "#"); err != nil {
log.Errorf("Unable to uncomment %s in the file %s.", defaultingWebhookTarget, kustomizeFilePath)
}

}
}
if doConversion {
if err := pluginutil.UncommentCode(kustomizeFilePath, conversionWebhookTarget, "#"); err != nil {
log.Errorf("Unable to uncomment %s in the file %s.", conversionWebhookTarget, kustomizeFilePath)
}
}

if doValidation {
if err := pluginutil.UncommentCode(kustomizeFilePath, validationWebhookTarget, "#"); err != nil {
log.Errorf("Unable to uncomment %s in the file %s.", validationWebhookTarget, kustomizeFilePath)
}
}
return nil

}

const webhookTarget = `#replacements:
# - source: # Uncomment the following block to enable certificates for metrics
# kind: Service
# version: v1
# name: controller-manager-metrics-service
# fieldPath: metadata.name
# targets:
# - select:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: metrics-certs
# fieldPaths:
# - spec.dnsNames.0
# - spec.dnsNames.1
# options:
# delimiter: '.'
# index: 0
# create: true
# - select:
# kind: ServiceMonitor
# group: monitoring.coreos.com
# version: v1
# name: controller-manager-metrics-monitor
# fieldPaths:
# - spec.endpoints.0.tlsConfig.serverName
# options:
# delimiter: '.'
# index: 0
# create: true
#
# - source:
# kind: Service
# version: v1
# name: controller-manager-metrics-service
# fieldPath: metadata.namespace
# targets:
# - select:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: metrics-certs
# fieldPaths:
# - spec.dnsNames.0
# - spec.dnsNames.1
# options:
# delimiter: '.'
# index: 1
# create: true
# - select:
# kind: ServiceMonitor
# group: monitoring.coreos.com
# version: v1
# name: controller-manager-metrics-monitor
# fieldPaths:
# - spec.endpoints.0.tlsConfig.serverName
# options:
# delimiter: '.'
# index: 1
# create: true
#
# - source: # Uncomment the following block if you have any webhook
# kind: Service
# version: v1
# name: webhook-service
# fieldPath: .metadata.name # Name of the service
# targets:
# - select:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert
# fieldPaths:
# - .spec.dnsNames.0
# - .spec.dnsNames.1
# options:
# delimiter: '.'
# index: 0
# create: true
# - source:
# kind: Service
# version: v1
# name: webhook-service
# fieldPath: .metadata.namespace # Namespace of the service
# targets:
# - select:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert
# fieldPaths:
# - .spec.dnsNames.0
# - .spec.dnsNames.1
# options:
# delimiter: '.'
# index: 1
# create: true`

const prometheusTarget = `#patches:
# - path: monitor_tls_patch.yaml
# target:
# kind: ServiceMonitor
`

const defaultingWebhookTarget = `# - source: # Uncomment the following block if you have a DefaultingWebhook (--defaulting )

Check failure on line 321 in pkg/plugins/golang/v4/scaffolds/webhook.go

View workflow job for this annotation

GitHub Actions / golangci-lint

The line is 124 characters long, which exceeds the maximum of 120 characters. (lll)
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert
# fieldPath: .metadata.namespace # Namespace of the certificate CR
# targets:
# - select:
# kind: MutatingWebhookConfiguration
# fieldPaths:
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
# options:
# delimiter: '/'
# index: 0
# create: true
# - source:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert
# fieldPath: .metadata.name
# targets:
# - select:
# kind: MutatingWebhookConfiguration
# fieldPaths:
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
# options:
# delimiter: '/'
# index: 1
# create: true`

const conversionWebhookTarget = `# - source: # Uncomment the following block if you have a ConversionWebhook (--conversion)

Check failure on line 352 in pkg/plugins/golang/v4/scaffolds/webhook.go

View workflow job for this annotation

GitHub Actions / golangci-lint

The line is 123 characters long, which exceeds the maximum of 120 characters. (lll)
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert
# fieldPath: .metadata.namespace # Namespace of the certificate CR
# targets: # Do not remove or uncomment the following scaffold marker; required to generate code for target CRD.
# - select:
# kind: CustomResourceDefinition
# name: guestbooks.webapp.my.domain
# fieldPaths:
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
# options:
# delimiter: '/'
# index: 0
# create: true
# +kubebuilder:scaffold:crdkustomizecainjectionns
# - source:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert
# fieldPath: .metadata.name
# targets: # Do not remove or uncomment the following scaffold marker; required to generate code for target CRD.
# - select:
# kind: CustomResourceDefinition
# name: guestbooks.webapp.my.domain
# fieldPaths:
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
# options:
# delimiter: '/'
# index: 1
# create: true`

const validationWebhookTarget = `# - source: # Uncomment the following block if you have a ValidatingWebhook (--programmatic-validation)

Check failure on line 386 in pkg/plugins/golang/v4/scaffolds/webhook.go

View workflow job for this annotation

GitHub Actions / golangci-lint

The line is 136 characters long, which exceeds the maximum of 120 characters. (lll)
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert # This name should match the one in certificate.yaml
# fieldPath: .metadata.namespace # Namespace of the certificate CR
# targets:
# - select:
# kind: ValidatingWebhookConfiguration
# fieldPaths:
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
# options:
# delimiter: '/'
# index: 0
# create: true
# - source:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert
# fieldPath: .metadata.name
# targets:
# - select:
# kind: ValidatingWebhookConfiguration
# fieldPaths:
# - .metadata.annotations.[cert-manager.io/inject-ca-from]
# options:
# delimiter: '/'
# index: 1
# create: true`
Loading