Skip to content

Commit

Permalink
openapi/v3: add e2e tests
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
  • Loading branch information
sttts committed Apr 18, 2024
1 parent 25639b8 commit 09cdde6
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
kcpdynamic "github.com/kcp-dev/client-go/dynamic"
kcpkubernetesinformers "github.com/kcp-dev/client-go/informers"
kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes"
"github.com/kcp-dev/kcp/pkg/server/openapiv3"
"github.com/kcp-dev/logicalcluster/v3"

apiextensionsapiserver "k8s.io/apiextensions-apiserver/pkg/apiserver"
"k8s.io/apiextensions-apiserver/pkg/apiserver/conversion"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -63,6 +63,7 @@ import (
"github.com/kcp-dev/kcp/pkg/informer"
"github.com/kcp-dev/kcp/pkg/server/bootstrap"
kcpfilters "github.com/kcp-dev/kcp/pkg/server/filters"
"github.com/kcp-dev/kcp/pkg/server/openapiv3"
kcpserveroptions "github.com/kcp-dev/kcp/pkg/server/options"
"github.com/kcp-dev/kcp/pkg/server/options/batteries"
"github.com/kcp-dev/kcp/pkg/server/requestinfo"
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
kcpdynamic "github.com/kcp-dev/client-go/dynamic"
kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes"
kcpmetadata "github.com/kcp-dev/client-go/metadata"
"github.com/kcp-dev/kcp/pkg/server/openapiv3"
"github.com/kcp-dev/logicalcluster/v3"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -79,6 +78,7 @@ import (
"github.com/kcp-dev/kcp/pkg/reconciler/tenancy/workspacemounts"
"github.com/kcp-dev/kcp/pkg/reconciler/tenancy/workspacetype"
"github.com/kcp-dev/kcp/pkg/reconciler/topology/partitionset"
"github.com/kcp-dev/kcp/pkg/server/openapiv3"
initializingworkspacesbuilder "github.com/kcp-dev/kcp/pkg/virtual/initializingworkspaces/builder"
corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1"
Expand Down
12 changes: 6 additions & 6 deletions pkg/server/openapiv3/controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*
Copyright 2024 The KCP Authors.
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,9 +27,7 @@ import (
kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
kcpapiextensionsv1informers "github.com/kcp-dev/client-go/apiextensions/informers/apiextensions/v1"
kcpapiextensionsv1listers "github.com/kcp-dev/client-go/apiextensions/listers/apiextensions/v1"
"github.com/kcp-dev/kcp/pkg/logging"
"github.com/kcp-dev/logicalcluster/v3"
"k8s.io/kube-openapi/pkg/cached"

apiextensionshelpers "k8s.io/apiextensions-apiserver/pkg/apihelpers"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand All @@ -42,7 +39,10 @@ import (
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"k8s.io/kube-openapi/pkg/cached"
"k8s.io/kube-openapi/pkg/spec3"

"github.com/kcp-dev/kcp/pkg/logging"
)

const ControllerName = "kcp-openapiv3"
Expand All @@ -51,7 +51,7 @@ type CRDSpecGetter interface {
GetCRDSpecs(clusterName logicalcluster.Name, name string) (specs map[string]cached.Data[*spec3.OpenAPI], err error)
}

// Controller watches CustomResourceDefinitions and publishes OpenAPI v3
// Controller watches CustomResourceDefinitions and publishes OpenAPI v3.
type Controller struct {
crdLister kcpapiextensionsv1listers.CustomResourceDefinitionClusterLister
crdsSynced cache.InformerSynced
Expand All @@ -63,7 +63,7 @@ type Controller struct {
byClusterNameVersion map[logicalcluster.Name]map[string]map[string]cached.Data[*spec3.OpenAPI]
}

// NewController creates a new Controller with input CustomResourceDefinition informer
// NewController creates a new Controller with input CustomResourceDefinition informer.
func NewController(crdInformer kcpapiextensionsv1informers.CustomResourceDefinitionClusterInformer) *Controller {
c := &Controller{
crdLister: crdInformer.Lister(),
Expand All @@ -72,7 +72,7 @@ func NewController(crdInformer kcpapiextensionsv1informers.CustomResourceDefinit
byClusterNameVersion: map[logicalcluster.Name]map[string]map[string]cached.Data[*spec3.OpenAPI]{},
}

crdInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
crdInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ // nolint:errcheck
AddFunc: c.addCustomResourceDefinition,
UpdateFunc: c.updateCustomResourceDefinition,
DeleteFunc: c.deleteCustomResourceDefinition,
Expand Down
19 changes: 18 additions & 1 deletion pkg/server/openapiv3/servicecache.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2024 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 openapiv3

import (
Expand All @@ -12,6 +28,7 @@ import (
"github.com/emicklei/go-restful/v3"
"github.com/go-logr/logr"
"github.com/kcp-dev/logicalcluster/v3"

apiextensionshelpers "k8s.io/apiextensions-apiserver/pkg/apihelpers"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apiextensions-apiserver/pkg/controller/openapi/builder"
Expand All @@ -36,7 +53,7 @@ const (
// Equal API configurations in multiple workspaces are shared.
DefaultServiceCacheSize = 100

// TODO(sttts): move to central place in kube
// TODO(sttts): move to central place in kube.
BoundAnnotationKey = "apis.kcp.io/bound-crd"
)

Expand Down
12 changes: 12 additions & 0 deletions test/e2e/apibinding/apibinding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ func TestAPIBinding(t *testing.T) {
require.False(t, groupExists(groups, wildwest.GroupName),
"should not have seen %s API group in %q group discovery", wildwest.GroupName, providerPath)

t.Logf("Make sure cowboys API group does NOT show up in workspace %q openapi v3 endpoint", providerPath)
providerOpenAPIV3 := providerWorkspaceClient.Cluster(providerPath).Discovery().OpenAPIV3()
paths, err := providerOpenAPIV3.Paths()
require.NoError(t, err, "error retrieving %q openapi v3 paths", providerPath)
require.NotContainsf(t, paths, "apis/"+wildwestv1alpha1.SchemeGroupVersion.String(), "should not have %s API group in %q openapi v3 paths", wildwest.GroupName, providerPath)

consumerWorkspaceClient, err := kcpclientset.NewForConfig(cfg)
require.NoError(t, err)

Expand All @@ -263,6 +269,12 @@ func TestAPIBinding(t *testing.T) {
wildwestClusterClient, err := wildwestclientset.NewForConfig(rest.CopyConfig(cfg))
require.NoError(t, err)

t.Logf("Make sure cowboys API group DOES show up in workspace %q openapi v3 endpoint", consumerWorkspace)
consumerOpenAPIV3 := consumerWorkspaceClient.Cluster(consumerWorkspace).Discovery().OpenAPIV3()
paths, err = consumerOpenAPIV3.Paths()
require.NoError(t, err, "error retrieving %q openapi v3 paths", consumerWorkspace)
require.Containsf(t, paths, "apis/"+wildwestv1alpha1.SchemeGroupVersion.String(), "should have %s API group in %q openapi v3 paths", wildwest.GroupName, consumerWorkspace)

t.Logf("Make sure we can perform CRUD operations against consumer workspace %q for the bound API", consumerWorkspace)

t.Logf("Make sure list shows nothing to start")
Expand Down
73 changes: 73 additions & 0 deletions test/e2e/openapi/openapi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2024 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 quota

import (
"strings"
"testing"

kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes"
"github.com/stretchr/testify/require"

"k8s.io/apimachinery/pkg/util/sets"

apisv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"
corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1"
topologyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/topology/v1alpha1"
"github.com/kcp-dev/kcp/test/e2e/framework"
)

func TestOpenAPIv3(t *testing.T) {
t.Parallel()
framework.Suite(t, "control-plane")

server := framework.SharedKcpServer(t)

cfg := server.BaseConfig(t)

kubeClusterClient, err := kcpkubernetesclientset.NewForConfig(cfg)
require.NoError(t, err, "error creating kube cluster client")

wsPath, _ := framework.NewOrganizationFixture(t, server)

t.Logf("Checking /openapi/v3 paths for %q", wsPath)
openAPIV3 := kubeClusterClient.Cluster(wsPath).Discovery().OpenAPIV3()
paths, err := openAPIV3.Paths()
require.NoError(t, err, "error retrieving %q openapi v3 paths", wsPath)
got := sets.NewString()
for path := range paths {
if !strings.HasPrefix(path, "/api/") && !strings.HasPrefix(path, "/apis/") {
continue
}
got.Insert(path)
}
expected := sets.NewString(
"api/v1",
"apis/admissionregistration.k8s.io/v1",
"apis/authentication.k8s.io/v1",
// any many more

"apis/"+tenancyv1alpha1.SchemeGroupVersion.String(),
"apis/"+apisv1alpha1.SchemeGroupVersion.String(),
"apis/"+corev1alpha1.SchemeGroupVersion.String(),
"apis/"+topologyv1alpha1.SchemeGroupVersion.String(),
)
if expected.Difference(got).Len() > 0 {
t.Errorf("missing paths: %v", expected.Difference(got).List())
}
}

0 comments on commit 09cdde6

Please sign in to comment.