Skip to content

Commit

Permalink
Merge pull request #253 from ypnuaa037/reduce-agent-access-to-apiserver
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-wangxu authored Mar 26, 2024
2 parents 166de16 + 6bc3046 commit 5ebf20f
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 7 deletions.
32 changes: 26 additions & 6 deletions pkg/agent/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"os"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -149,16 +150,35 @@ func (d *Discoverer) Discover() {
d.createSpdkBdevs(&nlsCopy.Status.FilteredStorageInfo.Devices)
}

// only update status
log.Infof("update nls %s", nlsCopy.Name)
_, err = d.localclientset.CsiV1alpha1().NodeLocalStorages().UpdateStatus(context.Background(), nlsCopy, metav1.UpdateOptions{})
if err != nil {
log.Errorf("local storage CRD updateStatus error: %s", err.Error())
return
// only update when NodeLocalStorage status changed, reduce pressure to apiserver in large cluster
oldStatus := &nls.Status
newStatusCopy := nlsCopy.Status.DeepCopy()
if isNlsStatusChanged(oldStatus, newStatusCopy) {
// only update status
log.Infof("update nls %s", nlsCopy.Name)
_, err = d.localclientset.CsiV1alpha1().NodeLocalStorages().UpdateStatus(context.Background(), nlsCopy, metav1.UpdateOptions{})
if err != nil {
log.Errorf("local storage CRD updateStatus error: %s", err.Error())
return
}
} else {
log.Infof("nls %s status not changed", nlsCopy.Name)
}
}
}

func isNlsStatusChanged(oldStatus, newStatus *localv1alpha1.NodeLocalStorageStatus) bool {
// Remove time-related fields and then compare
oldStatus.NodeStorageInfo.State.LastHeartbeatTime = nil
oldStatus.NodeStorageInfo.State.LastTransitionTime = nil
oldStatus.FilteredStorageInfo.UpdateStatus.LastUpdateTime = nil
newStatus.NodeStorageInfo.State.LastHeartbeatTime = nil
newStatus.NodeStorageInfo.State.LastTransitionTime = nil
newStatus.FilteredStorageInfo.UpdateStatus.LastUpdateTime = nil

return !reflect.DeepEqual(oldStatus, newStatus)
}

// Create AIO bdevs
func (d *Discoverer) createSpdkBdevs(devices *[]string) {
var found bool
Expand Down
116 changes: 116 additions & 0 deletions pkg/agent/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ limitations under the License.
package discovery

import (
localv1alpha1 "github.com/alibaba/open-local/pkg/apis/storage/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
"time"
)

func TestFilterInfo(t *testing.T) {
Expand Down Expand Up @@ -81,3 +84,116 @@ func sameStringSlice(x, y []string) bool {
}
return len(diff) == 0
}

func TestIsNlsStatusChanged(t *testing.T) {
type args struct {
oldStatus *localv1alpha1.NodeLocalStorageStatus
newStatus *localv1alpha1.NodeLocalStorageStatus
}
updatedTime := metav1.Now()
oldStatus := &localv1alpha1.NodeLocalStorageStatus{
NodeStorageInfo: localv1alpha1.NodeStorageInfo{
DeviceInfos: []localv1alpha1.DeviceInfo{
{
Name: "/dev/sdb",
MediaType: "hdd",
Total: 100000000,
},
},
VolumeGroups: []localv1alpha1.VolumeGroup{
{
Name: "pool0",
PhysicalVolumes: []string{
"/dev/sdb",
},
Total: 100000000,
Allocatable: 100000000,
Available: 100000000,
},
},
Phase: "Running",
State: localv1alpha1.StorageState{
Status: localv1alpha1.ConditionTrue,
Type: localv1alpha1.StorageReady,
},
},
FilteredStorageInfo: localv1alpha1.FilteredStorageInfo{
UpdateStatus: localv1alpha1.UpdateStatusInfo{
LastUpdateTime: &updatedTime,
Status: "accepted",
},
VolumeGroups: []string{"pool0"},
},
}

newStatus := &localv1alpha1.NodeLocalStorageStatus{
NodeStorageInfo: localv1alpha1.NodeStorageInfo{
DeviceInfos: []localv1alpha1.DeviceInfo{
{
Name: "/dev/sdb",
MediaType: "hdd",
Total: 100000000,
},
},
VolumeGroups: []localv1alpha1.VolumeGroup{
{
Name: "pool0",
PhysicalVolumes: []string{
"/dev/sdb",
},
Total: 100000000,
Allocatable: 50000000, // changed
Available: 50000000, // changed
},
},
Phase: "Running",
State: localv1alpha1.StorageState{
Status: localv1alpha1.ConditionTrue,
Type: localv1alpha1.StorageReady,
},
},
FilteredStorageInfo: localv1alpha1.FilteredStorageInfo{
UpdateStatus: localv1alpha1.UpdateStatusInfo{
LastUpdateTime: &updatedTime,
Status: "accepted",
},
VolumeGroups: []string{"pool0"},
},
}

newStatusOnlyTimeChanged := oldStatus
newUpdateTime := updatedTime.Add(1*time.Minute)
newStatusOnlyTimeChanged.FilteredStorageInfo.UpdateStatus.LastUpdateTime = &metav1.Time{Time: newUpdateTime}
newStatusOnlyTimeChanged.NodeStorageInfo.State.LastTransitionTime = &metav1.Time{Time: newUpdateTime}
newStatusOnlyTimeChanged.NodeStorageInfo.State.LastHeartbeatTime = &metav1.Time{Time: newUpdateTime}

tests := []struct {
name string
args args
want bool
}{
{
name: "test1",
args: args{
oldStatus: oldStatus,
newStatus: newStatus,
},
want: true,
},
{
name: "test2",
args: args{
oldStatus: oldStatus,
newStatus: newStatusOnlyTimeChanged,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isNlsStatusChanged(tt.args.oldStatus, tt.args.newStatus); got != tt.want {
t.Errorf("isNlsStatusChanged() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/csi/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,6 @@ func (ns *nodeServer) checkSPDKSupport() {
}
}

time.Sleep(time.Millisecond * 100)
time.Sleep(time.Second * 5)
}
}

0 comments on commit 5ebf20f

Please sign in to comment.