Skip to content

Commit

Permalink
Initialize driver's clientsets
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Sim <ihcsim@gmail.com>
  • Loading branch information
ihcsim committed May 23, 2024
1 parent 7f8f7c6 commit f520518
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 34 deletions.
26 changes: 23 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/ihcsim/k8s-dra/cmd/flags"
draclientset "github.com/ihcsim/k8s-dra/pkg/apis/clientset/versioned"
"github.com/ihcsim/k8s-dra/pkg/drivers/gpu"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -60,7 +61,7 @@ func run(ctx context.Context) error {
pprofPort = viper.GetInt("pprof-port")
pprofPath = "/debug/pprof/"

driver = gpu.NewDriver(viper.GetString("namespace"))
namespace = viper.GetString("namespace")
)

go func() {
Expand All @@ -79,7 +80,6 @@ func run(ctx context.Context) error {
}
}()

log.Info().Str("driver", driver.GetName()).Msg("starting driver controller")
log.Info().
Int("workers", workerCount).
Float64("qps", qps).
Expand All @@ -93,12 +93,23 @@ func run(ctx context.Context) error {
return err
}

draClientSets, err := draClientSets(kubeconfig)
if err != nil {
return err
}

var (
resync = time.Minute * 10
informerFactory = informers.NewSharedInformerFactory(coreClientSets, resync)
)

informerFactory.Start(ctx.Done())

driver, err := gpu.NewDriver(draClientSets, namespace, log)
if err != nil {
return err
}

log.Info().Str("driver", driver.GetName()).Msg("starting driver controller")
ctrl := controller.New(ctx, driver.GetName(), driver, coreClientSets, informerFactory)
ctrl.Run(workerCount)
return nil
Expand All @@ -119,3 +130,12 @@ func coreClientSets(kubeconfigPath string) (coreclientset.Interface, error) {

return coreclientset.NewForConfig(kubecfg)
}

func draClientSets(kubeconfigPath string) (draclientset.Interface, error) {
kubecfg, err := kubeConfig(kubeconfigPath)
if err != nil {
return nil, err
}

return draclientset.NewForConfig(kubecfg)
}
11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
k8s.io/api v0.30.0
k8s.io/apimachinery v0.30.0
k8s.io/client-go v0.30.0
k8s.io/code-generator v0.30.0
k8s.io/api v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
k8s.io/code-generator v0.30.1
k8s.io/dynamic-resource-allocation v0.30.0
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340
sigs.k8s.io/structured-merge-diff/v4 v4.4.1
Expand Down Expand Up @@ -61,8 +61,7 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/net v0.23.0 // indirect
Expand Down
22 changes: 10 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand Down Expand Up @@ -209,14 +207,14 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/api v0.30.0 h1:siWhRq7cNjy2iHssOB9SCGNCl2spiF1dO3dABqZ8niA=
k8s.io/api v0.30.0/go.mod h1:OPlaYhoHs8EQ1ql0R/TsUgaRPhpKNxIMrKQfWUp8QSE=
k8s.io/apimachinery v0.30.0 h1:qxVPsyDM5XS96NIh9Oj6LavoVFYff/Pon9cZeDIkHHA=
k8s.io/apimachinery v0.30.0/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc=
k8s.io/client-go v0.30.0 h1:sB1AGGlhY/o7KCyCEQ0bPWzYDL0pwOZO4vAtTSh/gJQ=
k8s.io/client-go v0.30.0/go.mod h1:g7li5O5256qe6TYdAMyX/otJqMhIiGgTapdLchhmOaY=
k8s.io/code-generator v0.30.0 h1:3VUVqHvWFSVSm9kqL/G6kD4ZwNdHF6J/jPyo3Jgjy3k=
k8s.io/code-generator v0.30.0/go.mod h1:mBMZhfRR4IunJUh2+7LVmdcWwpouCH5+LNPkZ3t/v7Q=
k8s.io/api v0.30.1 h1:kCm/6mADMdbAxmIh0LBjS54nQBE+U4KmbCfIkF5CpJY=
k8s.io/api v0.30.1/go.mod h1:ddbN2C0+0DIiPntan/bye3SW3PdwLa11/0yqwvuRrJM=
k8s.io/apimachinery v0.30.1 h1:ZQStsEfo4n65yAdlGTfP/uSHMQSoYzU/oeEbkmF7P2U=
k8s.io/apimachinery v0.30.1/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc=
k8s.io/client-go v0.30.1 h1:uC/Ir6A3R46wdkgCV3vbLyNOYyCJ8oZnjtJGKfytl/Q=
k8s.io/client-go v0.30.1/go.mod h1:wrAqLNs2trwiCH/wxxmT/x3hKVH9PuV0GGW0oDoHVqc=
k8s.io/code-generator v0.30.1 h1:ZsG++q5Vt0ScmKCeLhynUuWgcwFGg1Hl1AGfatqPJBI=
k8s.io/code-generator v0.30.1/go.mod h1:hFgxRsvOUg79mbpbVKfjJvRhVz1qLoe40yZDJ/hwRH4=
k8s.io/dynamic-resource-allocation v0.30.0 h1:CLMe/tsqOmIsR336A8vP4vGsdccfgMeUM2ksbxG5pyM=
k8s.io/dynamic-resource-allocation v0.30.0/go.mod h1:ltnb2UxylJw3MHeUIcXtIsxX23/4oHAY4Hr44I4RzZU=
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 h1:NGrVE502P0s0/1hudf8zjgwki1X/TByhmAoILTarmzo=
Expand Down
30 changes: 17 additions & 13 deletions pkg/drivers/gpu/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

"github.com/ihcsim/k8s-dra/pkg/apis"
allocationv1alpha1 "github.com/ihcsim/k8s-dra/pkg/apis/allocation/v1alpha1"
clientset "github.com/ihcsim/k8s-dra/pkg/apis/clientset/versioned"
draclientset "github.com/ihcsim/k8s-dra/pkg/apis/clientset/versioned"
gpuv1alpha1 "github.com/ihcsim/k8s-dra/pkg/apis/gpu/v1alpha1"
zlog "github.com/rs/zerolog"
corev1 "k8s.io/api/core/v1"
resourcev1alpha2 "k8s.io/api/resource/v1alpha2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -26,17 +27,20 @@ var _ dractrl.Driver = &driver{}
// driver implements the controller.Driver interface, to provide the actual
// allocation and deallocation operations of GPU resources.
type driver struct {
clientset clientset.Interface
gpu gpuPlugin
namespace string
clientsets draclientset.Interface
gpu gpuPlugin
namespace string
log zlog.Logger
}

// NewDriver returns a new instance of the GPU driver.
func NewDriver(namespace string) *driver {
func NewDriver(clientsets draclientset.Interface, namespace string, log zlog.Logger) (*driver, error) {
return &driver{
gpu: gpuPlugin{},
namespace: namespace,
}
clientsets: clientsets,
gpu: gpuPlugin{},
namespace: namespace,
log: log,
}, nil
}

// GetName returns the name of the driver.
Expand Down Expand Up @@ -67,7 +71,7 @@ func (d *driver) GetClassParameters(ctx context.Context, class *resourcev1alpha2
return nil, fmt.Errorf("incorrect API group %s (vs. %s)", class.ParametersRef.APIGroup, apiGroup)
}

dc, err := d.clientset.GpuV1alpha1().GPUClassParameters().Get(ctx, class.ParametersRef.Name, metav1.GetOptions{})
dc, err := d.clientsets.GpuV1alpha1().GPUClassParameters().Get(ctx, class.ParametersRef.Name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("error getting DeviceClassParameters called '%s': %w", class.ParametersRef.Name, err)
}
Expand Down Expand Up @@ -101,7 +105,7 @@ func (d *driver) GetClaimParameters(
return nil, fmt.Errorf("unsupported resource claim kind: %v", claim.Spec.ParametersRef.Kind)
}

rc, err := d.clientset.GpuV1alpha1().GPUClaimParameters(claim.Namespace).Get(ctx, claim.Spec.ParametersRef.Name, metav1.GetOptions{})
rc, err := d.clientsets.GpuV1alpha1().GPUClaimParameters(claim.Namespace).Get(ctx, claim.Spec.ParametersRef.Name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("error getting GPUClaimParameters called '%v' in namespace '%v': %v", claim.Spec.ParametersRef.Name, claim.Namespace, err)
}
Expand Down Expand Up @@ -134,7 +138,7 @@ func (d *driver) nodeDeviceAllocation(ctx context.Context, namespace, selectedNo
LabelSelector: "kubernetes.io/hostname=" + selectedNode,
}

deviceAllocations, err := d.clientset.AllocationV1alpha1().NodeDeviceAllocations(namespace).List(ctx, listOpts)
deviceAllocations, err := d.clientsets.AllocationV1alpha1().NodeDeviceAllocations(namespace).List(ctx, listOpts)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -197,7 +201,7 @@ func (d *driver) allocateGPU(
deviceAllocation.Status.AllocatedClaims[claimUID] = allocatedClaims

updateOpts := metav1.UpdateOptions{}
if _, err := d.clientset.AllocationV1alpha1().NodeDeviceAllocations(claimNamespace).Update(ctx, deviceAllocation, updateOpts); err != nil {
if _, err := d.clientsets.AllocationV1alpha1().NodeDeviceAllocations(claimNamespace).Update(ctx, deviceAllocation, updateOpts); err != nil {
return err
}

Expand Down Expand Up @@ -240,7 +244,7 @@ func (d *driver) Deallocate(ctx context.Context, claim *resourcev1alpha2.Resourc

claimNamespace := claim.GetNamespace()
updateOpts := metav1.UpdateOptions{}
if _, err := d.clientset.AllocationV1alpha1().NodeDeviceAllocations(claimNamespace).Update(ctx, deviceAllocation, updateOpts); err != nil {
if _, err := d.clientsets.AllocationV1alpha1().NodeDeviceAllocations(claimNamespace).Update(ctx, deviceAllocation, updateOpts); err != nil {
return err
}

Expand Down

0 comments on commit f520518

Please sign in to comment.