Skip to content

Commit

Permalink
Grpc boot service (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
majst01 authored Jun 10, 2022
1 parent c8666dd commit 43ece5a
Show file tree
Hide file tree
Showing 43 changed files with 3,315 additions and 973 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ jobs:
with:
go-version: '1.18.x'

- name: setup buf
uses: bufbuild/buf-setup-action@v1
with:
version: '1.5.0'

- name: generate proto and lint
working-directory: proto
run: make protoc

- name: Docker Login
uses: docker/login-action@v1
with:
Expand Down Expand Up @@ -47,10 +56,10 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Go 1.17
- name: Set up Go 1.18
uses: actions/setup-go@v2
with:
go-version: '1.17.x'
go-version: '1.18.x'

- name: Run integration tests
run: |
Expand Down
13 changes: 11 additions & 2 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ jobs:
with:
go-version: '1.18.x'

- name: setup buf
uses: bufbuild/buf-setup-action@v1
with:
version: '1.5.0'

- name: generate proto and lint
working-directory: proto
run: make protoc

- name: Figure out if running fork PR
id: fork
run: '["${{ secrets.DOCKER_REGISTRY_TOKEN }}" == ""] && echo "::set-output name=is_fork_pr::true" || echo "::set-output name=is_fork_pr::false"'
Expand Down Expand Up @@ -50,10 +59,10 @@ jobs:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Go 1.17
- name: Set up Go 1.18
uses: actions/setup-go@v2
with:
go-version: '1.17.x'
go-version: '1.18.x'

- name: Run integration tests
run: |
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ jobs:
with:
go-version: '1.18.x'

- name: setup buf
uses: bufbuild/buf-setup-action@v1
with:
version: '1.5.0'

- name: generate proto and lint
working-directory: proto
run: make protoc

- name: Docker Login
uses: docker/login-action@v1
with:
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
bin
vendor
.idea
.vscode/settings.json
.vscode
generate
coverage.out
__debug_bin
__debug_bin
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM metalstack/builder:latest as builder

FROM alpine:3.15
FROM alpine:3.16
RUN apk -U add ca-certificates
COPY --from=builder /work/bin/metal-api /metal-api
CMD ["/metal-api"]
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ MINI_LAB_KUBECONFIG := $(shell pwd)/../mini-lab/.kubeconfig

include $(COMMONDIR)/Makefile.inc

release:: protoc spec check-diff all ;
release:: spec check-diff all ;

.PHONY: spec
spec: all
Expand All @@ -22,12 +22,13 @@ redoc:

.PHONY: protoc
protoc:
protoc -I pkg --go_out plugins=grpc:pkg pkg/api/v1/*.proto
rm -rf pkg/api/v1
make -C proto protoc

.PHONY: protoc-docker
protoc-docker:
docker pull metalstack/builder
docker run --rm --user $$(id -u):$$(id -g) -v $(PWD):/work -w /work metalstack/builder protoc -I pkg --go_out plugins=grpc:pkg pkg/api/v1/*.proto
docker pull bufbuild/buf:1.5.0
docker run --rm --user $$(id -u):$$(id -g) -v $(PWD):/work --tmpfs /.cache -w /work/proto bufbuild/buf:1.5.0 generate -v

.PHONY: mini-lab-push
mini-lab-push:
Expand Down
4 changes: 2 additions & 2 deletions cmd/metal-api/internal/datastore/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func (rs *RethinkStore) FromHardware(hw metal.MachineHardware) (*metal.Size, []*
// this should not happen, so we do not return a notfound
return nil, nil, errors.New("no sizes found in database")
}
var sizes []metal.Size
var sizes metal.Sizes
for _, s := range sz {
if len(s.Constraints) < 1 {
rs.Error("missing constraints", "size", s)
continue
}
sizes = append(sizes, s)
}
return metal.Sizes(sizes).FromHardware(hw)
return sizes.FromHardware(hw)
}
101 changes: 101 additions & 0 deletions cmd/metal-api/internal/datastore/switch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package datastore

import (
"fmt"

"github.com/metal-stack/metal-api/cmd/metal-api/internal/metal"
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
)
Expand Down Expand Up @@ -64,6 +66,25 @@ func (rs *RethinkStore) SearchSwitches(rackid string, macs []string) ([]metal.Sw
return ss, nil
}

// SearchSwitchesByPartition searches for switches by the given partition.
func (rs *RethinkStore) SearchSwitchesByPartition(partitionID string) ([]metal.Switch, error) {
q := *rs.switchTable()

if partitionID != "" {
q = q.Filter(func(row r.Term) r.Term {
return row.Field("partitionid").Eq(partitionID)
})
}

var ss []metal.Switch
err := rs.searchEntities(&q, &ss)
if err != nil {
return nil, err
}

return ss, nil
}

// SearchSwitchesConnectedToMachine searches switches that are connected to the given machine.
func (rs *RethinkStore) SearchSwitchesConnectedToMachine(m *metal.Machine) ([]metal.Switch, error) {
switches, err := rs.SearchSwitches(m.RackID, nil)
Expand All @@ -79,3 +100,83 @@ func (rs *RethinkStore) SearchSwitchesConnectedToMachine(m *metal.Machine) ([]me
}
return res, nil
}

// SetVrfAtSwitches finds the switches connected to the given machine and puts the switch ports into the given vrf.
// Returns the updated switches.
func (rs *RethinkStore) SetVrfAtSwitches(m *metal.Machine, vrf string) ([]metal.Switch, error) {
switches, err := rs.SearchSwitchesConnectedToMachine(m)
if err != nil {
return nil, err
}
newSwitches := make([]metal.Switch, 0)
for i := range switches {
sw := switches[i]
oldSwitch := sw
sw.SetVrfOfMachine(m, vrf)
err := rs.UpdateSwitch(&oldSwitch, &sw)
if err != nil {
return nil, err
}
newSwitches = append(newSwitches, sw)
}
return newSwitches, nil
}

func (rs *RethinkStore) ConnectMachineWithSwitches(m *metal.Machine) error {
switches, err := rs.SearchSwitchesByPartition(m.PartitionID)
if err != nil {
return err
}
oldSwitches := []metal.Switch{}
newSwitches := []metal.Switch{}
for _, sw := range switches {
oldSwitch := sw
if cons := sw.ConnectMachine(m); cons > 0 {
oldSwitches = append(oldSwitches, oldSwitch)
newSwitches = append(newSwitches, sw)
}
}

if len(newSwitches) != 2 {
return fmt.Errorf("machine %v is not connected to exactly two switches, found connections to %d switches", m.ID, len(newSwitches))
}

s1 := newSwitches[0]
s2 := newSwitches[1]
cons1 := s1.MachineConnections[m.ID]
cons2 := s2.MachineConnections[m.ID]
connectionMapError := fmt.Errorf("twin-switches do not have a connection map that is mirrored crosswise for machine %v, switch %v (connections: %v), switch %v (connections: %v)", m.ID, s1.Name, cons1, s2.Name, cons2)
if len(cons1) != len(cons2) {
return connectionMapError
}

if s1.RackID != s2.RackID {
return fmt.Errorf("connected switches of a machine must reside in the same rack, rack of switch %s: %s, rack of switch %s: %s, machine: %s", s1.Name, s1.RackID, s2.Name, s2.RackID, m.ID)
}
// We detect the rackID of a machine by connections to leaf switches
m.RackID = s1.RackID
m.PartitionID = s1.PartitionID

byNicName, err := s2.MachineConnections.ByNicName()
if err != nil {
return err
}
for _, con := range s1.MachineConnections[m.ID] {
if con2, has := byNicName[con.Nic.Name]; has {
if con.Nic.Name != con2.Nic.Name {
return connectionMapError
}
} else {
return connectionMapError
}
}

for i := range oldSwitches {
err = rs.UpdateSwitch(&oldSwitches[i], &newSwitches[i])
if err != nil {
return err
}
}

return nil
}
Loading

0 comments on commit 43ece5a

Please sign in to comment.