-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support service dump #413
Merged
Merged
Support service dump #413
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
499ef93
Support service dump
hzxuzhonghu 1765bb9
Fix panic
hzxuzhonghu 92342ed
update service cache
hzxuzhonghu b588f41
Add unit test
hzxuzhonghu b66ff65
Add unit test
hzxuzhonghu ac8df7a
update
hzxuzhonghu 5cdfdac
Fix int
hzxuzhonghu 367f4d6
Fix lint
hzxuzhonghu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2024 The Kmesh Authors. | ||
* | ||
* 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://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 workloadapi | ||
|
||
// ResourceName returns the unique key of Workload. | ||
func (x *Workload) ResourceName() string { | ||
return x.Uid | ||
} | ||
|
||
// ResourceName returns the unique key of Service. | ||
func (x *Service) ResourceName() string { | ||
return x.Namespace + "/" + x.Hostname | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2024 The Kmesh Authors. | ||
* | ||
* 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://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 cache | ||
|
||
import ( | ||
"sync" | ||
|
||
"kmesh.net/kmesh/api/v2/workloadapi" | ||
) | ||
|
||
type ServiceCache interface { | ||
List() []*workloadapi.Service | ||
AddOrUpdateService(svc *workloadapi.Service) | ||
DeleteService(resourceName string) | ||
} | ||
|
||
type serviceCache struct { | ||
mutex sync.RWMutex | ||
// keyed by namespace/hostname->service | ||
servicesByResourceName map[string]*workloadapi.Service | ||
} | ||
|
||
func NewServiceCache() *serviceCache { | ||
return &serviceCache{ | ||
servicesByResourceName: make(map[string]*workloadapi.Service), | ||
} | ||
} | ||
|
||
func (s *serviceCache) AddOrUpdateService(svc *workloadapi.Service) { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
s.servicesByResourceName[svc.ResourceName()] = svc | ||
} | ||
|
||
func (s *serviceCache) DeleteService(resourceName string) { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
delete(s.servicesByResourceName, resourceName) | ||
} | ||
|
||
func (s *serviceCache) List() []*workloadapi.Service { | ||
s.mutex.RLock() | ||
defer s.mutex.RUnlock() | ||
out := make([]*workloadapi.Service, 0, len(s.servicesByResourceName)) | ||
for _, svc := range s.servicesByResourceName { | ||
out = append(out, svc) | ||
} | ||
|
||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright 2024 The Kmesh Authors. | ||
* | ||
* 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://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 status | ||
|
||
import ( | ||
"net" | ||
|
||
"kmesh.net/kmesh/api/v2/workloadapi" | ||
) | ||
|
||
type Workload struct { | ||
Uid string `json:"uid,omitempty"` | ||
Addresses []string `json:"addresses"` | ||
Waypoint *Waypoint `json:"waypoint,omitempty"` | ||
Protocol string `json:"protocol"` | ||
Name string `json:"name"` | ||
Namespace string `json:"namespace"` | ||
ServiceAccount string `json:"serviceAccount"` | ||
WorkloadName string `json:"workloadName"` | ||
WorkloadType string `json:"workloadType"` | ||
CanonicalName string `json:"canonicalName"` | ||
CanonicalRevision string `json:"canonicalRevision"` | ||
ClusterID string `json:"clusterId"` | ||
TrustDomain string `json:"trustDomain,omitempty"` | ||
Locality Locality `json:"locality,omitempty"` | ||
Node string `json:"node"` | ||
Network string `json:"network,omitempty"` | ||
Status string `json:"status"` | ||
ApplicationTunnel ApplicationTunnel `json:"applicationTunnel,omitempty"` | ||
} | ||
|
||
type Locality struct { | ||
Region string `json:"region,omitempty"` | ||
Zone string `json:"zone,omitempty"` | ||
Subzone string `json:"subzone,omitempty"` | ||
} | ||
|
||
type ApplicationTunnel struct { | ||
Protocol string `json:"protocol"` | ||
Port uint32 `json:"port,omitempty"` | ||
} | ||
|
||
type Waypoint struct { | ||
Destination string `json:"destination"` | ||
} | ||
|
||
type LoadBalancer struct { | ||
Mode string `json:"mode"` | ||
RoutingPreferences []string `json:"routingPreferences"` | ||
} | ||
|
||
type Service struct { | ||
Name string `json:"name"` | ||
Namespace string `json:"namespace"` | ||
Hostname string `json:"hostname"` | ||
Addresses []string `json:"vips"` | ||
Ports []*workloadapi.Port `json:"ports"` | ||
LoadBalancer *LoadBalancer `json:"loadBalancer"` | ||
Waypoint *Waypoint `json:"waypoint"` | ||
} | ||
|
||
type NetworkAddress struct { | ||
// Network represents the network this address is on. | ||
Network string | ||
// Address presents the IP (v4 or v6). | ||
Address net.IP | ||
} | ||
|
||
func ConvertWorkload(w *workloadapi.Workload) *Workload { | ||
ips := make([]string, 0, len(w.Addresses)) | ||
for _, addr := range w.Addresses { | ||
ips = append(ips, net.IP(addr).String()) | ||
} | ||
var waypoint string | ||
if waypointAddress := w.Waypoint.GetAddress(); waypointAddress != nil { | ||
waypoint = waypointAddress.Network + "/" + net.IP(waypointAddress.Address).String() | ||
} else if host := w.Waypoint.GetHostname(); host != nil { | ||
waypoint = host.Namespace + "/" + host.Hostname | ||
} | ||
|
||
out := &Workload{ | ||
Uid: w.Uid, | ||
Addresses: ips, | ||
Waypoint: &Waypoint{Destination: waypoint}, | ||
Protocol: w.TunnelProtocol.String(), | ||
Name: w.Name, | ||
Namespace: w.Namespace, | ||
ServiceAccount: w.ServiceAccount, | ||
WorkloadName: w.WorkloadName, | ||
WorkloadType: w.WorkloadType.String(), | ||
CanonicalName: w.CanonicalName, | ||
CanonicalRevision: w.CanonicalRevision, | ||
ClusterID: w.ClusterId, | ||
TrustDomain: w.TrustDomain, | ||
Node: w.Node, | ||
Network: w.Network, | ||
Status: w.Status.String(), | ||
} | ||
if w.Locality != nil { | ||
out.Locality = Locality{Region: w.Locality.Region, Zone: w.Locality.Zone, Subzone: w.Locality.Subzone} | ||
} | ||
if w.ApplicationTunnel != nil { | ||
out.ApplicationTunnel = ApplicationTunnel{Protocol: w.ApplicationTunnel.Protocol.String(), Port: w.ApplicationTunnel.Port} | ||
} | ||
return out | ||
} | ||
|
||
func ConvertService(s *workloadapi.Service) *Service { | ||
vips := make([]string, 0, len(s.Addresses)) | ||
for _, addr := range s.Addresses { | ||
vips = append(vips, addr.Network+"/"+net.IP(addr.Address).String()) | ||
} | ||
var waypoint string | ||
if waypointAddress := s.Waypoint.GetAddress(); waypointAddress != nil { | ||
waypoint = waypointAddress.Network + "/" + net.IP(waypointAddress.Address).String() | ||
} else if host := s.Waypoint.GetHostname(); host != nil { | ||
waypoint = host.Namespace + "/" + host.Hostname | ||
} | ||
|
||
out := &Service{ | ||
Name: s.Name, | ||
Namespace: s.Namespace, | ||
Hostname: s.Hostname, | ||
Addresses: vips, | ||
Ports: s.Ports, | ||
Waypoint: &Waypoint{Destination: waypoint}, | ||
} | ||
|
||
if s.LoadBalancing != nil { | ||
routingPreferences := make([]string, 0, len(s.LoadBalancing.RoutingPreference)) | ||
for _, p := range s.LoadBalancing.RoutingPreference { | ||
routingPreferences = append(routingPreferences, p.String()) | ||
} | ||
out.LoadBalancer = &LoadBalancer{Mode: s.LoadBalancing.Mode.String(), RoutingPreferences: routingPreferences} | ||
} | ||
|
||
return out | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would changing the name to GetResourceName be better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:I donot think so, it is actually generating resourceName.
ResourceName
looks much better/simpler to me.