-
Notifications
You must be signed in to change notification settings - Fork 492
/
service.go
150 lines (122 loc) · 3.63 KB
/
service.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package serviceregistry
import (
"fmt"
"reflect"
)
type (
// ServiceInstanceSpec is the service instance spec in Easegress.
ServiceInstanceSpec struct {
// RegistryName is required.
RegistryName string `json:"registryName"`
// ServiceName is required.
ServiceName string `json:"serviceName"`
// InstanceID is required.
InstanceID string `json:"instanceID"`
// Address is required.
Address string `json:"address"`
// Port is required.
Port uint16 `json:"port"`
// Scheme is optional.
Scheme string `json:"scheme"`
// Tags is optional.
Tags []string `json:"tags"`
// Weight is optional.
Weight int `json:"weight"`
}
)
// DeepCopy deep copies ServiceInstanceSpec.
func (s *ServiceInstanceSpec) DeepCopy() *ServiceInstanceSpec {
copy := *s
if s.Tags != nil {
copy.Tags = append(copy.Tags, s.Tags...)
}
return ©
}
// Validate validates itself.
func (s *ServiceInstanceSpec) Validate() error {
if s.RegistryName == "" {
return fmt.Errorf("registryName is empty")
}
if s.ServiceName == "" {
return fmt.Errorf("serviceName is empty")
}
if s.InstanceID == "" {
return fmt.Errorf("instanceID is empty")
}
if s.Address == "" {
return fmt.Errorf("address is empty")
}
if s.Port == 0 {
return fmt.Errorf("port is empty")
}
switch s.Scheme {
case "", "http", "https":
default:
return fmt.Errorf("unsupported scheme %s (support http, https)", s.Scheme)
}
return nil
}
// Key returns the unique key for the service instance.
func (s *ServiceInstanceSpec) Key() string {
return fmt.Sprintf("%s/%s/%s", s.RegistryName, s.ServiceName, s.InstanceID)
}
// URL returns the url of the server.
func (s *ServiceInstanceSpec) URL() string {
scheme := s.Scheme
if scheme == "" {
scheme = "http"
}
return fmt.Sprintf("%s:https://%s:%d", scheme, s.Address, s.Port)
}
// NewRegistryEventFromDiff creates a registry event from diff old and new specs.
// It only uses Apply and Delete excluding Replace.
// External drivers should use event.Replace in first time, then use this utility to generate next events.
// registryName is only assigned to the event, the registry name of service instance spec won't change.
func NewRegistryEventFromDiff(registryName string, oldSpecs, newSpecs map[string]*ServiceInstanceSpec) *RegistryEvent {
if oldSpecs == nil {
oldSpecs = make(map[string]*ServiceInstanceSpec)
}
if newSpecs == nil {
newSpecs = make(map[string]*ServiceInstanceSpec)
}
event := &RegistryEvent{
SourceRegistryName: registryName,
}
for _, oldSpec := range oldSpecs {
_, exists := newSpecs[oldSpec.Key()]
if !exists {
copy := oldSpec.DeepCopy()
if event.Delete == nil {
event.Delete = make(map[string]*ServiceInstanceSpec)
}
event.Delete[oldSpec.Key()] = copy
}
}
for _, newSpec := range newSpecs {
oldSpec, exists := oldSpecs[newSpec.Key()]
if !exists || !reflect.DeepEqual(oldSpec, newSpec) {
copy := newSpec.DeepCopy()
if event.Apply == nil {
event.Apply = make(map[string]*ServiceInstanceSpec)
}
event.Apply[newSpec.Key()] = copy
}
}
return event
}