Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discovery: use interface CIDR for auto-generated subnet, tests #1452

Merged
merged 9 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions api/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,13 @@ paths:
schema:
type: string
style: form
- explode: true
in: query
name: wait
required: true
schema:
type: boolean
style: form
responses:
"204":
description: No Content
Expand Down
10 changes: 10 additions & 0 deletions api/api_roles_discovery.go

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

6 changes: 4 additions & 2 deletions api/docs/RolesDiscoveryApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ No authorization required

## DiscoverySubnetStart

> DiscoverySubnetStart(ctx).Identifier(identifier).Execute()
> DiscoverySubnetStart(ctx).Identifier(identifier).Wait(wait).Execute()

Discovery Subnets

Expand All @@ -537,10 +537,11 @@ import (

func main() {
identifier := "identifier_example" // string |
wait := true // bool |

configuration := openapiclient.NewConfiguration()
apiClient := openapiclient.NewAPIClient(configuration)
r, err := apiClient.RolesDiscoveryApi.DiscoverySubnetStart(context.Background()).Identifier(identifier).Execute()
r, err := apiClient.RolesDiscoveryApi.DiscoverySubnetStart(context.Background()).Identifier(identifier).Wait(wait).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `RolesDiscoveryApi.DiscoverySubnetStart``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
Expand All @@ -560,6 +561,7 @@ Other parameters are passed through a pointer to a apiDiscoverySubnetStartReques
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**identifier** | **string** | |
**wait** | **bool** | |

### Return type

Expand Down
2 changes: 1 addition & 1 deletion pkg/extconfig/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
}
}
}
return nil, fmt.Errorf("faild to find interface for %s", forIp.String())
return nil, fmt.Errorf("failed to find interface for %s", forIp.String())

Check warning on line 66 in pkg/extconfig/ip.go

View check run for this annotation

Codecov / codecov/patch

pkg/extconfig/ip.go#L66

Added line #L66 was not covered by tests
}

func (e *ExtConfig) GetIP() (net.IP, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/roles/dhcp/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
func generateHW() net.HardwareAddr {
return net.HardwareAddr(securecookie.GenerateRandomKey(6))
}

func RoleConfig() []byte {
return []byte(tests.MustJSON(dhcp.RoleConfig{
Port: 0,
Expand Down
7 changes: 6 additions & 1 deletion pkg/roles/discovery/api_subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@

type APISubnetsStartInput struct {
Name string `query:"identifier" required:"true"`
Wait bool `query:"wait" required:"true"`
}

func (r *Role) APISubnetsStart() usecase.Interactor {
Expand All @@ -107,7 +108,11 @@
r.log.Warn("failed to parse subnet from KV", zap.Error(err))
return status.Wrap(err, status.Internal)
}
go s.RunDiscovery(context.Background())
if input.Wait {
s.RunDiscovery(context.Background())
} else {
go s.RunDiscovery(context.Background())
}

Check warning on line 115 in pkg/roles/discovery/api_subnets.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/discovery/api_subnets.go#L114-L115

Added lines #L114 - L115 were not covered by tests
return nil
})
u.SetName("discovery.subnet_start")
Expand Down
4 changes: 1 addition & 3 deletions pkg/roles/discovery/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
dhcptypes "beryju.io/gravity/pkg/roles/dhcp/types"
"beryju.io/gravity/pkg/roles/discovery/types"
dnstypes "beryju.io/gravity/pkg/roles/dns/types"
"github.com/google/uuid"
"go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
Expand All @@ -27,8 +26,7 @@ type Device struct {

func (r *Role) newDevice() *Device {
return &Device{
Identifier: uuid.New().String(),
inst: r.i,
inst: r.i,
}
}

Expand Down
34 changes: 29 additions & 5 deletions pkg/roles/discovery/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import (
"context"
"net/netip"
"errors"
"fmt"
"net"

"beryju.io/gravity/pkg/extconfig"
instanceTypes "beryju.io/gravity/pkg/instance/types"
Expand Down Expand Up @@ -63,14 +65,14 @@
})
r.i.AddEventListener(instanceTypes.EventTopicInstanceFirstStart, func(ev *roles.Event) {
// On first start create a subnet based on the instance IP
subnet := r.NewSubnet("default-instance-subnet")
ip := netip.MustParseAddr(extconfig.Get().Instance.IP)
prefix, err := ip.Prefix(24)
subnet := r.NewSubnet(fmt.Sprintf("instance-subnet-%s", extconfig.Get().Instance.Identifier))

cidr, err := GetCIDRFromIP()
if err != nil {
r.log.Warn("failed to get prefix", zap.Error(err))
return
}
subnet.CIDR = prefix.String()
subnet.CIDR = cidr
subnet.DNSResolver = extconfig.Get().FallbackDNS
subnet.DiscoveryTTL = 86400
err = subnet.put(ev.Context)
Expand All @@ -83,6 +85,28 @@
return r
}

func GetCIDRFromIP() (string, error) {
ip := net.ParseIP(extconfig.Get().Instance.IP)
intf, err := extconfig.Get().GetInterfaceForIP(ip)
if err != nil {
return "", err
}

Check warning on line 93 in pkg/roles/discovery/role.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/discovery/role.go#L92-L93

Added lines #L92 - L93 were not covered by tests
addrs, err := intf.Addrs()
if err != nil {
return "", err
}

Check warning on line 97 in pkg/roles/discovery/role.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/discovery/role.go#L96-L97

Added lines #L96 - L97 were not covered by tests
for _, addr := range addrs {
iip, net, err := net.ParseCIDR(addr.String())
if err != nil {
continue

Check warning on line 101 in pkg/roles/discovery/role.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/discovery/role.go#L101

Added line #L101 was not covered by tests
}
if ip.Equal(iip) {
return net.String(), nil
}
}
return "", errors.New("no CIDR found")

Check warning on line 107 in pkg/roles/discovery/role.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/discovery/role.go#L107

Added line #L107 was not covered by tests
}

func (r *Role) Start(ctx context.Context, config []byte) error {
r.cfg = r.decodeRoleConfig(config)
if !r.cfg.Enabled || extconfig.Get().ListenOnlyMode {
Expand Down
4 changes: 4 additions & 0 deletions pkg/roles/discovery/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@
dev.MAC = addr.Addr
} else {
dev.IP = addr.Addr
dev.Identifier = addr.Addr
}
}
if dev.Identifier == "" {
continue

Check warning on line 132 in pkg/roles/discovery/subnet.go

View check run for this annotation

Codecov / codecov/patch

pkg/roles/discovery/subnet.go#L132

Added line #L132 was not covered by tests
}
devices = append(devices, *dev)
err := dev.put(tr.Context(), int64(s.DiscoveryTTL))
if err != nil {
Expand Down
Loading
Loading