forked from dragonflyoss/nydus
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Daemon: implement http server daemon
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
Showing
15 changed files
with
1,095 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,5 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
/acceleration-service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
server: | ||
name: API | ||
host: 0.0.0.0 | ||
port: 2077 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.