• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / external-dns / 14749456359

30 Apr 2025 07:46AM UTC coverage: 72.24% (+0.2%) from 72.022%
14749456359

Pull #5333

github

web-flow
Update CONTRIBUTING.md
Pull Request #5333: docs(contributing): add conventional commits

14812 of 20504 relevant lines covered (72.24%)

688.82 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.51
/provider/webhook/api/httpapi.go
1
/*
2
Copyright 2023 The Kubernetes Authors.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package api
18

19
import (
20
        "context"
21
        "encoding/json"
22
        "net"
23
        "net/http"
24
        "time"
25

26
        "sigs.k8s.io/external-dns/endpoint"
27
        "sigs.k8s.io/external-dns/plan"
28
        "sigs.k8s.io/external-dns/provider"
29

30
        log "github.com/sirupsen/logrus"
31
)
32

33
const (
34
        MediaTypeFormatAndVersion = "application/external.dns.webhook+json;version=1"
35
        ContentTypeHeader         = "Content-Type"
36
)
37

38
type WebhookServer struct {
39
        Provider provider.Provider
40
}
41

42
func (p *WebhookServer) RecordsHandler(w http.ResponseWriter, req *http.Request) {
43
        switch req.Method {
44
        case http.MethodGet:
45
                records, err := p.Provider.Records(context.Background())
6✔
46
                if err != nil {
6✔
47
                        log.Errorf("Failed to get Records: %v", err)
2✔
48
                        w.WriteHeader(http.StatusInternalServerError)
2✔
49
                        return
3✔
50
                }
1✔
51
                w.Header().Set(ContentTypeHeader, MediaTypeFormatAndVersion)
1✔
52
                w.WriteHeader(http.StatusOK)
1✔
53
                if err := json.NewEncoder(w).Encode(records); err != nil {
1✔
54
                        log.Errorf("Failed to encode records: %v", err)
1✔
55
                }
1✔
56
                return
1✔
57
        case http.MethodPost:
×
58
                var changes plan.Changes
×
59
                if err := json.NewDecoder(req.Body).Decode(&changes); err != nil {
1✔
60
                        log.Errorf("Failed to decode changes: %v", err)
3✔
61
                        w.WriteHeader(http.StatusBadRequest)
3✔
62
                        return
4✔
63
                }
1✔
64
                err := p.Provider.ApplyChanges(context.Background(), &changes)
1✔
65
                if err != nil {
1✔
66
                        log.Errorf("Failed to apply changes: %v", err)
1✔
67
                        w.WriteHeader(http.StatusInternalServerError)
2✔
68
                        return
3✔
69
                }
1✔
70
                w.WriteHeader(http.StatusNoContent)
1✔
71
                return
1✔
72
        default:
1✔
73
                log.Errorf("Unsupported method %s", req.Method)
1✔
74
                w.WriteHeader(http.StatusBadRequest)
1✔
75
        }
1✔
76
}
1✔
77

1✔
78
func (p *WebhookServer) AdjustEndpointsHandler(w http.ResponseWriter, req *http.Request) {
79
        if req.Method != http.MethodPost {
80
                log.Errorf("Unsupported method %s", req.Method)
81
                w.WriteHeader(http.StatusBadRequest)
4✔
82
                return
5✔
83
        }
1✔
84

1✔
85
        pve := []*endpoint.Endpoint{}
1✔
86
        if err := json.NewDecoder(req.Body).Decode(&pve); err != nil {
1✔
87
                log.Errorf("Failed to decode in adjustEndpointsHandler: %v", err)
88
                w.WriteHeader(http.StatusBadRequest)
3✔
89
                return
4✔
90
        }
1✔
91
        w.Header().Set(ContentTypeHeader, MediaTypeFormatAndVersion)
1✔
92
        pve, err := p.Provider.AdjustEndpoints(pve)
1✔
93
        if err != nil {
1✔
94
                log.Errorf("Failed to call adjust endpoints: %v", err)
2✔
95
                w.WriteHeader(http.StatusInternalServerError)
2✔
96
        }
3✔
97
        if err := json.NewEncoder(w).Encode(&pve); err != nil {
1✔
98
                log.Errorf("Failed to encode in adjustEndpointsHandler: %v", err)
1✔
99
                w.WriteHeader(http.StatusInternalServerError)
1✔
100
                return
2✔
101
        }
×
102
}
×
103

×
104
func (p *WebhookServer) NegotiateHandler(w http.ResponseWriter, req *http.Request) {
×
105
        w.Header().Set(ContentTypeHeader, MediaTypeFormatAndVersion)
106
        json.NewEncoder(w).Encode(p.Provider.GetDomainFilter())
107
}
1✔
108

1✔
109
// StartHTTPApi starts a HTTP server given any provider.
1✔
110
// the function takes an optional channel as input which is used to signal that the server has started.
1✔
111
// The server will listen on port `providerPort`.
112
// The server will respond to the following endpoints:
113
// - / (GET): initialization, negotiates headers and returns the domain filter
114
// - /records (GET): returns the current records
115
// - /records (POST): applies the changes
116
// - /adjustendpoints (POST): executes the AdjustEndpoints method
117
func StartHTTPApi(provider provider.Provider, startedChan chan struct{}, readTimeout, writeTimeout time.Duration, providerPort string) {
118
        p := WebhookServer{
119
                Provider: provider,
120
        }
1✔
121

1✔
122
        m := http.NewServeMux()
1✔
123
        m.HandleFunc("/", p.NegotiateHandler)
1✔
124
        m.HandleFunc("/records", p.RecordsHandler)
1✔
125
        m.HandleFunc("/adjustendpoints", p.AdjustEndpointsHandler)
1✔
126

1✔
127
        s := &http.Server{
1✔
128
                Addr:         providerPort,
1✔
129
                Handler:      m,
1✔
130
                ReadTimeout:  readTimeout,
1✔
131
                WriteTimeout: writeTimeout,
1✔
132
        }
1✔
133

1✔
134
        l, err := net.Listen("tcp", providerPort)
1✔
135
        if err != nil {
1✔
136
                log.Fatal(err)
1✔
137
        }
1✔
138

1✔
139
        if startedChan != nil {
×
140
                startedChan <- struct{}{}
×
141
        }
142

2✔
143
        if err := s.Serve(l); err != nil {
1✔
144
                log.Fatal(err)
1✔
145
        }
146
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc