Skip to content

Commit

Permalink
Merge pull request #109 from ratschance/allow-extra-vols-and-webhook-url
Browse files Browse the repository at this point in the history
feat: Add support for mounting extra volumes and add webhook URL override capability
  • Loading branch information
FabianKramm authored Dec 7, 2023
2 parents 0fedc7b + e39a0fd commit 5b027ee
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 deletions.
8 changes: 8 additions & 0 deletions chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ spec:
{{- if .Values.tolerations }}
tolerations: {{- include "jspolicy.render" (dict "value" .Values.tolerations "context" .) | nindent 8 }}
{{- end }}
{{- if .Values.jspolicy.extraVolumes }}
volumes:
{{- toYaml .Values.jspolicy.extraVolumes | nindent 8 }}
{{- end }}
containers:
- ports:
- name: webhook
Expand Down Expand Up @@ -89,3 +93,7 @@ spec:
{{- end }}
resources:
{{ toYaml .Values.jspolicy.resources | indent 10 }}
{{- if .Values.jspolicy.extraVolumeMounts }}
volumeMounts:
{{- toYaml .Values.jspolicy.extraVolumeMounts | nindent 10 }}
{{- end }}
3 changes: 3 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jspolicy:
requests:
memory: 128Mi
cpu: 50m
# extraVolumes and extraVolumeMounts allows mounting other volumes to the jsPolicy pod.
extraVolumes: []
extraVolumeMounts: []

policyReports:
enabled: false
Expand Down
30 changes: 20 additions & 10 deletions pkg/controllers/jspolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,16 @@ func (r *JsPolicyReconciler) syncMutatingWebhookConfiguration(ctx context.Contex
// Ensure webhook fields
webhook.Webhooks[0].Name = jsPolicy.Name
path := "/policy/" + jsPolicy.Name
webhook.Webhooks[0].ClientConfig.Service = &admissionregistrationv1.ServiceReference{
Name: clienthelper.ServiceName(),
Namespace: namespace,
Path: &path,
Port: &port,
if url := clienthelper.WebhookURL(); url != "" {
url = url + path
webhook.Webhooks[0].ClientConfig.URL = &url
} else {
webhook.Webhooks[0].ClientConfig.Service = &admissionregistrationv1.ServiceReference{
Name: clienthelper.ServiceName(),
Namespace: namespace,
Path: &path,
Port: &port,
}
}
webhook.Webhooks[0].ClientConfig.CABundle = r.CaBundle
if len(webhook.Webhooks[0].Rules) != 1 {
Expand Down Expand Up @@ -639,11 +644,16 @@ func (r *JsPolicyReconciler) syncValidatingWebhookConfiguration(ctx context.Cont
// Ensure webhook fields
webhook.Webhooks[0].Name = jsPolicy.Name
path := "/policy/" + jsPolicy.Name
webhook.Webhooks[0].ClientConfig.Service = &admissionregistrationv1.ServiceReference{
Name: clienthelper.ServiceName(),
Namespace: namespace,
Path: &path,
Port: &port,
if url := clienthelper.WebhookURL(); url != "" {
url = url + path
webhook.Webhooks[0].ClientConfig.URL = &url
} else {
webhook.Webhooks[0].ClientConfig.Service = &admissionregistrationv1.ServiceReference{
Name: clienthelper.ServiceName(),
Namespace: namespace,
Path: &path,
Port: &port,
}
}
webhook.Webhooks[0].ClientConfig.CABundle = r.CaBundle
if len(webhook.Webhooks[0].Rules) != 1 {
Expand Down
35 changes: 35 additions & 0 deletions pkg/controllers/jspolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func TestSimple(t *testing2.T) {
err = fakeClient.List(context.TODO(), list)
assert.NilError(t, err)
assert.Equal(t, len(list.Items), 1)
var expectedURL *string
assert.Equal(t, list.Items[0].Webhooks[0].ClientConfig.URL, expectedURL, "the webhook url should be nil when JS_POLICY_WEBHOOK_URL is not set")
mList := &admissionregistrationv1.MutatingWebhookConfigurationList{}
err = fakeClient.List(context.TODO(), mList)
assert.NilError(t, err)
Expand All @@ -104,6 +106,39 @@ func TestSimple(t *testing2.T) {
assert.NilError(t, err)
assert.Equal(t, len(mList.Items), 1)
}
func TestSimpleURL(t *testing2.T) {
err := os.Setenv("KUBE_NAMESPACE", "default")
assert.NilError(t, err)
err = os.Setenv("JS_POLICY_WEBHOOK_URL", "https://testurl.example.local")
assert.NilError(t, err)

scheme := testing.NewScheme()
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(testPolicy).Build()

controller := &JsPolicyReconciler{
Client: fakeClient,
Log: loghelper.New("test"),
Scheme: scheme,
Bundler: nil,
ControllerPolicyManager: nil,
controllerPolicyHash: map[string]string{},
CaBundle: []byte("any"),
}

// sync the webhook
err = controller.syncWebhook(context.Background(), testPolicy)
assert.NilError(t, err)

// check if there was a validating webhook created
list := &admissionregistrationv1.ValidatingWebhookConfigurationList{}
err = fakeClient.List(context.TODO(), list)
assert.NilError(t, err)
assert.Equal(t, len(list.Items), 1)

// confirm that the webhook url is set correctly
expectedURL := "https://testurl.example.local" + "/policy/test.test.com"
assert.Equal(t, *list.Items[0].Webhooks[0].ClientConfig.URL, expectedURL)
}

type fakeBundler struct {
bundle []byte
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/clienthelper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ func ServiceName() string {
return "jspolicy"
}

// WebhookURL returns the URL of the webhook service if it is set in the environment variable JS_POLICY_WEBHOOK_URL.
// Otherwise, it returns an empty string which means a webhook service reference should be used.
func WebhookURL() string {
if os.Getenv("JS_POLICY_WEBHOOK_URL") != "" {
return os.Getenv("JS_POLICY_WEBHOOK_URL")
}
return ""
}

func CurrentNamespace() (string, error) {
envNamespace := os.Getenv("KUBE_NAMESPACE")
if envNamespace != "" {
Expand Down

0 comments on commit 5b027ee

Please sign in to comment.