Skip to content

Commit

Permalink
[release-3.3] Fix: when placement is empty return error (kubesphere#5218
Browse files Browse the repository at this point in the history
)

Fix: when placement is empty return error

Co-authored-by: Wenhao Zhou <[email protected]>
  • Loading branch information
ks-ci-bot and Wenhao Zhou authored Sep 15, 2022
1 parent c385dd9 commit 5b9c357
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions pkg/models/tenant/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"
"time"

"github.com/mitchellh/mapstructure"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -495,21 +496,37 @@ func (t *tenantOperator) PatchWorkspaceTemplate(user user.Info, workspace string

// If the request path is cluster, just collecting cluster name to set and continue to check cluster permission later.
// Or indicate that want to manage the workspace templates, so check if user has the permission to manage workspace templates.
if strings.HasPrefix(path, "/spec/placement/clusters/") {
if strings.HasPrefix(path, "/spec/placement") {
if patch.Kind() != "add" && patch.Kind() != "remove" {
err := errors.NewBadRequest("not support operation type")
klog.Error(err)
return nil, err
}
clusterValue := make(map[string]string)
clusterValue := make(map[string]interface{})
err := jsonpatchutil.GetValue(patch, &clusterValue)
if err != nil {
klog.Error(err)
return nil, err
}
if cName := clusterValue["name"]; cName != "" {
clusterNames.Insert(cName)

// if the placement is empty, the first patch need fill with "clusters" field.
if cName := clusterValue["name"]; cName != nil {
cn, ok := cName.(string)
if ok {
clusterNames.Insert(cn)
}
} else if cluster := clusterValue["clusters"]; cluster != nil {
clusterRefrences := []typesv1beta1.GenericClusterReference{}
err := mapstructure.Decode(cluster, &clusterRefrences)
if err != nil {
klog.Error(err)
return nil, err
}
for _, v := range clusterRefrences {
clusterNames.Insert(v.Name)
}
}

} else {
manageWorkspaceTemplateRequest = true
}
Expand Down

0 comments on commit 5b9c357

Please sign in to comment.