Skip to content

Commit b4eea99

Browse files
authored
Merge pull request #4458 from Raffo/webhook-annotations
feat(webhooks): pass webhook-* annotations to webhook providers
2 parents f46676f + bfa4e06 commit b4eea99

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

docs/tutorials/webhook-provider.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ The default recommended port is 8888, and should listen only on localhost (ie: o
3131

3232
**NOTE**: only `5xx` responses will be retried and only `20x` will be considered as successful. All status codes different from those will be considered a failure on ExternalDNS's side.
3333

34-
3534
## Metrics support
3635

3736
The metrics should listen ":8080" on `/metrics` following [Open Metrics](https://github.com/OpenObservability/OpenMetrics) format.
3837

38+
## Custom Annotations
39+
40+
The Webhook provider supports custom annotations for DNS records. This feature allows users to define additional configuration options for DNS records managed by the Webhook provider. Custom annotations are defined using the annotation format `external-dns.alpha.kubernetes.io/webhook-<custom-annotation>`.
41+
42+
Custom annotations can be used to influence DNS record creation and updates. Providers implementing the Webhook API should document the custom annotations they support and how they affect DNS record management.
43+
3944
## Provider registry
4045

4146
To simplify the discovery of providers, we will accept pull requests that will add links to providers in the [README](../../README.md) file. This list will only serve the purpose of simplifying finding providers and will not constitute an official endorsement of any of the externally implemented providers unless otherwise stated.

provider/webhook/webhook_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/stretchr/testify/require"
2828
"sigs.k8s.io/external-dns/endpoint"
29+
"sigs.k8s.io/external-dns/plan"
2930
"sigs.k8s.io/external-dns/provider"
3031
webhookapi "sigs.k8s.io/external-dns/provider/webhook/api"
3132
)
@@ -217,3 +218,54 @@ func TestAdjustendpointsWithError(t *testing.T) {
217218
require.Error(t, err)
218219
require.ErrorIs(t, err, provider.SoftError)
219220
}
221+
222+
// test apply changes with an endpoint with a provider specific property
223+
func TestApplyChangesWithProviderSpecificProperty(t *testing.T) {
224+
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
225+
if r.URL.Path == "/" {
226+
w.Header().Set(webhookapi.ContentTypeHeader, webhookapi.MediaTypeFormatAndVersion)
227+
w.Write([]byte(`{}`))
228+
return
229+
}
230+
if r.URL.Path == "/records" {
231+
w.Header().Set(webhookapi.ContentTypeHeader, webhookapi.MediaTypeFormatAndVersion)
232+
// assert that the request contains the provider specific property
233+
var changes plan.Changes
234+
defer r.Body.Close()
235+
b, err := io.ReadAll(r.Body)
236+
require.Nil(t, err)
237+
err = json.Unmarshal(b, &changes)
238+
require.Nil(t, err)
239+
require.Len(t, changes.Create, 1)
240+
require.Len(t, changes.Create[0].ProviderSpecific, 1)
241+
require.Equal(t, "prop1", changes.Create[0].ProviderSpecific[0].Name)
242+
require.Equal(t, "value1", changes.Create[0].ProviderSpecific[0].Value)
243+
w.WriteHeader(http.StatusNoContent)
244+
return
245+
}
246+
}))
247+
defer svr.Close()
248+
249+
p, err := NewWebhookProvider(svr.URL)
250+
require.NoError(t, err)
251+
e := &endpoint.Endpoint{
252+
DNSName: "test.example.com",
253+
RecordTTL: 10,
254+
RecordType: "A",
255+
Targets: endpoint.Targets{
256+
"",
257+
},
258+
ProviderSpecific: endpoint.ProviderSpecific{
259+
endpoint.ProviderSpecificProperty{
260+
Name: "prop1",
261+
Value: "value1",
262+
},
263+
},
264+
}
265+
err = p.ApplyChanges(context.TODO(), &plan.Changes{
266+
Create: []*endpoint.Endpoint{
267+
e,
268+
},
269+
})
270+
require.NoError(t, err)
271+
}

source/source.go

+7
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ func getProviderSpecificAnnotations(annotations map[string]string) (endpoint.Pro
224224
Name: fmt.Sprintf("ibmcloud-%s", attr),
225225
Value: v,
226226
})
227+
} else if strings.HasPrefix(k, "external-dns.alpha.kubernetes.io/webhook-") {
228+
// Support for wildcard annotations for webhook providers
229+
attr := strings.TrimPrefix(k, "external-dns.alpha.kubernetes.io/webhook-")
230+
providerSpecificAnnotations = append(providerSpecificAnnotations, endpoint.ProviderSpecificProperty{
231+
Name: fmt.Sprintf("webhook/%s", attr),
232+
Value: v,
233+
})
227234
}
228235
}
229236
return providerSpecificAnnotations, setIdentifier

0 commit comments

Comments
 (0)