Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
fc5235ae
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看板
提交
fc5235ae
编写于
11月 06, 2020
作者:
R
Roland.Ma
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine groupbinding controller
Signed-off-by:
N
Roland.Ma
<
rolandma@yunify.com
>
上级
80f3db3d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
45 addition
and
47 deletion
+45
-47
pkg/controller/groupbinding/groupbinding_controller.go
pkg/controller/groupbinding/groupbinding_controller.go
+45
-47
未找到文件。
pkg/controller/groupbinding/groupbinding_controller.go
浏览文件 @
fc5235ae
...
...
@@ -226,7 +226,7 @@ func (c *Controller) reconcile(key string) error {
}
else
{
// The object is being deleted
if
sliceutil
.
HasString
(
groupBinding
.
ObjectMeta
.
Finalizers
,
finalizer
)
{
if
err
=
c
.
bindUser
(
groupBinding
);
err
!=
nil
{
if
err
=
c
.
un
bindUser
(
groupBinding
);
err
!=
nil
{
klog
.
Error
(
err
)
return
err
}
...
...
@@ -257,67 +257,65 @@ func (c *Controller) Start(stopCh <-chan struct{}) error {
return
c
.
Run
(
4
,
stopCh
)
}
// Udpate user's Group property. So no need to query user's groups when authorizing.
func
(
c
*
Controller
)
unbindUser
(
groupBinding
*
iamv1alpha2
.
GroupBinding
)
error
{
return
c
.
updateUserGroups
(
groupBinding
,
func
(
groups
[]
string
,
group
string
)
(
bool
,
[]
string
)
{
// remove a group from the groups
if
sliceutil
.
HasString
(
groups
,
group
)
{
groups
:=
sliceutil
.
RemoveString
(
groups
,
func
(
item
string
)
bool
{
return
item
==
group
})
return
true
,
groups
}
return
false
,
groups
})
}
func
(
c
*
Controller
)
bindUser
(
groupBinding
*
iamv1alpha2
.
GroupBinding
)
error
{
return
c
.
updateUserGroups
(
groupBinding
,
func
(
groups
[]
string
,
group
string
)
(
bool
,
[]
string
)
{
// add group to the groups
if
!
sliceutil
.
HasString
(
groups
,
group
)
{
groups
:=
append
(
groups
,
group
)
return
true
,
groups
}
return
false
,
groups
})
}
// Udpate user's Group property. So no need to query user's groups when authorizing.
func
(
c
*
Controller
)
updateUserGroups
(
groupBinding
*
iamv1alpha2
.
GroupBinding
,
operator
func
(
groups
[]
string
,
group
string
)
(
bool
,
[]
string
))
error
{
users
:=
make
([]
string
,
0
)
// Ignore the user if the user if being deleted.
for
_
,
u
:=
range
groupBinding
.
Users
{
// Ignore the user if the user if being deleted.
if
user
,
err
:=
c
.
ksClient
.
IamV1alpha2
()
.
Users
()
.
Get
(
u
,
metav1
.
GetOptions
{});
err
==
nil
&&
user
.
ObjectMeta
.
DeletionTimestamp
.
IsZero
()
{
users
=
append
(
users
,
u
)
}
}
// Nothing to do
if
len
(
users
)
==
0
{
return
nil
}
if
errors
.
IsNotFound
(
err
)
{
klog
.
Infof
(
"user %s doesn't exist any more"
,
u
)
continue
}
// Get all GroupBindings and check whether user exists in the Group.
listOptions
:=
metav1
.
ListOptions
{}
groupBindingList
,
err
:=
c
.
ksClient
.
IamV1alpha2
()
.
GroupBindings
()
.
List
(
listOptions
)
if
err
!=
nil
{
klog
.
Error
(
err
)
return
err
}
if
changed
,
groups
:=
operator
(
user
.
Spec
.
Groups
,
groupBinding
.
GroupRef
.
Name
);
changed
{
userGroups
:=
make
(
map
[
string
][]
string
)
for
_
,
item
:=
range
groupBindingList
.
Items
{
if
item
.
ObjectMeta
.
DeletionTimestamp
.
IsZero
()
{
for
_
,
u
:=
range
users
{
if
sliceutil
.
HasString
(
item
.
Users
,
u
)
{
if
userGroups
[
u
]
==
nil
{
userGroups
[
u
]
=
make
([]
string
,
0
)
if
err
:=
c
.
patchUser
(
user
,
groups
);
err
!=
nil
{
if
errors
.
IsNotFound
(
err
)
{
klog
.
Infof
(
"user %s doesn't exist any more"
,
u
)
continue
}
userGroups
[
u
]
=
append
(
userGroups
[
u
],
item
.
GroupRef
.
Name
)
klog
.
Error
(
err
)
return
err
}
}
}
}
for
k
,
v
:=
range
userGroups
{
if
err
:=
c
.
patchUser
(
k
,
v
);
err
!=
nil
{
if
errors
.
IsNotFound
(
err
)
{
klog
.
Infof
(
"user %s doesn't exist any more"
,
k
)
return
nil
}
klog
.
Error
(
err
)
return
err
}
}
return
nil
}
func
(
c
*
Controller
)
patchUser
(
userName
string
,
groups
[]
string
)
error
{
if
user
,
err
:=
c
.
ksClient
.
IamV1alpha2
()
.
Users
()
.
Get
(
userName
,
metav1
.
GetOptions
{});
err
==
nil
&&
user
.
ObjectMeta
.
DeletionTimestamp
.
IsZero
()
{
newUser
:=
user
.
DeepCopy
()
newUser
.
Spec
.
Groups
=
groups
patch
:=
client
.
MergeFrom
(
user
)
patchData
,
_
:=
patch
.
Data
(
newUser
)
if
_
,
err
:=
c
.
ksClient
.
IamV1alpha2
()
.
Users
()
.
Patch
(
userName
,
patch
.
Type
(),
patchData
);
err
!=
nil
{
return
err
}
}
else
{
func
(
c
*
Controller
)
patchUser
(
user
*
iamv1alpha2
.
User
,
groups
[]
string
)
error
{
newUser
:=
user
.
DeepCopy
()
newUser
.
Spec
.
Groups
=
groups
patch
:=
client
.
MergeFrom
(
user
)
patchData
,
_
:=
patch
.
Data
(
newUser
)
if
_
,
err
:=
c
.
ksClient
.
IamV1alpha2
()
.
Users
()
.
Patch
(
user
.
Name
,
patch
.
Type
(),
patchData
);
err
!=
nil
{
return
err
}
return
nil
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录