Merge pull request #989 from JarHMJ/feature/namespace-filter-rule

feature: change namespace filter rule
This commit is contained in:
karmada-bot 2021-12-01 19:48:09 +08:00 committed by GitHub
commit bbf113adeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 24 deletions

View File

@ -99,7 +99,7 @@ func (o *Options) AddFlags(flags *pflag.FlagSet) {
"<group>/<version> for skip resources with a specific API version(e.g. networking.k8s.io/v1beta1),\n"+ "<group>/<version> for skip resources with a specific API version(e.g. networking.k8s.io/v1beta1),\n"+
"<group>/<version>/<kind>,<kind> for skip one or more specific resource(e.g. networking.k8s.io/v1beta1/Ingress,IngressClass) where the kinds are case-insensitive.") "<group>/<version>/<kind>,<kind> for skip one or more specific resource(e.g. networking.k8s.io/v1beta1/Ingress,IngressClass) where the kinds are case-insensitive.")
flags.StringSliceVar(&o.SkippedPropagatingNamespaces, "skipped-propagating-namespaces", []string{}, flags.StringSliceVar(&o.SkippedPropagatingNamespaces, "skipped-propagating-namespaces", []string{},
"Comma-separated namespaces that should be skipped from propagating in addition to the default skipped namespaces(namespaces prefixed by kube- and karmada-).") "Comma-separated namespaces that should be skipped from propagating in addition to the default skipped namespaces(karmada-system, karmada-cluster, namespaces prefixed by kube- and karmada-es-).")
flags.StringVar(&o.ClusterAPIContext, "cluster-api-context", "", "Name of the cluster context in cluster-api management cluster kubeconfig file.") flags.StringVar(&o.ClusterAPIContext, "cluster-api-context", "", "Name of the cluster context in cluster-api management cluster kubeconfig file.")
flags.StringVar(&o.ClusterAPIKubeconfig, "cluster-api-kubeconfig", "", "Path to the cluster-api management cluster kubeconfig file.") flags.StringVar(&o.ClusterAPIKubeconfig, "cluster-api-kubeconfig", "", "Path to the cluster-api management cluster kubeconfig file.")
flags.Float32Var(&o.ClusterAPIQPS, "cluster-api-qps", 40.0, "QPS to use while talking with cluster kube-apiserver. Doesn't cover events and node heartbeat apis which rate limiting is controlled by a different set of flags.") flags.Float32Var(&o.ClusterAPIQPS, "cluster-api-qps", 40.0, "QPS to use while talking with cluster kube-apiserver. Doesn't cover events and node heartbeat apis which rate limiting is controlled by a different set of flags.")

View File

@ -2,7 +2,6 @@ package namespace
import ( import (
"context" "context"
"strings"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
@ -30,12 +29,7 @@ import (
const ( const (
// ControllerName is the controller name that will be used when reporting events. // ControllerName is the controller name that will be used when reporting events.
ControllerName = "namespace-sync-controller" ControllerName = "namespace-sync-controller"
namespaceKarmadaSystem = "karmada-system"
namespaceKarmadaCluster = "karmada-cluster"
namespaceDefault = "default"
karmadaExecutionSpacePrefix = "karmada-es-"
kubeSystemNamespacePrefix = "kube-"
) )
// Controller is to sync Work. // Controller is to sync Work.
@ -86,8 +80,7 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques
} }
func (c *Controller) namespaceShouldBeSynced(namespace string) bool { func (c *Controller) namespaceShouldBeSynced(namespace string) bool {
if namespace == namespaceKarmadaCluster || namespace == namespaceKarmadaSystem || namespace == namespaceDefault || if names.IsReservedNamespace(namespace) || namespace == names.NamespaceDefault {
strings.HasPrefix(namespace, karmadaExecutionSpacePrefix) || strings.HasPrefix(namespace, kubeSystemNamespacePrefix) {
return false return false
} }

View File

@ -3,7 +3,6 @@ package detector
import ( import (
"context" "context"
"fmt" "fmt"
"strings"
"sync" "sync"
"time" "time"
@ -283,8 +282,7 @@ func (d *ResourceDetector) EventFilter(obj interface{}) bool {
return false return false
} }
if strings.HasPrefix(clusterWideKey.Namespace, names.KubernetesReservedNSPrefix) || if names.IsReservedNamespace(clusterWideKey.Namespace) {
strings.HasPrefix(clusterWideKey.Namespace, names.KarmadaReservedNSPrefix) {
return false return false
} }

View File

@ -16,16 +16,16 @@ const (
// - kube-public // - kube-public
// - kube-node-lease // - kube-node-lease
KubernetesReservedNSPrefix = "kube-" KubernetesReservedNSPrefix = "kube-"
//NamespaceKarmadaSystem is reserved namespace
// KarmadaReservedNSPrefix is the prefix of namespace which reserved by Karmada system, such as: NamespaceKarmadaSystem = "karmada-system"
// - karmada-system //NamespaceKarmadaCluster is reserved namespace
// - karmada-cluster NamespaceKarmadaCluster = "karmada-cluster"
// - karmada-es-* //NamespaceDefault is reserved namespace
KarmadaReservedNSPrefix = "karmada-" NamespaceDefault = "default"
) )
// executionSpacePrefix is the prefix of execution space // ExecutionSpacePrefix is the prefix of execution space
const executionSpacePrefix = "karmada-es-" const ExecutionSpacePrefix = "karmada-es-"
// endpointSlicePrefix is the prefix of collected EndpointSlice from member clusters. // endpointSlicePrefix is the prefix of collected EndpointSlice from member clusters.
const endpointSlicePrefix = "imported" const endpointSlicePrefix = "imported"
@ -41,16 +41,16 @@ func GenerateExecutionSpaceName(clusterName string) (string, error) {
if clusterName == "" { if clusterName == "" {
return "", fmt.Errorf("the member cluster name is empty") return "", fmt.Errorf("the member cluster name is empty")
} }
executionSpace := executionSpacePrefix + clusterName executionSpace := ExecutionSpacePrefix + clusterName
return executionSpace, nil return executionSpace, nil
} }
// GetClusterName returns member cluster name for the given execution space // GetClusterName returns member cluster name for the given execution space
func GetClusterName(executionSpaceName string) (string, error) { func GetClusterName(executionSpaceName string) (string, error) {
if !strings.HasPrefix(executionSpaceName, executionSpacePrefix) { if !strings.HasPrefix(executionSpaceName, ExecutionSpacePrefix) {
return "", fmt.Errorf("the execution space name is in wrong format") return "", fmt.Errorf("the execution space name is in wrong format")
} }
return strings.TrimPrefix(executionSpaceName, executionSpacePrefix), nil return strings.TrimPrefix(executionSpaceName, ExecutionSpacePrefix), nil
} }
// GenerateBindingName will generate binding name by kind and name // GenerateBindingName will generate binding name by kind and name
@ -108,3 +108,11 @@ func GenerateDerivedServiceName(serviceName string) string {
func GenerateEstimatorServiceName(clusterName string) string { func GenerateEstimatorServiceName(clusterName string) string {
return fmt.Sprintf("%s-%s", estimatorServicePrefix, clusterName) return fmt.Sprintf("%s-%s", estimatorServicePrefix, clusterName)
} }
// IsReservedNamespace return whether it is a reserved namespace
func IsReservedNamespace(namespace string) bool {
return namespace == NamespaceKarmadaSystem ||
namespace == NamespaceKarmadaCluster ||
strings.HasPrefix(namespace, ExecutionSpacePrefix) ||
strings.HasPrefix(namespace, KubernetesReservedNSPrefix)
}