Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore file updates only when both release id and config match rules are same --bug=128720431 #112

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions build/nodemanPlugin/etc/bkbscp.conf.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ apps:
{{ label.key }}: "{{ label.value }}"
{%- endfor %}
config_matches:
{%- for match in app.配置项匹配 %}
{%- for match in app.配置文件拉取筛选 %}
- "{{ match }}"
{%- endfor %}
{%- endfor %}
Expand All @@ -39,10 +39,10 @@ labels:
{{ label.key }}: "{{ label.value }}"
{%- endfor %}
{%- endif %}
{%- if 全局配置项匹配 %}
# 全局配置项匹配
{%- if 全局配置文件拉取筛选 %}
# 全局配置文件拉取筛选
config_matches:
{%- for match in 全局配置项匹配 %}
{%- for match in 全局配置文件拉取筛选 %}
- "{{ match }}"
{%- endfor %}
{%- endif %}
Expand Down
12 changes: 6 additions & 6 deletions build/nodemanPlugin/project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ config_templates:
value:
title: value
type: string
配置项匹配:
title: 配置项匹配
配置文件拉取筛选:
title: 配置文件拉取筛选
type: array
required: false
items:
title: 匹配项
title: 匹配规则
type: string
required: false
Feed服务地址:
Expand All @@ -83,12 +83,12 @@ config_templates:
value:
title: value
type: string
全局配置项匹配:
title: 全局配置项匹配
全局配置文件拉取筛选:
title: 全局配置文件拉取筛选
type: array
required: false
items:
title: 匹配项
title: 匹配规则
type: string
required: false
临时目录:
Expand Down
1 change: 1 addition & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func (c *client) PullFiles(app string, opts ...AppOption) (*Release, error) { //
App: app,
Labels: req.AppMeta.Labels,
Uid: req.AppMeta.Uid,
Match: option.Match,
StartTime: time.Now().UTC(),
},
}
Expand Down
1 change: 1 addition & 0 deletions client/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (w *watcher) loopHeartbeat() error { // nolint
App: subscriber.App,
Labels: subscriber.Labels,
Uid: subscriber.UID,
Match: subscriber.Match,
CursorID: subscriber.CursorID,
ReleaseChangeStatus: subscriber.ReleaseChangeStatus,
DownloadFileNum: subscriber.DownloadFileNum,
Expand Down
18 changes: 12 additions & 6 deletions client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ type Callback func(release *Release) error
// Function 定义类型
type Function func() error

// compareRelease 对比当前服务版本
// 返回值: 是否跳过本次版本变更事件(本地版本和事件版本一致), 错误信息
// compareRelease 对比当前服务版本、配置匹配规则
// 返回值: 是否跳过本次版本变更事件(本地已有和事件的版本、配置匹配规则一致), 错误信息
func (r *Release) compareRelease() (bool, error) {
lastMetadata, exist, err := eventmeta.GetLatestMetadataFromFile(r.AppDir)
if err != nil {
Expand All @@ -133,7 +133,8 @@ func (r *Release) compareRelease() (bool, error) {
logger.Warn("can not find metadata file, maybe you should exec pull command first")
return false, nil
}
if lastMetadata.ReleaseID == r.ReleaseID {
fmt.Printf("*** meta.match:%v, cmd.match:%v\n", lastMetadata.ConfigMatches, r.AppMate.Match)
fireyun marked this conversation as resolved.
Show resolved Hide resolved
if lastMetadata.ReleaseID == r.ReleaseID && util.StrSlicesEqual(lastMetadata.ConfigMatches, r.AppMate.Match) {
r.AppMate.CurrentReleaseID = r.ReleaseID
logger.Info("current release is consistent with the received release, skip", slog.Any("releaseID", r.ReleaseID))
return true, nil
Expand Down Expand Up @@ -238,10 +239,15 @@ func (r *Release) UpdateFiles() Function {
// UpdateMetadata 4.更新meatdata数据方法
func (r *Release) UpdateMetadata() Function {
return func() error {
match := r.AppMate.Match
if match == nil {
match = []string{}
}
metadata := &eventmeta.EventMeta{
ReleaseID: r.ReleaseID,
Status: eventmeta.EventStatusSuccess,
EventTime: time.Now().Format(time.RFC3339),
ReleaseID: r.ReleaseID,
Status: eventmeta.EventStatusSuccess,
ConfigMatches: match,
EventTime: time.Now().Format(time.RFC3339),
}
err := eventmeta.AppendMetadataToFile(r.AppDir, metadata)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions client/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ func (w *watcher) OnReleaseChange(event *sfs.ReleaseChangeEvent) { // nolint
App: subscriber.App,
Uid: subscriber.UID,
Labels: subscriber.Labels,
Match: subscriber.Match,
CurrentReleaseID: subscriber.CurrentReleaseID,
TargetReleaseID: pl.ReleaseMeta.ReleaseID,
TotalFileSize: totalFileSize,
Expand Down
2 changes: 2 additions & 0 deletions internal/util/eventmeta/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type EventMeta struct {
Status EventStatus `json:"status"`
// Message event message
Message string `json:"message"`
// ConfigMatches app config item's match conditions
ConfigMatches []string `json:"configMatches"`
// EventTime event time
EventTime string `json:"eventTime"`
}
Expand Down
41 changes: 41 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 util

// StrSlicesEqual to check if two string slices have the same elements regardless of order
func StrSlicesEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}

// Create maps to count occurrences of each element
countsA := make(map[string]int)
countsB := make(map[string]int)

for _, item := range a {
countsA[item]++
}

for _, item := range b {
countsB[item]++
}

// Compare the two maps
for key, countA := range countsA {
if countB, exists := countsB[key]; !exists || countA != countB {
return false
}
}

return true
}
78 changes: 78 additions & 0 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 util

import (
"testing"
)

func TestStrSlicesEqual(t *testing.T) {
tests := []struct {
name string
slice1 []string
slice2 []string
expected bool
}{
{
name: "Equal slices with same order",
slice1: []string{"apple", "banana", "cherry"},
slice2: []string{"apple", "banana", "cherry"},
expected: true,
},
{
name: "Equal slices with different order",
slice1: []string{"apple", "banana", "cherry"},
slice2: []string{"banana", "cherry", "apple"},
expected: true,
},
{
name: "Slices with different lengths",
slice1: []string{"apple", "banana"},
slice2: []string{"apple", "banana", "cherry"},
expected: false,
},
{
name: "Slices with different elements",
slice1: []string{"apple", "banana", "cherry"},
slice2: []string{"apple", "banana", "date"},
expected: false,
},
{
name: "Slices with different element counts",
slice1: []string{"apple", "banana", "banana"},
slice2: []string{"apple", "banana"},
expected: false,
},
{
name: "Empty slices",
slice1: []string{},
slice2: []string{},
expected: true,
},
{
name: "One empty slice",
slice1: []string{"apple", "banana"},
slice2: []string{},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := StrSlicesEqual(tt.slice1, tt.slice2)
if result != tt.expected {
t.Errorf("StrSlicesEqual(%v, %v) = %v; want %v", tt.slice1, tt.slice2, result, tt.expected)
}
})
}
}
Loading