Skip to content

Commit

Permalink
chore: allow to specify several endpoints
Browse files Browse the repository at this point in the history
This change relies on protobuf spec, where it doesn't matter how many times the field appears in the byte stream, because decoder will always take the last one for non-repeated fields. We use it to our advantage and simply pass primary endpoint address at the end, but we take it as the first.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Nov 24, 2023
1 parent 5ab8f9d commit 9304096
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 20 deletions.
41 changes: 41 additions & 0 deletions api/siderolink/provision.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// Package pb provides protobuf definitions for the SideroLink API.
package pb

import (
"errors"
"slices"
)

// MakeEndpoints sets the endpoints.
func MakeEndpoints(endpoints ...string) []string {
if len(endpoints) == 0 {
panic(errors.New("no endpoints provided"))
}

// To preserve GRPC backwards compatibility, we put the first endpoint at the end.
if len(endpoints) > 1 {
endpoints[0], endpoints[len(endpoints)-1] = endpoints[len(endpoints)-1], endpoints[0]
}

return endpoints
}

// GetEndpoints returns the endpoints.
func (m *ProvisionResponse) GetEndpoints() []string {
if m == nil || len(m.ServerEndpoint) == 0 {
return nil
}

endpoints := slices.Clone(m.ServerEndpoint)

// To preserve GRPC backwards compatibility, we put the first endpoint at the end.
if len(endpoints) > 1 {
endpoints[0], endpoints[len(endpoints)-1] = endpoints[len(endpoints)-1], endpoints[0]
}

return endpoints
}
8 changes: 4 additions & 4 deletions api/siderolink/provision.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/siderolink/provision.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ message ProvisionRequest {

message ProvisionResponse {
// Server Wireguard endpoint IP and port.
string server_endpoint = 1;
repeated string server_endpoint = 1;
// Server public Wireguard key encoded as string.
string server_public_key = 3;
// Node address with prefix on the Wireguard tunnel.
Expand Down
36 changes: 25 additions & 11 deletions api/siderolink/provision_vtproto.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (srv *Server) Provision(_ context.Context, req *pb.ProvisionRequest) (*pb.P
)

return &pb.ProvisionResponse{
ServerEndpoint: srv.cfg.ServerEndpoint.String(),
ServerEndpoint: pb.MakeEndpoints(srv.cfg.ServerEndpoint.String()),
ServerPublicKey: srv.cfg.ServerPublicKey.String(),
ServerAddress: srv.cfg.ServerAddress.String(),
NodeAddressPrefix: nodeAddress.String(),
Expand Down
6 changes: 3 additions & 3 deletions pkg/events/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func (s *state) HandleEvent(_ context.Context, e events.Event) error {
case *pb.ProvisionRequest:
s.NodeUUIDs = append(s.NodeUUIDs, msg.NodeUuid)
case *pb.ProvisionResponse:
if msg.ServerEndpoint != "" {
s.ServerEndpoints = append(s.ServerEndpoints, msg.ServerEndpoint)
if ep := msg.GetEndpoints(); ep != nil {
s.ServerEndpoints = append(s.ServerEndpoints, ep...)
}

if msg.ServerAddress != "" {
Expand Down Expand Up @@ -127,7 +127,7 @@ func (suite *SinkSuite) TestPublish() {
ServerAddress: "foo",
},
&pb.ProvisionResponse{
ServerEndpoint: "bar:123",
ServerEndpoint: []string{"bar:123"},
},
}

Expand Down

0 comments on commit 9304096

Please sign in to comment.