|
| 1 | +/* |
| 2 | +Copyright 2022 The Dapr Authors |
| 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 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package pluggable |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "os" |
| 19 | + "path/filepath" |
| 20 | + |
| 21 | + "github.com/dapr/dapr/utils" |
| 22 | + "github.com/dapr/kit/logger" |
| 23 | + |
| 24 | + "github.com/pkg/errors" |
| 25 | + |
| 26 | + "github.com/jhump/protoreflect/grpcreflect" |
| 27 | + |
| 28 | + "google.golang.org/grpc" |
| 29 | + reflectpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + discoveryLog = logger.NewLogger("pluggable-components-discovery") |
| 34 | + onServiceDiscovered map[string]func(name string, dialer GRPCConnectionDialer) |
| 35 | +) |
| 36 | + |
| 37 | +func init() { |
| 38 | + onServiceDiscovered = make(map[string]func(name string, dialer GRPCConnectionDialer)) |
| 39 | +} |
| 40 | + |
| 41 | +// AddServiceDiscoveryCallback adds a callback function that should be called when the given service was discovered. |
| 42 | +func AddServiceDiscoveryCallback(serviceName string, callbackFunc func(name string, dialer GRPCConnectionDialer)) { |
| 43 | + onServiceDiscovered[serviceName] = callbackFunc |
| 44 | +} |
| 45 | + |
| 46 | +// removeExt removes file extension |
| 47 | +func removeExt(fileName string) string { |
| 48 | + return fileName[:len(fileName)-len(filepath.Ext(fileName))] |
| 49 | +} |
| 50 | + |
| 51 | +const ( |
| 52 | + SocketFolderEnvVar = "DAPR_COMPONENTS_SOCKETS_FOLDER" |
| 53 | + defaultSocketFolder = "/tmp/dapr-components-sockets" |
| 54 | +) |
| 55 | + |
| 56 | +// GetSocketFolderPath returns the shared unix domain socket folder path |
| 57 | +func GetSocketFolderPath() string { |
| 58 | + return utils.GetEnvOrElse(SocketFolderEnvVar, defaultSocketFolder) |
| 59 | +} |
| 60 | + |
| 61 | +type service struct { |
| 62 | + // protoRef is the proto service name |
| 63 | + protoRef string |
| 64 | + // componentName is the component name that implements such service. |
| 65 | + componentName string |
| 66 | + // dialer is the used grpc connectiondialer. |
| 67 | + dialer GRPCConnectionDialer |
| 68 | +} |
| 69 | + |
| 70 | +type reflectServiceClient interface { |
| 71 | + ListServices() ([]string, error) |
| 72 | +} |
| 73 | + |
| 74 | +// serviceDiscovery returns all available discovered pluggable components services. |
| 75 | +// uses gRPC reflection package to list implemented services. |
| 76 | +func serviceDiscovery(reflectClientFactory func(string) (reflectServiceClient, func(), error)) ([]service, error) { |
| 77 | + services := []service{} |
| 78 | + componentsSocketPath := GetSocketFolderPath() |
| 79 | + _, err := os.Stat(componentsSocketPath) |
| 80 | + |
| 81 | + if os.IsNotExist(err) { // not exists is the same as empty. |
| 82 | + return services, nil |
| 83 | + } |
| 84 | + |
| 85 | + log.Debugf("loading pluggable components under path %s", componentsSocketPath) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + files, err := os.ReadDir(componentsSocketPath) |
| 91 | + if err != nil { |
| 92 | + return nil, errors.Wrap(err, "could not list pluggable components unix sockets") |
| 93 | + } |
| 94 | + |
| 95 | + for _, dirEntry := range files { |
| 96 | + if dirEntry.IsDir() { // skip dirs |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + f, err := dirEntry.Info() |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + socket := filepath.Join(componentsSocketPath, f.Name()) |
| 106 | + if !utils.IsSocket(f) { |
| 107 | + discoveryLog.Warnf("could not use socket for file %s", socket) |
| 108 | + continue |
| 109 | + } |
| 110 | + |
| 111 | + refctClient, cleanup, err := reflectClientFactory(socket) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + defer cleanup() |
| 116 | + |
| 117 | + serviceList, err := refctClient.ListServices() |
| 118 | + if err != nil { |
| 119 | + return nil, errors.Wrap(err, "unable to list services") |
| 120 | + } |
| 121 | + dialer := socketDialer(socket) |
| 122 | + |
| 123 | + componentName := removeExt(f.Name()) |
| 124 | + for _, svc := range serviceList { |
| 125 | + services = append(services, service{ |
| 126 | + componentName: componentName, |
| 127 | + protoRef: svc, |
| 128 | + dialer: dialer, |
| 129 | + }) |
| 130 | + } |
| 131 | + } |
| 132 | + log.Debugf("found %d pluggable component services", len(services)-1) // reflection api doesn't count. |
| 133 | + return services, nil |
| 134 | +} |
| 135 | + |
| 136 | +// callback invoke callback function for each given service |
| 137 | +func callback(services []service) { |
| 138 | + for _, service := range services { |
| 139 | + callback, ok := onServiceDiscovered[service.protoRef] |
| 140 | + if !ok { // ignoring unknown service |
| 141 | + continue |
| 142 | + } |
| 143 | + callback(service.componentName, service.dialer) |
| 144 | + log.Infof("pluggable component '%s' was successfully registered for '%s'", service.componentName, service.protoRef) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +// Discover discover the pluggable components and callback the service discovery with the given component name and grpc dialer. |
| 149 | +func Discover(ctx context.Context) error { |
| 150 | + services, err := serviceDiscovery(func(socket string) (reflectServiceClient, func(), error) { |
| 151 | + conn, err := SocketDial( |
| 152 | + ctx, |
| 153 | + socket, |
| 154 | + grpc.WithBlock(), |
| 155 | + ) |
| 156 | + if err != nil { |
| 157 | + return nil, nil, err |
| 158 | + } |
| 159 | + return grpcreflect.NewClient(ctx, reflectpb.NewServerReflectionClient(conn)), func() { |
| 160 | + conn.Close() |
| 161 | + }, nil |
| 162 | + }) |
| 163 | + if err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + |
| 167 | + callback(services) |
| 168 | + return nil |
| 169 | +} |
0 commit comments