Skip to content
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 8 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions api/v2/workloadapi/workload_helper.go
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 {
Copy link
Collaborator

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?

Copy link
Member Author

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.

return x.Namespace + "/" + x.Hostname
}
2 changes: 1 addition & 1 deletion daemon/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func Execute(configs *options.BootstrapConfigs) error {
log.Info("controller Start successful")
defer c.Stop()

statusServer := status.NewServer(c, configs)
statusServer := status.NewServer(c.GetXdsClient(), configs)
statusServer.StartServer()
defer func() {
_ = statusServer.StopServer()
Expand Down
64 changes: 64 additions & 0 deletions pkg/controller/workload/cache/service_cache.go
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
}
9 changes: 5 additions & 4 deletions pkg/controller/workload/workload_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Processor struct {
Sm *kmeshsecurity.SecretManager
nodeName string
WorkloadCache cache.WorkloadCache
ServiceCache cache.ServiceCache
}

type Endpoint struct {
Expand All @@ -71,6 +72,7 @@ func newProcessor(workloadMap bpf2go.KmeshCgroupSockWorkloadMaps) *Processor {
bpf: bpf.NewCache(workloadMap),
nodeName: os.Getenv("NODE_NAME"),
WorkloadCache: cache.NewWorkloadCache(),
ServiceCache: cache.NewServiceCache(),
}
}

Expand Down Expand Up @@ -279,6 +281,7 @@ func (p *Processor) removeServiceResource(resources []string) error {
)

for _, name := range resources {
p.ServiceCache.DeleteService(name)
serviceId := p.hashName.StrToNum(name)
skDelete.ServiceId = serviceId
if err = p.bpf.ServiceLookup(&skDelete, &svDelete); err == nil {
Expand Down Expand Up @@ -575,10 +578,8 @@ func (p *Processor) handleService(service *workloadapi.Service) error {
sk = bpf.ServiceKey{}
sv = bpf.ServiceValue{}
)

NamespaceHostname := []string{service.GetNamespace(), service.GetHostname()}
serviceName := strings.Join(NamespaceHostname, "/")

p.ServiceCache.AddOrUpdateService(service)
serviceName := service.ResourceName()
serviceId := p.hashName.StrToNum(serviceName)
sk.ServiceId = serviceId
// if service has exist, just need update frontend port info
Expand Down
152 changes: 152 additions & 0 deletions pkg/status/api.go
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
}
Loading
Loading