-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add list/details for Role resource (#4480)
* add list/details for Role resource * Update apihandler.go * fix some error * fix golint error * fix golint * fix golint * fix golint * fix golint * fix golint * fix golint
- Loading branch information
1 parent
83f14ff
commit 20dd713
Showing
7 changed files
with
299 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2017 The Kubernetes Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package role | ||
|
||
import ( | ||
"github.com/kubernetes/dashboard/src/app/backend/resource/dataselect" | ||
) | ||
|
||
// The code below allows to perform complex data section on []Role | ||
|
||
type RoleCell Role | ||
|
||
func (self RoleCell) GetProperty(name dataselect.PropertyName) dataselect.ComparableValue { | ||
switch name { | ||
case dataselect.NameProperty: | ||
return dataselect.StdComparableString(self.ObjectMeta.Name) | ||
case dataselect.CreationTimestampProperty: | ||
return dataselect.StdComparableTime(self.ObjectMeta.CreationTimestamp.Time) | ||
case dataselect.NamespaceProperty: | ||
return dataselect.StdComparableString(self.ObjectMeta.Namespace) | ||
default: | ||
// if name is not supported then just return a constant dummy value, sort will have no effect. | ||
return nil | ||
} | ||
} | ||
|
||
func toCells(std []Role) []dataselect.DataCell { | ||
cells := make([]dataselect.DataCell, len(std)) | ||
for i := range std { | ||
cells[i] = RoleCell(std[i]) | ||
} | ||
return cells | ||
} | ||
|
||
func fromCells(cells []dataselect.DataCell) []Role { | ||
std := make([]Role, len(cells)) | ||
for i := range std { | ||
std[i] = Role(cells[i].(RoleCell)) | ||
} | ||
return std | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2017 The Kubernetes Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package role | ||
|
||
import ( | ||
rbac "k8s.io/api/rbac/v1" | ||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
k8sClient "k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// RoleDetail contains Cron Job details. | ||
type RoleDetail struct { | ||
// Extends list item structure. | ||
Role `json:",inline"` | ||
|
||
Rules []rbac.PolicyRule `json:"rules"` | ||
|
||
// List of non-critical errors, that occurred during resource retrieval. | ||
Errors []error `json:"errors"` | ||
} | ||
|
||
// GetRoleDetail gets Role details. | ||
func GetRoleDetail(client k8sClient.Interface, namespace, name string) (*RoleDetail, error) { | ||
rawObject, err := client.RbacV1().Roles(namespace).Get(name, metaV1.GetOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cr := toRoleDetail(*rawObject) | ||
return &cr, nil | ||
} | ||
|
||
func toRoleDetail(cr rbac.Role) RoleDetail { | ||
return RoleDetail{ | ||
Role: toRole(cr), | ||
Rules: cr.Rules, | ||
Errors: []error{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright 2017 The Kubernetes Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package role | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/kubernetes/dashboard/src/app/backend/api" | ||
"github.com/kubernetes/dashboard/src/app/backend/errors" | ||
"github.com/kubernetes/dashboard/src/app/backend/resource/common" | ||
"github.com/kubernetes/dashboard/src/app/backend/resource/dataselect" | ||
rbac "k8s.io/api/rbac/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// RoleList contains a list of role in the cluster. | ||
type RoleList struct { | ||
ListMeta api.ListMeta `json:"listMeta"` | ||
Items []Role `json:"items"` | ||
|
||
// List of non-critical errors, that occurred during resource retrieval. | ||
Errors []error `json:"errors"` | ||
} | ||
|
||
// Role is a presentation layer view of Kubernetes role. This means it is role plus additional | ||
// augmented data we can get from other sources. | ||
type Role struct { | ||
ObjectMeta api.ObjectMeta `json:"objectMeta"` | ||
TypeMeta api.TypeMeta `json:"typeMeta"` | ||
} | ||
|
||
// GetRoleList returns a list of all Roles in the cluster. | ||
func GetRoleList(client kubernetes.Interface, nsQuery *common.NamespaceQuery, dsQuery *dataselect.DataSelectQuery) (*RoleList, error) { | ||
log.Print("Getting list of all roles in the cluster") | ||
channels := &common.ResourceChannels{ | ||
RoleList: common.GetRoleListChannel(client, nsQuery, 1), | ||
} | ||
|
||
return GetRoleListFromChannels(channels, dsQuery) | ||
} | ||
|
||
// GetRoleListFromChannels returns a list of all Roles in the cluster | ||
// reading required resource list once from the channels. | ||
func GetRoleListFromChannels(channels *common.ResourceChannels, dsQuery *dataselect.DataSelectQuery) (*RoleList, error) { | ||
roles := <-channels.RoleList.List | ||
err := <-channels.RoleList.Error | ||
nonCriticalErrors, criticalError := errors.HandleError(err) | ||
if criticalError != nil { | ||
return nil, criticalError | ||
} | ||
deploymentList := toRoleList(roles.Items, nonCriticalErrors, dsQuery) | ||
return deploymentList, nil | ||
} | ||
|
||
func toRole(role rbac.Role) Role { | ||
return Role{ | ||
ObjectMeta: api.NewObjectMeta(role.ObjectMeta), | ||
TypeMeta: api.NewTypeMeta(api.ResourceKindRole), | ||
} | ||
} | ||
|
||
func toRoleList(roles []rbac.Role, nonCriticalErrors []error, dsQuery *dataselect.DataSelectQuery) *RoleList { | ||
result := &RoleList{ | ||
ListMeta: api.ListMeta{TotalItems: len(roles)}, | ||
Errors: nonCriticalErrors, | ||
} | ||
|
||
items := make([]Role, 0) | ||
for _, item := range roles { | ||
items = append(items, toRole(item)) | ||
} | ||
|
||
roleCells, filteredTotal := dataselect.GenericDataSelectWithFilter(toCells(items), dsQuery) | ||
result.ListMeta = api.ListMeta{TotalItems: filteredTotal} | ||
result.Items = fromCells(roleCells) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2017 The Kubernetes Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package role | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/kubernetes/dashboard/src/app/backend/api" | ||
"github.com/kubernetes/dashboard/src/app/backend/resource/dataselect" | ||
rbac "k8s.io/api/rbac/v1" | ||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestToRbacRoleLists(t *testing.T) { | ||
cases := []struct { | ||
Roles []rbac.Role | ||
expected *RoleList | ||
}{ | ||
{nil, &RoleList{Items: []Role{}}}, | ||
{ | ||
[]rbac.Role{ | ||
{ | ||
ObjectMeta: metaV1.ObjectMeta{Name: "role"}, | ||
Rules: []rbac.PolicyRule{{ | ||
Verbs: []string{"post", "put"}, | ||
Resources: []string{"pods", "deployments"}, | ||
}}, | ||
}, | ||
}, | ||
&RoleList{ | ||
ListMeta: api.ListMeta{TotalItems: 1}, | ||
Items: []Role{{ | ||
ObjectMeta: api.ObjectMeta{Name: "role", Namespace: ""}, | ||
TypeMeta: api.TypeMeta{Kind: api.ResourceKindRole}, | ||
}}, | ||
}, | ||
}, | ||
} | ||
for _, c := range cases { | ||
actual := toRoleList(c.Roles, nil, dataselect.NoDataSelect) | ||
if !reflect.DeepEqual(actual, c.expected) { | ||
t.Errorf("toRbacRoleLists(%#v) == \n%#v\nexpected \n%#v\n", | ||
c.Roles, actual, c.expected) | ||
} | ||
} | ||
} |