Skip to content

Commit

Permalink
Merge branch 'master' into machine-pool-auto-scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Mar 6, 2024
2 parents fe95940 + 405ad42 commit 634d513
Show file tree
Hide file tree
Showing 34 changed files with 1,004 additions and 457 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

steps:
- name: Log in to the container registry
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_REGISTRY_USER }}
Expand All @@ -32,7 +32,7 @@ jobs:
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
cache: false
Expand Down Expand Up @@ -63,10 +63,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# metal-api

![Build](https://github.com/metal-stack/metal-api/workflows/Build%20from%20master/badge.svg)
[![Build](https://github.com/metal-stack/metal-api/actions/workflows/docker.yaml/badge.svg?branch=master)](https://github.com/metal-stack/metal-api/actions)
[![Slack](https://img.shields.io/badge/slack-metal--stack-brightgreen.svg?logo=slack)](https://metal-stack.slack.com/)
[![Go Report Card](https://goreportcard.com/badge/github.com/metal-stack/metal-api)](https://goreportcard.com/report/github.com/metal-stack/metal-api)
[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/metal-stack/metal-api)
Expand Down
4 changes: 3 additions & 1 deletion cmd/metal-api/internal/datastore/integer_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ func TestRethinkStore_AcquireUniqueIntegerPoolExhaustionIntegration(t *testing.T
go func() {
defer wg.Done()
got, err := pool.AcquireRandomUniqueInteger()
require.NoError(t, err)
if err != nil {
t.Fail()
}
assert.GreaterOrEqual(t, got, uint(rs.VRFPoolRangeMin))
assert.LessOrEqual(t, got, uint(rs.VRFPoolRangeMax))
t.Logf("acquired a vrf %d at: %s", got, time.Now())
Expand Down
9 changes: 2 additions & 7 deletions cmd/metal-api/internal/datastore/machine.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package datastore

import (
"crypto/rand"
"errors"
"fmt"
"math"
"math/big"
"math/rand/v2"

"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -668,11 +667,7 @@ func randomIndex(max int) int {
if max <= 0 {
return 0
}

b, _ := rand.Int(rand.Reader, big.NewInt(int64(max)))
idx := int(b.Uint64())

return idx
return rand.N(max)
}

func intersect[T comparable](a, b []T) []T {
Expand Down
4 changes: 2 additions & 2 deletions cmd/metal-api/internal/datastore/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ func BenchmarkElectMachine(b *testing.B) {
}
for _, t := range tests {
b.Run(t.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
for range b.N {
spreadAcrossRacks(t.args.allMachines, t.args.projectMachines, t.args.tags)
}
})
Expand All @@ -707,7 +707,7 @@ func getTestMachines(numPerRack int, rackids []string, tags []string) metal.Mach
machines := make(metal.Machines, 0)

for _, id := range rackids {
for i := 0; i < numPerRack; i++ {
for range numPerRack {
m := metal.Machine{
RackID: id,
Tags: tags,
Expand Down
42 changes: 42 additions & 0 deletions cmd/metal-api/internal/datastore/migrations/05_allocation_uuids.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package migrations

import (
r "gopkg.in/rethinkdb/rethinkdb-go.v6"

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

func init() {
datastore.MustRegisterMigration(datastore.Migration{
Name: "generate allocation uuids for already allocated machines",
Version: 5,
Up: func(db *r.Term, session r.QueryExecutor, rs *datastore.RethinkStore) error {
machines, err := rs.ListMachines()
if err != nil {
return err
}

for _, m := range machines {
m := m

if m.Allocation == nil {
continue
}

if m.Allocation.UUID != "" {
continue
}

newMachine := m
newMachine.Allocation.UUID = uuid.New().String()

err = rs.UpdateMachine(&m, &newMachine)
if err != nil {
return err
}
}
return nil
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,54 @@ func Test_Migration(t *testing.T) {
err = rs.Initialize()
require.NoError(t, err)

now := time.Now()
lastEventTime := now.Add(10 * time.Minute)
err = rs.UpsertProvisioningEventContainer(&metal.ProvisioningEventContainer{
Base: metal.Base{
ID: "1",
},
Liveliness: "",
Events: []metal.ProvisioningEvent{
{
Time: now,
Event: metal.ProvisioningEventPXEBooting,
var (
now = time.Now()
lastEventTime = now.Add(10 * time.Minute)
ec = &metal.ProvisioningEventContainer{
Base: metal.Base{
ID: "1",
},
{
Time: lastEventTime,
Event: metal.ProvisioningEventPreparing,
Liveliness: "",
Events: []metal.ProvisioningEvent{
{
Time: now,
Event: metal.ProvisioningEventPXEBooting,
},
{
Time: lastEventTime,
Event: metal.ProvisioningEventPreparing,
},
},
},
CrashLoop: false,
FailedMachineReclaim: false,
})
CrashLoop: false,
FailedMachineReclaim: false,
}
m = &metal.Machine{
Base: metal.Base{
ID: "1",
},
}
)

err = rs.UpsertProvisioningEventContainer(ec)
require.NoError(t, err)

err = rs.CreateMachine(m)
require.NoError(t, err)

updateM := *m
updateM.Allocation = &metal.MachineAllocation{}
err = rs.UpdateMachine(m, &updateM)
require.NoError(t, err)

err = rs.Migrate(nil, false)
require.NoError(t, err)

ec, err := rs.FindProvisioningEventContainer("1")
m, err = rs.FindMachineByID("1")
require.NoError(t, err)

assert.NotEmpty(t, m.Allocation.UUID, "allocation uuid was not generated")

ec, err = rs.FindProvisioningEventContainer("1")
require.NoError(t, err)
require.NoError(t, ec.Validate())

Expand Down
21 changes: 7 additions & 14 deletions cmd/metal-api/internal/grpc/boot-service-wait_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ package grpc

import (
"context"
"crypto/rand"
"errors"
"fmt"
"io"
"math/big"
"math/rand/v2"
"strconv"
"sync"
"testing"
Expand Down Expand Up @@ -129,7 +128,7 @@ func (t *test) run() {
}

ds, mock := datastore.InitMockDB(t.T)
for i := 0; i < t.numberMachineInstances; i++ {
for i := range t.numberMachineInstances {
machineID := strconv.Itoa(i)
mock.On(r.DB("mockdb").Table("machine").Get(machineID)).Return(metal.Machine{Base: metal.Base{ID: machineID}}, nil)
mock.On(insertMock(true, machineID)).Return(returnMock(true, machineID), nil)
Expand Down Expand Up @@ -214,7 +213,7 @@ func (t *test) stopMachineInstances() {
}

func (t *test) startApiInstances(ds *datastore.RethinkStore) {
for i := 0; i < t.numberApiInstances; i++ {
for i := range t.numberApiInstances {
ctx, cancel := context.WithCancel(context.Background())
allocate := make(chan string)

Expand Down Expand Up @@ -252,9 +251,9 @@ func (t *test) startMachineInstances() {
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithBlock(),
}
for i := 0; i < t.numberMachineInstances; i++ {
for i := range t.numberMachineInstances {
machineID := strconv.Itoa(i)
port := 50005 + t.randNumber(t.numberApiInstances)
port := 50005 + rand.N(t.numberApiInstances)
ctx, cancel := context.WithCancel(context.Background())
conn, err := grpc.DialContext(ctx, fmt.Sprintf("localhost:%d", port), opts...)
require.NoError(t, err)
Expand Down Expand Up @@ -317,7 +316,7 @@ func (t *test) waitForAllocation(machineID string, c v1.BootServiceClient, ctx c

func (t *test) allocateMachines() {
var alreadyAllocated []string
for i := 0; i < t.numberAllocations; i++ {
for range t.numberAllocations {
machineID := t.selectMachine(alreadyAllocated)
alreadyAllocated = append(alreadyAllocated, machineID)
t.mtx.Lock()
Expand All @@ -328,7 +327,7 @@ func (t *test) allocateMachines() {
}

func (t *test) selectMachine(except []string) string {
machineID := strconv.Itoa(t.randNumber(t.numberMachineInstances))
machineID := strconv.Itoa(rand.N(t.numberMachineInstances))
for _, id := range except {
if id == machineID {
return t.selectMachine(except)
Expand All @@ -342,9 +341,3 @@ func (t *test) simulateNsqNotifyAllocated(machineID string) {
s.allocate <- machineID
}
}

func (t *test) randNumber(n int) int {
nBig, err := rand.Int(rand.Reader, big.NewInt(int64(n)))
require.NoError(t, err)
return int(nBig.Int64())
}
Loading

0 comments on commit 634d513

Please sign in to comment.