forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usercluster_controller_tests.go
92 lines (73 loc) · 3.95 KB
/
usercluster_controller_tests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
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 main
import (
"context"
"fmt"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/types"
rbacusercluster "github.com/kubermatic/kubermatic/api/pkg/controller/user-cluster-controller-manager/rbac"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
"github.com/kubermatic/kubermatic/api/pkg/resources"
appsv1 "k8s.io/api/apps/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/equality"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
func (r *testRunner) testUserclusterControllerRBAC(log *zap.SugaredLogger, cluster *kubermaticv1.Cluster, userClusterClient, seedClusterClient ctrlruntimeclient.Client) error {
log.Info("Testing user cluster RBAC controller")
ctx := context.Background()
clusterNamespace := fmt.Sprintf("cluster-%s", cluster.Name)
// check if usercluster-controller was deployed on seed cluster
deployment := &appsv1.Deployment{}
if err := seedClusterClient.Get(ctx, types.NamespacedName{Namespace: clusterNamespace, Name: resources.UserClusterControllerDeploymentName}, deployment); err != nil {
return fmt.Errorf("failed to get Deployment: %s, error: %v", resources.UserClusterControllerDeploymentName, err)
}
if deployment.Status.AvailableReplicas == 0 {
return fmt.Errorf("%s deployment is not ready", resources.UserClusterControllerDeploymentName)
}
// check user cluster resources: ClusterRoles and ClusterRoleBindings
for _, resourceName := range rbacResourceNames() {
log.Info("Getting a Cluster Role: ", resourceName)
clusterRole := &rbacv1.ClusterRole{}
if err := userClusterClient.Get(ctx, types.NamespacedName{Name: resourceName}, clusterRole); err != nil {
return fmt.Errorf("failed to get Cluster Role: %s, error: %v", clusterRole, err)
}
defaultClusterRole, err := rbacusercluster.GenerateRBACClusterRole(resourceName)
if err != nil {
return fmt.Errorf("failed to generate default Cluster Role: %s, error: %v", resourceName, err)
}
if !equality.Semantic.DeepEqual(clusterRole.Rules, defaultClusterRole.Rules) {
return fmt.Errorf("incorrect Cluster Role Rules were returned, got: %v, want: %v", clusterRole.Rules, defaultClusterRole.Rules)
}
log.Info("Getting a Cluster Role Binding: ", resourceName)
clusterRoleBinding := &rbacv1.ClusterRoleBinding{}
if err := userClusterClient.Get(ctx, types.NamespacedName{Name: resourceName}, clusterRoleBinding); err != nil {
return fmt.Errorf("failed to get Cluster Role Binding: %s, error: %v", resourceName, err)
}
defaultClusterRoleBinding, err := rbacusercluster.GenerateRBACClusterRoleBinding(resourceName)
if err != nil {
return fmt.Errorf("failed to generate default Cluster Role Binding: %s, error: %v", resourceName, err)
}
if !equality.Semantic.DeepEqual(clusterRoleBinding.RoleRef, defaultClusterRoleBinding.RoleRef) {
return fmt.Errorf("incorrect Cluster Role Binding RoleRef were returned, got: %v, want: %v", clusterRoleBinding.RoleRef, defaultClusterRoleBinding.RoleRef)
}
if !equality.Semantic.DeepEqual(clusterRoleBinding.Subjects, defaultClusterRoleBinding.Subjects) {
return fmt.Errorf("incorrect Cluster Role Binding Subjects were returned, got: %v, want: %v", clusterRoleBinding.Subjects, defaultClusterRoleBinding.Subjects)
}
}
return nil
}
func rbacResourceNames() []string {
return []string{rbacusercluster.ResourceOwnerName, rbacusercluster.ResourceEditorName, rbacusercluster.ResourceViewerName}
}