forked from AliyunContainerService/log-pilot
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[master]feat(pod):add regex support for pod name (#102)
* chore(*):resorted import package order * feat(pod):add regex support for pod name
- Loading branch information
1 parent
d144e09
commit ed35575
Showing
7 changed files
with
272 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ package filebeat | |
|
||
import ( | ||
"testing" | ||
) | ||
) | ||
|
||
var ( | ||
expectRenderResult = ` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package discovery | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type BlackWhiteList struct { | ||
bListNS string | ||
wListNS string | ||
bListPod string | ||
wListPod string | ||
} | ||
|
||
func (b *BlackWhiteList) IsResponsible(namespace string, pod string) (bool, error) { | ||
bListPodRegex, err := regexp.Compile(b.bListPod) | ||
if err != nil { | ||
return false, err | ||
} | ||
wListSVCRegex, err := regexp.Compile(b.wListPod) | ||
if err != nil { | ||
return false, err | ||
} | ||
podFilterKey := fmt.Sprintf("%v/%v", namespace, pod) | ||
if len(bListPodRegex.String()) > 0 { | ||
if match := bListPodRegex.MatchString(podFilterKey); match { | ||
return false, nil | ||
} | ||
} | ||
if len(wListSVCRegex.String()) > 0 { | ||
match := wListSVCRegex.MatchString(podFilterKey) | ||
return match || b.namespaceFilter(namespace), nil | ||
} | ||
return b.namespaceFilter(namespace), err | ||
} | ||
func (b *BlackWhiteList) namespaceFilter(namespace string) bool { | ||
bListNS := listToSet(parseList(b.bListNS)) | ||
wListNS := listToSet(parseList(b.wListNS)) | ||
if _, inBList := bListNS[namespace]; inBList { | ||
return false | ||
} | ||
if len(b.wListNS) > 0 { | ||
_, inWList := wListNS[namespace] | ||
return inWList | ||
} | ||
return true | ||
} | ||
func parseList(raw string) []string { | ||
if raw == "" { | ||
return nil | ||
} | ||
splitted := strings.Split(raw, ",") | ||
for i := range splitted { | ||
splitted[i] = strings.Trim(splitted[i], " \n\t") | ||
} | ||
return splitted | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package discovery | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestBlackWhiteList_IsResponsible(t *testing.T) { | ||
type fields struct { | ||
bListNS string | ||
wListNS string | ||
bListPod string | ||
wListPod string | ||
} | ||
type args struct { | ||
namespace string | ||
pod string | ||
} | ||
|
||
const ( | ||
defaultNS = "default" | ||
kubeNS = "kube-system" | ||
defaultAndKubeNS = "default,kube-system" | ||
PodListRegex = "^kube-system/(lb-.+|apiserver-proxy-nginx-preset-.+)$" | ||
lbPodRegex = "^kube-system/(lb-3160836842-proxy-nginx-eqygq-cfb79fccf-s5zz5.*)$" | ||
apiServerPodName = "apiserver-proxy-nginx-preset-7db45cfddb-bsk97" | ||
lbPodName = "lb-3160836842-proxy-nginx-eqygq-cfb79fccf-s5zz5" | ||
) | ||
|
||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "priority in blacklist and whitelist pod", | ||
fields: fields{ | ||
bListNS: kubeNS, | ||
wListNS: defaultNS, | ||
bListPod: lbPodRegex, | ||
wListPod: PodListRegex, | ||
}, | ||
args: args{ | ||
namespace: kubeNS, | ||
pod: lbPodName, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "no pod name", | ||
fields: fields{ | ||
bListNS: kubeNS, | ||
wListPod: PodListRegex, | ||
}, | ||
args: args{ | ||
namespace: kubeNS, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "priority in whitelist pod and blacklist namespace\"", | ||
fields: fields{ | ||
bListNS: defaultAndKubeNS, | ||
wListPod: PodListRegex, | ||
}, | ||
args: args{ | ||
namespace: kubeNS, | ||
pod: apiServerPodName, | ||
}, | ||
want: true, | ||
}, | ||
|
||
{ | ||
name: "priority in blacklist pod and whitelist namespace", | ||
fields: fields{ | ||
bListNS: defaultNS, | ||
wListNS: kubeNS, | ||
bListPod: PodListRegex, | ||
}, | ||
args: args{ | ||
namespace: kubeNS, | ||
pod: apiServerPodName, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "in the whitelist, not in the blacklist namespace", | ||
fields: fields{ | ||
bListNS: defaultNS, | ||
wListNS: defaultAndKubeNS, | ||
}, | ||
args: args{ | ||
namespace: kubeNS, | ||
pod: lbPodName, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "in the whitelist, not in the blacklist namespace", | ||
fields: fields{ | ||
bListNS: defaultNS, | ||
wListNS: defaultAndKubeNS, | ||
}, | ||
args: args{ | ||
namespace: defaultNS, | ||
pod: lbPodName, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "in the blacklist, not in the whitelist namespace", | ||
fields: fields{ | ||
bListNS: kubeNS, | ||
wListNS: defaultNS, | ||
}, | ||
args: args{ | ||
namespace: kubeNS, | ||
pod: lbPodName, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "both in the blacklist and the whitelist namespace", | ||
fields: fields{ | ||
bListNS: kubeNS, | ||
wListNS: defaultAndKubeNS, | ||
}, | ||
args: args{ | ||
namespace: kubeNS, | ||
pod: lbPodName, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "empty whitelist namespace", | ||
fields: fields{ | ||
bListNS: defaultNS, | ||
}, | ||
args: args{ | ||
namespace: kubeNS, | ||
pod: lbPodName, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "empty blacklist and whitelist namespace", | ||
args: args{ | ||
namespace: kubeNS, | ||
pod: lbPodName, | ||
}, | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
b := &BlackWhiteList{ | ||
bListNS: tt.fields.bListNS, | ||
wListNS: tt.fields.wListNS, | ||
bListPod: tt.fields.bListPod, | ||
wListPod: tt.fields.wListPod, | ||
} | ||
got, err := b.IsResponsible(tt.args.namespace, tt.args.pod) | ||
if err != nil { | ||
t.Errorf("IsResponsible() error = %v", err) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("IsResponsible() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters