-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
391dec0
commit 90ef4f6
Showing
6 changed files
with
211 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ rules: | |
- authentication.liqo.io | ||
resources: | ||
- resourceslices | ||
- tenants | ||
verbs: | ||
- get | ||
- list | ||
|
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,16 @@ | ||
// Copyright 2019-2025 The Liqo 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 tenant contains the logic of the Tenant webhook. | ||
package tenant |
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,179 @@ | ||
// Copyright 2019-2025 The Liqo 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 tenant | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
admissionv1 "k8s.io/api/admission/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/klog/v2" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
authv1beta1 "github.com/liqotech/liqo/apis/authentication/v1beta1" | ||
"github.com/liqotech/liqo/pkg/consts" | ||
"github.com/liqotech/liqo/pkg/liqoctl/output" | ||
) | ||
|
||
// cluster-role | ||
// +kubebuilder:rbac:groups=authentication.liqo.io,resources=tenants,verbs=get;list;watch; | ||
// +kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list; | ||
|
||
type tenantDecoder struct { | ||
decoder admission.Decoder | ||
} | ||
|
||
type tenantWebhookHandler struct { | ||
client client.Client | ||
tenantDecoder | ||
} | ||
|
||
// NewValidator returns a new Tenant validating webhook. | ||
func NewValidator(cl client.Client) *webhook.Admission { | ||
return &webhook.Admission{ | ||
Handler: &tenantWebhookHandler{ | ||
tenantDecoder: tenantDecoder{ | ||
decoder: admission.NewDecoder(runtime.NewScheme()), | ||
}, | ||
client: cl, | ||
}, | ||
} | ||
} | ||
|
||
// DecodeTenant decodes the Tenant from the incoming request. | ||
func (w *tenantDecoder) DecodeTenant(obj runtime.RawExtension) (*authv1beta1.Tenant, error) { | ||
var tenant authv1beta1.Tenant | ||
err := w.decoder.DecodeRaw(obj, &tenant) | ||
return &tenant, err | ||
} | ||
|
||
// Handle implements the Tenant validating webhook logic. | ||
func (w *tenantWebhookHandler) Handle(ctx context.Context, req admission.Request) admission.Response { | ||
switch req.Operation { | ||
case admissionv1.Create: | ||
return w.handleCreate(ctx, &req) | ||
case admissionv1.Update: | ||
return w.handleUpdate(ctx, &req) | ||
default: | ||
return admission.Allowed("") | ||
} | ||
} | ||
|
||
func (w *tenantWebhookHandler) handleCreate(ctx context.Context, req *admission.Request) admission.Response { | ||
tenant, err := w.DecodeTenant(req.Object) | ||
if err != nil { | ||
klog.Errorf("Failed decoding Tenant object: %v", err) | ||
return admission.Errored(http.StatusInternalServerError, err) | ||
} | ||
|
||
if status, err := w.tenantConsistencyChecks(ctx, tenant); err != nil { | ||
return admission.Errored(status, err) | ||
} | ||
|
||
// Check that there is one single Tenant in the tenant namespace. | ||
tenantsInNamespace, err := w.getTenants(ctx, tenant.Namespace, nil) | ||
if err != nil { | ||
werr := fmt.Errorf("Failed getting Tenants in tenant namespace: %v", output.PrettyErr(err)) | ||
klog.Error(werr) | ||
return admission.Errored(http.StatusInternalServerError, werr) | ||
} | ||
|
||
if len(tenantsInNamespace) > 0 { | ||
return admission.Denied("a Tenant already exists in the tenant namespace") | ||
} | ||
|
||
// Check that the Tenant name is unique in the entire cluster. | ||
tenantsInCluster, err := w.getTenants(ctx, corev1.NamespaceAll, &tenant.Name) | ||
if err != nil { | ||
werr := fmt.Errorf("Failed getting Tenants in cluster: %v", output.PrettyErr(err)) | ||
klog.Error(werr) | ||
return admission.Errored(http.StatusInternalServerError, werr) | ||
} | ||
|
||
if len(tenantsInCluster) > 0 { | ||
return admission.Denied("a Tenant with the same name already exists in the cluster") | ||
} | ||
|
||
return admission.Allowed("") | ||
} | ||
|
||
func (w *tenantWebhookHandler) handleUpdate(ctx context.Context, req *admission.Request) admission.Response { | ||
tenant, err := w.DecodeTenant(req.Object) | ||
if err != nil { | ||
klog.Errorf("Failed decoding Tenant object: %v", err) | ||
return admission.Errored(http.StatusInternalServerError, err) | ||
} | ||
|
||
if status, err := w.tenantConsistencyChecks(ctx, tenant); err != nil { | ||
return admission.Errored(status, err) | ||
} | ||
|
||
// Check that the Tenant name is unique in the tenant namespace. | ||
tenantsInCluster, err := w.getTenants(ctx, corev1.NamespaceAll, &tenant.Name) | ||
if err != nil { | ||
werr := fmt.Errorf("Failed getting Tenants in cluster: %v", output.PrettyErr(err)) | ||
klog.Error(werr) | ||
return admission.Errored(http.StatusInternalServerError, werr) | ||
} | ||
|
||
// If there already is a Tenant with the given name but it is not the same Tenant, deny the update. | ||
if len(tenantsInCluster) == 1 && tenantsInCluster[0].UID != tenant.UID { | ||
return admission.Denied("a Tenant with the same name already exists in the cluster") | ||
} | ||
|
||
return admission.Allowed("") | ||
} | ||
|
||
func (w *tenantWebhookHandler) tenantConsistencyChecks(ctx context.Context, tenant *authv1beta1.Tenant) (code int32, err error) { | ||
// Check that the Tenant has been created in the proper tenant namespace. | ||
var ns corev1.Namespace | ||
if err := w.client.Get(ctx, client.ObjectKey{Name: tenant.Namespace}, &ns); err != nil { | ||
werr := fmt.Errorf("Failed getting the tenant namespace: %v", output.PrettyErr(err)) | ||
return http.StatusInternalServerError, werr | ||
} | ||
|
||
if ns.Labels[consts.TenantNamespaceLabel] != "true" { | ||
return http.StatusForbidden, errors.New("the Tenant must be created in a tenant namespace") | ||
} | ||
|
||
// Check that the Tenant has the same cluster ID of its tenant namespace. | ||
if string(tenant.Spec.ClusterID) != ns.Labels[consts.RemoteClusterID] { | ||
return http.StatusForbidden, errors.New("the Tenant must have the same cluster ID of its tenant namespace") | ||
} | ||
|
||
return http.StatusOK, nil | ||
} | ||
|
||
func (w *tenantWebhookHandler) getTenants(ctx context.Context, namespace string, name *string) ([]authv1beta1.Tenant, error) { | ||
var tenantList authv1beta1.TenantList | ||
|
||
opts := []client.ListOption{ | ||
client.InNamespace(namespace), | ||
} | ||
if name != nil { | ||
opts = append(opts, client.MatchingFields{"metadata.name": *name}) | ||
} | ||
if err := w.client.List(ctx, &tenantList, opts...); err != nil { | ||
return nil, err | ||
} | ||
|
||
return tenantList.Items, nil | ||
} |