From 062b3f21b0c783115e199fdfdf4bf6adf1a81e1d Mon Sep 17 00:00:00 2001 From: dehaocheng Date: Mon, 7 Aug 2023 09:34:02 +0800 Subject: [PATCH] add regex Signed-off-by: dehaocheng --- config/bundle.yaml | 132 +++++++----------- ...on.kubesphere.io_notificationmanagers.yaml | 10 +- .../notification.kubesphere.io_receivers.yaml | 95 +++++-------- .../notification.kubesphere.io_routers.yaml | 18 +-- .../notification.kubesphere.io_silences.yaml | 9 +- helm/crds/bundle.yaml | 132 +++++++----------- pkg/apis/v2beta1/zz_generated.deepcopy.go | 1 - pkg/apis/v2beta2/common.go | 115 +++++++++++++++ pkg/apis/v2beta2/receiver_conversion.go | 75 +++++++--- pkg/apis/v2beta2/receiver_types.go | 38 ++--- pkg/apis/v2beta2/receiver_webhook.go | 6 +- pkg/apis/v2beta2/router_types.go | 12 +- pkg/apis/v2beta2/silence_types.go | 4 +- pkg/apis/v2beta2/zz_generated.deepcopy.go | 94 ++++++++++--- pkg/controller/controller.go | 8 +- pkg/filter/filter.go | 24 ++-- pkg/internal/common.go | 23 ++- pkg/internal/interface.go | 8 +- pkg/notify/notifier/dingtalk/throttle.go | 1 - pkg/route/router.go | 3 +- pkg/silence/silence.go | 7 +- pkg/utils/alert.go | 23 --- 22 files changed, 449 insertions(+), 389 deletions(-) create mode 100644 pkg/apis/v2beta2/common.go delete mode 100644 pkg/utils/alert.go diff --git a/config/bundle.yaml b/config/bundle.yaml index 480350f6..9c8c7ae3 100644 --- a/config/bundle.yaml +++ b/config/bundle.yaml @@ -4968,9 +4968,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that - relates the key and values. properties: key: description: key is the label key that the selector @@ -4981,6 +4978,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array @@ -5281,9 +5280,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that - relates the key and values. properties: key: description: key is the label key that the selector @@ -5294,6 +5290,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array @@ -9281,9 +9279,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9294,6 +9289,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9423,9 +9420,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9436,6 +9430,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9502,9 +9498,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9515,6 +9508,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9616,9 +9611,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9629,6 +9621,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9660,9 +9654,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9673,6 +9664,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9746,9 +9739,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9759,6 +9749,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9871,9 +9863,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9884,6 +9873,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9949,9 +9940,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9962,6 +9950,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10028,9 +10018,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10041,6 +10028,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10103,9 +10092,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10116,6 +10102,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10155,9 +10143,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10168,6 +10153,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10227,9 +10214,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10240,6 +10224,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10279,9 +10265,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10292,6 +10275,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10351,9 +10336,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10364,6 +10346,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10408,9 +10392,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10421,6 +10402,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10480,9 +10463,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10493,6 +10473,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10787,9 +10769,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10800,6 +10779,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10834,9 +10815,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10847,6 +10825,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10963,9 +10943,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10976,6 +10953,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -11055,18 +11034,11 @@ spec: description: RouterSpec defines the desired state of Router properties: alertSelector: - description: A label selector is a label query over a set of resources. - The result of matchLabels and matchExpressions are ANDed. An empty - label selector matches all objects. A null label selector matches - no objects. properties: matchExpressions: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector that - contains values, a key, and an operator that relates the key - and values. properties: key: description: key is the label key that the selector applies @@ -11077,6 +11049,8 @@ spec: a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. @@ -11129,18 +11103,11 @@ spec: regexName: type: string selector: - description: A label selector is a label query over a set of resources. - The result of matchLabels and matchExpressions are ANDed. An - empty label selector matches all objects. A null label selector - matches no objects. properties: matchExpressions: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -11151,6 +11118,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -11244,18 +11213,11 @@ spec: description: whether the silence is enabled type: boolean matcher: - description: A label selector is a label query over a set of resources. - The result of matchLabels and matchExpressions are ANDed. An empty - label selector matches all objects. A null label selector matches - no objects. properties: matchExpressions: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector that - contains values, a key, and an operator that relates the key - and values. properties: key: description: key is the label key that the selector applies @@ -11266,6 +11228,8 @@ spec: a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. diff --git a/config/crd/bases/notification.kubesphere.io_notificationmanagers.yaml b/config/crd/bases/notification.kubesphere.io_notificationmanagers.yaml index de3c0003..ef2b6c77 100644 --- a/config/crd/bases/notification.kubesphere.io_notificationmanagers.yaml +++ b/config/crd/bases/notification.kubesphere.io_notificationmanagers.yaml @@ -3790,9 +3790,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that - relates the key and values. properties: key: description: key is the label key that the selector @@ -3803,6 +3800,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array @@ -4103,9 +4102,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that - relates the key and values. properties: key: description: key is the label key that the selector @@ -4116,6 +4112,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array diff --git a/config/crd/bases/notification.kubesphere.io_receivers.yaml b/config/crd/bases/notification.kubesphere.io_receivers.yaml index 53c4efa9..60caa143 100644 --- a/config/crd/bases/notification.kubesphere.io_receivers.yaml +++ b/config/crd/bases/notification.kubesphere.io_receivers.yaml @@ -812,9 +812,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -825,6 +822,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -954,9 +953,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -967,6 +963,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1033,9 +1031,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1046,6 +1041,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1147,9 +1144,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1160,6 +1154,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1191,9 +1187,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1204,6 +1197,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1277,9 +1272,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1290,6 +1282,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1402,9 +1396,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1415,6 +1406,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1480,9 +1473,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1493,6 +1483,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1559,9 +1551,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1572,6 +1561,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1634,9 +1625,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1647,6 +1635,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1686,9 +1676,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1699,6 +1686,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1758,9 +1747,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1771,6 +1757,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1810,9 +1798,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1823,6 +1808,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1882,9 +1869,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1895,6 +1879,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -1939,9 +1925,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -1952,6 +1935,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -2011,9 +1996,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -2024,6 +2006,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -2318,9 +2302,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -2331,6 +2312,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -2365,9 +2348,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -2378,6 +2358,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -2494,9 +2476,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -2507,6 +2486,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must diff --git a/config/crd/bases/notification.kubesphere.io_routers.yaml b/config/crd/bases/notification.kubesphere.io_routers.yaml index f640d467..a0a4084a 100644 --- a/config/crd/bases/notification.kubesphere.io_routers.yaml +++ b/config/crd/bases/notification.kubesphere.io_routers.yaml @@ -39,18 +39,11 @@ spec: description: RouterSpec defines the desired state of Router properties: alertSelector: - description: A label selector is a label query over a set of resources. - The result of matchLabels and matchExpressions are ANDed. An empty - label selector matches all objects. A null label selector matches - no objects. properties: matchExpressions: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector that - contains values, a key, and an operator that relates the key - and values. properties: key: description: key is the label key that the selector applies @@ -61,6 +54,8 @@ spec: a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. @@ -113,18 +108,11 @@ spec: regexName: type: string selector: - description: A label selector is a label query over a set of resources. - The result of matchLabels and matchExpressions are ANDed. An - empty label selector matches all objects. A null label selector - matches no objects. properties: matchExpressions: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -135,6 +123,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must diff --git a/config/crd/bases/notification.kubesphere.io_silences.yaml b/config/crd/bases/notification.kubesphere.io_silences.yaml index a1fadf4a..f037ab59 100644 --- a/config/crd/bases/notification.kubesphere.io_silences.yaml +++ b/config/crd/bases/notification.kubesphere.io_silences.yaml @@ -46,18 +46,11 @@ spec: description: whether the silence is enabled type: boolean matcher: - description: A label selector is a label query over a set of resources. - The result of matchLabels and matchExpressions are ANDed. An empty - label selector matches all objects. A null label selector matches - no objects. properties: matchExpressions: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector that - contains values, a key, and an operator that relates the key - and values. properties: key: description: key is the label key that the selector applies @@ -68,6 +61,8 @@ spec: a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. diff --git a/helm/crds/bundle.yaml b/helm/crds/bundle.yaml index 7e83b248..fb6b9dbb 100644 --- a/helm/crds/bundle.yaml +++ b/helm/crds/bundle.yaml @@ -4968,9 +4968,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that - relates the key and values. properties: key: description: key is the label key that the selector @@ -4981,6 +4978,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array @@ -5281,9 +5280,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that - relates the key and values. properties: key: description: key is the label key that the selector @@ -5294,6 +5290,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array @@ -9281,9 +9279,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9294,6 +9289,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9423,9 +9420,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9436,6 +9430,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9502,9 +9498,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9515,6 +9508,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9616,9 +9611,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9629,6 +9621,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9660,9 +9654,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9673,6 +9664,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9746,9 +9739,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9759,6 +9749,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9871,9 +9863,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9884,6 +9873,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -9949,9 +9940,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -9962,6 +9950,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10028,9 +10018,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10041,6 +10028,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10103,9 +10092,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10116,6 +10102,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10155,9 +10143,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10168,6 +10153,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10227,9 +10214,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10240,6 +10224,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10279,9 +10265,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10292,6 +10275,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10351,9 +10336,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10364,6 +10346,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10408,9 +10392,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10421,6 +10402,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10480,9 +10463,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10493,6 +10473,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10787,9 +10769,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10800,6 +10779,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10834,9 +10815,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10847,6 +10825,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -10963,9 +10943,6 @@ spec: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -10976,6 +10953,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -11055,18 +11034,11 @@ spec: description: RouterSpec defines the desired state of Router properties: alertSelector: - description: A label selector is a label query over a set of resources. - The result of matchLabels and matchExpressions are ANDed. An empty - label selector matches all objects. A null label selector matches - no objects. properties: matchExpressions: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector that - contains values, a key, and an operator that relates the key - and values. properties: key: description: key is the label key that the selector applies @@ -11077,6 +11049,8 @@ spec: a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. @@ -11129,18 +11103,11 @@ spec: regexName: type: string selector: - description: A label selector is a label query over a set of resources. - The result of matchLabels and matchExpressions are ANDed. An - empty label selector matches all objects. A null label selector - matches no objects. properties: matchExpressions: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector - that contains values, a key, and an operator that relates - the key and values. properties: key: description: key is the label key that the selector @@ -11151,6 +11118,8 @@ spec: to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must @@ -11244,18 +11213,11 @@ spec: description: whether the silence is enabled type: boolean matcher: - description: A label selector is a label query over a set of resources. - The result of matchLabels and matchExpressions are ANDed. An empty - label selector matches all objects. A null label selector matches - no objects. properties: matchExpressions: description: matchExpressions is a list of label selector requirements. The requirements are ANDed. items: - description: A label selector requirement is a selector that - contains values, a key, and an operator that relates the key - and values. properties: key: description: key is the label key that the selector applies @@ -11266,6 +11228,8 @@ spec: a set of values. Valid operators are In, NotIn, Exists and DoesNotExist. type: string + regexValue: + type: string values: description: values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. diff --git a/pkg/apis/v2beta1/zz_generated.deepcopy.go b/pkg/apis/v2beta1/zz_generated.deepcopy.go index 76c0d3d7..e5003ae3 100644 --- a/pkg/apis/v2beta1/zz_generated.deepcopy.go +++ b/pkg/apis/v2beta1/zz_generated.deepcopy.go @@ -1,4 +1,3 @@ -//go:build !ignore_autogenerated // +build !ignore_autogenerated /* diff --git a/pkg/apis/v2beta2/common.go b/pkg/apis/v2beta2/common.go new file mode 100644 index 00000000..4482219b --- /dev/null +++ b/pkg/apis/v2beta2/common.go @@ -0,0 +1,115 @@ +package v2beta2 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" + "regexp" +) + +type LabelSelector struct { + // matchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels + // map is equivalent to an element of matchExpressions, whose key field is "key", the + // operator is "In", and the values array contains only "value". The requirements are ANDed. + // +optional + MatchLabels map[string]string `json:"matchLabels,omitempty" protobuf:"bytes,1,rep,name=matchLabels"` + // matchExpressions is a list of label selector requirements. The requirements are ANDed. + // +optional + MatchExpressions []LabelSelectorRequirement `json:"matchExpressions,omitempty" protobuf:"bytes,2,rep,name=matchExpressions"` +} + +type LabelSelectorRequirement struct { + // key is the label key that the selector applies to. + // +patchMergeKey=key + // +patchStrategy=merge + Key string `json:"key" patchStrategy:"merge" patchMergeKey:"key" protobuf:"bytes,1,opt,name=key"` + // operator represents a key's relationship to a set of values. + // Valid operators are In, NotIn, Exists and DoesNotExist. + Operator LabelSelectorOperator `json:"operator" protobuf:"bytes,2,opt,name=operator,casttype=LabelSelectorOperator"` + // values is an array of string values. If the operator is In or NotIn, + // the values array must be non-empty. If the operator is Exists or DoesNotExist, + // the values array must be empty. This array is replaced during a strategic + // merge patch. + // +optional + Values []string `json:"values,omitempty" protobuf:"bytes,3,rep,name=values"` + RegexValue string `json:"regexValue,omitempty" protobuf:"bytes,3,rep,name=regexValue"` +} + +// A label selector operator is the set of operators that can be used in a selector requirement. +type LabelSelectorOperator string + +const ( + LabelSelectorOpIn LabelSelectorOperator = "In" + LabelSelectorOpNotIn LabelSelectorOperator = "NotIn" + LabelSelectorOpExists LabelSelectorOperator = "Exists" + LabelSelectorOpDoesNotExist LabelSelectorOperator = "DoesNotExist" + LabelSelectorOpMatch LabelSelectorOperator = "Match" +) + +func (ls *LabelSelector) Matches(label map[string]string) (bool, error) { + var selector *metav1.LabelSelector + selector.MatchLabels = ls.MatchLabels + + for _, requirement := range ls.MatchExpressions { + if requirement.Operator != LabelSelectorOpMatch { + selector.MatchExpressions = append(selector.MatchExpressions, metav1.LabelSelectorRequirement{ + Key: requirement.Key, + Operator: metav1.LabelSelectorOperator(requirement.Operator), + Values: requirement.Values, + }) + } else { + if v, ok := label[requirement.Key]; ok { + match, err := regexp.MatchString(requirement.RegexValue, v) + if err != nil || !match { + return false, err + } + } + } + } + + sl, err := metav1.LabelSelectorAsSelector(selector) + if err != nil { + return false, err + } + + return sl.Matches(labels.Set(label)), nil +} + +func (ls *LabelSelector) Validate() error { + var selector *metav1.LabelSelector + selector.MatchLabels = ls.MatchLabels + + for _, requirement := range ls.MatchExpressions { + if requirement.Operator != LabelSelectorOpMatch { + selector.MatchExpressions = append(selector.MatchExpressions, metav1.LabelSelectorRequirement{ + Key: requirement.Key, + Operator: metav1.LabelSelectorOperator(requirement.Operator), + Values: requirement.Values, + }) + } else { + _, err := regexp.Compile(requirement.RegexValue) + if err != nil { + return err + } + } + } + + _, err := metav1.LabelSelectorAsSelector(selector) + if err != nil { + return err + } + + return nil +} + +func LabelMatchSelector(label map[string]string, selector *LabelSelector) bool { + + if selector == nil { + return true + } + ok, err := selector.Matches(label) + if err != nil { + return false + } + + return ok +} diff --git a/pkg/apis/v2beta2/receiver_conversion.go b/pkg/apis/v2beta2/receiver_conversion.go index 15314285..1e38cef9 100644 --- a/pkg/apis/v2beta2/receiver_conversion.go +++ b/pkg/apis/v2beta2/receiver_conversion.go @@ -18,6 +18,7 @@ package v2beta2 import ( "github.com/kubesphere/notification-manager/pkg/apis/v2beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/conversion" ) @@ -88,8 +89,8 @@ func (src *Receiver) convertDingTalkTo(dst *v2beta1.Receiver) error { dingtalk := src.Spec.DingTalk dst.Spec.DingTalk = &v2beta1.DingTalkReceiver{ Enabled: dingtalk.Enabled, - DingTalkConfigSelector: dingtalk.DingTalkConfigSelector, - AlertSelector: dingtalk.AlertSelector, + DingTalkConfigSelector: convertToNativeLabelSelector(dingtalk.DingTalkConfigSelector), + AlertSelector: convertToNativeLabelSelector(dingtalk.AlertSelector), } if dingtalk.Conversation != nil { @@ -119,8 +120,8 @@ func (src *Receiver) convertEmailTo(dst *v2beta1.Receiver) error { dst.Spec.Email = &v2beta1.EmailReceiver{ Enabled: email.Enabled, To: email.To, - EmailConfigSelector: email.EmailConfigSelector, - AlertSelector: email.AlertSelector, + EmailConfigSelector: convertToNativeLabelSelector(email.EmailConfigSelector), + AlertSelector: convertToNativeLabelSelector(email.AlertSelector), } return nil @@ -135,8 +136,8 @@ func (src *Receiver) convertSlackTo(dst *v2beta1.Receiver) error { slack := src.Spec.Slack dst.Spec.Slack = &v2beta1.SlackReceiver{ Enabled: slack.Enabled, - SlackConfigSelector: slack.SlackConfigSelector, - AlertSelector: slack.AlertSelector, + SlackConfigSelector: convertToNativeLabelSelector(slack.SlackConfigSelector), + AlertSelector: convertToNativeLabelSelector(slack.AlertSelector), Channels: slack.Channels, } @@ -152,8 +153,8 @@ func (src *Receiver) convertWebhookTo(dst *v2beta1.Receiver) error { webhook := src.Spec.Webhook dst.Spec.Webhook = &v2beta1.WebhookReceiver{ Enabled: webhook.Enabled, - WebhookConfigSelector: webhook.WebhookConfigSelector, - AlertSelector: webhook.AlertSelector, + WebhookConfigSelector: convertToNativeLabelSelector(webhook.WebhookConfigSelector), + AlertSelector: convertToNativeLabelSelector(webhook.AlertSelector), URL: webhook.URL, } @@ -194,8 +195,8 @@ func (src *Receiver) convertWechatTo(dst *v2beta1.Receiver) error { wechat := src.Spec.Wechat dst.Spec.Wechat = &v2beta1.WechatReceiver{ Enabled: wechat.Enabled, - WechatConfigSelector: wechat.WechatConfigSelector, - AlertSelector: wechat.AlertSelector, + WechatConfigSelector: convertToNativeLabelSelector(wechat.WechatConfigSelector), + AlertSelector: convertToNativeLabelSelector(wechat.AlertSelector), ToUser: wechat.ToUser, ToParty: wechat.ToParty, ToTag: wechat.ToTag, @@ -213,8 +214,8 @@ func (dst *Receiver) convertDingTalkFrom(src *v2beta1.Receiver) error { dingtalk := src.Spec.DingTalk dst.Spec.DingTalk = &DingTalkReceiver{ Enabled: dingtalk.Enabled, - DingTalkConfigSelector: dingtalk.DingTalkConfigSelector, - AlertSelector: dingtalk.AlertSelector, + DingTalkConfigSelector: convertToLabelSelector(dingtalk.DingTalkConfigSelector), + AlertSelector: convertToLabelSelector(dingtalk.AlertSelector), } if dingtalk.Conversation != nil { @@ -244,8 +245,8 @@ func (dst *Receiver) convertEmailFrom(src *v2beta1.Receiver) error { dst.Spec.Email = &EmailReceiver{ Enabled: email.Enabled, To: email.To, - EmailConfigSelector: email.EmailConfigSelector, - AlertSelector: email.AlertSelector, + EmailConfigSelector: convertToLabelSelector(email.EmailConfigSelector), + AlertSelector: convertToLabelSelector(email.AlertSelector), } return nil @@ -260,8 +261,8 @@ func (dst *Receiver) convertSlackFrom(src *v2beta1.Receiver) error { slack := src.Spec.Slack dst.Spec.Slack = &SlackReceiver{ Enabled: slack.Enabled, - SlackConfigSelector: slack.SlackConfigSelector, - AlertSelector: slack.AlertSelector, + SlackConfigSelector: convertToLabelSelector(slack.SlackConfigSelector), + AlertSelector: convertToLabelSelector(slack.AlertSelector), Channels: slack.Channels, } @@ -277,8 +278,8 @@ func (dst *Receiver) convertWebhookFrom(src *v2beta1.Receiver) error { webhook := src.Spec.Webhook dst.Spec.Webhook = &WebhookReceiver{ Enabled: webhook.Enabled, - WebhookConfigSelector: webhook.WebhookConfigSelector, - AlertSelector: webhook.AlertSelector, + WebhookConfigSelector: convertToLabelSelector(webhook.WebhookConfigSelector), + AlertSelector: convertToLabelSelector(webhook.AlertSelector), URL: webhook.URL, } @@ -319,8 +320,8 @@ func (dst *Receiver) convertWechatFrom(src *v2beta1.Receiver) error { wechat := src.Spec.Wechat dst.Spec.Wechat = &WechatReceiver{ Enabled: wechat.Enabled, - WechatConfigSelector: wechat.WechatConfigSelector, - AlertSelector: wechat.AlertSelector, + WechatConfigSelector: convertToLabelSelector(wechat.WechatConfigSelector), + AlertSelector: convertToLabelSelector(wechat.AlertSelector), ToUser: wechat.ToUser, ToParty: wechat.ToParty, ToTag: wechat.ToTag, @@ -328,3 +329,37 @@ func (dst *Receiver) convertWechatFrom(src *v2beta1.Receiver) error { return nil } + +func convertToLabelSelector(ls *metav1.LabelSelector) *LabelSelector { + var selector *LabelSelector + selector.MatchLabels = ls.MatchLabels + + for _, requirement := range ls.MatchExpressions { + selector.MatchExpressions = append(selector.MatchExpressions, LabelSelectorRequirement{ + Key: requirement.Key, + Operator: LabelSelectorOperator(requirement.Operator), + Values: requirement.Values, + }) + } + + return selector +} + +func convertToNativeLabelSelector(ls *LabelSelector) *metav1.LabelSelector { + var selector *metav1.LabelSelector + selector.MatchLabels = ls.MatchLabels + + for _, requirement := range ls.MatchExpressions { + if requirement.Operator == LabelSelectorOpMatch { + continue + } else { + selector.MatchExpressions = append(selector.MatchExpressions, metav1.LabelSelectorRequirement{ + Key: requirement.Key, + Operator: metav1.LabelSelectorOperator(requirement.Operator), + Values: requirement.Values, + }) + } + } + + return selector +} diff --git a/pkg/apis/v2beta2/receiver_types.go b/pkg/apis/v2beta2/receiver_types.go index eca9c04b..101e386a 100644 --- a/pkg/apis/v2beta2/receiver_types.go +++ b/pkg/apis/v2beta2/receiver_types.go @@ -47,9 +47,9 @@ type DingTalkReceiver struct { // whether the receiver is enabled Enabled *bool `json:"enabled,omitempty"` // DingTalkConfig to be selected for this receiver - DingTalkConfigSelector *metav1.LabelSelector `json:"dingtalkConfigSelector,omitempty"` + DingTalkConfigSelector *LabelSelector `json:"dingtalkConfigSelector,omitempty"` // Selector to filter alerts. - AlertSelector *metav1.LabelSelector `json:"alertSelector,omitempty"` + AlertSelector *LabelSelector `json:"alertSelector,omitempty"` // Be careful, a ChatBot only can send 20 message per minute. ChatBot *DingTalkChatBot `json:"chatbot,omitempty"` // The conversation which message will send to. @@ -71,9 +71,9 @@ type EmailReceiver struct { // Receivers' email addresses To []string `json:"to"` // EmailConfig to be selected for this receiver - EmailConfigSelector *metav1.LabelSelector `json:"emailConfigSelector,omitempty"` + EmailConfigSelector *LabelSelector `json:"emailConfigSelector,omitempty"` // Selector to filter alerts. - AlertSelector *metav1.LabelSelector `json:"alertSelector,omitempty"` + AlertSelector *LabelSelector `json:"alertSelector,omitempty"` // The name of the template to generate notification. // If the global template is not set, it will use default. Template *string `json:"template,omitempty"` @@ -89,9 +89,9 @@ type SlackReceiver struct { // whether the receiver is enabled Enabled *bool `json:"enabled,omitempty"` // SlackConfig to be selected for this receiver - SlackConfigSelector *metav1.LabelSelector `json:"slackConfigSelector,omitempty"` + SlackConfigSelector *LabelSelector `json:"slackConfigSelector,omitempty"` // Selector to filter alerts. - AlertSelector *metav1.LabelSelector `json:"alertSelector,omitempty"` + AlertSelector *LabelSelector `json:"alertSelector,omitempty"` // The channel or user to send notifications to. Channels []string `json:"channels"` // The name of the template to generate notification. @@ -131,9 +131,9 @@ type WebhookReceiver struct { // whether the receiver is enabled Enabled *bool `json:"enabled,omitempty"` // WebhookConfig to be selected for this receiver - WebhookConfigSelector *metav1.LabelSelector `json:"webhookConfigSelector,omitempty"` + WebhookConfigSelector *LabelSelector `json:"webhookConfigSelector,omitempty"` // Selector to filter alerts. - AlertSelector *metav1.LabelSelector `json:"alertSelector,omitempty"` + AlertSelector *LabelSelector `json:"alertSelector,omitempty"` // `url` gives the location of the webhook, in standard URL form // (`scheme://host:port/path`). Exactly one of `url` or `service` // must be specified. @@ -181,9 +181,9 @@ type WechatReceiver struct { // whether the receiver is enabled Enabled *bool `json:"enabled,omitempty"` // WechatConfig to be selected for this receiver - WechatConfigSelector *metav1.LabelSelector `json:"wechatConfigSelector,omitempty"` + WechatConfigSelector *LabelSelector `json:"wechatConfigSelector,omitempty"` // Selector to filter alerts. - AlertSelector *metav1.LabelSelector `json:"alertSelector,omitempty"` + AlertSelector *LabelSelector `json:"alertSelector,omitempty"` // +optional ToUser []string `json:"toUser,omitempty"` ToParty []string `json:"toParty,omitempty"` @@ -228,16 +228,16 @@ type DiscordReceiver struct { MentionedRoles []string `json:"mentionedRoles,omitempty"` // Selector to filter alerts. - AlertSelector *metav1.LabelSelector `json:"alertSelector,omitempty"` + AlertSelector *LabelSelector `json:"alertSelector,omitempty"` } type SmsReceiver struct { // whether the receiver is enabled Enabled *bool `json:"enabled,omitempty"` // SmsConfig to be selected for this receiver - SmsConfigSelector *metav1.LabelSelector `json:"smsConfigSelector,omitempty"` + SmsConfigSelector *LabelSelector `json:"smsConfigSelector,omitempty"` // Selector to filter alerts. - AlertSelector *metav1.LabelSelector `json:"alertSelector,omitempty"` + AlertSelector *LabelSelector `json:"alertSelector,omitempty"` // Receivers' phone numbers PhoneNumbers []string `json:"phoneNumbers"` // The name of the template to generate notification. @@ -267,9 +267,9 @@ type PushoverReceiver struct { // whether the receiver is enabled Enabled *bool `json:"enabled,omitempty"` // PushoverConfig to be selected for this receiver - PushoverConfigSelector *metav1.LabelSelector `json:"pushoverConfigSelector,omitempty"` + PushoverConfigSelector *LabelSelector `json:"pushoverConfigSelector,omitempty"` // Selector to filter alerts. - AlertSelector *metav1.LabelSelector `json:"alertSelector,omitempty"` + AlertSelector *LabelSelector `json:"alertSelector,omitempty"` // The name of the template to generate notification. // If the global template is not set, it will use default. Template *string `json:"template,omitempty"` @@ -297,9 +297,9 @@ type FeishuReceiver struct { // whether the receiver is enabled Enabled *bool `json:"enabled,omitempty"` // FeishuConfig to be selected for this receiver - FeishuConfigSelector *metav1.LabelSelector `json:"feishuConfigSelector,omitempty"` + FeishuConfigSelector *LabelSelector `json:"feishuConfigSelector,omitempty"` // Selector to filter alerts. - AlertSelector *metav1.LabelSelector `json:"alertSelector,omitempty"` + AlertSelector *LabelSelector `json:"alertSelector,omitempty"` // +optional // +kubebuilder:validation:MaxItems=200 User []string `json:"user,omitempty"` @@ -320,9 +320,9 @@ type TelegramReceiver struct { // whether the receiver is enabled Enabled *bool `json:"enabled,omitempty"` // TelegramConfigSelector to be selected for this receiver - TelegramConfigSelector *metav1.LabelSelector `json:"telegramConfigSelector,omitempty"` + TelegramConfigSelector *LabelSelector `json:"telegramConfigSelector,omitempty"` // Selector to filter alerts. - AlertSelector *metav1.LabelSelector `json:"alertSelector,omitempty"` + AlertSelector *LabelSelector `json:"alertSelector,omitempty"` // The channel or user to send notifications to. Channels []string `json:"channels"` //optional diff --git a/pkg/apis/v2beta2/receiver_webhook.go b/pkg/apis/v2beta2/receiver_webhook.go index 82c65977..9e817f2f 100644 --- a/pkg/apis/v2beta2/receiver_webhook.go +++ b/pkg/apis/v2beta2/receiver_webhook.go @@ -21,7 +21,6 @@ import ( "regexp" "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/validation/field" @@ -388,12 +387,11 @@ func (r *Receiver) validateReceiver() error { r.Name, allErrs) } -func validateSelector(selector *metav1.LabelSelector) error { +func validateSelector(selector *LabelSelector) error { if selector == nil { return nil } - _, err := metav1.LabelSelectorAsSelector(selector) - return err + return selector.Validate() } diff --git a/pkg/apis/v2beta2/router_types.go b/pkg/apis/v2beta2/router_types.go index e78d4e0b..47b874f0 100644 --- a/pkg/apis/v2beta2/router_types.go +++ b/pkg/apis/v2beta2/router_types.go @@ -27,10 +27,10 @@ type Channel struct { } type ReceiverSelector struct { - Name []string `json:"name,omitempty"` - RegexName string `json:"regexName,omitempty"` - Selector *metav1.LabelSelector `json:"selector,omitempty"` - Channels []Channel `json:"channels,omitempty"` + Name []string `json:"name,omitempty"` + RegexName string `json:"regexName,omitempty"` + Selector *LabelSelector `json:"selector,omitempty"` + Channels []Channel `json:"channels,omitempty"` // Receiver type, known values are dingtalk, email, slack, sms, pushover, webhook, wechat. Type string `json:"type,omitempty"` } @@ -38,8 +38,8 @@ type ReceiverSelector struct { // RouterSpec defines the desired state of Router type RouterSpec struct { // whether the router is enabled - Enabled *bool `json:"enabled,omitempty"` - AlertSelector *metav1.LabelSelector `json:"alertSelector"` + Enabled *bool `json:"enabled,omitempty"` + AlertSelector *LabelSelector `json:"alertSelector"` // Receivers which need to receive the matched alert. Receivers ReceiverSelector `json:"receivers"` } diff --git a/pkg/apis/v2beta2/silence_types.go b/pkg/apis/v2beta2/silence_types.go index c1e8af90..4ee5295f 100644 --- a/pkg/apis/v2beta2/silence_types.go +++ b/pkg/apis/v2beta2/silence_types.go @@ -27,8 +27,8 @@ import ( // SilenceSpec defines the desired state of Silence type SilenceSpec struct { // whether the silence is enabled - Enabled *bool `json:"enabled,omitempty"` - Matcher *metav1.LabelSelector `json:"matcher"` + Enabled *bool `json:"enabled,omitempty"` + Matcher *LabelSelector `json:"matcher"` // The start time during which the silence is active. // // +kubebuilder:validation:Format: date-time diff --git a/pkg/apis/v2beta2/zz_generated.deepcopy.go b/pkg/apis/v2beta2/zz_generated.deepcopy.go index 8d9f0be3..ff69e441 100644 --- a/pkg/apis/v2beta2/zz_generated.deepcopy.go +++ b/pkg/apis/v2beta2/zz_generated.deepcopy.go @@ -1,4 +1,3 @@ -//go:build !ignore_autogenerated // +build !ignore_autogenerated /* @@ -463,12 +462,12 @@ func (in *DingTalkReceiver) DeepCopyInto(out *DingTalkReceiver) { } if in.DingTalkConfigSelector != nil { in, out := &in.DingTalkConfigSelector, &out.DingTalkConfigSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.AlertSelector != nil { in, out := &in.AlertSelector, &out.AlertSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.ChatBot != nil { @@ -573,7 +572,7 @@ func (in *DiscordReceiver) DeepCopyInto(out *DiscordReceiver) { } if in.AlertSelector != nil { in, out := &in.AlertSelector, &out.AlertSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } } @@ -681,12 +680,12 @@ func (in *EmailReceiver) DeepCopyInto(out *EmailReceiver) { } if in.EmailConfigSelector != nil { in, out := &in.EmailConfigSelector, &out.EmailConfigSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.AlertSelector != nil { in, out := &in.AlertSelector, &out.AlertSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.Template != nil { @@ -806,12 +805,12 @@ func (in *FeishuReceiver) DeepCopyInto(out *FeishuReceiver) { } if in.FeishuConfigSelector != nil { in, out := &in.FeishuConfigSelector, &out.FeishuConfigSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.AlertSelector != nil { in, out := &in.AlertSelector, &out.AlertSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.User != nil { @@ -966,6 +965,55 @@ func (in *HuaweiSMS) DeepCopy() *HuaweiSMS { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *LabelSelector) DeepCopyInto(out *LabelSelector) { + *out = *in + if in.MatchLabels != nil { + in, out := &in.MatchLabels, &out.MatchLabels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.MatchExpressions != nil { + in, out := &in.MatchExpressions, &out.MatchExpressions + *out = make([]LabelSelectorRequirement, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new LabelSelector. +func (in *LabelSelector) DeepCopy() *LabelSelector { + if in == nil { + return nil + } + out := new(LabelSelector) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *LabelSelectorRequirement) DeepCopyInto(out *LabelSelectorRequirement) { + *out = *in + if in.Values != nil { + in, out := &in.Values, &out.Values + *out = make([]string, len(*in)) + copy(*out, *in) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new LabelSelectorRequirement. +func (in *LabelSelectorRequirement) DeepCopy() *LabelSelectorRequirement { + if in == nil { + return nil + } + out := new(LabelSelectorRequirement) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *NotificationManager) DeepCopyInto(out *NotificationManager) { *out = *in @@ -1319,12 +1367,12 @@ func (in *PushoverReceiver) DeepCopyInto(out *PushoverReceiver) { } if in.PushoverConfigSelector != nil { in, out := &in.PushoverConfigSelector, &out.PushoverConfigSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.AlertSelector != nil { in, out := &in.AlertSelector, &out.AlertSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.Template != nil { @@ -1469,7 +1517,7 @@ func (in *ReceiverSelector) DeepCopyInto(out *ReceiverSelector) { } if in.Selector != nil { in, out := &in.Selector, &out.Selector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.Channels != nil { @@ -1670,7 +1718,7 @@ func (in *RouterSpec) DeepCopyInto(out *RouterSpec) { } if in.AlertSelector != nil { in, out := &in.AlertSelector, &out.AlertSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } in.Receivers.DeepCopyInto(&out.Receivers) @@ -1835,7 +1883,7 @@ func (in *SilenceSpec) DeepCopyInto(out *SilenceSpec) { } if in.Matcher != nil { in, out := &in.Matcher, &out.Matcher - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.StartsAt != nil { @@ -1931,12 +1979,12 @@ func (in *SlackReceiver) DeepCopyInto(out *SlackReceiver) { } if in.SlackConfigSelector != nil { in, out := &in.SlackConfigSelector, &out.SlackConfigSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.AlertSelector != nil { in, out := &in.AlertSelector, &out.AlertSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.Channels != nil { @@ -2016,12 +2064,12 @@ func (in *SmsReceiver) DeepCopyInto(out *SmsReceiver) { } if in.SmsConfigSelector != nil { in, out := &in.SmsConfigSelector, &out.SmsConfigSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.AlertSelector != nil { in, out := &in.AlertSelector, &out.AlertSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.PhoneNumbers != nil { @@ -2133,12 +2181,12 @@ func (in *TelegramReceiver) DeepCopyInto(out *TelegramReceiver) { } if in.TelegramConfigSelector != nil { in, out := &in.TelegramConfigSelector, &out.TelegramConfigSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.AlertSelector != nil { in, out := &in.AlertSelector, &out.AlertSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.Channels != nil { @@ -2317,12 +2365,12 @@ func (in *WebhookReceiver) DeepCopyInto(out *WebhookReceiver) { } if in.WebhookConfigSelector != nil { in, out := &in.WebhookConfigSelector, &out.WebhookConfigSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.AlertSelector != nil { in, out := &in.AlertSelector, &out.AlertSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.URL != nil { @@ -2449,12 +2497,12 @@ func (in *WechatReceiver) DeepCopyInto(out *WechatReceiver) { } if in.WechatConfigSelector != nil { in, out := &in.WechatConfigSelector, &out.WechatConfigSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.AlertSelector != nil { in, out := &in.AlertSelector, &out.AlertSelector - *out = new(metav1.LabelSelector) + *out = new(LabelSelector) (*in).DeepCopyInto(*out) } if in.ToUser != nil { diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index ccc063a9..fde9b59e 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -577,11 +577,11 @@ func (c *Controller) tenantIDFromNs(namespace string) ([]string, error) { // It will return true when config is found. func getMatchedConfig(r internal.Receiver, configs map[string]map[string]internal.Config) bool { - match := func(configs map[string]internal.Config, selector *metav1.LabelSelector) bool { + match := func(configs map[string]internal.Config, selector *v2beta2.LabelSelector) bool { p := math.MaxInt32 for k, v := range configs { if strings.HasPrefix(k, r.GetType()) { - if utils.LabelMatchSelector(v.GetLabels(), selector) { + if v2beta2.LabelMatchSelector(v.GetLabels(), selector) { if v.Validate() == nil { if v.GetPriority() < p { r.SetConfig(v.Clone()) @@ -680,7 +680,7 @@ func (c *Controller) RcvsFromName(names []string, regexName, receiverType string return val.([]internal.Receiver) } -func (c *Controller) RcvsFromSelector(selector *metav1.LabelSelector, receiverType string) []internal.Receiver { +func (c *Controller) RcvsFromSelector(selector *v2beta2.LabelSelector, receiverType string) []internal.Receiver { t := &task{ run: func(t *task) { @@ -691,7 +691,7 @@ func (c *Controller) RcvsFromSelector(selector *metav1.LabelSelector, receiverTy continue } - if utils.LabelMatchSelector(v.GetLabels(), selector) { + if v2beta2.LabelMatchSelector(v.GetLabels(), selector) { if v.Enabled() { rcv := v.Clone() getMatchedConfig(rcv, c.configs) diff --git a/pkg/filter/filter.go b/pkg/filter/filter.go index 4158542d..c551c5f5 100644 --- a/pkg/filter/filter.go +++ b/pkg/filter/filter.go @@ -2,17 +2,14 @@ package filter import ( "context" - "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" + "github.com/kubesphere/notification-manager/pkg/apis/v2beta2" "github.com/kubesphere/notification-manager/pkg/controller" "github.com/kubesphere/notification-manager/pkg/internal" "github.com/kubesphere/notification-manager/pkg/stage" "github.com/kubesphere/notification-manager/pkg/template" - "github.com/kubesphere/notification-manager/pkg/utils" "github.com/modern-go/reflect2" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" ) type filterStage struct { @@ -73,7 +70,7 @@ func (s *filterStage) mute(ctx context.Context, alerts []*template.Alert, receiv continue } - if utils.LabelMatchSelector(alert.Labels, silence.Spec.Matcher) { + if v2beta2.LabelMatchSelector(alert.Labels, silence.Spec.Matcher) { flag = true break } @@ -88,24 +85,19 @@ func (s *filterStage) mute(ctx context.Context, alerts []*template.Alert, receiv } // FilterAlerts filter the alerts with label selector,if the selector is not correct, return all of the alerts. -func filter(alerts []*template.Alert, selector *v1.LabelSelector) ([]*template.Alert, error) { +func filter(alerts []*template.Alert, selector *v2beta2.LabelSelector) ([]*template.Alert, error) { if selector == nil { return alerts, nil } - labelSelector, err := v1.LabelSelectorAsSelector(selector) - if err != nil { - return alerts, err - } - - if labelSelector.Empty() { - return alerts, nil - } - var as []*template.Alert for _, alert := range alerts { - if labelSelector.Matches(labels.Set(alert.Labels)) { + ok, err := selector.Matches(alert.Labels) + if err != nil { + return nil, err + } + if ok { as = append(as, alert) } } diff --git a/pkg/internal/common.go b/pkg/internal/common.go index 529925f3..99576c8f 100644 --- a/pkg/internal/common.go +++ b/pkg/internal/common.go @@ -6,7 +6,6 @@ import ( "strconv" "github.com/kubesphere/notification-manager/pkg/apis/v2beta2" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) const ( @@ -21,15 +20,15 @@ type Template struct { } type Common struct { - Name string `json:"name,omitempty"` - ResourceVersion uint64 `json:"resourceVersion,omitempty"` - Type string `json:"type,omitempty"` - TenantID string `json:"tenantID,omitempty"` - Labels map[string]string `json:"labels,omitempty"` - Enable *bool `json:"enable,omitempty"` - AlertSelector *metav1.LabelSelector `json:"alertSelector,omitempty"` - ConfigSelector *metav1.LabelSelector `json:"configSelector,omitempty"` - Hash string `json:"hash,omitempty"` + Name string `json:"name,omitempty"` + ResourceVersion uint64 `json:"resourceVersion,omitempty"` + Type string `json:"type,omitempty"` + TenantID string `json:"tenantID,omitempty"` + Labels map[string]string `json:"labels,omitempty"` + Enable *bool `json:"enable,omitempty"` + AlertSelector *v2beta2.LabelSelector `json:"alertSelector,omitempty"` + ConfigSelector *v2beta2.LabelSelector `json:"configSelector,omitempty"` + Hash string `json:"hash,omitempty"` Template `json:"template,omitempty"` } @@ -74,11 +73,11 @@ func (c *Common) GetType() string { return c.Type } -func (c *Common) GetAlertSelector() *metav1.LabelSelector { +func (c *Common) GetAlertSelector() *v2beta2.LabelSelector { return c.AlertSelector } -func (c *Common) GetConfigSelector() *metav1.LabelSelector { +func (c *Common) GetConfigSelector() *v2beta2.LabelSelector { return c.ConfigSelector } diff --git a/pkg/internal/interface.go b/pkg/internal/interface.go index 86e19c48..8b50db92 100644 --- a/pkg/internal/interface.go +++ b/pkg/internal/interface.go @@ -1,6 +1,8 @@ package internal -import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +import ( + "github.com/kubesphere/notification-manager/pkg/apis/v2beta2" +) type Receiver interface { GetTenantID() string @@ -9,8 +11,8 @@ type Receiver interface { Enabled() bool GetType() string GetLabels() map[string]string - GetAlertSelector() *metav1.LabelSelector - GetConfigSelector() *metav1.LabelSelector + GetAlertSelector() *v2beta2.LabelSelector + GetConfigSelector() *v2beta2.LabelSelector SetConfig(c Config) Validate() error Clone() Receiver diff --git a/pkg/notify/notifier/dingtalk/throttle.go b/pkg/notify/notifier/dingtalk/throttle.go index e54fcfb0..e3181c56 100644 --- a/pkg/notify/notifier/dingtalk/throttle.go +++ b/pkg/notify/notifier/dingtalk/throttle.go @@ -101,7 +101,6 @@ func (t *Throttle) Get(url string) *rateLimiter { // The queue stores the time of the last (threshold - 1) call. // // The logic of flow control is that the time of any `threshold` consecutive calls cannot be greater than `unitTime`. -// func (t *Throttle) Allow(key string, logger log.Logger) bool { r := t.Get(key) diff --git a/pkg/route/router.go b/pkg/route/router.go index 1de9f3db..6b20df69 100644 --- a/pkg/route/router.go +++ b/pkg/route/router.go @@ -106,7 +106,8 @@ func (s *routeStage) rcvsFromRouter(alert *template.Alert, routers []v2beta2.Rou var rcvs []internal.Receiver for _, router := range routers { - if !utils.LabelMatchSelector(alert.Labels, router.Spec.AlertSelector) { + ok, _ := router.Spec.AlertSelector.Matches(alert.Labels) + if !ok { continue } diff --git a/pkg/silence/silence.go b/pkg/silence/silence.go index 6883e4a0..6675744e 100644 --- a/pkg/silence/silence.go +++ b/pkg/silence/silence.go @@ -8,7 +8,6 @@ import ( "github.com/kubesphere/notification-manager/pkg/controller" "github.com/kubesphere/notification-manager/pkg/stage" "github.com/kubesphere/notification-manager/pkg/template" - "github.com/kubesphere/notification-manager/pkg/utils" "github.com/modern-go/reflect2" ) @@ -45,7 +44,11 @@ func (s *silenceStage) Exec(ctx context.Context, l log.Logger, data interface{}) for _, alert := range input { mute := false for _, silence := range ss { - if utils.LabelMatchSelector(alert.Labels, silence.Spec.Matcher) { + ok, err := silence.Spec.Matcher.Matches(alert.Labels) + if err != nil { + return nil, nil, err + } + if ok { mute = true break } diff --git a/pkg/utils/alert.go b/pkg/utils/alert.go deleted file mode 100644 index ea8be706..00000000 --- a/pkg/utils/alert.go +++ /dev/null @@ -1,23 +0,0 @@ -package utils - -import ( - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" -) - -func LabelMatchSelector(label map[string]string, selector *v1.LabelSelector) bool { - - if selector == nil { - return true - } - - labelSelector, err := v1.LabelSelectorAsSelector(selector) - if err != nil { - return false - } - if labelSelector.Empty() { - return true - } - - return labelSelector.Matches(labels.Set(label)) -}