generated from khulnasoft/khulnasoft-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.go
78 lines (66 loc) · 1.9 KB
/
process.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package model
import (
"context"
"fmt"
"net/url"
"strings"
"github.com/khulnasoft/meshkit/broker"
"github.com/khulnasoft/meshkit/utils"
"github.com/khulnasoft/meshkit/utils/kubernetes"
v1 "k8s.io/api/core/v1"
)
type K8SService struct{}
func (s *K8SService) Process(data []byte, k8sresource *KubernetesResource, evtype broker.EventType) error {
if evtype == broker.Delete {
return nil
}
k8sservice := &v1.Service{}
err := utils.Unmarshal(string(data), k8sservice)
if err != nil {
return err
}
urls := []string{}
endpoint, err := kubernetes.GetEndpoint(context.Background(), &kubernetes.ServiceOptions{}, k8sservice)
if err != nil {
return err
}
if endpoint != nil {
if endpoint.External != nil {
url, err := s.validateURL(endpoint.External.Address, endpoint.External.Port)
if err == nil {
urls = append(urls, url)
}
}
if endpoint.Internal != nil {
url, err := s.validateURL(endpoint.Internal.Address, endpoint.Internal.Port)
if err == nil {
urls = append(urls, url)
}
}
}
if k8sresource.ComponentMetadata == nil {
k8sresource.ComponentMetadata = make(map[string]interface{})
}
k8sresource.ComponentMetadata = map[string]interface{}{
"capabilities": map[string]interface{}{
// indicates that this svc can be upgraded to "Meshery Connection".
"connection": true,
"urls": urls,
},
}
return nil
}
func (s *K8SService) validateURL(address string, port int32) (serviceurl string, err error) {
protocol := "http"
if port == 443 {
protocol = "https"
}
// For some Cluster IP type svc the address is set as None
// Hence to prevent adding these IPs as URLs, below check is added.
if strings.Contains(strings.ToLower(address), "none") {
return serviceurl, kubernetes.ErrEndpointNotFound
}
serviceurl = fmt.Sprintf("%s://%s:%d", protocol, address, port)
_, err = url.ParseRequestURI(serviceurl)
return serviceurl, err
}