Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

Commit

Permalink
move reading docker gid when launching to proxy modes (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Replica <yumeko@outlook.com>
  • Loading branch information
雾雨 and nyanpassu authored Jan 29, 2021
1 parent a93b24c commit 577c87f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
34 changes: 31 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"os"
"os/signal"
"os/user"
"strconv"
"syscall"
"time"

Expand All @@ -29,7 +31,6 @@ import (
type Application struct {
NodeName string
Mode string
DockerGID int
DockerDaemonUnixSocket string
DockerAPIVersion string
Hosts []string
Expand Down Expand Up @@ -100,6 +101,7 @@ func (app Application) defaultMode() ([]service.Service, error) {
stor store.Store
agent vessel.CNMAgent
services []service.Service
gid int
err error
)
if apiConfig, err = app.getAPIConfig(); err != nil {
Expand All @@ -114,14 +116,17 @@ func (app Application) defaultMode() ([]service.Service, error) {
if stor, err = app.getEtcdClient(context.Background(), apiConfig); err != nil {
return nil, err
}
if gid, err = getDockerGid(); err != nil {
return nil, err
}
vess = vessel.NewHelper(vessel.NewVessel(app.NodeName, client, dockerCli, app.DriverName, stor), stor)
if app.EnableCNMAgent {
agent := vessel.NewAgent(vess, vessel.AgentConfig{})
services = append(services, agent)
}
services = append(services, proxyService{
Server: barrelHttp.NewServer(docker.NewHandler(app.DockerDaemonUnixSocket, app.DialTimeout, vess)),
gid: app.DockerGID,
gid: gid,
tlsConfig: barrelHttp.TLSConfig{
CertFile: app.CertFile,
KeyFile: app.KeyFile,
Expand All @@ -137,10 +142,17 @@ func (app Application) defaultMode() ([]service.Service, error) {
}

func (app Application) proxyOnlyMode() ([]service.Service, error) {
var (
gid int
err error
)
if gid, err = getDockerGid(); err != nil {
return nil, err
}
return []service.Service{
proxyService{
Server: barrelHttp.NewServer(docker.NewSimpleHandler(app.DockerDaemonUnixSocket, app.DialTimeout)),
gid: app.DockerGID,
gid: gid,
tlsConfig: barrelHttp.TLSConfig{
CertFile: app.CertFile,
KeyFile: app.KeyFile,
Expand Down Expand Up @@ -177,3 +189,19 @@ func (app Application) networkPluginOnlyMode() ([]service.Service, error) {
},
}, nil
}

func getDockerGid() (int, error) {
var (
group *user.Group
err error
dockerGid int64
)
if group, err = user.LookupGroup("docker"); err != nil {
return 0, err
}
log.Printf("The Gid of group docker is %s", group.Gid)
if dockerGid, err = strconv.ParseInt(group.Gid, 10, 64); err != nil {
return 0, err
}
return int(dockerGid), nil
}
13 changes: 0 additions & 13 deletions barrel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"fmt"
"os"
"os/user"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -39,23 +37,13 @@ func setupLog(l string) error {
func run(c *cli.Context) (err error) {
var (
dockerdPath = c.String("dockerd-path")
dockerGid int64
)
utils.Initialize(c.Int("buffer-size"))
if err = setupLog(c.String("log-level")); err != nil {
return err
}
log.Printf("Hello Barrel, dockerdPath = %s", dockerdPath)

var group *user.Group
if group, err = user.LookupGroup("docker"); err != nil {
return
}
log.Printf("The Gid of group docker is %s", group.Gid)
if dockerGid, err = strconv.ParseInt(group.Gid, 10, 64); err != nil {
return
}

hostEnvVars := c.StringSlice("host")
log.Printf("hostEnvVars = %v", hostEnvVars)

Expand All @@ -69,7 +57,6 @@ func run(c *cli.Context) (err error) {
barrel := app.Application{
NodeName: nodeName,
Mode: strings.ToLower(c.String("mode")),
DockerGID: int(dockerGid),
DockerDaemonUnixSocket: dockerdPath,
DockerAPIVersion: "1.32",
Hosts: hostEnvVars,
Expand Down

0 comments on commit 577c87f

Please sign in to comment.