Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ make APIExportEndpointSlice consumer aware #3256

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,20 @@ test-e2e-sharded-minimal: build-all
$(SUITES_ARGS) \
$(if $(value WAIT),|| { echo "Terminated with $$?"; wait "$$PID"; },)

# This is just easy target to run 2 shard test server locally until manually killed.
# You can targer test to it by running:
# go test ./test/e2e/apibinding/... --kcp-kubeconfig=$(pwd)/.kcp/admin.kubeconfig --shard-kubeconfigs=root=$(pwd)/.kcp-0/admin.kubeconfig -run=^TestAPIBindingEndpointSlicesSharded$
test-run-sharded-server: WORK_DIR ?= .
test-run-sharded-server: LOG_DIR ?= $(WORK_DIR)/.kcp
test-run-sharded-server:
mkdir -p "$(LOG_DIR)" "$(WORK_DIR)/.kcp"
rm -f "$(WORK_DIR)/.kcp/ready-to-test"
UNSAFE_E2E_HACK_DISABLE_ETCD_FSYNC=true NO_GORUN=1 ./bin/sharded-test-server --quiet --v=2 --log-dir-path="$(LOG_DIR)" --work-dir-path="$(WORK_DIR)" --shard-run-virtual-workspaces=false --shard-feature-gates=$(TEST_FEATURE_GATES) $(TEST_SERVER_ARGS) --number-of-shards=2 2>&1 & PID=$$!; echo "PID $$PID" && \
trap 'kill -TERM $$PID' TERM INT EXIT && \
while [ ! -f "$(WORK_DIR)/.kcp/ready-to-test" ]; do sleep 1; done && \
echo 'Server started' && \
wait $$PID

.PHONY: test
ifdef USE_GOTESTSUM
test: $(GOTESTSUM)
Expand Down
9 changes: 9 additions & 0 deletions config/crds/apis.kcp.io_apiexportendpointslices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ spec:
- url
type: object
type: array
x-kubernetes-list-map-keys:
- url
x-kubernetes-list-type: map
shardSelector:
description: |-
shardSelector is the selector used to filter the shards. It is used to filter the shards
when determining partition scope when deriving the endpoints. This is set by owning shard,
and is used by follower shards to determine if its inscope or not.
type: string
type: object
type: object
served: true
Expand Down
8 changes: 8 additions & 0 deletions pkg/authorization/bootstrap/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
rbacrest "k8s.io/kubernetes/pkg/registry/rbac/rest"
"k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac/bootstrappolicy"

"github.com/kcp-dev/kcp/sdk/apis/apis"
"github.com/kcp-dev/kcp/sdk/apis/core"
"github.com/kcp-dev/kcp/sdk/apis/tenancy"
)
Expand Down Expand Up @@ -101,6 +102,13 @@ func clusterRoles() []rbacv1.ClusterRole {
rbacv1helpers.NewRule("access").URLs("/").RuleOrDie(),
},
},
{
ObjectMeta: metav1.ObjectMeta{Name: SystemExternalLogicalClusterAdmin},
Rules: []rbacv1.PolicyRule{
rbacv1helpers.NewRule("update", "patch", "get").Groups(apis.GroupName).Resources("apiexportendpointslices/status").RuleOrDie(),
rbacv1helpers.NewRule("get", "list", "watch").Groups(apis.GroupName).Resources("apiexportendpointslices").RuleOrDie(),
},
},
}
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/indexers/apiexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package indexers

import (
"fmt"

kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
"github.com/kcp-dev/logicalcluster/v3"

Expand All @@ -33,6 +35,8 @@ const (
// APIExportByClaimedIdentities is the indexer name for retrieving APIExports that have a permission claim for a
// particular identity hash.
APIExportByClaimedIdentities = "APIExportByClaimedIdentities"
// APIExportEndpointSliceByAPIExport is the indexer name for retrieving APIExportEndpointSlices by their APIExport's Reference Path and Name.
APIExportEndpointSliceByAPIExport = "APIExportEndpointSliceByAPIExport"
)

// IndexAPIExportByIdentity is an index function that indexes an APIExport by its identity hash.
Expand Down Expand Up @@ -72,3 +76,23 @@ func IndexAPIExportByClaimedIdentities(obj interface{}) ([]string, error) {
}
return sets.List[string](claimedIdentities), nil
}

// IndexAPIExportEndpointSliceByAPIExportFunc indexes the APIExportEndpointSlice by their APIExport's Reference Path and Name.
func IndexAPIExportEndpointSliceByAPIExport(obj interface{}) ([]string, error) {
apiExportEndpointSlice, ok := obj.(*apisv1alpha1.APIExportEndpointSlice)
if !ok {
return []string{}, fmt.Errorf("obj %T is not an APIExportEndpointSlice", obj)
}

var result []string
pathRemote := logicalcluster.NewPath(apiExportEndpointSlice.Spec.APIExport.Path)
if !pathRemote.Empty() {
result = append(result, pathRemote.Join(apiExportEndpointSlice.Spec.APIExport.Name).String())
}
pathLocal := logicalcluster.From(apiExportEndpointSlice).Path()
if !pathLocal.Empty() {
result = append(result, pathLocal.Join(apiExportEndpointSlice.Spec.APIExport.Name).String())
}

return result, nil
}
95 changes: 95 additions & 0 deletions pkg/indexers/apiexport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2025 The KCP 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 indexers

import (
"reflect"
"testing"

"github.com/kcp-dev/logicalcluster/v3"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

apisv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"
)

func TestIndexAPIExportEndpointSliceByAPIExport(t *testing.T) {
tests := map[string]struct {
obj interface{}
want []string
wantErr bool
}{
"not an APIExportEndpointSlice": {
obj: "not an APIExportEndpointSlice",
want: []string{},
wantErr: true,
},
"valid APIExportEndpointSlice": {
obj: &apisv1alpha1.APIExportEndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
logicalcluster.AnnotationKey: "root:local",
},
Name: "foo",
},
Spec: apisv1alpha1.APIExportEndpointSliceSpec{
APIExport: apisv1alpha1.ExportBindingReference{
Path: "root:default",
Name: "foo",
},
},
},
want: []string{
logicalcluster.NewPath("root:default:foo").String(),
logicalcluster.NewPath("root:local:foo").String(),
},
wantErr: false,
},
"valid APIExportEndpointSlice local to export": {
obj: &apisv1alpha1.APIExportEndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
logicalcluster.AnnotationKey: "root:local",
},
Name: "foo",
},
Spec: apisv1alpha1.APIExportEndpointSliceSpec{
APIExport: apisv1alpha1.ExportBindingReference{
Name: "foo",
},
},
},
want: []string{
logicalcluster.NewPath("root:local:foo").String(),
},
wantErr: false,
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := IndexAPIExportEndpointSliceByAPIExport(tt.obj)
if (err != nil) != tt.wantErr {
t.Errorf("IndexAPIExportEndpointSliceByAPIExport() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("IndexAPIExportEndpointSliceByAPIExport() got = %v, want %v", got, tt.want)
}
})
}
}
15 changes: 15 additions & 0 deletions pkg/openapi/zz_generated.openapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading