-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move GetNamespaceTenant logic to utils from webhook
- Loading branch information
Maksim Fedotov
committed
Sep 3, 2020
1 parent
9d845b3
commit 1666f21
Showing
2 changed files
with
99 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2020 Clastix Labs. | ||
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 utils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/clastix/capsule/api/v1alpha1" | ||
authenticationv1 "k8s.io/api/authentication/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type TenantNotFoundError struct{} | ||
|
||
func (t TenantNotFoundError) Error() string { | ||
return "You do not have any Tenant assigned: please, reach out the system administrators" | ||
} | ||
|
||
func GetNamespaceTenant(ctx context.Context, namespaceName string, forceTenantPrefix bool, userInfo authenticationv1.UserInfo, client client.Client) (*v1alpha1.Tenant, error) { | ||
if forceTenantPrefix { | ||
t := &v1alpha1.Tenant{} | ||
tenantName := strings.Split(namespaceName, "-")[0] | ||
if err := client.Get(ctx, types.NamespacedName{Name: tenantName}, t); err != nil { | ||
return nil, err | ||
} | ||
return t, nil | ||
} | ||
|
||
tl, err := listTenantsForOwner(ctx, "User", userInfo.Username, client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(tl.Items) > 0 { | ||
return &tl.Items[0], nil | ||
} | ||
|
||
if len(userInfo.Groups) > 0 { | ||
for _, group := range userInfo.Groups { | ||
tl, err := listTenantsForOwner(ctx, "Group", group, client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(tl.Items) > 0 { | ||
return &tl.Items[0], nil | ||
} | ||
} | ||
} | ||
return nil, &TenantNotFoundError{} | ||
} | ||
|
||
func IsTenantOwner(os v1alpha1.OwnerSpec, userInfo authenticationv1.UserInfo) bool { | ||
if os.Kind == "User" && userInfo.Username == os.Name { | ||
return true | ||
} | ||
if os.Kind == "Group" { | ||
for _, group := range userInfo.Groups { | ||
if group == os.Name { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func listTenantsForOwner(ctx context.Context, ownerKind string, ownerName string, clt client.Client) (*v1alpha1.TenantList, error) { | ||
tl := &v1alpha1.TenantList{} | ||
f := client.MatchingFields{ | ||
".spec.owner.ownerkind": fmt.Sprintf("%s:%s", ownerKind, ownerName), | ||
} | ||
err := clt.List(ctx, tl, f) | ||
return tl, err | ||
} |
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