Skip to content

Commit

Permalink
fix: parse SideroLink API endpoint correctly
Browse files Browse the repository at this point in the history
In the status controller, there was a wrong method to parse the endpoint
which doesn't account for all supported formats.

Use already parsed version in the config resource instead.

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
  • Loading branch information
smira committed Sep 11, 2024
1 parent a9269ac commit a294b36
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (suite *ConfigSuite) TestConfig() {

siderolinkConfig := &siderolinkcfg.ConfigV1Alpha1{
APIUrlConfig: meta.URL{
URL: must.Value(url.Parse("https://api.sidero.dev"))(suite.T()),
URL: must.Value(url.Parse("https://api.sidero.dev:334"))(suite.T()),
},
}

Expand All @@ -55,7 +55,8 @@ func (suite *ConfigSuite) TestConfig() {

rtestutils.AssertResources(suite.Ctx(), suite.T(), suite.State(), []resource.ID{siderolink.ConfigID},
func(c *siderolink.Config, assert *assert.Assertions) {
assert.Equal("https://api.sidero.dev", c.TypedSpec().APIEndpoint)
assert.Equal("https://api.sidero.dev:334", c.TypedSpec().APIEndpoint)
assert.Equal("api.sidero.dev:334", c.TypedSpec().Host)
})
}

Expand Down
11 changes: 2 additions & 9 deletions internal/app/machined/pkg/controllers/siderolink/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"net"
"net/url"
"os"
"time"

Expand Down Expand Up @@ -125,15 +124,9 @@ func (ctrl *StatusController) reconcileStatus(ctx context.Context, r controller.
return nil
}

var parsed *url.URL

if parsed, err = url.Parse(cfg.TypedSpec().APIEndpoint); err != nil {
return fmt.Errorf("failed to parse siderolink API endpoint: %w", err)
}

host, _, err := net.SplitHostPort(parsed.Host)
host, _, err := net.SplitHostPort(cfg.TypedSpec().Host)
if err != nil {
host = parsed.Host
host = cfg.TypedSpec().Host
}

down, err := peerDown(wgClient)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func (suite *StatusSuite) TestStatus() {
siderolinkConfig := siderolink.NewConfig(config.NamespaceName, siderolink.ConfigID)

siderolinkConfig.TypedSpec().APIEndpoint = "https://siderolink.example.org:1234?jointoken=supersecret&foo=bar#some=fragment"
siderolinkConfig.TypedSpec().Host = "siderolink.example.org:1234"

suite.Require().NoError(suite.State().Create(suite.Ctx(), siderolinkConfig))

Expand Down Expand Up @@ -86,6 +87,7 @@ func (suite *StatusSuite) TestStatus() {
// update API endpoint

siderolinkConfig.TypedSpec().APIEndpoint = "https://new.example.org?jointoken=supersecret"
siderolinkConfig.TypedSpec().Host = "new.example.org"

suite.Require().NoError(suite.State().Update(suite.Ctx(), siderolinkConfig))
suite.assertStatus("new.example.org", true)
Expand Down

0 comments on commit a294b36

Please sign in to comment.