Skip to content

Commit

Permalink
Merge pull request #29 from whywaita/refactor/awesome
Browse files Browse the repository at this point in the history
Refactor awesome
  • Loading branch information
whywaita committed Feb 20, 2024
2 parents f301cfd + f9f35e4 commit 3f3030b
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 84 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/build-docker-sha.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- uses: docker/setup-qemu-action@v2
- uses: docker/setup-buildx-action@v2
uses: actions/checkout@v4
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- name: Cache Docker layers
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/metadata-action@v3
- uses: docker/metadata-action@v5
id: meta
with:
images: ghcr.io/whywaita/shoes-lxd-multi
tags: |
type=sha
- name: Build container image
uses: docker/build-push-action@v3
uses: docker/build-push-action@v5
with:
push: true
tags: ${{ steps.meta.outputs.tags }}
Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: 1.19
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v3
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --rm-dist
Expand All @@ -27,9 +27,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- uses: docker/setup-qemu-action@v2
- uses: docker/setup-buildx-action@v2
uses: actions/checkout@v4
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- name: Cache Docker layers
uses: actions/cache@v3
with:
Expand All @@ -38,12 +38,12 @@ jobs:
restore-keys: |
${{ runner.os }}-buildx-
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
uses: docker/login-action@v5
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/metadata-action@v3
- uses: docker/metadata-action@v5
id: meta
with:
images: ghcr.io/whywaita/shoes-lxd-multi
Expand All @@ -52,7 +52,7 @@ jobs:
type=semver,pattern={{raw}}
type=sha
- name: Build container image
uses: docker/build-push-action@v3
uses: docker/build-push-action@v5
with:
push: true
tags: ${{ steps.meta.outputs.tags }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ jobs:
- shoes-lxd-multi
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Setup go
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version-file: ${{ matrix.target }}/go.mod
- name: lint
Expand All @@ -39,7 +39,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: docker build
Expand Down
2 changes: 1 addition & 1 deletion server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19 AS builder
FROM golang:1.21 AS builder

WORKDIR /go/src/github.com/whywaita/shoes-lxd-multi/server

Expand Down
3 changes: 3 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Server-side implementation for shoes-lxd-multi
- `LXD_MULTI_OVER_COMMIT_PERCENT`
- Percent of able over commit in CPU
- default: `100`
- `LXD_MULTI_RESOURCE_CACHE_PERIOD_SEC`
- Period of cache resource in seconds
- default: `10`

## Note
LXD Server can't use `zfs` in storageclass if use `--privileged`. ref: https://discuss.linuxcontainers.org/t/docker-with-overlay-driver-in-lxd-cluster-not-working/9243
Expand Down
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/whywaita/shoes-lxd-multi/server

go 1.19
go 1.21

require (
github.com/docker/go-units v0.5.0
Expand Down
5 changes: 3 additions & 2 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down Expand Up @@ -136,6 +134,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v41 v41.0.0 h1:HseJrM2JFf2vfiZJ8anY2hqBjdfY1Vlj/K27ueww4gg=
github.com/google/go-github/v41 v41.0.0/go.mod h1:XgmCA5H323A9rtgExdTcnDkcqp6S30AVACCBDOonIxg=
github.com/google/go-github/v47 v47.1.0 h1:Cacm/WxQBOa9lF0FT0EMjZ2BWMetQ1TQfyurn4yF1z8=
Expand Down Expand Up @@ -283,6 +282,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/whywaita/myshoes v1.14.0 h1:wIJoInogzSErsdVEwiKjWJuqXzg0mKpxxf5YKeaT334=
github.com/whywaita/myshoes v1.14.0/go.mod h1:Fea/XeUlwjWSZdqfG/Ub3+U278kGilIG9o9f6DxRpq8=
github.com/whywaita/shoes-lxd-multi/proto.go v0.0.0-20230331051154-d763b94b0dd7 h1:MdNKtHc/T+46wn7JfEi5P72Q/GHVPHLInqLldZB4eWE=
Expand Down Expand Up @@ -607,6 +607,7 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
11 changes: 5 additions & 6 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"fmt"
"log"
"math/rand"
"log/slog"
"net/http"
_ "net/http/pprof"
"os"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -19,7 +20,7 @@ import (
)

func main() {
rand.Seed(time.Now().UnixNano())
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, nil)))

if err := run(); err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -83,17 +84,15 @@ func serveMetrics(ctx context.Context, hostConfigs *config.HostConfigMap) {

func setLXDResourceCacheWithTicker(hcs []config.HostConfig, ticker *time.Ticker) {
for {
_ = <-ticker.C

log.Print("LXD cache is updating")
<-ticker.C
if err := setLXDResourceCache(hcs); err != nil {
log.Fatal(err)
}
}
}

func setLXDResourceCache(hcs []config.HostConfig) error {
hosts, err := lxdclient.ConnectLXDs(hcs)
hosts, _, err := lxdclient.ConnectLXDs(hcs)
if err != nil {
return fmt.Errorf("failed to connect LXD hosts: %s", err)
}
Expand Down
11 changes: 6 additions & 5 deletions server/pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"fmt"
"log"
"log/slog"
"net"
"sync"

Expand Down Expand Up @@ -41,7 +41,7 @@ func (s *ShoesLXDMultiServer) Run(listenPort int) error {
if err != nil {
return fmt.Errorf("failed to listen: %w", err)
}
log.Printf("start listen :%d\n", listenPort)
slog.Info("start listen", "port", listenPort)

grpcServer := grpc.NewServer()
pb.RegisterShoesLXDMultiServer(grpcServer, s)
Expand All @@ -52,13 +52,14 @@ func (s *ShoesLXDMultiServer) Run(listenPort int) error {
return nil
}

func (s *ShoesLXDMultiServer) validateTargetHosts(targetHosts []string) ([]lxdclient.LXDHost, error) {
func (s *ShoesLXDMultiServer) validateTargetHosts(targetHosts []string, logger *slog.Logger) ([]lxdclient.LXDHost, error) {
var hostConfigs []config.HostConfig

for _, target := range targetHosts {
l := logger.With("target", target)
host, err := s.hostConfigs.Load(target)
if err != nil {
log.Printf("ignore host in target (target: %s): %+v\n", target, err)
l.Warn("ignore host in target", "err", err.Error())
continue
}

Expand All @@ -69,7 +70,7 @@ func (s *ShoesLXDMultiServer) validateTargetHosts(targetHosts []string) ([]lxdcl
return nil, fmt.Errorf("valid target host is not found")
}

targetLXDHosts, err := lxdclient.ConnectLXDs(hostConfigs)
targetLXDHosts, _, err := lxdclient.ConnectLXDs(hostConfigs)
if err != nil {
return nil, fmt.Errorf("failed to connect LXD: %w", err)
}
Expand Down
Loading

0 comments on commit 3f3030b

Please sign in to comment.