Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
refactor(plugin): add package level mutexes to plugin runtimehandlers (
Browse files Browse the repository at this point in the history
  • Loading branch information
zsoltkacsandi authored Jul 26, 2024
1 parent 3fd770c commit 10a0e7d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
8 changes: 4 additions & 4 deletions plugins/runner/internal/runtimehandler/binary/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ type binaryRuntimeHandler struct {
inputDirMountPoint string
imageCleanup func()
ready bool

mu sync.Mutex
}

var mu sync.Mutex

func New(ctx context.Context, config types.PluginConfig) (runtimehandler.PluginRuntimeHandler, error) {
return &binaryRuntimeHandler{
config: config,
Expand All @@ -65,8 +65,8 @@ func New(ctx context.Context, config types.PluginConfig) (runtimehandler.PluginR

//nolint:cyclop
func (h *binaryRuntimeHandler) Start(ctx context.Context) error {
h.mu.Lock()
defer h.mu.Unlock()
mu.Lock()
defer mu.Unlock()

image, cleanup, err := containerrootfs.GetImageWithCleanup(ctx, h.config.ImageName)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions plugins/runner/internal/runtimehandler/docker/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -62,6 +63,8 @@ type containerRuntimeHandler struct {
runningErr atomic.Pointer[error]
}

var mu sync.Mutex

func New(ctx context.Context, config types.PluginConfig) (runtimehandler.PluginRuntimeHandler, error) {
// Load docker client
client, err := newDockerClient()
Expand All @@ -76,6 +79,9 @@ func New(ctx context.Context, config types.PluginConfig) (runtimehandler.PluginR
}

func (h *containerRuntimeHandler) Start(ctx context.Context) error {
mu.Lock()
defer mu.Unlock()

// Pull scanner image if required
err := h.pullPluginImage(ctx)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions scanner/families/plugins/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
"fmt"
"io"
"sync"

apitypes "github.com/openclarity/vmclarity/api/types"
"github.com/openclarity/vmclarity/core/log"
Expand All @@ -39,8 +38,6 @@ import (
type Scanner struct {
name string
config config.Config

mu sync.Mutex
}

func New(_ context.Context, name string, config types.ScannersConfig) (families.Scanner[*types.ScannerResult], error) {
Expand Down Expand Up @@ -104,13 +101,11 @@ func (s *Scanner) Scan(ctx context.Context, inputType common.InputType, userInpu
Err: nil,
}

s.mu.Lock()
if err := rr.Start(ctx); err != nil {
res.Err = fmt.Errorf("failed to start plugin runner: %w", err)
resChan <- res
return
}
s.mu.Unlock()

if err := rr.WaitReady(ctx); err != nil {
res.Err = fmt.Errorf("failed to wait for plugin scanner to be ready: %w", err)
Expand Down

0 comments on commit 10a0e7d

Please sign in to comment.