mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 19:01:49 +00:00
remove example apiserver replaced by sample and test/integration/examples
This commit is contained in:
parent
acba2cbd6d
commit
e1b40bfc5c
@ -69,7 +69,6 @@ filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//examples/apiserver:all-srcs",
|
||||
"//examples/explorer:all-srcs",
|
||||
"//examples/guestbook-go:all-srcs",
|
||||
"//examples/https-nginx:all-srcs",
|
||||
|
@ -1,48 +0,0 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["apiserver.go"],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//cmd/libs/go2idl/client-gen/test_apis/testgroup/install:go_default_library",
|
||||
"//cmd/libs/go2idl/client-gen/test_apis/testgroup/v1:go_default_library",
|
||||
"//examples/apiserver/rest:go_default_library",
|
||||
"//pkg/api:go_default_library",
|
||||
"//pkg/kubeapiserver/options:go_default_library",
|
||||
"//vendor:github.com/golang/glog",
|
||||
"//vendor:k8s.io/apimachinery/pkg/runtime/schema",
|
||||
"//vendor:k8s.io/apimachinery/pkg/util/errors",
|
||||
"//vendor:k8s.io/apiserver/pkg/authorization/authorizerfactory",
|
||||
"//vendor:k8s.io/apiserver/pkg/registry/generic",
|
||||
"//vendor:k8s.io/apiserver/pkg/registry/rest",
|
||||
"//vendor:k8s.io/apiserver/pkg/server",
|
||||
"//vendor:k8s.io/apiserver/pkg/server/options",
|
||||
"//vendor:k8s.io/apiserver/pkg/server/storage",
|
||||
"//vendor:k8s.io/apiserver/pkg/storage/storagebackend",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [
|
||||
":package-srcs",
|
||||
"//examples/apiserver/rest:all-srcs",
|
||||
"//examples/apiserver/server:all-srcs",
|
||||
],
|
||||
tags = ["automanaged"],
|
||||
)
|
@ -1,22 +0,0 @@
|
||||
# API Server
|
||||
|
||||
This is a work in progress example for an API Server.
|
||||
We are working on isolating the generic api server code from kubernetes specific
|
||||
API objects. Some relevant issues:
|
||||
|
||||
* https://github.com/kubernetes/kubernetes/issues/17412
|
||||
* https://github.com/kubernetes/kubernetes/issues/2742
|
||||
* https://github.com/kubernetes/kubernetes/issues/13541
|
||||
|
||||
This code here is to examplify what it takes to write your own API server.
|
||||
|
||||
To start this example api server, run:
|
||||
|
||||
```
|
||||
$ go run examples/apiserver/server/main.go
|
||||
```
|
||||
|
||||
|
||||
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
|
||||
[]()
|
||||
<!-- END MUNGE: GENERATED_ANALYTICS -->
|
@ -1,159 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 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.
|
||||
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 apiserver
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||
"k8s.io/apiserver/pkg/authorization/authorizerfactory"
|
||||
"k8s.io/apiserver/pkg/registry/generic"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
genericapiserver "k8s.io/apiserver/pkg/server"
|
||||
genericoptions "k8s.io/apiserver/pkg/server/options"
|
||||
serverstorage "k8s.io/apiserver/pkg/server/storage"
|
||||
"k8s.io/apiserver/pkg/storage/storagebackend"
|
||||
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/test_apis/testgroup/v1"
|
||||
testgroupetcd "k8s.io/kubernetes/examples/apiserver/rest"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
kubeoptions "k8s.io/kubernetes/pkg/kubeapiserver/options"
|
||||
|
||||
// Install the testgroup API
|
||||
_ "k8s.io/kubernetes/cmd/libs/go2idl/client-gen/test_apis/testgroup/install"
|
||||
)
|
||||
|
||||
const (
|
||||
// Ports on which to run the server.
|
||||
// Explicitly setting these to a different value than the default values, to prevent this from clashing with a local cluster.
|
||||
InsecurePort = 8081
|
||||
SecurePort = 6444
|
||||
)
|
||||
|
||||
type ServerRunOptions struct {
|
||||
GenericServerRunOptions *genericoptions.ServerRunOptions
|
||||
Etcd *genericoptions.EtcdOptions
|
||||
SecureServing *genericoptions.SecureServingOptions
|
||||
InsecureServing *genericoptions.ServingOptions
|
||||
Authentication *kubeoptions.BuiltInAuthenticationOptions
|
||||
CloudProvider *kubeoptions.CloudProviderOptions
|
||||
}
|
||||
|
||||
func NewServerRunOptions() *ServerRunOptions {
|
||||
s := ServerRunOptions{
|
||||
GenericServerRunOptions: genericoptions.NewServerRunOptions(),
|
||||
Etcd: genericoptions.NewEtcdOptions(storagebackend.NewDefaultConfig(kubeoptions.DefaultEtcdPathPrefix, api.Scheme, nil)),
|
||||
SecureServing: kubeoptions.NewSecureServingOptions(),
|
||||
InsecureServing: genericoptions.NewInsecureServingOptions(),
|
||||
Authentication: kubeoptions.NewBuiltInAuthenticationOptions().WithAll(),
|
||||
CloudProvider: kubeoptions.NewCloudProviderOptions(),
|
||||
}
|
||||
s.InsecureServing.BindPort = InsecurePort
|
||||
s.SecureServing.ServingOptions.BindPort = SecurePort
|
||||
s.Etcd.StorageConfig.ServerList = []string{"http://127.0.0.1:2379"}
|
||||
|
||||
return &s
|
||||
}
|
||||
|
||||
func (serverOptions *ServerRunOptions) Run(stopCh <-chan struct{}) error {
|
||||
serverOptions.Etcd.StorageConfig.ServerList = []string{"http://127.0.0.1:2379"}
|
||||
|
||||
// set defaults
|
||||
if err := serverOptions.CloudProvider.DefaultExternalHost(serverOptions.GenericServerRunOptions); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := serverOptions.SecureServing.MaybeDefaultWithSelfSignedCerts(serverOptions.GenericServerRunOptions.AdvertiseAddress.String()); err != nil {
|
||||
glog.Fatalf("Error creating self-signed certificates: %v", err)
|
||||
}
|
||||
|
||||
// validate options
|
||||
if errs := serverOptions.Etcd.Validate(); len(errs) > 0 {
|
||||
return utilerrors.NewAggregate(errs)
|
||||
}
|
||||
if errs := serverOptions.SecureServing.Validate(); len(errs) > 0 {
|
||||
return utilerrors.NewAggregate(errs)
|
||||
}
|
||||
if errs := serverOptions.InsecureServing.Validate("insecure-port"); len(errs) > 0 {
|
||||
return utilerrors.NewAggregate(errs)
|
||||
}
|
||||
|
||||
// create config from options
|
||||
config := genericapiserver.NewConfig().
|
||||
WithSerializer(api.Codecs)
|
||||
|
||||
if err := serverOptions.GenericServerRunOptions.ApplyTo(config); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := serverOptions.InsecureServing.ApplyTo(config); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := serverOptions.SecureServing.ApplyTo(config); err != nil {
|
||||
return fmt.Errorf("failed to configure https: %s", err)
|
||||
}
|
||||
if err := serverOptions.Authentication.ApplyTo(config); err != nil {
|
||||
return fmt.Errorf("failed to configure authentication: %s", err)
|
||||
}
|
||||
|
||||
config.Authorizer = authorizerfactory.NewAlwaysAllowAuthorizer()
|
||||
config.SwaggerConfig = genericapiserver.DefaultSwaggerConfig()
|
||||
|
||||
groupVersion := v1.SchemeGroupVersion
|
||||
groupName := groupVersion.Group
|
||||
groupMeta, err := api.Registry.Group(groupName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v", err)
|
||||
}
|
||||
storageFactory := serverstorage.NewDefaultStorageFactory(serverOptions.Etcd.StorageConfig, "application/json", api.Codecs, serverstorage.NewDefaultResourceEncodingConfig(api.Registry), serverstorage.NewResourceConfig())
|
||||
storageConfig, err := storageFactory.NewConfig(schema.GroupResource{Group: groupName, Resource: "testtype"})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to get storage config: %v", err)
|
||||
}
|
||||
if err := serverOptions.Etcd.ApplyWithStorageFactoryTo(storageFactory, config); err != nil {
|
||||
return fmt.Errorf("failed to configure authentication: %s", err)
|
||||
}
|
||||
|
||||
s, err := config.Complete().New()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error in bringing up the server: %v", err)
|
||||
}
|
||||
|
||||
testTypeOpts := generic.RESTOptions{
|
||||
StorageConfig: storageConfig,
|
||||
Decorator: generic.UndecoratedStorage,
|
||||
ResourcePrefix: "testtypes",
|
||||
DeleteCollectionWorkers: 1,
|
||||
}
|
||||
|
||||
restStorageMap := map[string]rest.Storage{
|
||||
"testtypes": testgroupetcd.NewREST(testTypeOpts),
|
||||
}
|
||||
apiGroupInfo := genericapiserver.APIGroupInfo{
|
||||
GroupMeta: *groupMeta,
|
||||
VersionedResourcesStorageMap: map[string]map[string]rest.Storage{
|
||||
groupVersion.Version: restStorageMap,
|
||||
},
|
||||
Scheme: api.Scheme,
|
||||
NegotiatedSerializer: api.Codecs,
|
||||
}
|
||||
if err := s.InstallAPIGroup(&apiGroupInfo); err != nil {
|
||||
return fmt.Errorf("Error in installing API: %v", err)
|
||||
}
|
||||
s.PrepareRun().Run(stopCh)
|
||||
return nil
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["reststorage.go"],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//cmd/libs/go2idl/client-gen/test_apis/testgroup:go_default_library",
|
||||
"//pkg/api:go_default_library",
|
||||
"//vendor:k8s.io/apimachinery/pkg/fields",
|
||||
"//vendor:k8s.io/apimachinery/pkg/labels",
|
||||
"//vendor:k8s.io/apimachinery/pkg/runtime",
|
||||
"//vendor:k8s.io/apimachinery/pkg/util/validation/field",
|
||||
"//vendor:k8s.io/apiserver/pkg/endpoints/request",
|
||||
"//vendor:k8s.io/apiserver/pkg/registry/generic",
|
||||
"//vendor:k8s.io/apiserver/pkg/registry/generic/registry",
|
||||
"//vendor:k8s.io/apiserver/pkg/storage",
|
||||
"//vendor:k8s.io/apiserver/pkg/storage/names",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
@ -1,92 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 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.
|
||||
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 rest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/apimachinery/pkg/fields"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
|
||||
"k8s.io/apiserver/pkg/registry/generic"
|
||||
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
|
||||
"k8s.io/apiserver/pkg/storage"
|
||||
"k8s.io/apiserver/pkg/storage/names"
|
||||
"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/test_apis/testgroup"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
)
|
||||
|
||||
type REST struct {
|
||||
*genericregistry.Store
|
||||
}
|
||||
|
||||
// NewREST returns a RESTStorage object that will work with testtype.
|
||||
func NewREST(optsGetter generic.RESTOptionsGetter) *REST {
|
||||
store := &genericregistry.Store{
|
||||
Copier: api.Scheme,
|
||||
NewFunc: func() runtime.Object { return &testgroup.TestType{} },
|
||||
// NewListFunc returns an object capable of storing results of an etcd list.
|
||||
NewListFunc: func() runtime.Object { return &testgroup.TestTypeList{} },
|
||||
// Retrieve the name field of the resource.
|
||||
ObjectNameFunc: func(obj runtime.Object) (string, error) {
|
||||
return obj.(*testgroup.TestType).Name, nil
|
||||
},
|
||||
// Used to match objects based on labels/fields for list.
|
||||
PredicateFunc: matcher,
|
||||
// QualifiedResource should always be plural
|
||||
QualifiedResource: api.Resource("testtypes"),
|
||||
|
||||
CreateStrategy: strategy,
|
||||
}
|
||||
options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: getAttrs}
|
||||
if err := store.CompleteWithOptions(options); err != nil {
|
||||
panic(err) // TODO: Propagate error up
|
||||
}
|
||||
return &REST{store}
|
||||
}
|
||||
|
||||
type fakeStrategy struct {
|
||||
runtime.ObjectTyper
|
||||
names.NameGenerator
|
||||
}
|
||||
|
||||
func (*fakeStrategy) NamespaceScoped() bool { return false }
|
||||
func (*fakeStrategy) PrepareForCreate(ctx genericapirequest.Context, obj runtime.Object) {}
|
||||
func (*fakeStrategy) Validate(ctx genericapirequest.Context, obj runtime.Object) field.ErrorList {
|
||||
return nil
|
||||
}
|
||||
func (*fakeStrategy) Canonicalize(obj runtime.Object) {}
|
||||
|
||||
var strategy = &fakeStrategy{api.Scheme, names.SimpleNameGenerator}
|
||||
|
||||
func getAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
|
||||
testType, ok := obj.(*testgroup.TestType)
|
||||
if !ok {
|
||||
return nil, nil, fmt.Errorf("not a TestType")
|
||||
}
|
||||
return labels.Set(testType.ObjectMeta.Labels), fields.Set{}, nil
|
||||
}
|
||||
|
||||
func matcher(label labels.Selector, field fields.Selector) storage.SelectionPredicate {
|
||||
return storage.SelectionPredicate{
|
||||
Label: label,
|
||||
Field: field,
|
||||
GetAttrs: getAttrs,
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_binary",
|
||||
"go_library",
|
||||
)
|
||||
|
||||
go_binary(
|
||||
name = "server",
|
||||
library = ":go_default_library",
|
||||
tags = ["automanaged"],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["main.go"],
|
||||
tags = ["automanaged"],
|
||||
deps = [
|
||||
"//examples/apiserver:go_default_library",
|
||||
"//vendor:github.com/golang/glog",
|
||||
"//vendor:github.com/spf13/pflag",
|
||||
"//vendor:k8s.io/apimachinery/pkg/util/wait",
|
||||
"//vendor:k8s.io/apiserver/pkg/util/flag",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
@ -1,43 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 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.
|
||||
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 main
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/apiserver/pkg/util/flag"
|
||||
"k8s.io/kubernetes/examples/apiserver"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
func main() {
|
||||
serverRunOptions := apiserver.NewServerRunOptions()
|
||||
|
||||
// Parse command line flags.
|
||||
serverRunOptions.GenericServerRunOptions.AddUniversalFlags(pflag.CommandLine)
|
||||
serverRunOptions.Etcd.AddFlags(pflag.CommandLine)
|
||||
serverRunOptions.SecureServing.AddFlags(pflag.CommandLine)
|
||||
serverRunOptions.SecureServing.AddDeprecatedFlags(pflag.CommandLine)
|
||||
serverRunOptions.InsecureServing.AddFlags(pflag.CommandLine)
|
||||
serverRunOptions.InsecureServing.AddDeprecatedFlags(pflag.CommandLine)
|
||||
flag.InitFlags()
|
||||
|
||||
if err := serverRunOptions.Run(wait.NeverStop); err != nil {
|
||||
glog.Fatalf("Error in bringing up the server: %v", err)
|
||||
}
|
||||
}
|
@ -38,7 +38,6 @@ cmd/libs/go2idl/openapi-gen
|
||||
cmd/libs/go2idl/set-gen
|
||||
cmd/linkcheck
|
||||
examples
|
||||
examples/apiserver/server
|
||||
examples/explorer
|
||||
examples/https-nginx
|
||||
examples/sharing-clusters
|
||||
@ -384,7 +383,6 @@ test/images/resource-consumer/consume-cpu
|
||||
test/images/serve_hostname
|
||||
test/integration/examples
|
||||
test/integration/federation
|
||||
test/integration/kubeaggregator
|
||||
test/integration/metrics
|
||||
test/integration/objectmeta
|
||||
test/integration/openshift
|
||||
|
@ -37,7 +37,6 @@ filegroup(
|
||||
"//test/integration/examples:all-srcs",
|
||||
"//test/integration/federation:all-srcs",
|
||||
"//test/integration/framework:all-srcs",
|
||||
"//test/integration/kubeaggregator:all-srcs",
|
||||
"//test/integration/metrics:all-srcs",
|
||||
"//test/integration/objectmeta:all-srcs",
|
||||
"//test/integration/openshift:all-srcs",
|
||||
|
@ -1,28 +0,0 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
licenses(["notice"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["aggregator_test.go"],
|
||||
tags = ["automanaged"],
|
||||
deps = ["//examples/apiserver:go_default_library"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
@ -1,91 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 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.
|
||||
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 kubeaggregator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"k8s.io/kubernetes/examples/apiserver"
|
||||
)
|
||||
|
||||
func waitForServerUp(serverURL string) error {
|
||||
for start := time.Now(); time.Since(start) < time.Minute; time.Sleep(5 * time.Second) {
|
||||
_, err := http.Get(serverURL)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("waiting for server timed out")
|
||||
}
|
||||
|
||||
func testResponse(t *testing.T, serverURL, path string, expectedStatusCode int) {
|
||||
response, err := http.Get(serverURL + path)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error in GET %s: %v", path, err)
|
||||
}
|
||||
if response.StatusCode != expectedStatusCode {
|
||||
t.Errorf("unexpected status code for %q: %v, expected: %v", path, response.StatusCode, expectedStatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func runAPIServer(t *testing.T, stopCh <-chan struct{}) string {
|
||||
serverRunOptions := apiserver.NewServerRunOptions()
|
||||
// Change the ports, because otherwise it will fail if examples/apiserver/apiserver_test and this are run in parallel.
|
||||
serverRunOptions.SecureServing.ServingOptions.BindPort = 6443 + 3
|
||||
serverRunOptions.InsecureServing.BindPort = 8080 + 3
|
||||
|
||||
// Avoid default cert-dir of /var/run/kubernetes to allow this to run on darwin
|
||||
certDir, _ := ioutil.TempDir("", "test-integration-kubeaggregator")
|
||||
defer os.Remove(certDir)
|
||||
serverRunOptions.SecureServing.ServerCert.CertDirectory = certDir
|
||||
|
||||
go func() {
|
||||
if err := serverRunOptions.Run(stopCh); err != nil {
|
||||
t.Fatalf("Error in bringing up the example apiserver: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
serverURL := fmt.Sprintf("http://localhost:%d", serverRunOptions.InsecureServing.BindPort)
|
||||
if err := waitForServerUp(serverURL); err != nil {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
return serverURL
|
||||
}
|
||||
|
||||
// Runs a discovery summarizer server and tests that all endpoints work as expected.
|
||||
func TestRunKubeAggregator(t *testing.T) {
|
||||
// Run the APIServer now to test the good case.
|
||||
stopCh := make(chan struct{})
|
||||
discoveryURL := runAPIServer(t, stopCh)
|
||||
defer close(stopCh)
|
||||
|
||||
// Test /api path.
|
||||
// There is no server running at that URL, so we will get a 500.
|
||||
testResponse(t, discoveryURL, "/api", http.StatusNotFound)
|
||||
|
||||
// Test /apis path.
|
||||
// There is no server running at that URL, so we will get a 500.
|
||||
testResponse(t, discoveryURL, "/apis", http.StatusOK)
|
||||
|
||||
// Test a random path, which should give a 404.
|
||||
testResponse(t, discoveryURL, "/randomPath", http.StatusNotFound)
|
||||
}
|
Loading…
Reference in New Issue
Block a user