Skip to content

Commit

Permalink
✨ Support all in one
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone committed Jul 6, 2023
1 parent ae4da39 commit 12ebee0
Show file tree
Hide file tree
Showing 20 changed files with 259 additions and 75 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ cd web && yarn && yarn build && cd .. && make build
- [x] Support OCI artifact such as helm and so on.
- [x] Support OCI sbom.
- [x] Support Image security scan.
- [ ] Support registry proxy.
- [ ] Support Namespace quota.
- [x] Support registry proxy.
- [x] Support Namespace quota.
- [x] Support Image automatic garbage collection.
- [x] Support Multi-tenancy.
- [ ] Support Image replication.
- [ ] Support Image build in docker, podman and kubernetes.
- [ ] Support Image sign.
- [ ] Support Image automatic garbage collection.
- [ ] Support Multi-tenancy.
- [ ] Support helm chart search and index.json.
61 changes: 61 additions & 0 deletions cmd/distribution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/ximager/ximager/pkg/cmds/distribution"
"github.com/ximager/ximager/pkg/configs"
"github.com/ximager/ximager/pkg/daemon"
"github.com/ximager/ximager/pkg/dal"
"github.com/ximager/ximager/pkg/inits"
"github.com/ximager/ximager/pkg/logger"
)

// distributionCmd represents the distribution command
var distributionCmd = &cobra.Command{
Use: "distribution",
Aliases: []string{"ds"},
Short: "Start the XImager distribution server",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
initConfig()
logger.SetLevel(viper.GetString("log.level"))
},
Run: func(_ *cobra.Command, _ []string) {
err := configs.Initialize()
if err != nil {
log.Error().Err(err).Msg("Initialize configs with error")
return
}

err = dal.Initialize()
if err != nil {
log.Error().Err(err).Msg("Initialize database with error")
return
}

err = inits.Initialize()
if err != nil {
log.Error().Err(err).Msg("Initialize inits with error")
return
}

err = daemon.InitializeClient()
if err != nil {
log.Error().Err(err).Msg("Initialize daemon client with error")
return
}

err = distribution.Serve()
if err != nil {
log.Error().Err(err).Msg("Start distribution with error")
return
}
},
}

func init() {
distributionCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is /etc/ximager/ximager.yaml)")
rootCmd.AddCommand(distributionCmd)
}
10 changes: 10 additions & 0 deletions cmd/imports/apis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cmd

import (
_ "github.com/ximager/ximager/pkg/handlers/artifact"
_ "github.com/ximager/ximager/pkg/handlers/namespaces"
_ "github.com/ximager/ximager/pkg/handlers/repositories"
_ "github.com/ximager/ximager/pkg/handlers/tag"
_ "github.com/ximager/ximager/pkg/handlers/token"
_ "github.com/ximager/ximager/pkg/handlers/user"
)
8 changes: 8 additions & 0 deletions cmd/imports/distribution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package cmd

import (
_ "github.com/ximager/ximager/pkg/handlers/distribution/base"
_ "github.com/ximager/ximager/pkg/handlers/distribution/blob"
_ "github.com/ximager/ximager/pkg/handlers/distribution/manifest"
_ "github.com/ximager/ximager/pkg/handlers/distribution/upload"
)
7 changes: 7 additions & 0 deletions cmd/imports/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cmd

import (
_ "github.com/ximager/ximager/pkg/storage/cos"
_ "github.com/ximager/ximager/pkg/storage/filesystem"
_ "github.com/ximager/ximager/pkg/storage/s3"
)
5 changes: 1 addition & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

_ "github.com/golang/mock/mockgen/model"
_ "github.com/ximager/ximager/pkg/storage/cos"
_ "github.com/ximager/ximager/pkg/storage/filesystem"
_ "github.com/ximager/ximager/pkg/storage/s3"
_ "github.com/ximager/ximager/cmd/imports"
)

var cfgFile string
Expand Down
10 changes: 9 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,23 @@ var serverCmd = &cobra.Command{
return
}

err = server.Serve()
err = server.Serve(server.ServerConfig{
WithoutDistribution: withoutDistribution,
WithoutWorker: withoutWorker,
})
if err != nil {
log.Error().Err(err).Msg("Serve with error")
return
}
},
}

var withoutDistribution bool
var withoutWorker bool

func init() {
serverCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is /etc/ximager/ximager.yaml)")
serverCmd.PersistentFlags().BoolVar(&withoutDistribution, "without-distribution", false, "server without distribution service")
serverCmd.PersistentFlags().BoolVar(&withoutWorker, "without-worker", false, "server without worker service")
rootCmd.AddCommand(serverCmd)
}
10 changes: 5 additions & 5 deletions e2e/sc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function () {

const token = JSON.parse(response.body).token;

response = http.post(`${host}/namespaces/`, JSON.stringify({ "name": "test", "description": "test" }), {
response = http.post(`${host}/api/v1/namespaces/`, JSON.stringify({ "name": "test", "description": "test" }), {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
Expand All @@ -31,7 +31,7 @@ export default function () {
});
const namespaceId = JSON.parse(response.body).id;

response = http.del(`${host}/namespaces/${namespaceId}`, null, {
response = http.del(`${host}/api/v1/namespaces/${namespaceId}`, null, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
Expand All @@ -41,9 +41,9 @@ export default function () {
'delete namespace status is 204': (r) => r.status === 204,
});

let page_size = 100;
let page_num = 1;
response = http.get(`${host}/namespaces/?page_size=${page_size}&page_num=${page_num}`, {
let limit = 100;
let last = 0;
response = http.get(`${host}/namespaces/?limit=${limit}&last=${last}`, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import "github.com/ximager/ximager/cmd"
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0

// @BasePath /
// @BasePath /api/v1

// @securityDefinitions.basic BasicAuth
// @in header
Expand Down
77 changes: 77 additions & 0 deletions pkg/cmds/distribution/distribution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package distribution

import (
"context"
"net/http"
"os"
"os/signal"
"time"

"github.com/labstack/echo-contrib/pprof"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"

"github.com/ximager/ximager/pkg/handlers"
"github.com/ximager/ximager/pkg/middlewares"
"github.com/ximager/ximager/pkg/storage"
"github.com/ximager/ximager/pkg/utils/serializer"
)

func Serve() error {
e := echo.New()
e.HideBanner = true
e.HidePort = true
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{Level: 5}))
e.Use(echo.MiddlewareFunc(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
n := next(c)
log.Debug().
Str("method", c.Request().Method).
Str("path", c.Request().URL.Path).
Str("query", c.Request().URL.RawQuery).
Interface("req-header", c.Request().Header).
Interface("resp-header", c.Response().Header()).
Int("status", c.Response().Status).
Msg("Request debugger")
return n
}
}))

e.Use(middleware.CORS())
e.Use(middlewares.Healthz())
e.JSONSerializer = new(serializer.DefaultJSONSerializer)

if viper.GetInt("log.level") < 1 {
pprof.Register(e)
}

handlers.InitializeDistribution(e)
err := storage.Initialize()
if err != nil {
return err
}

go func() {
log.Info().Str("addr", viper.GetString("http.server")).Msg("Server listening")
err = e.Start(viper.GetString("http.server"))
if err != http.ErrServerClosed {
log.Fatal().Err(err).Msg("Listening on interface failed")
}
}()

// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
// Use a buffered channel to avoid missing signals as recommended for signal.Notify
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = e.Shutdown(ctx)
if err != nil {
log.Error().Err(err).Msg("Server shutdown failed")
}

return nil
}
32 changes: 19 additions & 13 deletions pkg/cmds/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,21 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/viper"

"github.com/ximager/ximager/pkg/daemon"
"github.com/ximager/ximager/pkg/handlers"
"github.com/ximager/ximager/pkg/middlewares"
"github.com/ximager/ximager/pkg/storage"
"github.com/ximager/ximager/pkg/utils/serializer"

_ "github.com/ximager/ximager/pkg/handlers/artifact"
_ "github.com/ximager/ximager/pkg/handlers/distribution/base"
_ "github.com/ximager/ximager/pkg/handlers/distribution/blob"
_ "github.com/ximager/ximager/pkg/handlers/distribution/manifest"
_ "github.com/ximager/ximager/pkg/handlers/distribution/upload"
_ "github.com/ximager/ximager/pkg/handlers/namespaces"
_ "github.com/ximager/ximager/pkg/handlers/repositories"
_ "github.com/ximager/ximager/pkg/handlers/tag"
_ "github.com/ximager/ximager/pkg/handlers/token"
_ "github.com/ximager/ximager/pkg/handlers/user"
)

// ServerConfig ...
type ServerConfig struct {
WithoutDistribution bool
WithoutWorker bool
}

// Serve starts the server
func Serve() error {
func Serve(config ServerConfig) error {
e := echo.New()
e.HideBanner = true
e.HidePort = true
Expand All @@ -64,7 +60,7 @@ func Serve() error {
return n
}
}))
// e.Use(middleware.RequestID())

e.Use(middleware.CORS())
e.Use(middlewares.Healthz())
e.JSONSerializer = new(serializer.DefaultJSONSerializer)
Expand All @@ -73,6 +69,16 @@ func Serve() error {
pprof.Register(e)
}

if !config.WithoutDistribution {
handlers.InitializeDistribution(e)
}
if !config.WithoutWorker {
err := daemon.InitializeServer()
if err != nil {
return err
}
}

err := handlers.Initialize(e)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,8 @@ const (
// CacherManifest ...
CacherManifest = "manifest"
)

const (
// APIV1 api v1 for api router
APIV1 = "/api/v1"
)
26 changes: 13 additions & 13 deletions pkg/handlers/apidocs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 12ebee0

Please sign in to comment.