Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/metal-stack/metal-api int…
Browse files Browse the repository at this point in the history
…o ipv6-support
  • Loading branch information
majst01 committed Jul 11, 2024
2 parents 9f52dfd + ebd379a commit d431f24
Show file tree
Hide file tree
Showing 19 changed files with 931 additions and 470 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
r "gopkg.in/rethinkdb/rethinkdb-go.v6"

"github.com/metal-stack/metal-api/cmd/metal-api/internal/datastore"
"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
)

func init() {
Expand All @@ -20,10 +21,6 @@ func init() {
}

for _, old := range nws {
if !old.PrivateSuper {
continue
}

cursor, err := db.Table("partition").Get(old.PartitionID).Run(session)
if err != nil {
return err
Expand All @@ -36,7 +33,17 @@ func init() {

// TODO: does not work somehow
new := old
new.ChildPrefixLength = &partition.PrivateNetworkPrefixLength

af, err := metal.GetAddressFamily(new.Prefixes)
if err != nil {
return err
}
if af != nil {
new.AddressFamily = *af
}
if new.PrivateSuper {
new.DefaultChildPrefixLength = &partition.PrivateNetworkPrefixLength
}
err = rs.UpdateNetwork(&old, &new)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,29 @@ func Test_MigrationChildPrefixLength(t *testing.T) {
Base: metal.Base{
ID: "n1",
},
PartitionID: "p1",
PartitionID: "p1",
Prefixes: metal.Prefixes{
{IP: "10.0.0.0", Length: "8"},
},
PrivateSuper: true,
}
n2 = &metal.Network{
Base: metal.Base{
ID: "n2",
},
Prefixes: metal.Prefixes{
{IP: "2001::", Length: "64"},
},
PartitionID: "p2",
PrivateSuper: true,
}
n3 = &metal.Network{
Base: metal.Base{
ID: "n3",
},
Prefixes: metal.Prefixes{
{IP: "100.1.0.0", Length: "22"},
},
PartitionID: "p2",
PrivateSuper: false,
}
Expand Down Expand Up @@ -210,15 +219,18 @@ func Test_MigrationChildPrefixLength(t *testing.T) {
n1fetched, err := rs.FindNetworkByID(n1.ID)
require.NoError(t, err)
require.NotNil(t, n1fetched)
require.Equal(t, p1.PrivateNetworkPrefixLength, *n1fetched.ChildPrefixLength, fmt.Sprintf("childprefixlength:%d", *n1fetched.ChildPrefixLength))
require.Equal(t, p1.PrivateNetworkPrefixLength, *n1fetched.DefaultChildPrefixLength, fmt.Sprintf("childprefixlength:%d", *n1fetched.DefaultChildPrefixLength))
require.Equal(t, metal.IPv4AddressFamily, n1fetched.AddressFamily)

n2fetched, err := rs.FindNetworkByID(n2.ID)
require.NoError(t, err)
require.NotNil(t, n2fetched)
require.Equal(t, p2.PrivateNetworkPrefixLength, *n2fetched.ChildPrefixLength, fmt.Sprintf("childprefixlength:%d", *n2fetched.ChildPrefixLength))
require.Equal(t, p2.PrivateNetworkPrefixLength, *n2fetched.DefaultChildPrefixLength, fmt.Sprintf("childprefixlength:%d", *n2fetched.DefaultChildPrefixLength))
require.Equal(t, metal.IPv6AddressFamily, n2fetched.AddressFamily)

n3fetched, err := rs.FindNetworkByID(n3.ID)
require.NoError(t, err)
require.NotNil(t, n3fetched)
require.Nil(t, n3fetched.ChildPrefixLength)
require.Nil(t, n3fetched.DefaultChildPrefixLength)
require.Equal(t, metal.IPv4AddressFamily, n3fetched.AddressFamily)
}
31 changes: 19 additions & 12 deletions cmd/metal-api/internal/datastore/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ import (

// NetworkSearchQuery can be used to search networks.
type NetworkSearchQuery struct {
ID *string `json:"id" optional:"true"`
Name *string `json:"name" optional:"true"`
PartitionID *string `json:"partitionid" optional:"true"`
ProjectID *string `json:"projectid" optional:"true"`
Prefixes []string `json:"prefixes" optional:"true"`
DestinationPrefixes []string `json:"destinationprefixes" optional:"true"`
Nat *bool `json:"nat" optional:"true"`
PrivateSuper *bool `json:"privatesuper" optional:"true"`
Underlay *bool `json:"underlay" optional:"true"`
Vrf *int64 `json:"vrf" optional:"true"`
ParentNetworkID *string `json:"parentnetworkid" optional:"true"`
Labels map[string]string `json:"labels" optional:"true"`
ID *string `json:"id" optional:"true"`
Name *string `json:"name" optional:"true"`
PartitionID *string `json:"partitionid" optional:"true"`
ProjectID *string `json:"projectid" optional:"true"`
Prefixes []string `json:"prefixes" optional:"true"`
DestinationPrefixes []string `json:"destinationprefixes" optional:"true"`
Nat *bool `json:"nat" optional:"true"`
PrivateSuper *bool `json:"privatesuper" optional:"true"`
Underlay *bool `json:"underlay" optional:"true"`
Vrf *int64 `json:"vrf" optional:"true"`
ParentNetworkID *string `json:"parentnetworkid" optional:"true"`
Labels map[string]string `json:"labels" optional:"true"`
AddressFamily *metal.AddressFamily `json:"addressfamily" optional:"true"`
}

func (p *NetworkSearchQuery) Validate() error {
Expand Down Expand Up @@ -104,6 +105,12 @@ func (p *NetworkSearchQuery) generateTerm(rs *RethinkStore) (*r.Term, error) {
})
}

if p.AddressFamily != nil {
q = q.Filter(func(row r.Term) r.Term {
return row.Field("addressfamily").Eq(string(*p.AddressFamily))
})
}

for k, v := range p.Labels {
k := k
v := v
Expand Down
2 changes: 1 addition & 1 deletion cmd/metal-api/internal/datastore/rethinkdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (rs *RethinkStore) findEntity(query *r.Term, entity interface{}) error {
}
defer res.Close()
if res.IsNil() {
return metal.NotFound("no %v with found", getEntityName(entity))
return metal.NotFound("no %v found", getEntityName(entity))
}

hasResult := res.Next(entity)
Expand Down
62 changes: 62 additions & 0 deletions cmd/metal-api/internal/datastore/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,65 @@ import (
"errors"

"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
)

// SizeSearchQuery can be used to search sizes.
type SizeSearchQuery struct {
ID *string `json:"id" optional:"true"`
Name *string `json:"name" optional:"true"`
Labels map[string]string `json:"labels" optional:"true"`
Reservation Reservation `json:"reservation" optional:"true"`
}

type Reservation struct {
Partition *string `json:"partition" optional:"true"`
Project *string `json:"project" optional:"true"`
}

// GenerateTerm generates the project search query term.
func (s *SizeSearchQuery) generateTerm(rs *RethinkStore) *r.Term {
q := *rs.sizeTable()

if s.ID != nil {
q = q.Filter(func(row r.Term) r.Term {
return row.Field("id").Eq(*s.ID)
})
}

if s.Name != nil {
q = q.Filter(func(row r.Term) r.Term {
return row.Field("name").Eq(*s.Name)
})
}

for k, v := range s.Labels {
k := k
v := v
q = q.Filter(func(row r.Term) r.Term {
return row.Field("labels").Field(k).Eq(v)
})
}

if s.Reservation.Project != nil {
q = q.Filter(func(row r.Term) r.Term {
return row.Field("reservations").Contains(func(p r.Term) r.Term {
return p.Field("projectid").Eq(r.Expr(*s.Reservation.Project))
})
})
}

if s.Reservation.Partition != nil {
q = q.Filter(func(row r.Term) r.Term {
return row.Field("reservations").Contains(func(p r.Term) r.Term {
return p.Field("partitionids").Contains(r.Expr(*s.Reservation.Partition))
})
})
}

return &q
}

// FindSize return a size for a given id.
func (rs *RethinkStore) FindSize(id string) (*metal.Size, error) {
var s metal.Size
Expand All @@ -16,6 +73,11 @@ func (rs *RethinkStore) FindSize(id string) (*metal.Size, error) {
return &s, nil
}

// SearchSizes returns the result of the sizes search request query.
func (rs *RethinkStore) SearchSizes(q *SizeSearchQuery, sizes *metal.Sizes) error {
return rs.searchEntities(q.generateTerm(rs), sizes)
}

// ListSizes returns all sizes.
func (rs *RethinkStore) ListSizes() (metal.Sizes, error) {
szs := make(metal.Sizes, 0)
Expand Down
Loading

0 comments on commit d431f24

Please sign in to comment.