Skip to content

Commit

Permalink
feat: add npd controller
Browse files Browse the repository at this point in the history
  • Loading branch information
WangZzzhe committed Jul 12, 2024
1 parent 65905ce commit d71801f
Show file tree
Hide file tree
Showing 17 changed files with 1,402 additions and 3 deletions.
57 changes: 57 additions & 0 deletions cmd/katalyst-controller/app/controller/npd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2022 The Katalyst Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"context"
"fmt"

"k8s.io/klog/v2"

katalyst "github.com/kubewharf/katalyst-core/cmd/base"
"github.com/kubewharf/katalyst-core/pkg/config"
"github.com/kubewharf/katalyst-core/pkg/controller/npd"
)

const (
NPDControllerName = "npd"
)

func StartNPDController(ctx context.Context, controlCtx *katalyst.GenericContext,
conf *config.Configuration, extraConf interface{}, _ string,
) (bool, error) {
if controlCtx == nil || conf == nil {
err := fmt.Errorf("controlCtx and controllerConf can't be nil")
klog.Error(err)
return false, err
}

npdController, err := npd.NewNPDController(
ctx,
controlCtx, conf.GenericConfiguration,
conf.GenericControllerConfiguration,
conf.NPDConfig,
extraConf,
)
if err != nil {
klog.Errorf("failed to new npd controller")
return false, err
}

go npdController.Run()
return true, nil
}
1 change: 1 addition & 0 deletions cmd/katalyst-controller/app/enablecontrollers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func init() {
controllerInitializers.Store(controller.VPAControllerName, ControllerStarter{Starter: controller.StartVPAController})
controllerInitializers.Store(controller.KCCControllerName, ControllerStarter{Starter: controller.StartKCCController})
controllerInitializers.Store(controller.SPDControllerName, ControllerStarter{Starter: controller.StartSPDController})
controllerInitializers.Store(controller.NPDControllerName, ControllerStarter{Starter: controller.StartNPDController})
controllerInitializers.Store(controller.LifeCycleControllerName, ControllerStarter{Starter: controller.StartLifeCycleController})
controllerInitializers.Store(controller.MonitorControllerName, ControllerStarter{Starter: controller.StartMonitorController})
controllerInitializers.Store(controller.OvercommitControllerName, ControllerStarter{Starter: controller.StartOvercommitController})
Expand Down
4 changes: 4 additions & 0 deletions cmd/katalyst-controller/app/options/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ControllersOptions struct {
*VPAOptions
*KCCOptions
*SPDOptions
*NPDOptions
*LifeCycleOptions
*MonitorOptions
*OvercommitOptions
Expand All @@ -40,6 +41,7 @@ func NewControllersOptions() *ControllersOptions {
VPAOptions: NewVPAOptions(),
KCCOptions: NewKCCOptions(),
SPDOptions: NewSPDOptions(),
NPDOptions: NewNPDOptions(),
LifeCycleOptions: NewLifeCycleOptions(),
MonitorOptions: NewMonitorOptions(),
OvercommitOptions: NewOvercommitOptions(),
Expand All @@ -52,6 +54,7 @@ func (o *ControllersOptions) AddFlags(fss *cliflag.NamedFlagSets) {
o.VPAOptions.AddFlags(fss)
o.KCCOptions.AddFlags(fss)
o.SPDOptions.AddFlags(fss)
o.NPDOptions.AddFlags(fss)
o.LifeCycleOptions.AddFlags(fss)
o.MonitorOptions.AddFlags(fss)
o.OvercommitOptions.AddFlags(fss)
Expand All @@ -66,6 +69,7 @@ func (o *ControllersOptions) ApplyTo(c *controllerconfig.ControllersConfiguratio
errList = append(errList, o.VPAOptions.ApplyTo(c.VPAConfig))
errList = append(errList, o.KCCOptions.ApplyTo(c.KCCConfig))
errList = append(errList, o.SPDOptions.ApplyTo(c.SPDConfig))
errList = append(errList, o.NPDOptions.ApplyTo(c.NPDConfig))
errList = append(errList, o.LifeCycleOptions.ApplyTo(c.LifeCycleConfig))
errList = append(errList, o.MonitorOptions.ApplyTo(c.MonitorConfig))
errList = append(errList, o.OvercommitOptions.ApplyTo(c.OvercommitConfig))
Expand Down
64 changes: 64 additions & 0 deletions cmd/katalyst-controller/app/options/npd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2022 The Katalyst Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package options

import (
cliflag "k8s.io/component-base/cli/flag"

"github.com/kubewharf/katalyst-core/pkg/config/controller"
)

type NPDOptions struct {
NPDIndicatorPlugins []string
EnableScopeDuplicated bool
SyncWorkers int
}

func NewNPDOptions() *NPDOptions {
return &NPDOptions{
NPDIndicatorPlugins: []string{},
EnableScopeDuplicated: false,
SyncWorkers: 1,
}
}

func (o *NPDOptions) AddFlags(fss *cliflag.NamedFlagSets) {
fs := fss.FlagSet("npd")

fs.StringSliceVar(&o.NPDIndicatorPlugins, "npd-indicator-plugins", o.NPDIndicatorPlugins,
"A list of indicator plugins to be used")
fs.BoolVar(&o.EnableScopeDuplicated, "npd-enable-scope-duplicated", o.EnableScopeDuplicated,
"Whether metrics with the same scope can be updated by multiple plugins")
fs.IntVar(&o.SyncWorkers, "npd-sync-workers", o.SyncWorkers,
"Number of workers to sync npd status")
}

func (o *NPDOptions) ApplyTo(c *controller.NPDConfig) error {
c.NPDIndicatorPlugins = o.NPDIndicatorPlugins
c.EnableScopeDuplicated = o.EnableScopeDuplicated
c.SyncWorkers = o.SyncWorkers
return nil
}

func (o *NPDOptions) Config() (*controller.NPDConfig, error) {
c := &controller.NPDConfig{}
if err := o.ApplyTo(c); err != nil {
return nil, err
}

return c, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/google/uuid v1.3.0
github.com/h2non/gock v1.2.0
github.com/klauspost/cpuid/v2 v2.2.6
github.com/kubewharf/katalyst-api v0.5.1-0.20240702044746-be552fd7ea7d
github.com/kubewharf/katalyst-api v0.5.1-0.20240712060613-77c1cafa7c40
github.com/montanaflynn/stats v0.7.1
github.com/opencontainers/runc v1.1.6
github.com/opencontainers/selinux v1.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubewharf/katalyst-api v0.5.1-0.20240702044746-be552fd7ea7d h1:6CuK3axf2B63zIkEu5XyxbaC+JArE/3Jo3QHvb+Hn0M=
github.com/kubewharf/katalyst-api v0.5.1-0.20240702044746-be552fd7ea7d/go.mod h1:Y2IeIorxQamF2a3oa0+URztl5QCSty6Jj3zD83R8J9k=
github.com/kubewharf/katalyst-api v0.5.1-0.20240712060613-77c1cafa7c40 h1:YC6OCC8+eyXSXmq9nuDgyXv3NzZkkQXhExzST9W3kY4=
github.com/kubewharf/katalyst-api v0.5.1-0.20240712060613-77c1cafa7c40/go.mod h1:Y2IeIorxQamF2a3oa0+URztl5QCSty6Jj3zD83R8J9k=
github.com/kubewharf/kubelet v1.24.6-kubewharf.8 h1:2e89T/nZTgzaVhyRsZuwEdRk8V8kJXs4PRkgfeG4Ai4=
github.com/kubewharf/kubelet v1.24.6-kubewharf.8/go.mod h1:MxbSZUx3wXztFneeelwWWlX7NAAStJ6expqq7gY2J3c=
github.com/kyoh86/exportloopref v0.1.7/go.mod h1:h1rDl2Kdj97+Kwh4gdz3ujE7XHmH51Q0lUiZ1z4NLj8=
Expand Down
77 changes: 77 additions & 0 deletions pkg/client/control/npd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
Copyright 2022 The Katalyst Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package control

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kubewharf/katalyst-api/pkg/apis/node/v1alpha1"
clientset "github.com/kubewharf/katalyst-api/pkg/client/clientset/versioned"
)

type NodeProfileControl interface {
CreateNPD(ctx context.Context, npd *v1alpha1.NodeProfileDescriptor, opts metav1.CreateOptions) (*v1alpha1.NodeProfileDescriptor, error)
UpdateNPDStatus(ctx context.Context, npd *v1alpha1.NodeProfileDescriptor, opts metav1.UpdateOptions) (*v1alpha1.NodeProfileDescriptor, error)
DeleteNPD(ctx context.Context, npdName string, opts metav1.DeleteOptions) error
}

type DummyNPDControl struct{}

func (d *DummyNPDControl) CreateNPD(ctx context.Context, npd *v1alpha1.NodeProfileDescriptor, opts metav1.CreateOptions) (*v1alpha1.NodeProfileDescriptor, error) {
return nil, nil
}

func (d *DummyNPDControl) UpdateNPDStatus(ctx context.Context, npd *v1alpha1.NodeProfileDescriptor, opts metav1.UpdateOptions) (*v1alpha1.NodeProfileDescriptor, error) {
return nil, nil
}

func (d *DummyNPDControl) DeleteNPD(ctx context.Context, npdName string, opts metav1.DeleteOptions) error {
return nil
}

type NPDControlImp struct {
client clientset.Interface
}

func NewNPDControlImp(client clientset.Interface) *NPDControlImp {
return &NPDControlImp{
client: client,
}
}

func (n *NPDControlImp) CreateNPD(ctx context.Context, npd *v1alpha1.NodeProfileDescriptor, opts metav1.CreateOptions) (*v1alpha1.NodeProfileDescriptor, error) {
if npd == nil {
return nil, fmt.Errorf("npd is nil")
}

return n.client.NodeV1alpha1().NodeProfileDescriptors().Create(ctx, npd, opts)
}

func (n *NPDControlImp) UpdateNPDStatus(ctx context.Context, npd *v1alpha1.NodeProfileDescriptor, opts metav1.UpdateOptions) (*v1alpha1.NodeProfileDescriptor, error) {
if npd == nil {
return nil, fmt.Errorf("npd is nil")
}

return n.client.NodeV1alpha1().NodeProfileDescriptors().UpdateStatus(ctx, npd, opts)
}

func (n *NPDControlImp) DeleteNPD(ctx context.Context, npdName string, opts metav1.DeleteOptions) error {
return n.client.NodeV1alpha1().NodeProfileDescriptors().Delete(ctx, npdName, opts)
}
2 changes: 2 additions & 0 deletions pkg/config/controller/controller_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ControllersConfiguration struct {
*VPAConfig
*KCCConfig
*SPDConfig
*NPDConfig
*LifeCycleConfig
*MonitorConfig
*OvercommitConfig
Expand All @@ -63,6 +64,7 @@ func NewControllersConfiguration() *ControllersConfiguration {
VPAConfig: NewVPAConfig(),
KCCConfig: NewKCCConfig(),
SPDConfig: NewSPDConfig(),
NPDConfig: NewNPDConfig(),
LifeCycleConfig: NewLifeCycleConfig(),
MonitorConfig: NewMonitorConfig(),
OvercommitConfig: NewOvercommitConfig(),
Expand Down
33 changes: 33 additions & 0 deletions pkg/config/controller/npd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2022 The Katalyst Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

type NPDConfig struct {
NPDIndicatorPlugins []string

EnableScopeDuplicated bool

SyncWorkers int
}

func NewNPDConfig() *NPDConfig {
return &NPDConfig{
NPDIndicatorPlugins: []string{},
EnableScopeDuplicated: false,
SyncWorkers: 1,
}
}
71 changes: 71 additions & 0 deletions pkg/controller/npd/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2022 The Katalyst Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package npd

import (
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
)

func (nc *NPDController) onNodeAdd(obj interface{}) {
node, ok := obj.(*v1.Node)
if !ok {
klog.Errorf("[npd] cannot convert obj to *v1.node")
return
}

nc.enqueueNode(node)
}

func (nc *NPDController) onNodeUpdate(_, newObj interface{}) {
node, ok := newObj.(*v1.Node)
if !ok {
klog.Errorf("[npd] cannot convert obj to *v1.node")
return
}

nc.enqueueNode(node)
}

func (nc *NPDController) onNodeDelete(obj interface{}) {
node, ok := obj.(*v1.Node)
if !ok {
klog.Errorf("[npd] cannot convert obj to *v1.node")
return
}

err := nc.deleteNPD(node.Name)
if err != nil {
klog.Errorf("delete node %v fail: %v", node.Name, err)
}
}

func (nc *NPDController) enqueueNode(node *v1.Node) {
if node == nil {
klog.Warningf("[npd] enqueue a nil node")
return
}

key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(node)
if err != nil {
klog.Errorf("[npd] couldn't get key for node: %v, err: %v", node.Name, err)
return
}

nc.nodeQueue.Add(key)
}
Loading

0 comments on commit d71801f

Please sign in to comment.