Repository: kubernetes/kubernetes
File: test/e2e/dra/utils/builder.go (Go)

Code before:
	"regexp"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/onsi/ginkgo/v2"
	"github.com/onsi/gomega"

	appsv1 "k8s.io/api/apps/v1"
	v1 "k8s.io/api/core/v1"
	resourceapi "k8s.io/api/resource/v1"
	resourcealphaapi "k8s.io/api/resource/v1alpha3"
	resourcev1beta1 "k8s.io/api/resource/v1beta1"
	resourcev1beta2 "k8s.io/api/resource/v1beta2"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	cgoresource "k8s.io/client-go/kubernetes/typed/resource/v1"
	draclient "k8s.io/dynamic-resource-allocation/client"
	"k8s.io/dynamic-resource-allocation/resourceslice"
	"k8s.io/klog/v2"
	"k8s.io/kubernetes/test/e2e/dra/test-driver/app"
	"k8s.io/kubernetes/test/e2e/framework"
	e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
	e2enode "k8s.io/kubernetes/test/e2e_node"
	"k8s.io/kubernetes/test/utils/ktesting"
	admissionapi "k8s.io/pod-security-admission/api"
	"k8s.io/utils/ptr"
)

// ExtendedResourceName returns hard coded extended resource name with a variable
// suffix from the input integer when it's greater than or equal to 0.
// "example.com/resource" is not special, any valid extended resource name can be used
// instead, except when using example device plugin in the test, which hard coded it,
// see test/e2e/dra/deploy_device_plugin.go.
// i == -1 == SingletonIndex is special, the extended resource name has no extra suffix
// and matches the one used by the example device plugin.
func (b *Builder) ExtendedResourceName(i int) string {
	switch i {
	case SingletonIndex:
		return e2enode.SampleDeviceResourceName
	default:
		return b.driver.Name + "/resource" + fmt.Sprintf("-%d", i)
	}
}

// Builder contains a running counter to make objects unique within their
// namespace.
type Builder struct {
	namespace               string

Review comment (refactor):
I don't like that this package depends on `k8s.io/kubernetes/test/e2e_node`. That's the E2E node suite, not some helper package that is meant to be imported.

Perhaps reverse the dependency and define the constant here?


Code after:
	"regexp"
	"sort"
	"strconv"
	"strings"
	"time"

	"github.com/onsi/ginkgo/v2"
	"github.com/onsi/gomega"

	appsv1 "k8s.io/api/apps/v1"
	v1 "k8s.io/api/core/v1"
	resourceapi "k8s.io/api/resource/v1"
	resourcealphaapi "k8s.io/api/resource/v1alpha3"
	resourcev1beta1 "k8s.io/api/resource/v1beta1"
	resourcev1beta2 "k8s.io/api/resource/v1beta2"
	apierrors "k8s.io/apimachinery/pkg/api/errors"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	cgoresource "k8s.io/client-go/kubernetes/typed/resource/v1"
	draclient "k8s.io/dynamic-resource-allocation/client"
	"k8s.io/dynamic-resource-allocation/resourceslice"
	"k8s.io/klog/v2"
	"k8s.io/kubernetes/test/e2e/dra/test-driver/app"
	"k8s.io/kubernetes/test/e2e/framework"
	e2enode "k8s.io/kubernetes/test/e2e/framework/node"
	e2epod "k8s.io/kubernetes/test/e2e/framework/pod"
	"k8s.io/kubernetes/test/utils/ktesting"
	admissionapi "k8s.io/pod-security-admission/api"
	"k8s.io/utils/ptr"
)

// ExtendedResourceName returns hard coded extended resource name with a variable
// suffix from the input integer when it's greater than or equal to 0.
// i == -1 == SingletonIndex is special, the extended resource name has no extra suffix
// and matches the one used by the example device plugin.
func (b *Builder) ExtendedResourceName(i int) string {
	switch i {
	case SingletonIndex:
		return e2enode.SampleDeviceResourceName
	default:
		return b.driver.Name + "/resource" + fmt.Sprintf("-%d", i)
	}
}

// Builder contains a running counter to make objects unique within their
// namespace.
type Builder struct {
	namespace               string
	driver                  *Driver
	UseExtendedResourceName bool