Skip to content
Draft
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod: ## Go mod things

install: ## Install the program to /usr/bin 🎉
@echo "Installing..."
sudo cp $(target) /usr/bin/$(target)
sudo cp bin/$(target) /usr/bin/$(target)

test: clean compile install ## 🤓 Run go tests
@echo "Testing..."
Expand Down
18 changes: 18 additions & 0 deletions client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories
vendor/

# Generated protobuf
pkg/api
44 changes: 44 additions & 0 deletions client/.golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
linters-settings:
gci:
sections:
- standard
- default
- prefix(github.com/aurae-runtime/ae)

linters:
disable:
- wrapcheck
- gomnd
- scopelint
- varnamelen
- testpackage
- tagliatelle
- paralleltest
- ireturn
- goerr113
- gochecknoglobals
- exhaustivestruct
- wsl
- exhaustive
- nosprintfhostport
- nonamedreturns
- interfacebloat
- exhaustruct
- lll
- gosec
- gomoddirectives
- godox
- gochecknoinits
- funlen
- dupl
- cyclop
# deprecated linters
- deadcode
- golint
- interfacer
- structcheck
- varcheck
- nosnakecase
- ifshort
- maligned
enable-all: true
66 changes: 66 additions & 0 deletions client/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# ---------------------------------------------------------------------------- #
# Apache 2.0 License Copyright © 2023 The Aurae Authors #
# #
# +--------------------------------------------+ #
# | █████╗ ██╗ ██╗██████╗ █████╗ ███████╗ | #
# | ██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ | #
# | ███████║██║ ██║██████╔╝███████║█████╗ | #
# | ██╔══██║██║ ██║██╔══██╗██╔══██║██╔══╝ | #
# | ██║ ██║╚██████╔╝██║ ██║██║ ██║███████╗ | #
# | ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ | #
# +--------------------------------------------+ #
# #
# Distributed Systems Runtime #
# #
# ---------------------------------------------------------------------------- #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
# ---------------------------------------------------------------------------- #

all: test

.PHONY: proto
proto: ## Generate code from protobuf schemas
@buf --version >/dev/null 2>&1 || (echo "Warning: buf is not installed! Please install the 'buf' command line tool: https://docs.buf.build/installation"; exit 1)
buf generate -v https://github.com/aurae-runtime/aurae.git#branch=main,subdir=api

.PHONY: mod
mod: ## Go mod things
go mod tidy
go mod vendor
go mod download

.PHONY: test
test: clean proto ## 🤓 Run go tests
@echo "Testing..."
go test -v ./...

.PHONY: fmt
fmt:
@gofumpt -version >/dev/null 2>&1 || (echo "Warning: gofumpt is not installed! Please install the 'gofumpt' command line tool: https://pkg.go.dev/mvdan.cc/gofumpt"; exit 1)
gofumpt -l -w .

.PHONY: lint
lint: fmt ## Linting the code according to the styling guide.
@golangci-lint --version >/dev/null 2>&1 || (echo "Warning: golangci-lint is not installed! Please install the 'golangci-lint' command line tool: https://golangci-lint.run/usage/install/"; exit 1)
golangci-lint run -c .golangci.yaml

.PHONY: clean
clean: ## Clean your artifacts 🧼
@echo "Cleaning..."
rm -rvf pkg/api/

.PHONY: help
help: ## Show help messages for make targets
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}'
8 changes: 8 additions & 0 deletions client/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: v1
plugins:
- plugin: buf.build/protocolbuffers/go:v1.28.1
out: pkg/api
opt: paths=source_relative
- plugin: buf.build/grpc/go:v1.2.0
out: pkg/api
opt: paths=source_relative
137 changes: 137 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/* -------------------------------------------------------------------------- *\
* Apache 2.0 License Copyright © 2022 The Aurae Authors *
* *
* +--------------------------------------------+ *
* | █████╗ ██╗ ██╗██████╗ █████╗ ███████╗ | *
* | ██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ | *
* | ███████║██║ ██║██████╔╝███████║█████╗ | *
* | ██╔══██║██║ ██║██╔══██╗██╔══██║██╔══╝ | *
* | ██║ ██║╚██████╔╝██║ ██║██║ ██║███████╗ | *
* | ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ | *
* +--------------------------------------------+ *
* *
* Distributed Systems Runtime *
* *
* -------------------------------------------------------------------------- *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
\* -------------------------------------------------------------------------- */

package client

import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"log"
"net"
"os"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials"

"github.com/aurae-runtime/ae/client/pkg/config"
"github.com/aurae-runtime/ae/client/pkg/runtime"
)

var ErrOptionNotAvailable = errors.New("option is not available")

type AuraeClient interface {
Runtime() (runtime.Runtime, error)
}

type auraeClient struct {
cfg *config.Configs
conn grpc.ClientConnInterface
runtime runtime.Runtime
}

func New(ctx context.Context, cfg ...config.Config) (AuraeClient, error) {
cf, err := config.From(cfg...)
if err != nil {
log.Fatal("Cannot initialize config", err)
}

tlsCredentials, err := loadTLSCredentials(cf.Auth)
if err != nil {
log.Fatal("Cannot load TLS credentials", err)
}

dialer := func(ctx context.Context, addr string) (net.Conn, error) {
d := net.Dialer{}

return d.DialContext(ctx, cf.System.Protocol, addr)
}

conn, err := grpc.Dial(
cf.System.Socket,
grpc.WithTransportCredentials(tlsCredentials),
grpc.WithContextDialer(dialer),
)
if err != nil {
log.Fatal("Cannot Dial", err)

return nil, err
}

r, err := runtime.New(ctx, conn)
if err != nil {
log.Fatal("Cannot crete runtime client", err)

return nil, err
}

c := &auraeClient{
cfg: cf,
conn: conn,
runtime: r,
}

return c, nil
}

func loadTLSCredentials(auth config.Auth) (credentials.TransportCredentials, error) {
caPEM, err := os.ReadFile(auth.CaCert)
if err != nil {
return nil, err
}

certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(caPEM) {
return nil, fmt.Errorf("failed to add server CA's certificate")
}

clientKeyPair, err := tls.LoadX509KeyPair(auth.ClientCert, auth.ClientKey)
if err != nil {
return nil, err
}

config := &tls.Config{
Certificates: []tls.Certificate{clientKeyPair},
RootCAs: certPool,
ServerName: auth.ServerName,
}

return credentials.NewTLS(config), nil
}

func (c *auraeClient) Runtime() (runtime.Runtime, error) {
if c.runtime == nil {
return nil, fmt.Errorf("configuration: %w", ErrOptionNotAvailable)
}

return c.runtime, nil
}
92 changes: 92 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* -------------------------------------------------------------------------- *\
* Apache 2.0 License Copyright © 2022 The Aurae Authors *
* *
* +--------------------------------------------+ *
* | █████╗ ██╗ ██╗██████╗ █████╗ ███████╗ | *
* | ██╔══██╗██║ ██║██╔══██╗██╔══██╗██╔════╝ | *
* | ███████║██║ ██║██████╔╝███████║█████╗ | *
* | ██╔══██║██║ ██║██╔══██╗██╔══██║██╔══╝ | *
* | ██║ ██║╚██████╔╝██║ ██║██║ ██║███████╗ | *
* | ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝ | *
* +--------------------------------------------+ *
* *
* Distributed Systems Runtime *
* *
* -------------------------------------------------------------------------- *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
\* -------------------------------------------------------------------------- */

package client

import (
"context"
"testing"

runtimev0 "github.com/aurae-runtime/ae/client/pkg/api/v0/runtime"
)

const (
testCellName = "MyBrandNewCell"
)

func TestClientRuntimeAllocate(t *testing.T) {
c, err := New(context.Background())
if err != nil {
t.Errorf("Cannot create client")
}

req := &runtimev0.CellServiceAllocateRequest{
Cell: &runtimev0.Cell{
Name: testCellName,
CpuQuota: 400000,
CpuShares: 2,
},
}

r, err := c.Runtime()
if err != nil {
t.Errorf("Runtime service not available")
}

rsp, err := r.AllocateCell(context.Background(), req)
if err != nil {
t.Errorf("AllocateCell should have NOT returned an error")
}

if rsp.CellName != testCellName {
t.Error("Cell name wrong")
}
}

func TestClientRuntimeFree(t *testing.T) {
c, err := New(context.Background())
if err != nil {
t.Errorf("Cannot create client")
}

req := &runtimev0.CellServiceFreeRequest{
CellName: testCellName,
}

r, err := c.Runtime()
if err != nil {
t.Errorf("Runtime service not available")
}

err = r.FreeCell(context.Background(), req)
if err != nil {
t.Errorf("FreeCell should have NOT returned an error")
}
}
16 changes: 16 additions & 0 deletions client/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/aurae-runtime/ae/client

go 1.19

require (
google.golang.org/grpc v1.51.0
google.golang.org/protobuf v1.28.1
)

require (
github.com/golang/protobuf v1.5.2 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
)
Loading