Skip to content

Commit

Permalink
add validating webhook for config-gateway Config Map (#705)
Browse files Browse the repository at this point in the history
* add validation webhook for configmap

* add EOF new line in yaml files

* run ./hack/update-deps.sh

* rename config-gateway yaml file

* renamed yaml files

* add object selector labels to config-gateway in contour folder
  • Loading branch information
izabelacg authored Apr 22, 2024
1 parent 76a9d18 commit 3fcf453
Show file tree
Hide file tree
Showing 14 changed files with 889 additions and 0 deletions.
61 changes: 61 additions & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2024 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"

gatewayapiconfig "knative.dev/net-gateway-api/pkg/reconciler/ingress/config"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/signals"
"knative.dev/pkg/webhook"
"knative.dev/pkg/webhook/certificates"
"knative.dev/pkg/webhook/configmaps"
)

func NewConfigValidationController(ctx context.Context, _ configmap.Watcher) *controller.Impl {
return configmaps.NewAdmissionController(ctx,

// Name of the resource webhook.
"config.webhook.gateway-api.networking.internal.knative.dev",

// The path on which to serve the webhook.
"/config-validation",

// The configmaps to validate.
configmap.Constructors{
gatewayapiconfig.GatewayConfigName: gatewayapiconfig.FromConfigMap,
},
)
}

func main() {
ctx := webhook.WithOptions(signals.NewContext(), webhook.Options{
ServiceName: "net-gateway-api-webhook",
SecretName: "net-gateway-api-webhook-certs",
Port: webhook.PortFromEnv(8443),
})

ctx = sharedmain.WithHealthProbesDisabled(ctx)
sharedmain.WebhookMainWithContext(
ctx, "net-gateway-api-webhook",
certificates.NewController,
NewConfigValidationController,
)
}
File renamed without changes.
103 changes: 103 additions & 0 deletions config/400-webhook-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Copyright 2024 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: apps/v1
kind: Deployment
metadata:
name: net-gateway-api-webhook
namespace: knative-serving
labels:
app.kubernetes.io/component: net-gateway-api
app.kubernetes.io/name: knative-serving
app.kubernetes.io/version: devel
networking.knative.dev/ingress-provider: gateway-api
spec:
selector:
matchLabels:
app: net-gateway-api-webhook
role: net-gateway-api-webhook
template:
metadata:
labels:
app: net-gateway-api-webhook
role: net-gateway-api-webhook
app.kubernetes.io/component: net-gateway-api
app.kubernetes.io/name: knative-serving
app.kubernetes.io/version: devel
spec:
serviceAccountName: controller
containers:
- name: webhook
# This is the Go import path for the binary that is containerized
# and substituted here.
image: ko://knative.dev/net-gateway-api/cmd/webhook

resources:
requests:
cpu: 20m
memory: 20Mi
limits:
cpu: 200m
memory: 200Mi

env:
- name: SYSTEM_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: CONFIG_LOGGING_NAME
value: config-logging
- name: CONFIG_OBSERVABILITY_NAME
value: config-observability

# TODO(https://github.com/knative/pkg/pull/953): Remove stackdriver specific config
- name: METRICS_DOMAIN
value: knative.dev/net-gateway-api
- name: WEBHOOK_NAME
value: net-gateway-api-webhook
# If you change WEBHOOK_PORT, you will also need to change the
# containerPort "https-webhook" to the same value.
- name: WEBHOOK_PORT
value: "8443"

securityContext:
runAsNonRoot: true
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
seccompProfile:
type: RuntimeDefault

readinessProbe:
periodSeconds: 1
httpGet:
scheme: HTTPS
port: 8443
failureThreshold: 3
livenessProbe:
periodSeconds: 1
httpGet:
scheme: HTTPS
port: 8443
failureThreshold: 6
initialDelaySeconds: 20

ports:
- name: metrics
containerPort: 9090
- name: profiling
containerPort: 8008
- name: https-webhook
containerPort: 8443
24 changes: 24 additions & 0 deletions config/400-webhook-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: Secret
metadata:
name: net-gateway-api-webhook-certs
namespace: knative-serving
labels:
app.kubernetes.io/component: net-gateway-api
app.kubernetes.io/name: knative-serving
app.kubernetes.io/version: devel
networking.knative.dev/ingress-provider: gateway-api
39 changes: 39 additions & 0 deletions config/400-webhook-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2024 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: Service
metadata:
name: net-gateway-api-webhook
namespace: knative-serving
labels:
role: net-gateway-api-webhook
app.kubernetes.io/component: net-gateway-api
app.kubernetes.io/name: knative-serving
app.kubernetes.io/version: devel
networking.knative.dev/ingress-provider: gateway-api
spec:
ports:
# Define metrics and profiling for them to be accessible within service meshes.
- name: http-metrics
port: 9090
targetPort: metrics
- name: http-profiling
port: 8008
targetPort: profiling
- name: https-webhook
port: 443
targetPort: https-webhook
selector:
app: net-gateway-api-webhook
38 changes: 38 additions & 0 deletions config/500-validating-webhook.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2024 The Knative Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: config.webhook.gateway-api.networking.internal.knative.dev
labels:
app.kubernetes.io/component: net-gateway-api
app.kubernetes.io/name: knative-serving
app.kubernetes.io/version: devel
networking.knative.dev/ingress-provider: gateway-api
webhooks:
- admissionReviewVersions:
- v1
- v1beta1
clientConfig:
service:
name: net-gateway-api-webhook
namespace: knative-serving
failurePolicy: Fail
sideEffects: None
name: config.webhook.gateway-api.networking.internal.knative.dev
objectSelector:
matchLabels:
app.kubernetes.io/name: knative-serving
app.kubernetes.io/component: net-gateway-api
2 changes: 2 additions & 0 deletions third_party/contour/config-gateway.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ metadata:
name: config-gateway
namespace: knative-serving
labels:
app.kubernetes.io/component: net-gateway-api
app.kubernetes.io/name: knative-serving
serving.knative.dev/release: devel
data:
external-gateways: |
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package secret

import (
context "context"

v1 "k8s.io/client-go/informers/core/v1"
controller "knative.dev/pkg/controller"
injection "knative.dev/pkg/injection"
factory "knative.dev/pkg/injection/clients/namespacedkube/informers/factory"
logging "knative.dev/pkg/logging"
)

func init() {
injection.Default.RegisterInformer(withInformer)
}

// Key is used for associating the Informer inside the context.Context.
type Key struct{}

func withInformer(ctx context.Context) (context.Context, controller.Informer) {
f := factory.Get(ctx)
inf := f.Core().V1().Secrets()
return context.WithValue(ctx, Key{}, inf), inf.Informer()
}

// Get extracts the typed informer from the context.
func Get(ctx context.Context) v1.SecretInformer {
untyped := ctx.Value(Key{})
if untyped == nil {
logging.FromContext(ctx).Panic(
"Unable to fetch k8s.io/client-go/informers/core/v1.SecretInformer from context.")
}
return untyped.(v1.SecretInformer)
}
Loading

0 comments on commit 3fcf453

Please sign in to comment.