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

emqx / emqx-operator / 3693077438

pending completion
3693077438

Pull #512

github

raoxiaoli
docs: add license task doc
Pull Request #512: docs: add license task doc

3018 of 3991 relevant lines covered (75.62%)

1.43 hits per line

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

77.92
/controllers/apps/v2alpha1/api.go
1
/*
2
Copyright 2021.
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 v2alpha1
18

19
import (
20
        "encoding/json"
21
        "fmt"
22
        "net"
23
        "regexp"
24
        "strconv"
25
        "strings"
26

27
        emperror "emperror.dev/errors"
28

29
        appsv2alpha1 "github.com/emqx/emqx-operator/apis/apps/v2alpha1"
30
        "github.com/emqx/emqx-operator/pkg/handler"
31
        corev1 "k8s.io/api/core/v1"
32
        "k8s.io/apimachinery/pkg/util/intstr"
33
        "sigs.k8s.io/controller-runtime/pkg/client"
34
)
35

36
type requestAPI struct {
37
        Username string
38
        Password string
39
        Port     string
40
        handler.Handler
41
}
42

43
func (r *requestAPI) getNodeStatuesByAPI(obj client.Object) ([]appsv2alpha1.EMQXNode, error) {
1✔
44
        resp, body, err := r.Handler.RequestAPI(obj, EMQXContainerName, "GET", r.Username, r.Password, r.Port, "api/v5/nodes")
1✔
45
        if err != nil {
2✔
46
                return nil, emperror.Wrap(err, "failed to get API api/v5/nodes")
1✔
47
        }
1✔
48
        if resp.StatusCode != 200 {
2✔
49
                return nil, emperror.Errorf("failed to get API %s, status : %s, body: %s", "api/v5/nodes", resp.Status, body)
1✔
50
        }
1✔
51

52
        nodeStatuses := []appsv2alpha1.EMQXNode{}
1✔
53
        if err := json.Unmarshal(body, &nodeStatuses); err != nil {
1✔
54
                return nil, emperror.Wrap(err, "failed to unmarshal node statuses")
×
55
        }
×
56
        return nodeStatuses, nil
1✔
57
}
58

59
type emqxGateway struct {
60
        Name   string `json:"name"`
61
        Status string `json:"status"`
62
}
63

64
type emqxListener struct {
65
        Enable bool   `json:"enable"`
66
        ID     string `json:"id"`
67
        Bind   string `json:"bind"`
68
        Type   string `json:"type"`
69
}
70

71
func (r *requestAPI) getAllListenersByAPI(obj client.Object) ([]corev1.ServicePort, error) {
1✔
72
        ports, err := r.getListenerPortsByAPI(obj, "api/v5/listeners")
1✔
73
        if err != nil {
1✔
74
                return nil, err
×
75
        }
×
76

77
        gateways, err := r.getGatewaysByAPI(obj)
1✔
78
        if err != nil {
2✔
79
                return nil, err
1✔
80
        }
1✔
81

82
        for _, gateway := range gateways {
2✔
83
                if strings.ToLower(gateway.Status) == "running" {
2✔
84
                        apiPath := fmt.Sprintf("api/v5/gateway/%s/listeners", gateway.Name)
1✔
85
                        gatewayPorts, err := r.getListenerPortsByAPI(obj, apiPath)
1✔
86
                        if err != nil {
1✔
87
                                return nil, err
×
88
                        }
×
89
                        ports = append(ports, gatewayPorts...)
1✔
90
                }
91
        }
92

93
        return ports, nil
1✔
94
}
95

96
func (r *requestAPI) getGatewaysByAPI(obj client.Object) ([]emqxGateway, error) {
1✔
97
        resp, body, err := r.Handler.RequestAPI(obj, EMQXContainerName, "GET", r.Username, r.Password, r.Port, "api/v5/gateway")
1✔
98
        if err != nil {
1✔
99
                return nil, emperror.Wrap(err, "failed to get API api/v5/gateway")
×
100
        }
×
101
        if resp.StatusCode != 200 {
2✔
102
                return nil, emperror.Errorf("failed to get API %s, status : %s, body: %s", "api/v5/gateway", resp.Status, body)
1✔
103
        }
1✔
104
        gateway := []emqxGateway{}
1✔
105
        if err := json.Unmarshal(body, &gateway); err != nil {
1✔
106
                return nil, emperror.Wrap(err, "failed to parse gateway")
×
107
        }
×
108
        return gateway, nil
1✔
109
}
110

111
func (r *requestAPI) getListenerPortsByAPI(obj client.Object, apiPath string) ([]corev1.ServicePort, error) {
1✔
112
        resp, body, err := r.Handler.RequestAPI(obj, EMQXContainerName, "GET", r.Username, r.Password, r.Port, apiPath)
1✔
113
        if err != nil {
1✔
114
                return nil, emperror.Wrapf(err, "failed to get API %s", apiPath)
×
115
        }
×
116
        if resp.StatusCode != 200 {
1✔
117
                return nil, emperror.Errorf("failed to get API %s, status : %s, body: %s", apiPath, resp.Status, body)
×
118
        }
×
119
        ports := []corev1.ServicePort{}
1✔
120
        listeners := []emqxListener{}
1✔
121
        if err := json.Unmarshal(body, &listeners); err != nil {
1✔
122
                return nil, emperror.Wrap(err, "failed to parse listeners")
×
123
        }
×
124
        for _, listener := range listeners {
2✔
125
                if !listener.Enable {
1✔
126
                        continue
×
127
                }
128

129
                var protocol corev1.Protocol
1✔
130
                compile := regexp.MustCompile(".*(udp|dtls|quic).*")
1✔
131
                if compile.MatchString(listener.Type) {
2✔
132
                        protocol = corev1.ProtocolUDP
1✔
133
                } else {
2✔
134
                        protocol = corev1.ProtocolTCP
1✔
135
                }
1✔
136

137
                _, strPort, _ := net.SplitHostPort(listener.Bind)
1✔
138
                intPort, _ := strconv.Atoi(strPort)
1✔
139

1✔
140
                ports = append(ports, corev1.ServicePort{
1✔
141
                        Name:       strings.ReplaceAll(listener.ID, ":", "-"),
1✔
142
                        Protocol:   protocol,
1✔
143
                        Port:       int32(intPort),
1✔
144
                        TargetPort: intstr.FromInt(intPort),
1✔
145
                })
1✔
146
        }
147
        return ports, nil
1✔
148
}
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

© 2025 Coveralls, Inc