Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
eb216066
K
kubesphere
项目概览
水淹萌龙
/
kubesphere
与 Fork 源项目一致
Fork自
KubeSphere / kubesphere
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kubesphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
eb216066
编写于
7月 23, 2020
作者:
D
Duan Jiong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
networkpolicy: add new parameters allowedIngressNamespaces for user customization
Signed-off-by:
N
Duan Jiong
<
djduanjiong@gmail.com
>
上级
afcd0efe
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
47 addition
and
11 deletion
+47
-11
cmd/controller-manager/app/controllers.go
cmd/controller-manager/app/controllers.go
+4
-3
cmd/controller-manager/app/server.go
cmd/controller-manager/app/server.go
+1
-1
pkg/apiserver/config/config_test.go
pkg/apiserver/config/config_test.go
+3
-0
pkg/controller/network/nsnetworkpolicy/nsnetworkpolicy_controller.go
...ler/network/nsnetworkpolicy/nsnetworkpolicy_controller.go
+18
-3
pkg/controller/network/nsnetworkpolicy/nsnetworkpolicy_test.go
...ontroller/network/nsnetworkpolicy/nsnetworkpolicy_test.go
+11
-3
pkg/simple/client/network/options.go
pkg/simple/client/network/options.go
+10
-1
未找到文件。
cmd/controller-manager/app/controllers.go
浏览文件 @
eb216066
...
...
@@ -49,6 +49,7 @@ import (
"kubesphere.io/kubesphere/pkg/simple/client/devops"
"kubesphere.io/kubesphere/pkg/simple/client/k8s"
ldapclient
"kubesphere.io/kubesphere/pkg/simple/client/ldap"
"kubesphere.io/kubesphere/pkg/simple/client/network"
"kubesphere.io/kubesphere/pkg/simple/client/openpitrix"
"kubesphere.io/kubesphere/pkg/simple/client/s3"
"sigs.k8s.io/controller-runtime/pkg/manager"
...
...
@@ -65,7 +66,7 @@ func addControllers(
authenticationOptions
*
authoptions
.
AuthenticationOptions
,
openpitrixClient
openpitrix
.
Client
,
multiClusterEnabled
bool
,
network
PolicyEnabled
bool
,
network
Options
*
network
.
Options
,
serviceMeshEnabled
bool
,
kubectlImage
string
,
stopCh
<-
chan
struct
{})
error
{
...
...
@@ -267,7 +268,7 @@ func addControllers(
}
var
nsnpController
manager
.
Runnable
if
network
PolicyEnabled
{
if
network
Options
.
EnableNetworkPolicy
{
nsnpProvider
,
err
:=
provider
.
NewNsNetworkPolicyProvider
(
client
.
Kubernetes
(),
kubernetesInformer
.
Networking
()
.
V1
()
.
NetworkPolicies
())
if
err
!=
nil
{
return
err
...
...
@@ -279,7 +280,7 @@ func addControllers(
kubernetesInformer
.
Core
()
.
V1
()
.
Services
(),
kubernetesInformer
.
Core
()
.
V1
()
.
Nodes
(),
kubesphereInformer
.
Tenant
()
.
V1alpha1
()
.
Workspaces
(),
kubernetesInformer
.
Core
()
.
V1
()
.
Namespaces
(),
nsnpProvider
)
kubernetesInformer
.
Core
()
.
V1
()
.
Namespaces
(),
nsnpProvider
,
networkOptions
.
NSNPOptions
)
}
controllers
:=
map
[
string
]
manager
.
Runnable
{
...
...
cmd/controller-manager/app/server.go
浏览文件 @
eb216066
...
...
@@ -205,7 +205,7 @@ func Run(s *options.KubeSphereControllerManagerOptions, stopCh <-chan struct{})
s
.
AuthenticationOptions
,
openpitrixClient
,
s
.
MultiClusterOptions
.
Enable
,
s
.
NetworkOptions
.
EnableNetworkPolicy
,
s
.
NetworkOptions
,
servicemeshEnabled
,
s
.
AuthenticationOptions
.
KubectlImage
,
stopCh
);
err
!=
nil
{
klog
.
Fatalf
(
"unable to register controllers to the manager: %v"
,
err
)
...
...
pkg/apiserver/config/config_test.go
浏览文件 @
eb216066
...
...
@@ -102,6 +102,9 @@ func newTestConfig() (*Config, error) {
},
NetworkOptions
:
&
network
.
Options
{
EnableNetworkPolicy
:
true
,
NSNPOptions
:
network
.
NSNPOptions
{
AllowedIngressNamespaces
:
[]
string
{},
},
},
MonitoringOptions
:
&
prometheus
.
Options
{
Endpoint
:
"http://prometheus.kubesphere-monitoring-system.svc"
,
...
...
pkg/controller/network/nsnetworkpolicy/nsnetworkpolicy_controller.go
浏览文件 @
eb216066
...
...
@@ -29,6 +29,7 @@ import (
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/controller/network"
"kubesphere.io/kubesphere/pkg/controller/network/provider"
options
"kubesphere.io/kubesphere/pkg/simple/client/network"
)
const
(
...
...
@@ -77,6 +78,7 @@ type NSNetworkPolicyController struct {
namespaceInformerSynced
cache
.
InformerSynced
provider
provider
.
NsNetworkPolicyProvider
options
options
.
NSNPOptions
nsQueue
workqueue
.
RateLimitingInterface
nsnpQueue
workqueue
.
RateLimitingInterface
...
...
@@ -301,7 +303,7 @@ func (c *NSNetworkPolicyController) generateNodeRule() (netv1.NetworkPolicyIngre
return
rule
,
nil
}
func
generateNSNP
(
workspace
string
,
namespace
string
,
matchWorkspace
bool
)
*
netv1
.
NetworkPolicy
{
func
(
c
*
NSNetworkPolicyController
)
generateNSNP
(
workspace
string
,
namespace
string
,
matchWorkspace
bool
)
*
netv1
.
NetworkPolicy
{
policy
:=
&
netv1
.
NetworkPolicy
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
AnnotationNPNAME
,
...
...
@@ -328,6 +330,17 @@ func generateNSNP(workspace string, namespace string, matchWorkspace bool) *netv
policy
.
Spec
.
Ingress
[
0
]
.
From
[
0
]
.
NamespaceSelector
.
MatchLabels
[
constants
.
NamespaceLabelKey
]
=
namespace
}
for
_
,
allowedIngressNamespace
:=
range
c
.
options
.
AllowedIngressNamespaces
{
defaultAllowedIngress
:=
netv1
.
NetworkPolicyPeer
{
NamespaceSelector
:
&
metav1
.
LabelSelector
{
MatchLabels
:
map
[
string
]
string
{
constants
.
NamespaceLabelKey
:
allowedIngressNamespace
,
},
},
}
policy
.
Spec
.
Ingress
[
0
]
.
From
=
append
(
policy
.
Spec
.
Ingress
[
0
]
.
From
,
defaultAllowedIngress
)
}
return
policy
}
...
...
@@ -445,7 +458,7 @@ func (c *NSNetworkPolicyController) syncNs(key string) error {
}
}
policy
:=
generateNSNP
(
workspaceName
,
ns
.
Name
,
matchWorkspace
)
policy
:=
c
.
generateNSNP
(
workspaceName
,
ns
.
Name
,
matchWorkspace
)
if
shouldAddDNSRule
(
nsnpList
)
{
ruleDNS
,
err
:=
generateDNSRule
([]
string
{
DNSLocalIP
})
if
err
!=
nil
{
...
...
@@ -589,7 +602,8 @@ func NewNSNetworkPolicyController(
nodeInformer
v1
.
NodeInformer
,
workspaceInformer
workspace
.
WorkspaceInformer
,
namespaceInformer
v1
.
NamespaceInformer
,
policyProvider
provider
.
NsNetworkPolicyProvider
)
*
NSNetworkPolicyController
{
policyProvider
provider
.
NsNetworkPolicyProvider
,
options
options
.
NSNPOptions
)
*
NSNetworkPolicyController
{
controller
:=
&
NSNetworkPolicyController
{
client
:
client
,
...
...
@@ -607,6 +621,7 @@ func NewNSNetworkPolicyController(
provider
:
policyProvider
,
nsQueue
:
workqueue
.
NewNamedRateLimitingQueue
(
workqueue
.
DefaultControllerRateLimiter
(),
"namespace"
),
nsnpQueue
:
workqueue
.
NewNamedRateLimitingQueue
(
workqueue
.
DefaultControllerRateLimiter
(),
"namespacenp"
),
options
:
options
,
}
workspaceInformer
.
Informer
()
.
AddEventHandler
(
cache
.
ResourceEventHandlerFuncs
{
...
...
pkg/controller/network/nsnetworkpolicy/nsnetworkpolicy_test.go
浏览文件 @
eb216066
...
...
@@ -22,6 +22,7 @@ import (
workspaceinformer
"kubesphere.io/kubesphere/pkg/client/informers/externalversions/tenant/v1alpha1"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/pkg/controller/network/provider"
options
"kubesphere.io/kubesphere/pkg/simple/client/network"
)
var
(
...
...
@@ -48,6 +49,9 @@ spec:
- namespaceSelector:
matchLabels:
%s: %s
- namespaceSelector:
matchLabels:
"kubesphere.io/namespace" : "kubesphere-monitoring-system"
policyTypes:
- Ingress`
...
...
@@ -113,8 +117,12 @@ var _ = Describe("Nsnetworkpolicy", func() {
nodeInforemer
:=
kubeInformer
.
Core
()
.
V1
()
.
Nodes
()
workspaceInformer
:=
ksInformer
.
Tenant
()
.
V1alpha1
()
.
Workspaces
()
namespaceInformer
:=
kubeInformer
.
Core
()
.
V1
()
.
Namespaces
()
nsnpOptions
:=
options
.
NewNetworkOptions
()
nsnpOptions
.
NSNPOptions
.
AllowedIngressNamespaces
=
append
(
nsnpOptions
.
NSNPOptions
.
AllowedIngressNamespaces
,
"kubesphere-monitoring-system"
)
c
=
NewNSNetworkPolicyController
(
kubeClient
,
ksClient
.
NetworkV1alpha1
(),
nsnpInformer
,
serviceInformer
,
nodeInforemer
,
workspaceInformer
,
namespaceInformer
,
calicoProvider
)
c
=
NewNSNetworkPolicyController
(
kubeClient
,
ksClient
.
NetworkV1alpha1
(),
nsnpInformer
,
serviceInformer
,
nodeInforemer
,
workspaceInformer
,
namespaceInformer
,
calicoProvider
,
nsnpOptions
.
NSNPOptions
)
serviceObj
:=
&
corev1
.
Service
{}
Expect
(
StringToObject
(
serviceTmp
,
serviceObj
))
.
ShouldNot
(
HaveOccurred
())
...
...
@@ -158,7 +166,7 @@ var _ = Describe("Nsnetworkpolicy", func() {
obj
:=
&
netv1
.
NetworkPolicy
{}
Expect
(
StringToObject
(
objSrt
,
obj
))
.
ShouldNot
(
HaveOccurred
())
policy
:=
generateNSNP
(
"testworkspace"
,
"testns"
,
true
)
policy
:=
c
.
generateNSNP
(
"testworkspace"
,
"testns"
,
true
)
Expect
(
reflect
.
DeepEqual
(
obj
.
Spec
,
policy
.
Spec
))
.
To
(
BeTrue
())
})
...
...
@@ -167,7 +175,7 @@ var _ = Describe("Nsnetworkpolicy", func() {
obj
:=
&
netv1
.
NetworkPolicy
{}
Expect
(
StringToObject
(
objSrt
,
obj
))
.
ShouldNot
(
HaveOccurred
())
policy
:=
generateNSNP
(
"testworkspace"
,
"testns"
,
false
)
policy
:=
c
.
generateNSNP
(
"testworkspace"
,
"testns"
,
false
)
Expect
(
reflect
.
DeepEqual
(
obj
.
Spec
,
policy
.
Spec
))
.
To
(
BeTrue
())
})
...
...
pkg/simple/client/network/options.go
浏览文件 @
eb216066
...
...
@@ -2,14 +2,22 @@ package network
import
"github.com/spf13/pflag"
type
NSNPOptions
struct
{
AllowedIngressNamespaces
[]
string
`json:"allowedIngressNamespaces,omitempty" yaml:"allowedIngressNamespaces,omitempty"`
}
type
Options
struct
{
EnableNetworkPolicy
bool
`json:"enableNetworkPolicy,omitempty" yaml:"enableNetworkPolicy"`
EnableNetworkPolicy
bool
`json:"enableNetworkPolicy,omitempty" yaml:"enableNetworkPolicy"`
NSNPOptions
NSNPOptions
`json:"nsnpOptions,omitempty" yaml:"nsnpOptions,omitempty"`
}
// NewNetworkOptions returns a `zero` instance
func
NewNetworkOptions
()
*
Options
{
return
&
Options
{
EnableNetworkPolicy
:
false
,
NSNPOptions
:
NSNPOptions
{
AllowedIngressNamespaces
:
[]
string
{},
},
}
}
...
...
@@ -20,6 +28,7 @@ func (s *Options) Validate() []error {
func
(
s
*
Options
)
ApplyTo
(
options
*
Options
)
{
options
.
EnableNetworkPolicy
=
s
.
EnableNetworkPolicy
options
.
NSNPOptions
=
s
.
NSNPOptions
}
func
(
s
*
Options
)
AddFlags
(
fs
*
pflag
.
FlagSet
,
c
*
Options
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录