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

kubernetes-sigs / external-dns / 15225059238

24 May 2025 08:04AM UTC coverage: 74.162% (-0.002%) from 74.164%
15225059238

push

github

web-flow
chore(code-quality): added linter errchkjson (#5448)

* chore(code-quality): added linter errchkjson

Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>

* chore(code-quality): added linter errchkjson

Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>

---------

Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>

2 of 4 new or added lines in 1 file covered. (50.0%)

15126 of 20396 relevant lines covered (74.16%)

691.78 hits per line

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

86.52
/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
        UrlAdjustEndpoints        = "/adjustendpoints"
37
        UrlApplyChanges           = "/applychanges"
38
        UrlRecords                = "/records"
39
)
40

41
type WebhookServer struct {
42
        Provider provider.Provider
43
}
44

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

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

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

107
func (p *WebhookServer) NegotiateHandler(w http.ResponseWriter, _ *http.Request) {
3✔
108
        w.Header().Set(ContentTypeHeader, MediaTypeFormatAndVersion)
3✔
109
        err := json.NewEncoder(w).Encode(p.Provider.GetDomainFilter())
3✔
110
        if err != nil {
3✔
NEW
111
                w.WriteHeader(http.StatusInternalServerError)
×
NEW
112
        }
×
113
}
114

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

1✔
128
        m := http.NewServeMux()
1✔
129
        m.HandleFunc("/", p.NegotiateHandler)
1✔
130
        m.HandleFunc(UrlRecords, p.RecordsHandler)
1✔
131
        m.HandleFunc(UrlAdjustEndpoints, p.AdjustEndpointsHandler)
1✔
132

1✔
133
        s := &http.Server{
1✔
134
                Addr:         providerPort,
1✔
135
                Handler:      m,
1✔
136
                ReadTimeout:  readTimeout,
1✔
137
                WriteTimeout: writeTimeout,
1✔
138
        }
1✔
139

1✔
140
        l, err := net.Listen("tcp", providerPort)
1✔
141
        if err != nil {
1✔
142
                log.Fatal(err)
×
143
        }
×
144

145
        if startedChan != nil {
2✔
146
                startedChan <- struct{}{}
1✔
147
        }
1✔
148

149
        if err := s.Serve(l); err != nil {
1✔
150
                log.Fatal(err)
×
151
        }
×
152
}
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