Skip to content

Commit

Permalink
Daemon: implement http server daemon
Browse files Browse the repository at this point in the history
Use echo framework to build an HTTP server with router, metric,
etc, it serves harbor webhook requests.

Signed-off-by: Yan Song <imeoer@linux.alibaba.com>
  • Loading branch information
imeoer committed Aug 23, 2021
1 parent ad19745 commit 59fbaef
Show file tree
Hide file tree
Showing 15 changed files with 1,095 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

/acceleration-service
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GIT_COMMIT := $(shell git rev-list -1 HEAD)
BUILD_TIME := $(shell date -u +%Y%m%d.%H%M)
CWD := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

default:
go build -ldflags '-X main.versionGitCommit=${GIT_COMMIT} -X main.versionBuildTime=${BUILD_TIME}' -gcflags=all="-N -l" ./cmd/acceleration-service
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# acceleration-service
Provides a general service to support image acceleration based on kinds of accelerator like Nydus and Stargz etc.
Provides a general service to support image acceleration based on kinds of accelerator like Nydus and eStargz etc.
70 changes: 70 additions & 0 deletions cmd/acceleration-service/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright Project Harbor Authors
//
// 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 main

import (
"fmt"
"os"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

"github.com/goharbor/acceleration-service/pkg/config"
"github.com/goharbor/acceleration-service/pkg/daemon"
)

var versionGitCommit string
var versionBuildTime string

func main() {
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})

version := fmt.Sprintf("%s.%s", versionGitCommit, versionBuildTime)
logrus.Infof("Version: %s\n", version)

app := &cli.App{
Name: "acceleration-service",
Usage: "Provides a general service to support image acceleration based on kinds of accelerator like Nydus and eStargz etc.",
Version: version,
Flags: []cli.Flag{
&cli.StringFlag{Name: "config", Required: true, Usage: "Specify the path of config in yaml format"},
},
Action: func(c *cli.Context) error {
cfg, err := config.Parse(c.String("config"))
if err != nil {
return err
}

d, err := daemon.NewDaemon(cfg)
if err != nil {
return errors.Wrapf(err, "create daemon")
}

if err := d.Run(); err != nil {
return errors.Wrapf(err, "run daemon")
}

return nil
},
}

err := app.Run(os.Args)
if err != nil {
logrus.Fatal(err)
}
}
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/goharbor/acceleration-service

go 1.16

require (
github.com/labstack/echo-contrib v0.11.0
github.com/labstack/echo/v4 v4.5.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.10.0
github.com/sirupsen/logrus v1.8.1
github.com/urfave/cli/v2 v2.3.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
520 changes: 520 additions & 0 deletions go.sum

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions misc/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
server:
name: API
host: 0.0.0.0
port: 2077
47 changes: 47 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Project Harbor Authors
//
// 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 config

import (
"io/ioutil"

"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)

type Config struct {
Server ServerConfig `yaml:"server"`
}

type ServerConfig struct {
Name string `yaml:"name"`
Uds string `yaml:"uds"`
Host string `yaml:"host"`
Port string `yaml:"port"`
}

func Parse(configPath string) (*Config, error) {
bytes, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to load config: %s", configPath)
}

var config Config
if err := yaml.Unmarshal(bytes, &config); err != nil {
return nil, errors.Wrapf(err, "failed to parse config: %s", configPath)
}

return &config, nil
}
47 changes: 47 additions & 0 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Project Harbor Authors
//
// 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 daemon

import (
"github.com/goharbor/acceleration-service/pkg/config"
"github.com/goharbor/acceleration-service/pkg/handler"
"github.com/goharbor/acceleration-service/pkg/router"
"github.com/goharbor/acceleration-service/pkg/server"
)

type Daemon struct {
server server.Server
}

func NewDaemon(cfg *config.Config) (*Daemon, error) {
apiHandler, err := handler.NewAPIHandler(cfg)
if err != nil {
return nil, err
}

router := router.NewLocalRouter(apiHandler)
srv, err := server.NewHttpServer(&cfg.Server, router)
if err != nil {
return nil, err
}

return &Daemon{
server: srv,
}, nil
}

func (d *Daemon) Run() error {
return d.server.Run()
}
47 changes: 47 additions & 0 deletions pkg/handler/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright Project Harbor Authors
//
// 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 handler

import (
"github.com/pkg/errors"

"github.com/goharbor/acceleration-service/pkg/config"
)

var (
ErrIllegalParameter = errors.New("ERR_ILLEGAL_PARAMETER")
ErrUnauthorized = errors.New("ERR_UNAUTHORIZED")
ErrNotHealthy = errors.New("ERR_NOT_HEALTHY")
)

type Handler interface {
Ping() error
}

type ApiHandler struct {
config *config.Config
}

func NewAPIHandler(cfg *config.Config) (*ApiHandler, error) {
h := &ApiHandler{
config: cfg,
}

return h, nil
}

func (handler *ApiHandler) Ping() error {
return nil
}
92 changes: 92 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright Project Harbor Authors
//
// 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 metrics

import (
"fmt"
"time"

"github.com/prometheus/client_golang/prometheus"
)

type OpWrapper struct {
OpDuration *prometheus.HistogramVec
OpTotal *prometheus.CounterVec
OpErrorTotal *prometheus.CounterVec
}

var subsystem = "acceleration_service_core"

func NewOpWrapper(scope string, labelNames []string) *OpWrapper {
return &OpWrapper{
OpDuration: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Subsystem: subsystem,
Name: fmt.Sprintf("%s_op_duration_seconds", scope),
Help: fmt.Sprintf("The latency of %s operations in seconds.", scope),
},
labelNames,
),
OpTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: subsystem,
Name: fmt.Sprintf("%s_op_total", scope),
Help: fmt.Sprintf("How many %s operations.", scope),
},
labelNames,
),
OpErrorTotal: prometheus.NewCounterVec(
prometheus.CounterOpts{
Subsystem: subsystem,
Name: fmt.Sprintf("%s_op_error_total", scope),
Help: fmt.Sprintf("How many %s operation errors.", scope),
},
labelNames,
),
}
}

func (metrics *OpWrapper) OpWrap(op func() error, lvs ...string) error {
start := time.Now()

err := op()
Duration(err, metrics.OpDuration, start, lvs...)
CountInc(err, metrics.OpTotal, metrics.OpErrorTotal, lvs...)

return err
}

func Duration(err error, metric *prometheus.HistogramVec, start time.Time, lvs ...string) {
if err != nil {
return
}
elapsed := float64(time.Since(start)) / float64(time.Second)
metric.WithLabelValues(lvs...).Observe(elapsed)
}

func CountInc(err error, totalMetric *prometheus.CounterVec, errorMetric *prometheus.CounterVec, lvs ...string) {
totalMetric.WithLabelValues(lvs...).Inc()
if err != nil && errorMetric != nil {
errorMetric.WithLabelValues(lvs...).Inc()
}
}

func CountDesc(metric *prometheus.CounterVec, lvs ...string) {
metric.WithLabelValues(lvs...).Desc()
}

func CountSet(metric *prometheus.GaugeVec, val float64, lvs ...string) {
metric.WithLabelValues(lvs...).Set(val)
}
29 changes: 29 additions & 0 deletions pkg/router/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright Project Harbor Authors
//
// 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 router

import (
"net/http"

"github.com/labstack/echo/v4"
)

type Data struct {
LogLevel string `json:"log_level"`
}

func (r *LocalRouter) Ping(ctx echo.Context) error {
return ctx.String(http.StatusOK, "Ok")
}
Loading

0 comments on commit 59fbaef

Please sign in to comment.