提交 e494cae0 编写于 作者: H hongming

fix panic

Signed-off-by: Nhongming <talonwan@yunify.com>
上级 bda48c24
...@@ -37,6 +37,8 @@ func (t *tenantOperator) ListDevOpsProjects(user user.Info, workspace string, qu ...@@ -37,6 +37,8 @@ func (t *tenantOperator) ListDevOpsProjects(user user.Info, workspace string, qu
scope := request.ClusterScope scope := request.ClusterScope
if workspace != "" { if workspace != "" {
scope = request.WorkspaceScope scope = request.WorkspaceScope
// filter by workspace
queryParam.Filters[query.FieldLabel] = query.Value(fmt.Sprintf("%s=%s", tenantv1alpha1.WorkspaceLabel, workspace))
} }
listDevOps := authorizer.AttributesRecord{ listDevOps := authorizer.AttributesRecord{
...@@ -54,12 +56,8 @@ func (t *tenantOperator) ListDevOpsProjects(user user.Info, workspace string, qu ...@@ -54,12 +56,8 @@ func (t *tenantOperator) ListDevOpsProjects(user user.Info, workspace string, qu
return nil, err return nil, err
} }
// allowed list devops in the specified scope // allowed list devops project in the specified scope
if decision == authorizer.DecisionAllow { if decision == authorizer.DecisionAllow {
// filter by workspace
if workspace != "" {
queryParam.Filters[query.FieldLabel] = query.Value(fmt.Sprintf("%s=%s", tenantv1alpha1.WorkspaceLabel, workspace))
}
result, err := t.resourceGetter.List(devopsv1alpha3.ResourcePluralDevOpsProject, "", queryParam) result, err := t.resourceGetter.List(devopsv1alpha3.ResourcePluralDevOpsProject, "", queryParam)
if err != nil { if err != nil {
klog.Error(err) klog.Error(err)
...@@ -74,15 +72,17 @@ func (t *tenantOperator) ListDevOpsProjects(user user.Info, workspace string, qu ...@@ -74,15 +72,17 @@ func (t *tenantOperator) ListDevOpsProjects(user user.Info, workspace string, qu
return nil, err return nil, err
} }
// list the devops projects that the user joined
devopsProjects := make([]runtime.Object, 0) devopsProjects := make([]runtime.Object, 0)
for _, roleBinding := range roleBindings { for _, roleBinding := range roleBindings {
// the namespace to which role binding belongs
obj, err := t.resourceGetter.Get("namespaces", "", roleBinding.Namespace) obj, err := t.resourceGetter.Get("namespaces", "", roleBinding.Namespace)
if err != nil { if err != nil {
klog.Error(err) klog.Error(err)
return nil, err return nil, err
} }
namespace := obj.(*corev1.Namespace)
controlledDevOpsProject := namespace.Labels[constants.DevOpsProjectLabelKey] controlledDevOpsProject := obj.(*corev1.Namespace).Labels[constants.DevOpsProjectLabelKey]
// skip if not controlled by devops project // skip if not controlled by devops project
if controlledDevOpsProject == "" { if controlledDevOpsProject == "" {
continue continue
...@@ -91,35 +91,25 @@ func (t *tenantOperator) ListDevOpsProjects(user user.Info, workspace string, qu ...@@ -91,35 +91,25 @@ func (t *tenantOperator) ListDevOpsProjects(user user.Info, workspace string, qu
devopsProject, err := t.resourceGetter.Get(devopsv1alpha3.ResourcePluralDevOpsProject, "", controlledDevOpsProject) devopsProject, err := t.resourceGetter.Get(devopsv1alpha3.ResourcePluralDevOpsProject, "", controlledDevOpsProject)
if err != nil { if err != nil {
if errors.IsNotFound(err) { if errors.IsNotFound(err) {
klog.Warning("orphan devops project found ", devopsProject) klog.Warningf("orphan devops project found: %s", roleBinding.Namespace)
continue continue
} }
klog.Error(err) klog.Error(err)
return nil, err return nil, err
} }
// skip if not controlled by the specified workspace // avoid duplication
if workspace != "" &&
devopsProject.(*devopsv1alpha3.DevOpsProject).Labels[tenantv1alpha1.WorkspaceLabel] != workspace {
continue
}
if !contains(devopsProjects, devopsProject) { if !contains(devopsProjects, devopsProject) {
devopsProjects = append(devopsProjects, namespace) devopsProjects = append(devopsProjects, devopsProject)
} }
} }
// devops project filtering
result := resources.DefaultList(devopsProjects, queryParam, func(left runtime.Object, right runtime.Object, field query.Field) bool { result := resources.DefaultList(devopsProjects, queryParam, func(left runtime.Object, right runtime.Object, field query.Field) bool {
return resources.DefaultObjectMetaCompare(left.(*devopsv1alpha3.DevOpsProject).ObjectMeta, right.(*devopsv1alpha3.DevOpsProject).ObjectMeta, field) return resources.DefaultObjectMetaCompare(left.(*devopsv1alpha3.DevOpsProject).ObjectMeta, right.(*devopsv1alpha3.DevOpsProject).ObjectMeta, field)
}, func(object runtime.Object, filter query.Filter) bool { }, func(object runtime.Object, filter query.Filter) bool {
devopsProject := object.(*devopsv1alpha3.DevOpsProject).ObjectMeta devopsProject := object.(*devopsv1alpha3.DevOpsProject)
if workspace != "" { return resources.DefaultObjectMetaFilter(devopsProject.ObjectMeta, filter)
if workspaceLabel, ok := devopsProject.Labels[tenantv1alpha1.WorkspaceLabel]; !ok || workspaceLabel != workspace {
return false
}
}
return resources.DefaultObjectMetaFilter(devopsProject, filter)
}) })
return result, nil return result, nil
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册