@@ -29,6 +29,7 @@ import (
2929 "sync/atomic"
3030 "time"
3131
32+ "github.com/dlclark/regexp2"
3233 "github.com/docker/docker/api/types/container"
3334 "github.com/docker/docker/api/types/events"
3435 "github.com/docker/docker/api/types/image"
@@ -74,13 +75,13 @@ type EnvConfigInfo struct {
7475
7576// K8SFilter used for find specific container
7677type K8SFilter struct {
77- NamespaceReg * regexp .Regexp
78- PodReg * regexp .Regexp
79- ContainerReg * regexp .Regexp
78+ NamespaceReg * regexp2 .Regexp
79+ PodReg * regexp2 .Regexp
80+ ContainerReg * regexp2 .Regexp
8081 IncludeLabels map [string ]string
8182 ExcludeLabels map [string ]string
82- IncludeLabelRegs map [string ]* regexp .Regexp
83- ExcludeLabelRegs map [string ]* regexp .Regexp
83+ IncludeLabelRegs map [string ]* regexp2 .Regexp
84+ ExcludeLabelRegs map [string ]* regexp2 .Regexp
8485 hashKey uint64
8586}
8687
@@ -90,21 +91,21 @@ func CreateK8SFilter(ns, pod, container string, includeK8sLabels, excludeK8sLabe
9091 var err error
9192 var hashStrBuilder strings.Builder
9293 if len (ns ) > 0 {
93- if filter .NamespaceReg , err = regexp .Compile (ns ); err != nil {
94+ if filter .NamespaceReg , err = regexp2 .Compile (ns , 0 ); err != nil {
9495 return nil , err
9596 }
9697 }
9798 hashStrBuilder .WriteString (ns )
9899 hashStrBuilder .WriteString ("$$$" )
99100 if len (pod ) > 0 {
100- if filter .PodReg , err = regexp .Compile (pod ); err != nil {
101+ if filter .PodReg , err = regexp2 .Compile (pod , 0 ); err != nil {
101102 return nil , err
102103 }
103104 }
104105 hashStrBuilder .WriteString (pod )
105106 hashStrBuilder .WriteString ("$$$" )
106107 if len (container ) > 0 {
107- if filter .ContainerReg , err = regexp .Compile (container ); err != nil {
108+ if filter .ContainerReg , err = regexp2 .Compile (container , 0 ); err != nil {
108109 return nil , err
109110 }
110111 }
@@ -227,14 +228,32 @@ func (info *K8SInfo) IsMatch(filter *K8SFilter) bool {
227228
228229// innerMatch ...
229230func (info * K8SInfo ) innerMatch (filter * K8SFilter ) bool {
230- if filter .NamespaceReg != nil && ! filter .NamespaceReg .MatchString (info .Namespace ) {
231- return false
231+ if filter .NamespaceReg != nil {
232+ matched , err := filter .NamespaceReg .MatchString (info .Namespace )
233+ if err != nil {
234+ logger .Debug (context .Background (), "namespace regex match error" , err , "namespace" , info .Namespace , "pattern" , filter .NamespaceReg .String ())
235+ }
236+ if ! matched {
237+ return false
238+ }
232239 }
233- if filter .PodReg != nil && ! filter .PodReg .MatchString (info .Pod ) {
234- return false
240+ if filter .PodReg != nil {
241+ matched , err := filter .PodReg .MatchString (info .Pod )
242+ if err != nil {
243+ logger .Debug (context .Background (), "pod regex match error" , err , "pod" , info .Pod , "pattern" , filter .PodReg .String ())
244+ }
245+ if ! matched {
246+ return false
247+ }
235248 }
236- if filter .ContainerReg != nil && ! filter .ContainerReg .MatchString (info .ContainerName ) {
237- return false
249+ if filter .ContainerReg != nil {
250+ matched , err := filter .ContainerReg .MatchString (info .ContainerName )
251+ if err != nil {
252+ logger .Debug (context .Background (), "container regex match error" , err , "container" , info .ContainerName , "pattern" , filter .ContainerReg .String ())
253+ }
254+ if ! matched {
255+ return false
256+ }
238257 }
239258 // if labels is nil, create an empty map
240259 if info .Labels == nil {
@@ -891,8 +910,8 @@ func (dc *ContainerCenter) setLastError(err error, msg string) {
891910
892911func isMapLabelsMatch (includeLabel map [string ]string ,
893912 excludeLabel map [string ]string ,
894- includeLabelRegex map [string ]* regexp .Regexp ,
895- excludeLabelRegex map [string ]* regexp .Regexp ,
913+ includeLabelRegex map [string ]* regexp2 .Regexp ,
914+ excludeLabelRegex map [string ]* regexp2 .Regexp ,
896915 labels map [string ]string ) bool {
897916 if len (includeLabel ) != 0 || len (includeLabelRegex ) != 0 {
898917 matchedFlag := false
@@ -905,9 +924,15 @@ func isMapLabelsMatch(includeLabel map[string]string,
905924 // if matched, do not need check regex
906925 if ! matchedFlag {
907926 for key , reg := range includeLabelRegex {
908- if dockerVal , ok := labels [key ]; ok && reg .MatchString (dockerVal ) {
909- matchedFlag = true
910- break
927+ if dockerVal , ok := labels [key ]; ok {
928+ matched , err := reg .MatchString (dockerVal )
929+ if err != nil {
930+ logger .Debug (context .Background (), "include label regex match error" , err , "key" , key , "value" , dockerVal , "pattern" , reg .String ())
931+ }
932+ if matched {
933+ matchedFlag = true
934+ break
935+ }
911936 }
912937 }
913938 }
@@ -922,24 +947,30 @@ func isMapLabelsMatch(includeLabel map[string]string,
922947 }
923948 }
924949 for key , reg := range excludeLabelRegex {
925- if dockerVal , ok := labels [key ]; ok && reg .MatchString (dockerVal ) {
926- return false
950+ if dockerVal , ok := labels [key ]; ok {
951+ matched , err := reg .MatchString (dockerVal )
952+ if err != nil {
953+ logger .Debug (context .Background (), "exclude label regex match error" , err , "key" , key , "value" , dockerVal , "pattern" , reg .String ())
954+ }
955+ if matched {
956+ return false
957+ }
927958 }
928959 }
929960 return true
930961}
931962
932963func isContainerLabelMatch (includeLabel map [string ]string ,
933964 excludeLabel map [string ]string ,
934- includeLabelRegex map [string ]* regexp .Regexp ,
935- excludeLabelRegex map [string ]* regexp .Regexp ,
965+ includeLabelRegex map [string ]* regexp2 .Regexp ,
966+ excludeLabelRegex map [string ]* regexp2 .Regexp ,
936967 info * DockerInfoDetail ) bool {
937968 return isMapLabelsMatch (includeLabel , excludeLabel , includeLabelRegex , excludeLabelRegex , info .ContainerInfo .Config .Labels )
938969}
939970
940971func isMathEnvItem (env string ,
941972 staticEnv map [string ]string ,
942- regexEnv map [string ]* regexp .Regexp ) bool {
973+ regexEnv map [string ]* regexp2 .Regexp ) bool {
943974 var envKey , envValue string
944975 splitArray := strings .SplitN (env , "=" , 2 )
945976 if len (splitArray ) < 2 {
@@ -956,17 +987,23 @@ func isMathEnvItem(env string,
956987 }
957988
958989 if len (regexEnv ) > 0 {
959- if reg , ok := regexEnv [envKey ]; ok && reg .MatchString (envValue ) {
960- return true
990+ if reg , ok := regexEnv [envKey ]; ok {
991+ matched , err := reg .MatchString (envValue )
992+ if err != nil {
993+ logger .Debug (context .Background (), "env regex match error" , err , "key" , envKey , "value" , envValue , "pattern" , reg .String ())
994+ }
995+ if matched {
996+ return true
997+ }
961998 }
962999 }
9631000 return false
9641001}
9651002
9661003func isContainerEnvMatch (includeEnv map [string ]string ,
9671004 excludeEnv map [string ]string ,
968- includeEnvRegex map [string ]* regexp .Regexp ,
969- excludeEnvRegex map [string ]* regexp .Regexp ,
1005+ includeEnvRegex map [string ]* regexp2 .Regexp ,
1006+ excludeEnvRegex map [string ]* regexp2 .Regexp ,
9701007 info * DockerInfoDetail ) bool {
9711008
9721009 if len (includeEnv ) != 0 || len (includeEnvRegex ) != 0 {
@@ -996,12 +1033,12 @@ func isContainerEnvMatch(includeEnv map[string]string,
9961033func (dc * ContainerCenter ) getAllAcceptedInfo (
9971034 includeLabel map [string ]string ,
9981035 excludeLabel map [string ]string ,
999- includeLabelRegex map [string ]* regexp .Regexp ,
1000- excludeLabelRegex map [string ]* regexp .Regexp ,
1036+ includeLabelRegex map [string ]* regexp2 .Regexp ,
1037+ excludeLabelRegex map [string ]* regexp2 .Regexp ,
10011038 includeEnv map [string ]string ,
10021039 excludeEnv map [string ]string ,
1003- includeEnvRegex map [string ]* regexp .Regexp ,
1004- excludeEnvRegex map [string ]* regexp .Regexp ,
1040+ includeEnvRegex map [string ]* regexp2 .Regexp ,
1041+ excludeEnvRegex map [string ]* regexp2 .Regexp ,
10051042 k8sFilter * K8SFilter ,
10061043) map [string ]* DockerInfoDetail {
10071044 containerMap := make (map [string ]* DockerInfoDetail )
@@ -1022,12 +1059,12 @@ func (dc *ContainerCenter) getAllAcceptedInfoV2(
10221059 matchList map [string ]* DockerInfoDetail ,
10231060 includeLabel map [string ]string ,
10241061 excludeLabel map [string ]string ,
1025- includeLabelRegex map [string ]* regexp .Regexp ,
1026- excludeLabelRegex map [string ]* regexp .Regexp ,
1062+ includeLabelRegex map [string ]* regexp2 .Regexp ,
1063+ excludeLabelRegex map [string ]* regexp2 .Regexp ,
10271064 includeEnv map [string ]string ,
10281065 excludeEnv map [string ]string ,
1029- includeEnvRegex map [string ]* regexp .Regexp ,
1030- excludeEnvRegex map [string ]* regexp .Regexp ,
1066+ includeEnvRegex map [string ]* regexp2 .Regexp ,
1067+ excludeEnvRegex map [string ]* regexp2 .Regexp ,
10311068 k8sFilter * K8SFilter ,
10321069) (newCount , delCount int , matchAddedList , matchDeletedList []string ) {
10331070
0 commit comments