Skip to content

Commit

Permalink
fix cnc update
Browse files Browse the repository at this point in the history
  • Loading branch information
luomingmeng committed Aug 31, 2023
1 parent 0131c46 commit cffcbb6
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 12 deletions.
12 changes: 2 additions & 10 deletions pkg/controller/kcc/cnc.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,23 +228,15 @@ func (c *CustomNodeConfigController) addCustomNodeConfigEventHandle(obj interfac
c.enqueueCustomNodeConfig(t)
}

func (c *CustomNodeConfigController) updateCustomNodeConfigEventHandle(old, new interface{}) {
oldCNC, ok := old.(*apisv1alpha1.CustomNodeConfig)
if !ok {
klog.Errorf("[cnc] cannot convert obj to *CustomNodeConfig: %v", new)
return
}

func (c *CustomNodeConfigController) updateCustomNodeConfigEventHandle(_, new interface{}) {
newCNC, ok := new.(*apisv1alpha1.CustomNodeConfig)
if !ok {
klog.Errorf("[cnc] cannot convert obj to *CustomNodeConfig: %v", new)
return
}

klog.V(4).Infof("[cnc] notice update of CustomNodeConfig %s", native.GenerateUniqObjectNameKey(newCNC))
if !apiequality.Semantic.DeepEqual(oldCNC.Status, newCNC.Status) {
c.enqueueCustomNodeConfig(newCNC)
}
c.enqueueCustomNodeConfig(newCNC)
}

func (c *CustomNodeConfigController) enqueueCustomNodeConfig(cnc *apisv1alpha1.CustomNodeConfig) {
Expand Down
18 changes: 18 additions & 0 deletions pkg/controller/kcc/cnc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestCustomNodeConfigController_Run(t *testing.T) {
type args struct {
cncAndKCCList []runtime.Object
kccTargetList []runtime.Object
updateFunc func(ctx context.Context, genericContext *katalyst_base.GenericContext, cncAndKCCList, kccTargetList []runtime.Object)
kccConfig *config.Configuration
}
tests := []struct {
Expand Down Expand Up @@ -95,6 +96,20 @@ func TestCustomNodeConfigController_Run(t *testing.T) {
},
},
},
updateFunc: func(ctx context.Context, genericContext *katalyst_base.GenericContext, cncAndKCCList, kccTargetList []runtime.Object) {
for _, obj := range cncAndKCCList {
if cnc, ok := obj.(*v1alpha1.CustomNodeConfig); ok {
nodeConfig, err := genericContext.Client.InternalClient.ConfigV1alpha1().CustomNodeConfigs().Get(ctx, cnc.Name, v1.GetOptions{ResourceVersion: "0"})
assert.NoError(t, err)
nodeConfig.Labels = map[string]string{
"aa": "bb",
}

_, err = genericContext.Client.InternalClient.ConfigV1alpha1().CustomNodeConfigs().Update(ctx, nodeConfig, v1.UpdateOptions{})
assert.NoError(t, err)
}
}
},
},
},
}
Expand Down Expand Up @@ -130,6 +145,9 @@ func TestCustomNodeConfigController_Run(t *testing.T) {

cache.WaitForCacheSync(cnc.ctx.Done(), cnc.syncedFunc...)
time.Sleep(1 * time.Second)

tt.args.updateFunc(ctx, genericContext, tt.args.cncAndKCCList, tt.args.kccTargetList)
time.Sleep(1 * time.Second)
})
}
}
11 changes: 10 additions & 1 deletion pkg/controller/lifecycle/cnc.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (cl *CNCLifecycle) sync(key string) error {
}
node, err := cl.nodeLister.Get(name)
if errors.IsNotFound(err) {
klog.Info("node has been deleted %v", key)
klog.Infof("node has been deleted %v", key)
return nil
}
if err != nil {
Expand Down Expand Up @@ -294,6 +294,9 @@ func (cl *CNCLifecycle) clearUnexpectedCNC() {

func (cl *CNCLifecycle) updateOrCreateCNC(node *corev1.Node) error {
cnc, err := cl.cncLister.Get(node.Name)
if err != nil && !errors.IsNotFound(err) {
return fmt.Errorf("failed to get cnc from lister %s: %v", node.Name, err)
}
if errors.IsNotFound(err) {
cnc = &apis.CustomNodeConfig{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -307,6 +310,12 @@ func (cl *CNCLifecycle) updateOrCreateCNC(node *corev1.Node) error {
if err != nil && !errors.IsAlreadyExists(err) {
return fmt.Errorf("failed to create cnc %s: %v", cnc.Name, err)
}
if errors.IsAlreadyExists(err) {
cnc, err = cl.client.InternalClient.ConfigV1alpha1().CustomNodeConfigs().Get(cl.ctx, node.Name, metav1.GetOptions{ResourceVersion: "0"})
if err != nil {
return fmt.Errorf("failed to get cnc from apiserver %s: %v", node.Name, err)
}
}
}

newCNC := cnc.DeepCopy()
Expand Down
79 changes: 79 additions & 0 deletions pkg/controller/lifecycle/cnc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,82 @@ func TestCNCLifecycle_Run(t *testing.T) {
})
}
}

func TestCNCLifecycle_updateOrCreateCNC(t *testing.T) {
t.Parallel()

type fields struct {
node *corev1.Node
cnc *configapis.CustomNodeConfig
}
tests := []struct {
name string
fields fields
wantCNC *configapis.CustomNodeConfig
}{
{
name: "test-update",
fields: fields{
node: &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Labels: map[string]string{
"test": "test-1",
},
},
},
cnc: &configapis.CustomNodeConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Labels: map[string]string{
"test": "test",
},
},
},
},
wantCNC: &configapis.CustomNodeConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Labels: map[string]string{
"test": "test-1",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "v1",
Kind: "Node",
Name: "node1",
UID: "",
Controller: pointer.Bool(true),
BlockOwnerDeletion: pointer.Bool(true),
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
genericCtx, err := katalyst_base.GenerateFakeGenericContext([]runtime.Object{tt.fields.node}, []runtime.Object{tt.fields.cnc})
assert.NoError(t, err)

conf, err := options.NewOptions().Config()
require.NoError(t, err)
require.NotNil(t, conf)

cl, err := NewCNCLifecycle(context.Background(),
conf.GenericConfiguration,
conf.GenericControllerConfiguration,
conf.ControllersConfiguration.CNCLifecycleConfig,
genericCtx.Client,
genericCtx.KubeInformerFactory.Core().V1().Nodes(),
genericCtx.InternalInformerFactory.Config().V1alpha1().CustomNodeConfigs(),
genericCtx.EmitterPool.GetDefaultMetricsEmitter(),
)
assert.NoError(t, err)

// test cache not synced
err = cl.updateOrCreateCNC(tt.fields.node)
assert.NoError(t, err)
})
}
}
2 changes: 1 addition & 1 deletion pkg/controller/lifecycle/cnr.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (cl *CNRLifecycle) sync(key string) error {
}
node, err := cl.nodeLister.Get(name)
if errors.IsNotFound(err) {
klog.Info("node has been deleted %v", key)
klog.Infof("node has been deleted %v", key)
return nil
}
if err != nil {
Expand Down
110 changes: 110 additions & 0 deletions pkg/controller/lifecycle/cnr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,113 @@ func TestCNRLifecycle_Run(t *testing.T) {
})
}
}

func TestCNRLifecycle_updateOrCreateCNR(t *testing.T) {
t.Parallel()

type fields struct {
node *corev1.Node
cnr *nodeapis.CustomNodeResource
}
tests := []struct {
name string
fields fields
wantCNR *nodeapis.CustomNodeResource
}{
{
name: "test-create",
fields: fields{
node: &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Labels: map[string]string{
"test": "test",
},
},
},
},
wantCNR: &nodeapis.CustomNodeResource{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Labels: map[string]string{
"test": "test",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "v1",
Kind: "Node",
Name: "node1",
UID: "",
Controller: pointer.Bool(true),
BlockOwnerDeletion: pointer.Bool(true),
},
},
},
},
},
{
name: "test-update",
fields: fields{
node: &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Labels: map[string]string{
"test": "test-1",
},
},
},
cnr: &nodeapis.CustomNodeResource{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Labels: map[string]string{
"test": "test",
},
},
},
},
wantCNR: &nodeapis.CustomNodeResource{
ObjectMeta: metav1.ObjectMeta{
Name: "node1",
Labels: map[string]string{
"test": "test-1",
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "v1",
Kind: "Node",
Name: "node1",
UID: "",
Controller: pointer.Bool(true),
BlockOwnerDeletion: pointer.Bool(true),
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
genericCtx, err := katalyst_base.GenerateFakeGenericContext([]runtime.Object{tt.fields.node}, []runtime.Object{tt.fields.cnr})
assert.NoError(t, err)

conf, err := options.NewOptions().Config()
require.NoError(t, err)
require.NotNil(t, conf)

cl, err := NewCNRLifecycle(context.Background(),
conf.GenericConfiguration,
conf.GenericControllerConfiguration,
conf.ControllersConfiguration.CNRLifecycleConfig,
genericCtx.Client,
genericCtx.KubeInformerFactory.Core().V1().Nodes(),
genericCtx.InternalInformerFactory.Node().V1alpha1().CustomNodeResources(),
genericCtx.EmitterPool.GetDefaultMetricsEmitter(),
)
assert.NoError(t, err)

// test cache not synced
err = cl.updateOrCreateCNR(tt.fields.node)
assert.NoError(t, err)
})
}
}

0 comments on commit cffcbb6

Please sign in to comment.