Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

daemon: add socket ownership flags #59

Merged
merged 1 commit into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
daemon: add socket ownership flags
- --socket-gid default 0 aka root with legacy alias --group
- --socket-uid default 0 aka root
  • Loading branch information
dweomer committed Jun 11, 2020
commit bfdc41d64051ef50af1aa401b0aa68a588dba182
15 changes: 15 additions & 0 deletions cmd/daemon/daemon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ the backend to support the Docker work-alike frontend of k3c.`
if sf, ok := flag.(cliv1.StringFlag); ok {
sf.Value = filepath.Join(config.DefaultDaemonStateDir, "k3c.sock")
sf.EnvVar = "K3C_ADDRESS"
sf.Destination = &config.Socket.Address
app.Flags[i] = sf
} else {
logrus.Warnf("unexpected type for flag %q = %#v", flag.GetName(), flag)
Expand Down Expand Up @@ -144,6 +145,18 @@ the backend to support the Docker work-alike frontend of k3c.`
Usage: "containerd-style image ref for sandboxes",
Destination: &cri.Config.SandboxImage,
},
cliv1.IntFlag{
Name: "socket-gid,group",
EnvVar: "K3C_SOCKET_GID",
Usage: "gRPC socket gid",
Destination: &config.Socket.GID,
},
cliv1.IntFlag{
Name: "socket-uid",
EnvVar: "K3C_SOCKET_UID",
Usage: "gRPC socket uid",
Destination: &config.Socket.UID,
},
}...)

app.Action = func(action interface{}) cliv1.ActionFunc {
Expand All @@ -162,6 +175,8 @@ the backend to support the Docker work-alike frontend of k3c.`
e = t.EnvVar
case cliv1.StringFlag:
e = t.EnvVar
case cliv1.IntFlag:
e = t.EnvVar
}
if e != "" {
if err := os.Setenv(e, clx.GlobalString(n)); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/daemon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/BurntSushi/toml"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/services/server/config"
buildkit "github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/rancher/k3c/pkg/defaults"
)
Expand All @@ -28,6 +29,10 @@ var (
DefaultVolumesDir = filepath.Join(DefaultDaemonRootDir, "volumes")
)

var (
Socket config.GRPCConfig
)

type K3Config struct {
BootstrapSkip bool `toml:"bootstrap_skip"`
BootstrapImage string `toml:"bootstrap_image"`
Expand Down
28 changes: 28 additions & 0 deletions pkg/daemon/plugins.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package daemon

import (
"os"
"time"

"github.com/containerd/containerd/log"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/plugin"
Expand Down Expand Up @@ -68,5 +71,30 @@ func PluginInitFunc(ic *plugin.InitContext) (interface{}, error) {
service := server.NewContainerService(daemon)
service.SetInitialized(true)
log.G(ic.Context).WithField("bridge", cfg.BridgeName).WithField("cidr", cfg.BridgeCIDR).Info("K3C daemon")
go func() {
var (
addr = config.Socket.Address
gid = config.Socket.GID
uid = config.Socket.UID
)
for {
select {
case <-time.After(100 * time.Millisecond):
err := os.Chown(addr, uid, gid)
if os.IsNotExist(err) {
continue
}
log := log.G(ic.Context).WithField("address", addr).WithField("gid", gid).WithField("uid", uid)
if err != nil {
log.WithError(err).Warn("K3C socket")
} else {
log.Debug("K3C socket")
}
return
case <-ic.Context.Done():
return
}
}
}()
return service, nil
}