-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'anatolychernov/get_tenants'
- Loading branch information
Showing
12 changed files
with
176 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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/projectcapsule/capsule-proxy | ||
|
||
go 1.19 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/go-logr/logr v1.3.0 | ||
|
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,8 @@ | ||
// Copyright 2020-2023 Project Capsule Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tenants | ||
|
||
const ( | ||
basePath = "/apis/capsule.clastix.io/v1beta2/{endpoint:tenants/?}" | ||
) |
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,68 @@ | ||
// Copyright 2020-2023 Project Capsule Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tenants | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/gorilla/mux" | ||
capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/projectcapsule/capsule-proxy/internal/modules" | ||
"github.com/projectcapsule/capsule-proxy/internal/modules/errors" | ||
"github.com/projectcapsule/capsule-proxy/internal/request" | ||
"github.com/projectcapsule/capsule-proxy/internal/tenant" | ||
) | ||
|
||
type get struct { | ||
capsuleLabel string | ||
client client.Reader | ||
log logr.Logger | ||
gk schema.GroupKind | ||
} | ||
|
||
func Get(client client.Reader) modules.Module { | ||
label, _ := capsulev1beta2.GetTypeLabel(&capsulev1beta2.Tenant{}) | ||
|
||
return &get{ | ||
capsuleLabel: label, | ||
client: client, | ||
log: ctrl.Log.WithName("tenant_get"), | ||
gk: schema.GroupKind{ | ||
Group: corev1.GroupName, | ||
Kind: "tenants", | ||
}, | ||
} | ||
} | ||
|
||
func (l get) Path() string { | ||
return "/apis/capsule.clastix.io/v1beta2/tenants/{name}" | ||
} | ||
|
||
func (l get) Methods() []string { | ||
return []string{http.MethodGet} | ||
} | ||
|
||
func (l get) Handle(proxyTenants []*tenant.ProxyTenant, proxyRequest request.Request) (selector labels.Selector, err error) { | ||
name := mux.Vars(proxyRequest.GetHTTPRequest())["name"] | ||
|
||
userTenants := sets.New[string]() | ||
|
||
for _, tnt := range proxyTenants { | ||
userTenants.Insert(tnt.Tenant.Name) | ||
} | ||
|
||
if userTenants.Has(name) { | ||
return labels.NewSelector(), nil | ||
} | ||
|
||
return nil, errors.NewNotFoundError(name, l.gk) | ||
} |
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,66 @@ | ||
// Copyright 2020-2023 Project Capsule Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tenants | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-logr/logr" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/selection" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
"github.com/projectcapsule/capsule-proxy/internal/modules" | ||
"github.com/projectcapsule/capsule-proxy/internal/modules/errors" | ||
"github.com/projectcapsule/capsule-proxy/internal/request" | ||
"github.com/projectcapsule/capsule-proxy/internal/tenant" | ||
) | ||
|
||
type list struct { | ||
log logr.Logger | ||
gk schema.GroupKind | ||
} | ||
|
||
func List() modules.Module { | ||
return &list{ | ||
log: ctrl.Log.WithName("tenant_list"), | ||
gk: schema.GroupKind{ | ||
Group: corev1.GroupName, | ||
Kind: "tenants", | ||
}, | ||
} | ||
} | ||
|
||
func (l list) Path() string { | ||
return basePath | ||
} | ||
|
||
func (l list) Methods() []string { | ||
return []string{http.MethodGet} | ||
} | ||
|
||
func (l list) Handle(proxyTenants []*tenant.ProxyTenant, _ request.Request) (selector labels.Selector, err error) { | ||
userTenants := make([]string, 0, len(proxyTenants)) | ||
|
||
for _, tnt := range proxyTenants { | ||
userTenants = append(userTenants, tnt.Tenant.Name) | ||
} | ||
|
||
var r *labels.Requirement | ||
|
||
switch { | ||
case len(userTenants) > 0: | ||
r, err = labels.NewRequirement("kubernetes.io/metadata.name", selection.In, userTenants) | ||
default: | ||
r, err = labels.NewRequirement("dontexistsignoreme", selection.Exists, []string{}) | ||
} | ||
|
||
if err != nil { | ||
return nil, errors.NewBadRequest(err, l.gk) | ||
} | ||
|
||
return labels.NewSelector().Add(*r), nil | ||
} |
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