|
| 1 | +// Copyright Red Hat, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the License); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an AS IS BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package meshfederation |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + "sync" |
| 22 | + |
| 23 | + "google.golang.org/protobuf/proto" |
| 24 | + "google.golang.org/protobuf/types/known/anypb" |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + "k8s.io/apimachinery/pkg/labels" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + |
| 29 | + protov1alpha1 "github.com/openshift-service-mesh/federation/internal/api/federation/v1alpha1" |
| 30 | + "github.com/openshift-service-mesh/federation/internal/pkg/discovery" |
| 31 | +) |
| 32 | + |
| 33 | +// TODO(design): should we have one server per MF or single server broadcasting to all -> that would imply recognizing subscribers somehow |
| 34 | +// TODO(design): currently we won't be able to run two meshfederations at once due to port conflict |
| 35 | +type serverExporter struct { |
| 36 | + server *discovery.Server |
| 37 | + handler *exportedServicesBroadcaster |
| 38 | +} |
| 39 | + |
| 40 | +type serviceExporterRegistry struct { |
| 41 | + exporters sync.Map |
| 42 | +} |
| 43 | + |
| 44 | +func (r *serviceExporterRegistry) LoadOrStore(name string, serviceExporter *exportedServicesBroadcaster) *discovery.Server { |
| 45 | + actual, exists := r.exporters.LoadOrStore(name, serverExporter{ |
| 46 | + server: discovery.NewServer(serviceExporter), |
| 47 | + handler: serviceExporter, |
| 48 | + }) |
| 49 | + |
| 50 | + exporter := actual.(serverExporter) |
| 51 | + if exists { |
| 52 | + // update settings |
| 53 | + exporter.handler.selector = serviceExporter.selector |
| 54 | + } |
| 55 | + |
| 56 | + return exporter.server |
| 57 | +} |
| 58 | + |
| 59 | +var _ discovery.RequestHandler = (*exportedServicesBroadcaster)(nil) |
| 60 | + |
| 61 | +type exportedServicesBroadcaster struct { |
| 62 | + client client.Client |
| 63 | + typeUrl string |
| 64 | + selector labels.Selector |
| 65 | +} |
| 66 | + |
| 67 | +func (e exportedServicesBroadcaster) GetTypeUrl() string { |
| 68 | + return e.typeUrl |
| 69 | +} |
| 70 | + |
| 71 | +func (e exportedServicesBroadcaster) GenerateResponse() ([]*anypb.Any, error) { |
| 72 | + services := &corev1.ServiceList{} |
| 73 | + // TODO: rework ads(s|c) to get ctx? |
| 74 | + // We cannot latch into ctx from owning Reconcile call, as it generator can be called from outside reconcile loop |
| 75 | + if errSvcList := e.client.List(context.TODO(), services, client.MatchingLabelsSelector{Selector: e.selector}); errSvcList != nil { |
| 76 | + return []*anypb.Any{}, errSvcList |
| 77 | + } |
| 78 | + |
| 79 | + return convert(services.Items) |
| 80 | +} |
| 81 | + |
| 82 | +func convert(services []corev1.Service) ([]*anypb.Any, error) { |
| 83 | + var federatedServices []*protov1alpha1.FederatedService |
| 84 | + |
| 85 | + for _, svc := range services { |
| 86 | + var ports []*protov1alpha1.ServicePort |
| 87 | + for _, port := range svc.Spec.Ports { |
| 88 | + servicePort := &protov1alpha1.ServicePort{ |
| 89 | + Name: port.Name, |
| 90 | + Number: uint32(port.Port), |
| 91 | + } |
| 92 | + if port.TargetPort.IntVal != 0 { |
| 93 | + servicePort.TargetPort = uint32(port.TargetPort.IntVal) |
| 94 | + } |
| 95 | + servicePort.Protocol = detectProtocol(port.Name) |
| 96 | + ports = append(ports, servicePort) |
| 97 | + } |
| 98 | + federatedSvc := &protov1alpha1.FederatedService{ |
| 99 | + Hostname: fmt.Sprintf("%s.%s.svc.cluster.local", svc.Name, svc.Namespace), |
| 100 | + Ports: ports, |
| 101 | + Labels: svc.Labels, |
| 102 | + } |
| 103 | + federatedServices = append(federatedServices, federatedSvc) |
| 104 | + } |
| 105 | + |
| 106 | + return serialize(federatedServices) |
| 107 | +} |
| 108 | + |
| 109 | +// TODO: check appProtocol and reject UDP |
| 110 | +func detectProtocol(portName string) string { |
| 111 | + if portName == "https" || strings.HasPrefix(portName, "https-") { |
| 112 | + return "HTTPS" |
| 113 | + } else if portName == "http" || strings.HasPrefix(portName, "http-") { |
| 114 | + return "HTTP" |
| 115 | + } else if portName == "http2" || strings.HasPrefix(portName, "http2-") { |
| 116 | + return "HTTP2" |
| 117 | + } else if portName == "grpc" || strings.HasPrefix(portName, "grpc-") { |
| 118 | + return "GRPC" |
| 119 | + } else if portName == "tls" || strings.HasPrefix(portName, "tls-") { |
| 120 | + return "TLS" |
| 121 | + } else if portName == "mongo" || strings.HasPrefix(portName, "mongo-") { |
| 122 | + return "MONGO" |
| 123 | + } |
| 124 | + return "TCP" |
| 125 | +} |
| 126 | + |
| 127 | +func serialize(exportedServices []*protov1alpha1.FederatedService) ([]*anypb.Any, error) { |
| 128 | + var serializedServices []*anypb.Any |
| 129 | + for _, exportedService := range exportedServices { |
| 130 | + serializedExportedService := &anypb.Any{} |
| 131 | + if err := anypb.MarshalFrom(serializedExportedService, exportedService, proto.MarshalOptions{}); err != nil { |
| 132 | + return []*anypb.Any{}, fmt.Errorf("failed to serialize ExportedService %s to protobuf message: %w", exportedService.Hostname, err) |
| 133 | + } |
| 134 | + serializedServices = append(serializedServices, serializedExportedService) |
| 135 | + } |
| 136 | + return serializedServices, nil |
| 137 | +} |
0 commit comments