Skip to content

Commit cbccbd4

Browse files
learner0810wawa0210
authored andcommitted
Optimize the time format layout
Signed-off-by: learner0810 <zhongjun.li@daocloud.io>
1 parent ee7447a commit cbccbd4

File tree

5 files changed

+200
-5
lines changed

5 files changed

+200
-5
lines changed

pkg/device/ascend/device_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ func Test_CheckHealth(t *testing.T) {
510510
n: corev1.Node{
511511
ObjectMeta: metav1.ObjectMeta{
512512
Annotations: map[string]string{
513-
util.HandshakeAnnos["huawei.com/Ascend910"]: "Requesting_2128.12.02 00:00:00",
513+
util.HandshakeAnnos["huawei.com/Ascend910"]: "Requesting_2128-12-02 00:00:00",
514514
},
515515
},
516516
},
@@ -564,7 +564,7 @@ func Test_CheckHealth(t *testing.T) {
564564
n: corev1.Node{
565565
ObjectMeta: metav1.ObjectMeta{
566566
Annotations: map[string]string{
567-
util.HandshakeAnnos["huawei.com/Ascend910"]: "Requesting_2024.01.02 00:00:00",
567+
util.HandshakeAnnos["huawei.com/Ascend910"]: "Requesting_2024-01-02 00:00:00",
568568
},
569569
},
570570
},

pkg/scheduler/scheduler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (s *Scheduler) RegisterFromNodeAnnotations() {
198198
_, ok := util.HandshakeAnnos[devhandsk]
199199
if ok {
200200
tmppat := make(map[string]string)
201-
tmppat[util.HandshakeAnnos[devhandsk]] = "Requesting_" + time.Now().Format("2006.01.02 15:04:05")
201+
tmppat[util.HandshakeAnnos[devhandsk]] = "Requesting_" + time.Now().Format(time.DateTime)
202202
klog.V(5).InfoS("New timestamp", util.HandshakeAnnos[devhandsk], tmppat[util.HandshakeAnnos[devhandsk]], "nodeName", val.Name)
203203
n, err := util.GetNode(val.Name)
204204
if err != nil {

pkg/scheduler/scheduler_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package scheduler
1818

1919
import (
2020
"context"
21+
"strings"
2122
"testing"
2223
"time"
2324

@@ -604,3 +605,59 @@ func Test_Filter(t *testing.T) {
604605
})
605606
}
606607
}
608+
609+
func Test_RegisterFromNodeAnnotations(t *testing.T) {
610+
tests := []struct {
611+
name string
612+
Scheduler *Scheduler
613+
want func(node *corev1.Node) bool
614+
}{
615+
{
616+
name: "test node hand shake annotations layout",
617+
Scheduler: func() *Scheduler {
618+
s := NewScheduler()
619+
s.stopCh = make(chan struct{})
620+
s.nodeNotify = make(chan struct{})
621+
client.KubeClient = fake.NewSimpleClientset()
622+
s.kubeClient = client.KubeClient
623+
informerFactory := informers.NewSharedInformerFactoryWithOptions(client.KubeClient, time.Hour*1)
624+
s.nodeLister = informerFactory.Core().V1().Nodes().Lister()
625+
node := &corev1.Node{
626+
ObjectMeta: metav1.ObjectMeta{
627+
Name: "node",
628+
},
629+
}
630+
client.KubeClient.CoreV1().Nodes().Create(context.TODO(), node, metav1.CreateOptions{})
631+
err := informerFactory.Core().V1().Nodes().Informer().GetIndexer().Add(node)
632+
if err != nil {
633+
t.Errorf("failed to create node err; %v", err)
634+
return nil
635+
}
636+
return s
637+
}(),
638+
want: func(node *corev1.Node) bool {
639+
_, err := time.Parse(time.DateTime, strings.TrimPrefix(node.Annotations["hami.io/node-handshake"], "Requesting_"))
640+
_, err2 := time.Parse(time.DateTime, strings.TrimPrefix(node.Annotations["hami.io/node-handshake-dcu"], "Requesting_"))
641+
return err == nil && err2 == nil
642+
},
643+
},
644+
}
645+
646+
for _, test := range tests {
647+
t.Run(test.name, func(t *testing.T) {
648+
time.AfterFunc(5*time.Second, func() {
649+
close(test.Scheduler.stopCh)
650+
})
651+
go func() {
652+
test.Scheduler.nodeNotify <- struct{}{}
653+
}()
654+
test.Scheduler.RegisterFromNodeAnnotations()
655+
node, err := test.Scheduler.kubeClient.CoreV1().Nodes().Get(context.TODO(), "node", metav1.GetOptions{})
656+
if err != nil {
657+
t.Errorf("failed to get node err; %v", err)
658+
return
659+
}
660+
assert.Equal(t, test.want(node), true)
661+
})
662+
}
663+
}

pkg/util/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func InitKlogFlags() *flag.FlagSet {
371371
func CheckHealth(devType string, n *corev1.Node) (bool, bool) {
372372
handshake := n.Annotations[HandshakeAnnos[devType]]
373373
if strings.Contains(handshake, "Requesting") {
374-
formertime, _ := time.Parse("2006.01.02 15:04:05", strings.Split(handshake, "_")[1])
374+
formertime, _ := time.Parse(time.DateTime, strings.Split(handshake, "_")[1])
375375
return time.Now().Before(formertime.Add(time.Second * 60)), false
376376
} else if strings.Contains(handshake, "Deleted") {
377377
return true, false
@@ -382,7 +382,7 @@ func CheckHealth(devType string, n *corev1.Node) (bool, bool) {
382382

383383
func MarkAnnotationsToDelete(devType string, nn string) error {
384384
tmppat := make(map[string]string)
385-
tmppat[devType] = "Deleted_" + time.Now().Format("2006.01.02 15:04:05")
385+
tmppat[devType] = "Deleted_" + time.Now().Format(time.DateTime)
386386
n, err := GetNode(nn)
387387
if err != nil {
388388
klog.Errorln("get node failed", err.Error())

pkg/util/util_test.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ limitations under the License.
1717
package util
1818

1919
import (
20+
"context"
2021
"encoding/json"
2122
"errors"
2223
"fmt"
2324
"testing"
2425

2526
"gotest.tools/v3/assert"
27+
corev1 "k8s.io/api/core/v1"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/client-go/kubernetes/fake"
30+
31+
"github.com/Project-HAMi/HAMi/pkg/util/client"
2632
)
2733

2834
var inRequestDevices map[string]string
@@ -465,3 +471,135 @@ func Test_EncodeNodeDevices(t *testing.T) {
465471
})
466472
}
467473
}
474+
475+
func Test_CheckHealth(t *testing.T) {
476+
tests := []struct {
477+
name string
478+
args struct {
479+
devType string
480+
n corev1.Node
481+
}
482+
want1 bool
483+
want2 bool
484+
}{
485+
{
486+
name: "Requesting state",
487+
args: struct {
488+
devType string
489+
n corev1.Node
490+
}{
491+
devType: "huawei.com/Ascend910",
492+
n: corev1.Node{
493+
ObjectMeta: metav1.ObjectMeta{
494+
Annotations: map[string]string{
495+
HandshakeAnnos["huawei.com/Ascend910"]: "Requesting_2128-12-02 00:00:00",
496+
},
497+
},
498+
},
499+
},
500+
want1: true,
501+
want2: false,
502+
},
503+
{
504+
name: "Deleted state",
505+
args: struct {
506+
devType string
507+
n corev1.Node
508+
}{
509+
devType: "huawei.com/Ascend910",
510+
n: corev1.Node{
511+
ObjectMeta: metav1.ObjectMeta{
512+
Annotations: map[string]string{
513+
HandshakeAnnos["huawei.com/Ascend910"]: "Deleted",
514+
},
515+
},
516+
},
517+
},
518+
want1: true,
519+
want2: false,
520+
},
521+
{
522+
name: "Unknown state",
523+
args: struct {
524+
devType string
525+
n corev1.Node
526+
}{
527+
devType: "huawei.com/Ascend910",
528+
n: corev1.Node{
529+
ObjectMeta: metav1.ObjectMeta{
530+
Annotations: map[string]string{
531+
HandshakeAnnos["huawei.com/Ascend910"]: "Unknown",
532+
},
533+
},
534+
},
535+
},
536+
want1: true,
537+
want2: true,
538+
},
539+
{
540+
name: "Requesting state expired",
541+
args: struct {
542+
devType string
543+
n corev1.Node
544+
}{
545+
devType: "huawei.com/Ascend910",
546+
n: corev1.Node{
547+
ObjectMeta: metav1.ObjectMeta{
548+
Annotations: map[string]string{
549+
HandshakeAnnos["huawei.com/Ascend910"]: "Requesting_2024-01-02 00:00:00",
550+
},
551+
},
552+
},
553+
},
554+
want1: false,
555+
want2: false,
556+
},
557+
}
558+
for _, test := range tests {
559+
t.Run(test.name, func(t *testing.T) {
560+
result1, result2 := CheckHealth(test.args.devType, &test.args.n)
561+
assert.Equal(t, result1, test.want1)
562+
assert.Equal(t, result2, test.want2)
563+
})
564+
}
565+
}
566+
567+
func TestMarkAnnotationsToDelete(t *testing.T) {
568+
client.KubeClient = fake.NewSimpleClientset()
569+
client.KubeClient.CoreV1().Nodes().Create(context.TODO(), &corev1.Node{
570+
ObjectMeta: metav1.ObjectMeta{Name: "node-worker2"},
571+
}, metav1.CreateOptions{})
572+
type args struct {
573+
devType string
574+
nn string
575+
}
576+
tests := []struct {
577+
name string
578+
args args
579+
wantErr bool
580+
}{
581+
{
582+
name: "node not found",
583+
args: args{
584+
devType: "huawei.com/Ascend910",
585+
nn: "node-worker1",
586+
},
587+
wantErr: true,
588+
},
589+
{
590+
name: "mark annotations to delete",
591+
args: args{
592+
devType: "huawei.com/Ascend910",
593+
nn: "node-worker2",
594+
},
595+
wantErr: false,
596+
},
597+
}
598+
for _, tt := range tests {
599+
t.Run(tt.name, func(t *testing.T) {
600+
if err := MarkAnnotationsToDelete(tt.args.devType, tt.args.nn); (err != nil) != tt.wantErr {
601+
t.Errorf("MarkAnnotationsToDelete() error = %v, wantErr %v", err, tt.wantErr)
602+
}
603+
})
604+
}
605+
}

0 commit comments

Comments
 (0)