<commit_before>package nodedriver

import (
	"fmt"
	"strings"
	"sync"

	errs "github.com/pkg/errors"
	"github.com/rancher/types/apis/management.cattle.io/v3"
	"github.com/rancher/types/config"
	"github.com/sirupsen/logrus"
	"k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
)

var (
	schemaLock = sync.Mutex{}
)

const (
	driverNameLabel = "io.cattle.node_driver.name"
)

func Register(management *config.ManagementContext) {
	nodeDriverClient := management.Management.NodeDrivers("")
	nodeDriverLifecycle := &Lifecycle{
		nodeDriverClient: nodeDriverClient,
		schemaClient:     management.Management.DynamicSchemas(""),
		schemaLister:     management.Management.DynamicSchemas("").Controller().Lister(),
	}

	nodeDriverClient.
		AddLifecycle("node-driver-controller", nodeDriverLifecycle)
}

type Lifecycle struct {
	nodeDriverClient v3.NodeDriverInterface
	schemaClient     v3.DynamicSchemaInterface
	schemaLister     v3.DynamicSchemaLister
}

func (m *Lifecycle) Create(obj *v3.NodeDriver) (*v3.NodeDriver, error) {
	return m.download(obj)
}

func (m *Lifecycle) download(obj *v3.NodeDriver) (*v3.NodeDriver, error) {
	if !obj.Spec.Active {
		return obj, nil
	}

	err := errs.New("not found")
	// if node driver was created, we also activate the driver by default
	driver := NewDriver(obj.Spec.Builtin, obj.Spec.DisplayName, obj.Spec.URL, obj.Spec.Checksum)
	if obj.Spec.DisplayName != "" {
		schemaName := obj.Spec.DisplayName + "config"
		_, err = m.schemaLister.Get("", schemaName)
	}<commit_msg>add lock during machine driver creation<commit_after>package nodedriver

import (
	"fmt"
	"strings"
	"sync"

	errs "github.com/pkg/errors"
	"github.com/rancher/types/apis/management.cattle.io/v3"
	"github.com/rancher/types/config"
	"github.com/sirupsen/logrus"
	"k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
)

var (
	schemaLock = sync.Mutex{}
	driverLock = sync.Mutex{}
)

const (
	driverNameLabel = "io.cattle.node_driver.name"
)

func Register(management *config.ManagementContext) {
	nodeDriverClient := management.Management.NodeDrivers("")
	nodeDriverLifecycle := &Lifecycle{
		nodeDriverClient: nodeDriverClient,
		schemaClient:     management.Management.DynamicSchemas(""),
		schemaLister:     management.Management.DynamicSchemas("").Controller().Lister(),
	}

	nodeDriverClient.
		AddLifecycle("node-driver-controller", nodeDriverLifecycle)
}

type Lifecycle struct {
	nodeDriverClient v3.NodeDriverInterface
	schemaClient     v3.DynamicSchemaInterface
	schemaLister     v3.DynamicSchemaLister
}

func (m *Lifecycle) Create(obj *v3.NodeDriver) (*v3.NodeDriver, error) {
	return m.download(obj)
}

func (m *Lifecycle) download(obj *v3.NodeDriver) (*v3.NodeDriver, error) {
	driverLock.Lock()
	defer driverLock.Unlock()
	if !obj.Spec.Active {
		return obj, nil
	}

	err := errs.New("not found")
	// if node driver was created, we also activate the driver by default
	driver := NewDriver(obj.Spec.Builtin, obj.Spec.DisplayName, obj.Spec.URL, obj.Spec.Checksum)
	if obj.Spec.DisplayName != "" {
		schemaName := obj.Spec.DisplayName + "config"
		_, err = m.schemaLister.Get("", schemaName)
	}
{'fraction_non_alphanumeric': 0.13452638074668313, 'fraction_numerical': 0.008022215365627893, 'mean_word_length': 4.851985559566787, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6033411', 'n_tokens_mistral': 1170, 'n_tokens_neox': 1149, 'n_words': 200}