Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ require (
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.10.0
github.com/psampaz/go-mod-outdated v0.7.0
github.com/rjeczalik/notify v0.9.2
github.com/sirupsen/logrus v1.8.1
github.com/soheilhy/cmux v0.1.5
github.com/stretchr/testify v1.7.0
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1139,8 +1139,6 @@ github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uY
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8=
github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM=
github.com/robfig/cron v1.1.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
Expand Down Expand Up @@ -1521,7 +1519,6 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
63 changes: 63 additions & 0 deletions internal/dirnotifier/dirnotifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package dirnotifier

import (
"sync"

"github.com/fsnotify/fsnotify"
"github.com/pkg/errors"
)

type DirectoryNotifier struct {
watcher *fsnotify.Watcher
filesToNotifiers sync.Map
opsToWatch []fsnotify.Op
dir string
}

func New(dir string, opsToWatch ...fsnotify.Op) (*DirectoryNotifier, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}

dn := &DirectoryNotifier{
watcher: watcher,
opsToWatch: opsToWatch,
dir: dir,
}

go dn.initializeWatcher()

if err := watcher.Add(dir); err != nil {
return nil, err
}
return dn, nil
}

func (dn *DirectoryNotifier) initializeWatcher() {
defer dn.watcher.Close()
for event := range dn.watcher.Events {
for op := range dn.opsToWatch {
if event.Op&fsnotify.Op(op) == fsnotify.Op(op) {
if notifyChan, ok := dn.filesToNotifiers.Load(event.Name); ok {
dn.filesToNotifiers.Delete(event.Name)
close(notifyChan.(chan struct{}))
break
}
}
}
}
}

func (dn *DirectoryNotifier) NotifierForFile(file string) (chan struct{}, error) {
if _, ok := dn.filesToNotifiers.Load(file); ok {
return nil, errors.Errorf("exec watcher already watching file %s", file)
}
c := make(chan struct{}, 1)
dn.filesToNotifiers.Store(file, c)
return c, nil
}

func (dn *DirectoryNotifier) Directory() string {
return dn.dir
}
64 changes: 64 additions & 0 deletions internal/dirnotifier/dirnotifier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package dirnotifier_test

import (
"os"
"path/filepath"

"github.com/cri-o/cri-o/internal/dirnotifier"
"github.com/fsnotify/fsnotify"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

// The actual test suite
var _ = t.Describe("DirectoryNotifier", func() {
var testDir, testFile string
BeforeEach(func() {
testDir = t.MustTempDir("dirnotifier")
testFile = filepath.Join(testDir, "file")
})
t.Describe("New", func() {
It("should fail with invalid directory", func() {
// When
_, err := dirnotifier.New("doesnotexist", fsnotify.Create)
// Then
Expect(err).NotTo(BeNil())
})
})
t.Describe("Directory", func() {
It("should be configured", func() {
// When
dn, err := dirnotifier.New(testDir, fsnotify.Create)
Expect(err).To(BeNil())
// Then
Expect(dn.Directory()).To(Equal(testDir))
})
})
t.Describe("NotifierForFile", func() {
It("should fail on duplicate", func() {
// Given
dn, err := dirnotifier.New(testDir, fsnotify.Create)
Expect(err).To(BeNil())
// When
_, err = dn.NotifierForFile(testFile)
Expect(err).To(BeNil())
// Then
_, err = dn.NotifierForFile(testFile)
Expect(err).NotTo(BeNil())
})
It("should notify on configured operation", func() {
// Given
dn, err := dirnotifier.New(testDir, fsnotify.Create)
Expect(err).To(BeNil())
// When
notifierChan, err := dn.NotifierForFile(testFile)
Expect(err).To(BeNil())
f, err := os.Create(testFile)
Expect(err).To(BeNil())
f.Close()

// Then
<-notifierChan
})
})
})
26 changes: 26 additions & 0 deletions internal/dirnotifier/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dirnotifier_test

import (
"testing"

. "github.com/cri-o/cri-o/test/framework"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

// TestDirNotifier runs the created specs
func TestDirNotifier(t *testing.T) {
RegisterFailHandler(Fail)
RunFrameworkSpecs(t, "DirectoryNotifier")
}

var t *TestFramework

var _ = BeforeSuite(func() {
t = NewTestFramework(NilFunc, NilFunc)
t.Setup()
})

var _ = AfterSuite(func() {
t.Teardown()
})
5 changes: 4 additions & 1 deletion internal/lib/container_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ func New(ctx context.Context, configIface libconfig.Iface) (*ContainerServer, er

storageRuntimeService := storage.GetRuntimeService(ctx, imageService)

runtime := oci.New(config)
runtime, err := oci.New(config)
if err != nil {
return nil, err
}

newHooks, err := hooks.New(ctx, config.HooksDir, []string{})
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/lib/container_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var _ = t.Describe("ContainerServer", func() {
config, err := libconfig.DefaultConfig()
Expect(err).To(BeNil())
config.HooksDir = []string{}
// so we have permission to make a directory within it
config.ContainerAttachSocketDir = t.MustTempDir("crio")

// Specify mocks
gomock.InOrder(
Expand Down
2 changes: 2 additions & 0 deletions internal/lib/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func beforeEach() {
Expect(err).To(BeNil())
config.LogDir = "."
config.HooksDir = []string{}
// so we have permission to make a directory within it
config.ContainerAttachSocketDir = t.MustTempDir("crio")

gomock.InOrder(
libMock.EXPECT().GetStore().Return(storeMock, nil),
Expand Down
19 changes: 17 additions & 2 deletions internal/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package oci
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"time"

"github.com/cri-o/cri-o/internal/dirnotifier"
"github.com/cri-o/cri-o/pkg/config"
"github.com/cri-o/cri-o/server/cri/types"
"github.com/fsnotify/fsnotify"
rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"golang.org/x/net/context"

"k8s.io/client-go/tools/remotecommand"
Expand Down Expand Up @@ -40,6 +44,7 @@ type Runtime struct {
config *config.Config
runtimeImplMap map[string]RuntimeImpl
runtimeImplMapMutex sync.RWMutex
execNotifier *dirnotifier.DirectoryNotifier
}

// RuntimeImpl is an interface used by the caller to interact with the
Expand Down Expand Up @@ -72,11 +77,21 @@ type RuntimeImpl interface {
}

// New creates a new Runtime with options provided
func New(c *config.Config) *Runtime {
func New(c *config.Config) (*Runtime, error) {
execNotifyDir := filepath.Join(c.ContainerAttachSocketDir, "exec-pid-dir")
if err := os.MkdirAll(execNotifyDir, 0o750); err != nil {
return nil, errors.Wrapf(err, "create oci runtime pid dir")
}

notifier, err := dirnotifier.New(execNotifyDir, fsnotify.Write, fsnotify.Rename)
if err != nil {
return nil, err
}
return &Runtime{
config: c,
runtimeImplMap: make(map[string]RuntimeImpl),
}
execNotifier: notifier,
}, nil
}

// Runtimes returns the map of OCI runtimes.
Expand Down
10 changes: 8 additions & 2 deletions internal/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ var _ = t.Describe("Oci", func() {
// Given
c, err := config.DefaultConfig()
Expect(err).To(BeNil())
// so we have permission to make a directory within it
c.ContainerAttachSocketDir = t.MustTempDir("crio")

// When
runtime := oci.New(c)
runtime, err := oci.New(c)
Expect(err).To(BeNil())

// Then
Expect(runtime).NotTo(BeNil())
Expand Down Expand Up @@ -73,8 +76,11 @@ var _ = t.Describe("Oci", func() {
Expect(err).To(BeNil())
c.DefaultRuntime = defaultRuntime
c.Runtimes = runtimes
// so we have permission to make a directory within it
c.ContainerAttachSocketDir = t.MustTempDir("crio")

sut = oci.New(c)
sut, err = oci.New(c)
Expect(err).To(BeNil())
Expect(sut).NotTo(BeNil())
})

Expand Down
Loading