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

PTEUDO-1990: Prevent deletion requests DatabaseClaim CRs #368

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
domain: atlas.infoblox.com
layout:
- go.kubebuilder.io/v4
multigroup: true
projectName: migration
repo: github.com/infobloxopen/db-controller
resources:
Expand All @@ -18,6 +17,9 @@ resources:
kind: DatabaseClaim
path: github.com/infobloxopen/db-controller/api/v1
version: v1
webhooks:
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
Expand All @@ -27,6 +29,9 @@ resources:
kind: DbRoleClaim
path: github.com/infobloxopen/db-controller/api/v1
version: v1
webhooks:
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
Expand Down
30 changes: 21 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ import (
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.

persistanceinfobloxcomv1alpha1 "github.com/infobloxopen/db-controller/api/persistance.infoblox.com/v1alpha1"
persistancev1 "github.com/infobloxopen/db-controller/api/v1"
"github.com/infobloxopen/db-controller/internal/controller"
"github.com/infobloxopen/db-controller/internal/metrics"
mutating "github.com/infobloxopen/db-controller/internal/webhook"
"github.com/infobloxopen/db-controller/pkg/config"
"github.com/infobloxopen/db-controller/pkg/databaseclaim"
"github.com/infobloxopen/db-controller/pkg/rdsauth"
"github.com/infobloxopen/db-controller/pkg/roleclaim"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -44,6 +35,17 @@ import (
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

persistanceinfobloxcomv1alpha1 "github.com/infobloxopen/db-controller/api/persistance.infoblox.com/v1alpha1"
persistancev1 "github.com/infobloxopen/db-controller/api/v1"
"github.com/infobloxopen/db-controller/internal/controller"
"github.com/infobloxopen/db-controller/internal/metrics"
mutating "github.com/infobloxopen/db-controller/internal/webhook"
"github.com/infobloxopen/db-controller/pkg/config"
"github.com/infobloxopen/db-controller/pkg/databaseclaim"
"github.com/infobloxopen/db-controller/pkg/rdsauth"
"github.com/infobloxopen/db-controller/pkg/roleclaim"

webhookpersistancev1 "github.com/infobloxopen/db-controller/internal/webhook/v1"
// +kubebuilder:scaffold:imports
crossplanerdsv1alpha1 "github.com/crossplane-contrib/provider-aws/apis/rds/v1alpha1"
crossplanegcpv1beta2 "github.com/upbound/provider-gcp/apis/alloydb/v1beta2"
Expand Down Expand Up @@ -231,6 +233,16 @@ func main() {
os.Exit(1)
}

if err = webhookpersistancev1.SetupDatabaseClaimWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "DatabaseClaim")
os.Exit(1)
}

if err = webhookpersistancev1.SetupDbRoleClaimWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "DbRoleClaim")
os.Exit(1)
}

// +kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
35 changes: 35 additions & 0 deletions config/certmanager/certificate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# The following manifests contain a self-signed issuer CR and a certificate CR.
# More document can be found at https://docs.cert-manager.io
# WARNING: Targets CertManager v1.0. Check https://cert-manager.io/docs/installation/upgrading/ for breaking changes.
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
labels:
app.kubernetes.io/name: migration
app.kubernetes.io/managed-by: kustomize
name: selfsigned-issuer
namespace: system
spec:
selfSigned: {}
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
labels:
app.kubernetes.io/name: certificate
app.kubernetes.io/instance: serving-cert
app.kubernetes.io/component: certificate
app.kubernetes.io/created-by: migration
app.kubernetes.io/part-of: migration
app.kubernetes.io/managed-by: kustomize
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
namespace: system
spec:
# SERVICE_NAME and SERVICE_NAMESPACE will be substituted by kustomize
dnsNames:
- SERVICE_NAME.SERVICE_NAMESPACE.svc
- SERVICE_NAME.SERVICE_NAMESPACE.svc.cluster.local
issuerRef:
kind: Issuer
name: selfsigned-issuer
secretName: webhook-server-cert # this secret will not be prefixed, since it's not managed by kustomize
5 changes: 5 additions & 0 deletions config/certmanager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resources:
- certificate.yaml

configurations:
- kustomizeconfig.yaml
8 changes: 8 additions & 0 deletions config/certmanager/kustomizeconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This configuration is for teaching kustomize how to update name ref substitution
nameReference:
- kind: Issuer
group: cert-manager.io
fieldSpecs:
- kind: Certificate
group: cert-manager.io
path: spec/issuerRef/name
6 changes: 3 additions & 3 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ resources:
- ../manager
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- ../webhook
- ../webhook
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
#- ../certmanager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
Expand All @@ -29,7 +29,7 @@ resources:
#- metrics_service.yaml

# Uncomment the patches line if you enable Metrics, and/or are using webhooks and cert-manager
#patches:
patches:
# [METRICS] The following patch will enable the metrics endpoint. Ensure that you also protect this endpoint.
# More info: https://book.kubebuilder.io/reference/metrics
# If you want to expose the metric endpoint of your controller-manager uncomment the following line.
Expand All @@ -39,7 +39,7 @@ resources:

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- path: manager_webhook_patch.yaml
- path: manager_webhook_patch.yaml

# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
Expand Down
26 changes: 26 additions & 0 deletions config/default/manager_webhook_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
labels:
app.kubernetes.io/name: migration
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
26 changes: 26 additions & 0 deletions config/network-policy/allow-webhook-traffic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This NetworkPolicy allows ingress traffic to your webhook server running
# as part of the controller-manager from specific namespaces and pods. CR(s) which uses webhooks
# will only work when applied in namespaces labeled with 'webhook: enabled'
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
labels:
app.kubernetes.io/name: migration
app.kubernetes.io/managed-by: kustomize
name: allow-webhook-traffic
namespace: system
spec:
podSelector:
matchLabels:
control-plane: controller-manager
policyTypes:
- Ingress
ingress:
# This allows ingress traffic from any namespace with the label webhook: enabled
- from:
- namespaceSelector:
matchLabels:
webhook: enabled # Only from namespaces with this label
ports:
- port: 443
protocol: TCP
6 changes: 6 additions & 0 deletions config/webhook/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resources:
- manifests.yaml
- service.yaml

configurations:
- kustomizeconfig.yaml
22 changes: 22 additions & 0 deletions config/webhook/kustomizeconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# the following config is for teaching kustomize where to look at when substituting nameReference.
# It requires kustomize v2.1.0 or newer to work properly.
nameReference:
- kind: Service
version: v1
fieldSpecs:
- kind: MutatingWebhookConfiguration
group: admissionregistration.k8s.io
path: webhooks/clientConfig/service/name
- kind: ValidatingWebhookConfiguration
group: admissionregistration.k8s.io
path: webhooks/clientConfig/service/name

namespace:
- kind: MutatingWebhookConfiguration
group: admissionregistration.k8s.io
path: webhooks/clientConfig/service/namespace
create: true
- kind: ValidatingWebhookConfiguration
group: admissionregistration.k8s.io
path: webhooks/clientConfig/service/namespace
create: true
44 changes: 44 additions & 0 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,47 @@ webhooks:
- pods
sideEffects: None
timeoutSeconds: 10
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: validating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-persistance-atlas-infoblox-com-v1-databaseclaim
failurePolicy: Fail
name: vdatabaseclaim-v1.kb.io
rules:
- apiGroups:
- persistance.atlas.infoblox.com
apiVersions:
- v1
operations:
- DELETE
resources:
- databaseclaims
sideEffects: None
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /validate-persistance-atlas-infoblox-com-v1-dbroleclaim
failurePolicy: Fail
name: vdbroleclaim-v1.kb.io
rules:
- apiGroups:
- persistance.atlas.infoblox.com
apiVersions:
- v1
operations:
- DELETE
resources:
- dbroleclaims
sideEffects: None
15 changes: 15 additions & 0 deletions config/webhook/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/name: migration
app.kubernetes.io/managed-by: kustomize
name: webhook-service
namespace: system
spec:
ports:
- port: 443
protocol: TCP
targetPort: 9443
selector:
control-plane: controller-manager
1 change: 1 addition & 0 deletions internal/webhook/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var _ = Describe("annotation conversions", func() {
ObjectMeta: metav1.ObjectMeta{
Name: dbcName,
Namespace: "default",
Labels: map[string]string{"persistance.atlas.infoblox.com/allow-deletion": "true"},
},
Spec: v1.DatabaseClaimSpec{
SecretName: dbcSecretName,
Expand Down
1 change: 1 addition & 0 deletions internal/webhook/dbproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var _ = Describe("dbproxy defaulting", func() {
ObjectMeta: metav1.ObjectMeta{
Name: "default-db",
Namespace: "default",
Labels: map[string]string{"persistance.atlas.infoblox.com/allow-deletion": "true"},
},
Spec: v1.DatabaseClaimSpec{
SecretName: "test",
Expand Down
1 change: 1 addition & 0 deletions internal/webhook/dsnexec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var _ = Describe("dsnexec defaulting", func() {
ObjectMeta: metav1.ObjectMeta{
Name: "default-db",
Namespace: "default",
Labels: map[string]string{"persistance.atlas.infoblox.com/allow-deletion": "true"},
},
Spec: v1.DatabaseClaimSpec{
SecretName: "test",
Expand Down
7 changes: 5 additions & 2 deletions internal/webhook/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
admissionv1beta1 "k8s.io/api/admission/v1beta1"
//+kubebuilder:scaffold:imports

v1 "github.com/infobloxopen/db-controller/api/v1"
webhookpersistancev1 "github.com/infobloxopen/db-controller/internal/webhook/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -25,8 +27,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

v1 "github.com/infobloxopen/db-controller/api/v1"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
Expand Down Expand Up @@ -113,6 +113,9 @@ var _ = BeforeSuite(func() {
})
Expect(err).NotTo(HaveOccurred())

err = webhookpersistancev1.SetupDatabaseClaimWebhookWithManager(mgr)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:webhook

go func() {
Expand Down
Loading
Loading