Skip to content

Commit 0e93562

Browse files
authored
[RSDK-9630][RSDK-9633] delete discover components (viamrobotics#4831)
1 parent 12939c4 commit 0e93562

18 files changed

+40
-830
lines changed

module/modmanager/manager.go

+4-38
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"google.golang.org/grpc/codes"
2828
"google.golang.org/grpc/credentials/insecure"
2929
"google.golang.org/grpc/status"
30-
"google.golang.org/protobuf/types/known/structpb"
3130

3231
"go.viam.com/rdk/config"
3332
"go.viam.com/rdk/ftdc"
@@ -88,7 +87,6 @@ type module struct {
8887
sharedConn rdkgrpc.SharedConn
8988
client pb.ModuleServiceClient
9089
// robotClient supplements the ModuleServiceClient client to serve select robot level methods from the module server
91-
// such as the DiscoverComponents API
9290
robotClient robotpb.RobotServiceClient
9391
addr string
9492
resources map[resource.Name]*addedResource
@@ -627,19 +625,19 @@ func (mgr *Manager) Configs() []config.Module {
627625
return configs
628626
}
629627

630-
// AllModels returns a slice of resource.ModuleModelDiscovery representing the available models
628+
// AllModels returns a slice of resource.ModuleModel representing the available models
631629
// from the currently managed modules.
632-
func (mgr *Manager) AllModels() []resource.ModuleModelDiscovery {
630+
func (mgr *Manager) AllModels() []resource.ModuleModel {
633631
moduleTypes := map[string]config.ModuleType{}
634-
models := []resource.ModuleModelDiscovery{}
632+
models := []resource.ModuleModel{}
635633
for _, moduleConfig := range mgr.Configs() {
636634
moduleName := moduleConfig.Name
637635
moduleTypes[moduleName] = moduleConfig.Type
638636
}
639637
for moduleName, handleMap := range mgr.Handles() {
640638
for api, handle := range handleMap {
641639
for _, model := range handle {
642-
modelModel := resource.ModuleModelDiscovery{
640+
modelModel := resource.ModuleModel{
643641
ModuleName: moduleName, Model: model, API: api.API,
644642
FromLocalModule: moduleTypes[moduleName] == config.ModuleTypeLocal,
645643
}
@@ -1328,10 +1326,6 @@ func (m *module) registerResources(mgr modmaninterface.ModuleManager) {
13281326
case api.API.IsComponent():
13291327
for _, model := range models {
13301328
m.logger.Infow("Registering component API and model from module", "module", m.cfg.Name, "API", api.API, "model", model)
1331-
// We must copy because the Discover closure func relies on api and model, but they are iterators and mutate.
1332-
// Copying prevents mutation.
1333-
modelCopy := model
1334-
apiCopy := api
13351329
resource.RegisterComponent(api.API, model, resource.Registration[resource.Resource, resource.NoNativeConfig]{
13361330
Constructor: func(
13371331
ctx context.Context,
@@ -1341,34 +1335,6 @@ func (m *module) registerResources(mgr modmaninterface.ModuleManager) {
13411335
) (resource.Resource, error) {
13421336
return mgr.AddResource(ctx, conf, DepsToNames(deps))
13431337
},
1344-
Discover: func(ctx context.Context, logger logging.Logger, extra map[string]interface{}) (interface{}, error) {
1345-
extraStructPb, err := structpb.NewStruct(extra)
1346-
if err != nil {
1347-
return nil, err
1348-
}
1349-
1350-
//nolint:deprecated,staticcheck
1351-
req := &robotpb.DiscoverComponentsRequest{
1352-
Queries: []*robotpb.DiscoveryQuery{
1353-
{Subtype: apiCopy.API.String(), Model: modelCopy.String(), Extra: extraStructPb},
1354-
},
1355-
}
1356-
1357-
//nolint:deprecated,staticcheck
1358-
res, err := m.robotClient.DiscoverComponents(ctx, req)
1359-
if err != nil {
1360-
m.logger.Errorf("error in modular DiscoverComponents: %s", err)
1361-
return nil, err
1362-
}
1363-
switch len(res.Discovery) {
1364-
case 0:
1365-
return nil, errors.New("modular DiscoverComponents response did not contain any discoveries")
1366-
case 1:
1367-
return res.Discovery[0].Results.AsMap(), nil
1368-
default:
1369-
return nil, errors.New("modular DiscoverComponents response contains more than one discovery")
1370-
}
1371-
},
13721338
})
13731339
}
13741340
case api.API.IsService():

module/modmanager/manager_test.go

-67
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package modmanager
33
import (
44
"bytes"
55
"context"
6-
"encoding/json"
76
"errors"
87
"fmt"
98
"os"
@@ -42,8 +41,6 @@ import (
4241
rutils "go.viam.com/rdk/utils"
4342
)
4443

45-
type testDiscoveryResult map[string]interface{}
46-
4744
func setupSocketWithRobot(t *testing.T) string {
4845
t.Helper()
4946

@@ -1537,70 +1534,6 @@ func TestFTDCAfterModuleCrash(t *testing.T) {
15371534
test.That(t, numModuleElapsedTimeMetricsSeen, test.ShouldBeGreaterThan, 0)
15381535
}
15391536

1540-
func TestModularDiscoverFunc(t *testing.T) {
1541-
ctx := context.Background()
1542-
logger := logging.NewTestLogger(t)
1543-
1544-
modPath := rtestutils.BuildTempModule(t, "module/testmodule")
1545-
1546-
modCfg := config.Module{
1547-
Name: "test-module",
1548-
ExePath: modPath,
1549-
}
1550-
1551-
parentAddr := setupSocketWithRobot(t)
1552-
1553-
mgr := setupModManager(t, ctx, parentAddr, logger, modmanageroptions.Options{UntrustedEnv: false})
1554-
1555-
err := mgr.Add(ctx, modCfg)
1556-
test.That(t, err, test.ShouldBeNil)
1557-
1558-
// The "helper" model implements actual (foobar) discovery
1559-
reg, ok := resource.LookupRegistration(generic.API, resource.NewModel("rdk", "test", "helper"))
1560-
test.That(t, ok, test.ShouldBeTrue)
1561-
test.That(t, reg, test.ShouldNotBeNil)
1562-
test.That(t, reg.Discover, test.ShouldNotBeNil)
1563-
1564-
testCases := []struct {
1565-
name string
1566-
params map[string]interface{}
1567-
expectedExtra string
1568-
}{
1569-
{
1570-
name: "Without extra set",
1571-
params: map[string]interface{}{},
1572-
expectedExtra: "default",
1573-
},
1574-
{
1575-
name: "With extra set",
1576-
params: map[string]interface{}{"extra": "not the default"},
1577-
expectedExtra: "not the default",
1578-
},
1579-
}
1580-
1581-
for _, tc := range testCases {
1582-
t.Run(tc.name, func(t *testing.T) {
1583-
result, err := reg.Discover(ctx, logger, tc.params)
1584-
test.That(t, err, test.ShouldBeNil)
1585-
t.Log("Discovery result: ", result)
1586-
1587-
jsonData, err := json.Marshal(result)
1588-
test.That(t, err, test.ShouldBeNil)
1589-
t.Logf("Raw JSON: %s", string(jsonData))
1590-
1591-
var discoveryResult testDiscoveryResult
1592-
err = json.Unmarshal(jsonData, &discoveryResult)
1593-
test.That(t, err, test.ShouldBeNil)
1594-
t.Logf("Casted struct: %+v", discoveryResult)
1595-
1596-
test.That(t, len(discoveryResult), test.ShouldEqual, 1)
1597-
extraStr, ok := discoveryResult["extra"].(string)
1598-
test.That(t, ok, test.ShouldBeTrue)
1599-
test.That(t, extraStr, test.ShouldEqual, tc.expectedExtra)
1600-
})
1601-
}
1602-
}
1603-
16041537
func TestFirstRun(t *testing.T) {
16051538
t.Run("fails", func(t *testing.T) {
16061539
ctx := context.Background()

module/modmaninterface/interface.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type ModuleManager interface {
2424
CleanModuleDataDirectory() error
2525

2626
Configs() []config.Module
27-
AllModels() []resource.ModuleModelDiscovery
27+
AllModels() []resource.ModuleModel
2828
Provides(cfg resource.Config) bool
2929
Handles() map[string]module.HandlerMap
3030

module/module.go

-63
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net"
99
"os"
1010
"path/filepath"
11-
"strings"
1211
"sync"
1312
"time"
1413

@@ -25,7 +24,6 @@ import (
2524
robotpb "go.viam.com/api/robot/v1"
2625
streampb "go.viam.com/api/stream/v1"
2726
"go.viam.com/utils"
28-
vprotoutils "go.viam.com/utils/protoutils"
2927
"go.viam.com/utils/rpc"
3028
"golang.org/x/exp/maps"
3129
"google.golang.org/grpc"
@@ -249,7 +247,6 @@ func NewModule(ctx context.Context, address string, logger logging.Logger) (*Mod
249247
return nil, err
250248
}
251249
// We register the RobotService API to supplement the ModuleService in order to serve select robot level methods from the module server
252-
// such as the DiscoverComponents API
253250
if err := m.server.RegisterServiceServer(ctx, &robotpb.RobotService_ServiceDesc, m); err != nil {
254251
return nil, err
255252
}
@@ -561,66 +558,6 @@ func (m *Module) AddResource(ctx context.Context, req *pb.AddResourceRequest) (*
561558
return &pb.AddResourceResponse{}, nil
562559
}
563560

564-
// DiscoverComponents is DEPRECATED!!! Please use the Discovery Service instead.
565-
// DiscoverComponents takes a list of discovery queries and returns corresponding
566-
// component configurations.
567-
//
568-
//nolint:deprecated,staticcheck
569-
func (m *Module) DiscoverComponents(
570-
ctx context.Context,
571-
req *robotpb.DiscoverComponentsRequest,
572-
) (*robotpb.DiscoverComponentsResponse, error) {
573-
var discoveries []*robotpb.Discovery
574-
575-
for _, q := range req.Queries {
576-
// Handle triplet edge case i.e. if the subtype doesn't contain ':', add the "rdk:component:" prefix
577-
if !strings.ContainsRune(q.Subtype, ':') {
578-
q.Subtype = "rdk:component:" + q.Subtype
579-
}
580-
581-
api, err := resource.NewAPIFromString(q.Subtype)
582-
if err != nil {
583-
return nil, fmt.Errorf("invalid subtype: %s: %w", q.Subtype, err)
584-
}
585-
model, err := resource.NewModelFromString(q.Model)
586-
if err != nil {
587-
return nil, fmt.Errorf("invalid model: %s: %w", q.Model, err)
588-
}
589-
590-
resInfo, ok := resource.LookupRegistration(api, model)
591-
if !ok {
592-
return nil, fmt.Errorf("no registration found for API %s and model %s", api, model)
593-
}
594-
595-
if resInfo.Discover == nil {
596-
return nil, fmt.Errorf("discovery not supported for API %s and model %s", api, model)
597-
}
598-
599-
results, err := resInfo.Discover(ctx, m.logger, q.Extra.AsMap())
600-
if err != nil {
601-
return nil, fmt.Errorf("error discovering components for API %s and model %s: %w", api, model, err)
602-
}
603-
if results == nil {
604-
return nil, fmt.Errorf("error discovering components for API %s and model %s: results was nil", api, model)
605-
}
606-
607-
pbResults, err := vprotoutils.StructToStructPb(results)
608-
if err != nil {
609-
return nil, fmt.Errorf("unable to convert discovery results to pb struct for query %v: %w", q, err)
610-
}
611-
612-
pbDiscovery := &robotpb.Discovery{
613-
Query: q,
614-
Results: pbResults,
615-
}
616-
discoveries = append(discoveries, pbDiscovery)
617-
}
618-
619-
return &robotpb.DiscoverComponentsResponse{
620-
Discovery: discoveries,
621-
}, nil
622-
}
623-
624561
// ReconfigureResource receives the component/service configuration from the parent.
625562
func (m *Module) ReconfigureResource(ctx context.Context, req *pb.ReconfigureResourceRequest) (*pb.ReconfigureResourceResponse, error) {
626563
var res resource.Resource

module/testmodule/main.go

-13
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,6 @@ func mainWithArgs(ctx context.Context, args []string, logger logging.Logger) err
4949
helperModel,
5050
resource.Registration[resource.Resource, resource.NoNativeConfig]{
5151
Constructor: newHelper,
52-
Discover: func(ctx context.Context, logger logging.Logger, extra map[string]interface{}) (interface{}, error) {
53-
extraVal, ok := extra["extra"]
54-
if !ok {
55-
extraVal = "default"
56-
}
57-
extraValStr, ok := extraVal.(string)
58-
if !ok {
59-
return nil, errors.New("'extra' value must be a string")
60-
}
61-
return map[string]interface{}{
62-
"extra": extraValStr,
63-
}, nil
64-
},
6552
})
6653
err = myMod.AddModelFromRegistry(ctx, generic.API, helperModel)
6754
if err != nil {

resource/discovery.go

-63
This file was deleted.

resource/module_model.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package resource
2+
3+
import (
4+
pb "go.viam.com/api/robot/v1"
5+
)
6+
7+
type (
8+
// ModuleModel holds the API and Model information of models within a module.
9+
ModuleModel struct {
10+
ModuleName string
11+
API API
12+
Model Model
13+
FromLocalModule bool
14+
}
15+
)
16+
17+
// ToProto converts a ModuleModel into the equivalent proto message.
18+
func (mm *ModuleModel) ToProto() *pb.ModuleModel {
19+
return &pb.ModuleModel{
20+
Model: mm.Model.String(), Api: mm.API.String(), ModuleName: mm.ModuleName,
21+
FromLocalModule: mm.FromLocalModule,
22+
}
23+
}

resource/resource_registry.go

-4
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,6 @@ type Registration[ResourceT Resource, ConfigT any] struct {
121121
// NOTE: This is currently an experimental feature and subject to change.
122122
WeakDependencies []Matcher
123123

124-
// Discover looks around for information about this specific model.
125-
Discover DiscoveryFunc
126-
127124
// configType can be used to dynamically inspect the resource config type.
128125
configType reflect.Type
129126

@@ -293,7 +290,6 @@ func makeGenericResourceRegistration[ResourceT Resource, ConfigT ConfigValidator
293290
reg := Registration[Resource, ConfigValidator]{
294291
// NOTE: any fields added to Registration must be copied/adapted here.
295292
WeakDependencies: typed.WeakDependencies,
296-
Discover: typed.Discover,
297293
isDefault: typed.isDefault,
298294
api: typed.api,
299295
configType: typed.configType,

0 commit comments

Comments
 (0)