未验证 提交 2f5202f3 编写于 作者: K KubeSphere CI Bot 提交者: GitHub

Merge pull request #3366 from RolandMa1986/feat-e2e

e2e: add client library
......@@ -44,6 +44,10 @@ ks-apiserver: fmt vet
ks-controller-manager: fmt vet
hack/gobuild.sh cmd/controller-manager
# Build e2e binary
e2e: fmt vet
hack/build_e2e.sh test/e2e
# Run go fmt against code
fmt:
gofmt -w ./pkg ./cmd ./tools ./api
......
......@@ -103,6 +103,7 @@ require (
sigs.k8s.io/controller-runtime v0.6.4
sigs.k8s.io/controller-tools v0.4.0
sigs.k8s.io/kubefed v0.4.0
kubesphere.io/client-go v0.0.0
)
replace (
......@@ -757,4 +758,6 @@ replace (
sigs.k8s.io/yaml => sigs.k8s.io/yaml v1.2.0
sourcegraph.com/sourcegraph/appdash => sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0
vbom.ml/util => vbom.ml/util v0.0.0-20160121211510-db5cfe13f5cc
kubesphere.io/client-go => ./staging/src/kubesphere.io/client-go
)
#!/usr/bin/env bash
# Copyright 2020 KubeSphere 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.
#
# This script builds and link stamps the output
set -o errexit
set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
VERBOSE=${VERBOSE:-"0"}
V=""
if [[ "${VERBOSE}" == "1" ]];then
V="-x"
set -x
fi
ROOTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
OUTPUT_DIR=bin
BUILDPATH=./${1:?"path to build"}
OUT=${OUTPUT_DIR}/${1:?"output path"}
BUILD_GOOS=${GOOS:-$(go env GOOS)}
BUILD_GOARCH=${GOARCH:-$(go env GOARCH)}
GOBINARY=${GOBINARY:-go}
LDFLAGS=$(kube::version::ldflags)
time GOOS=${BUILD_GOOS} GOARCH=${BUILD_GOARCH} ${GOBINARY} test \
-c \
-ldflags "${LDFLAGS}" \
-o ${OUT} \
${BUILDPATH}
......@@ -540,7 +540,7 @@ EOF
# $KUBE_ROOT must be set.
function kube::util::list_staging_repos() {
(
cd "${KUBE_ROOT}/staging/src/k8s.io" && \
cd "${KUBE_ROOT}/staging/src/kubesphere.io" && \
find . -mindepth 1 -maxdepth 1 -type d | cut -c 3- | sort
)
}
......
......@@ -122,6 +122,17 @@ function add_generated_comments() {
# Phase 1: ensure go.mod files for staging modules and main module
for repo in $(kube::util::list_staging_repos); do
pushd "staging/src/kubesphere.io/${repo}" >/dev/null 2>&1
if [[ ! -f go.mod ]]; then
kube::log::status "go.mod: initialize ${repo}"
rm -f Godeps/Godeps.json # remove before initializing, staging Godeps are not authoritative
go mod init "kubesphere.io/${repo}"
go mod edit -fmt
fi
popd >/dev/null 2>&1
done
if [[ ! -f go.mod ]]; then
kube::log::status "go.mod: initialize kubesphere.io/kubesphere"
go mod init "kubesphere.io/kubesphere"
......@@ -134,6 +145,9 @@ kube::log::status "go.mod: update references"
# Prune
go mod edit -json | jq -r '.Require[]? | select(.Version == "v0.0.0") | "-droprequire \(.Path)"' | xargs -L 100 go mod edit -fmt
go mod edit -json | jq -r '.Replace[]? | select(.New.Path | startswith("./staging/")) | "-dropreplace \(.Old.Path)"' | xargs -L 100 go mod edit -fmt
# Readd
kube::util::list_staging_repos | xargs -n 1 -I {} echo "-require kubesphere.io/{}@v0.0.0" | xargs -L 100 go mod edit -fmt
kube::util::list_staging_repos | xargs -n 1 -I {} echo "-replace kubesphere.io/{}=./staging/src/kubesphere.io/{}" | xargs -L 100 go mod edit -fmt
# Phase 3: capture required (minimum) versions from all modules, and replaced (pinned) versions from the root module
......
......@@ -22,6 +22,9 @@ package request
import (
"context"
"fmt"
"net/http"
"strings"
"k8s.io/apimachinery/pkg/api/validation/path"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metainternalversionscheme "k8s.io/apimachinery/pkg/apis/meta/internalversion/scheme"
......@@ -32,8 +35,6 @@ import (
"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/constants"
netutils "kubesphere.io/kubesphere/pkg/utils/net"
"net/http"
"strings"
k8srequest "k8s.io/apiserver/pkg/endpoints/request"
)
......@@ -131,7 +132,15 @@ func (r *RequestInfoFactory) NewRequestInfo(req *http.Request) (*RequestInfo, er
}
defer func() {
if kubernetesAPIPrefixes.Has(requestInfo.APIPrefix) {
prefix := requestInfo.APIPrefix
if prefix == "" {
currentParts := splitPath(requestInfo.Path)
//Proxy discovery API
if len(currentParts) > 0 && len(currentParts) < 3 {
prefix = currentParts[0]
}
}
if kubernetesAPIPrefixes.Has(prefix) {
requestInfo.IsKubernetesRequest = true
}
}()
......
# Guide
This [document](https://github.com/kubesphere/community) walks you through how to get started contributing KubeSphere.
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
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.
approvers:
- zryfish #oncall
- rayzhou2017
reviewers:
- rayzhou2017
- zryfish
- benjaminhuo
- calvinyv
- FeynmanZhou
- Ma-Dan
- pixiake
- wansir
- zheng1
- shaowenchen
- stoneshi-yunify
- linuxsuren
# KubeSphere client-go Project
The KubeSphere client-go Project is a rest-client of go libraries for communicating with the KubeSphere API Server.
# How to use it
1. Import client-go packages:
```golang
import (
"k8s.io/client-go/rest"
"kubesphere.io/client-go/client"
"kubesphere.io/client-go/client/generic"
)
```
2. Create a generic client instance:
```golang
var client client.Client
config := &rest.Config{
Host: "127.0.0.1:9090",
Username: "admin",
Password: "P@88w0rd",
}
client = generic.NewForConfigOrDie(config, client.Options{Scheme: f.Scheme})
```
> generic.NewForConfigOrDie returns a client.Client that reads and writes from/to an KubeSphere API server.
> It's only compatible with Kubernetes-like API objects.
3. KubeSphere API server provided a proxy to Kubernetes API Server. The client can read and write those Kubernetes native objects with the client directly.
```golang
deploy := &appsv1.Deployment{}
client.Get(context.TODO(), client.ObjectKey{Namespace: "kubesphere-system", Name: "ks-apiserver"}, deploy)
```
4. URLOptions and WorkspaceOptions can be provided to read and write Kubernetes likely Object that provided by KubeSphere API.
```golang
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "ks-test",
Labels: map[string]string{
constants.WorkspaceLabelKey: "Workspace",
},
},
}
opts := &client.URLOptions{
Group: "tenant.kubesphere.io",
Version: "v1alpha2",
}
err := f.GenericClient(f.BaseName).Create(context.TODO(), ns, opts, &client.WorkspaceOptions{Name: "Workspace"})
```
The KubeSphere API Architecture can be found at https://kubesphere.io/docs/api-reference/api-docs/
# Where does it come from?
client-go is synced from https://github.com/kubesphere/kubesphere/blob/master/staging/src/kubesphere.io/client-go. Code changes are made in that location, merged into `kubesphere.io/client-go` and later synced here.
\ No newline at end of file
/*
Copyright 2020 KubeSphere 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 client
import (
"strings"
"sync"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)
type ClientCache interface {
GetObjMeta(obj runtime.Object) (*ObjMeta, error)
GetResource(obj runtime.Object) (*ResourceMeta, error)
}
func NewClientCache(config *rest.Config, options Options) ClientCache {
cc := &clientCache{
config: config,
scheme: options.Scheme,
mapper: options.Mapper,
codecs: serializer.NewCodecFactory(options.Scheme),
resourceByType: make(map[schema.GroupVersionKind]*ResourceMeta),
}
return cc
}
// clientCache creates and caches rest clients and metadata for Kubernetes types
type clientCache struct {
// config is the rest.Config to talk to an apiserver
config *rest.Config
// scheme maps go structs to GroupVersionKinds
scheme *runtime.Scheme
// mapper maps GroupVersionKinds to Resources
mapper meta.RESTMapper
// codecs are used to create a REST client for a gvk
codecs serializer.CodecFactory
// resourceByType caches type metadata
resourceByType map[schema.GroupVersionKind]*ResourceMeta
mu sync.RWMutex
}
// newResource maps obj to a Kubernetes Resource and constructs a client for that Resource.
// If the object is a list, the resource represents the item's type instead.
func (c *clientCache) newResource(gvk schema.GroupVersionKind, isList bool) (*ResourceMeta, error) {
if strings.HasSuffix(gvk.Kind, "List") && isList {
// if this was a list, treat it as a request for the item's resource
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
}
client, err := apiutil.RESTClientForGVK(gvk, c.config, c.codecs)
if err != nil {
return nil, err
}
mapping, err := c.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, err
}
return &ResourceMeta{Interface: client, mapping: mapping, gvk: gvk}, nil
}
// GetResource returns the resource meta information for the given type of object.
// If the object is a list, the resource represents the item's type instead.
func (c *clientCache) GetResource(obj runtime.Object) (*ResourceMeta, error) {
gvk, err := apiutil.GVKForObject(obj, c.scheme)
if err != nil {
return nil, err
}
// It's better to do creation work twice than to not let multiple
// people make requests at once
c.mu.RLock()
r, known := c.resourceByType[gvk]
c.mu.RUnlock()
if known {
return r, nil
}
// Initialize a new Client
c.mu.Lock()
defer c.mu.Unlock()
r, err = c.newResource(gvk, meta.IsListType(obj))
if err != nil {
return nil, err
}
c.resourceByType[gvk] = r
return r, err
}
// getObjMeta returns ObjMeta containing both type and object metadata and state
func (c *clientCache) GetObjMeta(obj runtime.Object) (*ObjMeta, error) {
r, err := c.GetResource(obj)
if err != nil {
return nil, err
}
m, err := meta.Accessor(obj)
if err != nil {
return nil, err
}
return &ObjMeta{ResourceMeta: r, Object: m}, err
}
// ResourceMeta caches state for a Kubernetes type.
type ResourceMeta struct {
// client is the rest client used to talk to the apiserver
rest.Interface
// gvk is the GroupVersionKind of the ResourceMeta
gvk schema.GroupVersionKind
// mapping is the rest mapping
mapping *meta.RESTMapping
}
// IsNamespaced returns true if the type is namespaced
func (r *ResourceMeta) IsNamespaced() bool {
return r.mapping.Scope.Name() != meta.RESTScopeNameRoot
}
// resource returns the resource name of the type
func (r *ResourceMeta) Resource() string {
return r.mapping.Resource.Resource
}
// ObjMeta stores type and object information about a Kubernetes type
type ObjMeta struct {
// ResourceMeta contains type information for the object
*ResourceMeta
// Object contains meta data for the object instance
metav1.Object
}
/*
Copyright 2020 KubeSphere 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 client
import (
"errors"
"net/url"
"k8s.io/apimachinery/pkg/conversion/queryparams"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
var _ runtime.ParameterCodec = NoConversionParamCodec{}
// NoConversionParamCodec is a no-conversion codec for serializing parameters into URL query strings.
// it's useful in scenarios with the unstructured client and arbitrary resouces.
type NoConversionParamCodec struct{}
func (NoConversionParamCodec) EncodeParameters(obj runtime.Object, to schema.GroupVersion) (url.Values, error) {
return queryparams.Convert(obj)
}
func (NoConversionParamCodec) DecodeParameters(parameters url.Values, from schema.GroupVersion, into runtime.Object) error {
return errors.New("DecodeParameters not implemented on noConversionParamCodec")
}
/*
Copyright 2020 KubeSphere 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 generic
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"kubesphere.io/client-go/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)
// New returns a new Client using the provided config and Options.
// The returned client reads *and* writes directly from the server
// (it doesn't use object caches). It understands how to work with
// normal types (both custom resources and aggregated/built-in resources),
// as well as unstructured types.
//
// In the case of normal types, the scheme will be used to look up the
// corresponding group, version, and kind for the given type. In the
// case of unstructured types, the group, version, and kind will be extracted
// from the corresponding fields on the object.
func New(config *rest.Config, options client.Options) (client.Client, error) {
if config == nil {
return nil, fmt.Errorf("must provide non-nil rest.Config to client.New")
}
// Init a scheme if none provided
if options.Scheme == nil {
options.Scheme = scheme.Scheme
}
// Init a Mapper if none provided
if options.Mapper == nil {
var err error
options.Mapper, err = apiutil.NewDynamicRESTMapper(config)
if err != nil {
return nil, err
}
}
clientcache := client.NewClientCache(config, options)
c := &genericClient{
typedClient: typedClient{
cache: clientcache,
paramCodec: runtime.NewParameterCodec(options.Scheme),
},
unstructuredClient: unstructuredClient{
cache: clientcache,
paramCodec: client.NoConversionParamCodec{},
},
}
return c, nil
}
func NewForConfigOrDie(config *rest.Config, options client.Options) client.Client {
client, err := New(config, options)
if err != nil {
panic(err)
}
return client
}
var _ client.Client = &genericClient{}
// genericClient is a client.Client that reads and writes directly from/to an KubeSphere API server. It lazily initializes
// new clients at the time they are used, and caches the client.
type genericClient struct {
typedClient typedClient
unstructuredClient unstructuredClient
}
// resetGroupVersionKind is a helper function to restore and preserve GroupVersionKind on an object.
func (c *genericClient) resetGroupVersionKind(obj runtime.Object, gvk schema.GroupVersionKind) {
if gvk != schema.EmptyObjectKind.GroupVersionKind() {
if v, ok := obj.(schema.ObjectKind); ok {
v.SetGroupVersionKind(gvk)
}
}
}
// Create implements client.Client
func (c *genericClient) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOption) error {
_, ok := obj.(*unstructured.Unstructured)
if ok {
return c.unstructuredClient.Create(ctx, obj, opts...)
}
return c.typedClient.Create(ctx, obj, opts...)
}
// Update implements client.Client
func (c *genericClient) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error {
defer c.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
_, ok := obj.(*unstructured.Unstructured)
if ok {
return c.unstructuredClient.Update(ctx, obj, opts...)
}
return c.typedClient.Update(ctx, obj, opts...)
}
// Delete implements client.Client
func (c *genericClient) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOption) error {
_, ok := obj.(*unstructured.Unstructured)
if ok {
return c.unstructuredClient.Delete(ctx, obj, opts...)
}
return c.typedClient.Delete(ctx, obj, opts...)
}
// DeleteAllOf implements client.Client
func (c *genericClient) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...client.DeleteAllOfOption) error {
_, ok := obj.(*unstructured.Unstructured)
if ok {
return c.unstructuredClient.DeleteAllOf(ctx, obj, opts...)
}
return c.typedClient.DeleteAllOf(ctx, obj, opts...)
}
// Patch implements client.Client
func (c *genericClient) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOption) error {
defer c.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
_, ok := obj.(*unstructured.Unstructured)
if ok {
return c.unstructuredClient.Patch(ctx, obj, patch, opts...)
}
return c.typedClient.Patch(ctx, obj, patch, opts...)
}
// Get implements client.Client
func (c *genericClient) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object, opts ...client.GetOption) error {
_, ok := obj.(*unstructured.Unstructured)
if ok {
return c.unstructuredClient.Get(ctx, key, obj, opts...)
}
return c.typedClient.Get(ctx, key, obj, opts...)
}
// List implements client.Client
func (c *genericClient) List(ctx context.Context, obj runtime.Object, opts ...client.ListOption) error {
_, ok := obj.(*unstructured.UnstructuredList)
if ok {
return c.unstructuredClient.List(ctx, obj, opts...)
}
return c.typedClient.List(ctx, obj, opts...)
}
// Status implements client.StatusClient
func (c *genericClient) Status() client.StatusWriter {
return &statusWriter{client: c}
}
// statusWriter is client.StatusWriter that writes status subresource
type statusWriter struct {
client *genericClient
}
// ensure statusWriter implements client.StatusWriter
var _ client.StatusWriter = &statusWriter{}
// Update implements client.StatusWriter
func (sw *statusWriter) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error {
defer sw.client.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
_, ok := obj.(*unstructured.Unstructured)
if ok {
return sw.client.unstructuredClient.UpdateStatus(ctx, obj, opts...)
}
return sw.client.typedClient.UpdateStatus(ctx, obj, opts...)
}
// Patch implements client.Client
func (sw *statusWriter) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOption) error {
defer sw.client.resetGroupVersionKind(obj, obj.GetObjectKind().GroupVersionKind())
_, ok := obj.(*unstructured.Unstructured)
if ok {
return sw.client.unstructuredClient.PatchStatus(ctx, obj, patch, opts...)
}
return sw.client.typedClient.PatchStatus(ctx, obj, patch, opts...)
}
/*
Copyright 2020 KubeSphere 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 generic
import (
"context"
"k8s.io/apimachinery/pkg/runtime"
"kubesphere.io/client-go/client"
)
// client is a client.Client that reads and writes directly from/to an API server. It lazily initializes
// new clients at the time they are used, and caches the client.
type typedClient struct {
cache client.ClientCache
paramCodec runtime.ParameterCodec
}
// Create implements client.Client
func (c *typedClient) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOption) error {
o, err := c.cache.GetObjMeta(obj)
if err != nil {
return err
}
createOpts := &client.CreateOptions{}
createOpts.ApplyOptions(opts)
req := o.Post().
Body(obj).
VersionedParams(createOpts.AsCreateOptions(), c.paramCodec)
// Overwrites GVK based URL when has URLOption
if createOpts.URLOption != nil {
absPath := createOpts.URLOption.URL()
if createOpts.Workspace != nil {
absPath = append(absPath, "workspaces", createOpts.Workspace.Name)
}
req = req.AbsPath(absPath...)
}
if createOpts.URLOption == nil || createOpts.URLOption.AbsPath == "" {
req = req.NamespaceIfScoped(o.GetNamespace(), o.IsNamespaced()).
Resource(o.Resource())
}
return req.
Do(ctx).
Into(obj)
}
// Update implements client.Client
func (c *typedClient) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error {
o, err := c.cache.GetObjMeta(obj)
if err != nil {
return err
}
updateOpts := &client.UpdateOptions{}
updateOpts.ApplyOptions(opts)
req := o.Put()
// Overwrites GVK based URL when has URLOption
if updateOpts.URLOption != nil {
absPath := updateOpts.URLOption.URL()
if updateOpts.Workspace != nil {
absPath = append(absPath, "workspaces", updateOpts.Workspace.Name)
}
req = req.AbsPath(absPath...)
}
if updateOpts.URLOption == nil || updateOpts.URLOption.AbsPath == "" {
req = req.NamespaceIfScoped(o.GetNamespace(), o.IsNamespaced()).
Resource(o.Resource()).
Name(o.GetName())
}
return req.Body(obj).
VersionedParams(updateOpts.AsUpdateOptions(), c.paramCodec).
Do(ctx).
Into(obj)
}
// Delete implements client.Client
func (c *typedClient) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOption) error {
o, err := c.cache.GetObjMeta(obj)
if err != nil {
return err
}
deleteOpts := client.DeleteOptions{}
deleteOpts.ApplyOptions(opts)
req := o.Delete()
// Overwrites GVK based URL when has URLOption
if deleteOpts.URLOption != nil {
absPath := deleteOpts.URLOption.URL()
if deleteOpts.Workspace != nil {
absPath = append(absPath, "workspaces", deleteOpts.Workspace.Name)
}
req = req.AbsPath(absPath...)
}
if deleteOpts.URLOption == nil || deleteOpts.URLOption.AbsPath == "" {
req = req.NamespaceIfScoped(o.GetNamespace(), o.IsNamespaced()).
Resource(o.Resource()).
Name(o.GetName())
}
return req.Body(deleteOpts.AsDeleteOptions()).
Do(ctx).
Error()
}
// Patch implements client.Client
func (c *typedClient) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOption) error {
o, err := c.cache.GetObjMeta(obj)
if err != nil {
return err
}
data, err := patch.Data(obj)
if err != nil {
return err
}
patchOpts := &client.PatchOptions{}
req := o.Patch(patch.Type())
// Overwrites GVK based URL when has URLOption
if patchOpts.URLOption != nil {
absPath := patchOpts.URLOption.URL()
if patchOpts.Workspace != nil {
absPath = append(absPath, "workspaces", patchOpts.Workspace.Name)
}
req = req.AbsPath(absPath...)
}
if patchOpts.URLOption == nil || patchOpts.URLOption.AbsPath == "" {
req.NamespaceIfScoped(o.GetNamespace(), o.IsNamespaced()).
Resource(o.Resource()).
Name(o.GetName())
}
return req.VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), c.paramCodec).
Body(data).
Do(ctx).
Into(obj)
}
// Get implements client.Client
func (c *typedClient) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object, opts ...client.GetOption) error {
r, err := c.cache.GetResource(obj)
if err != nil {
return err
}
getOpts := client.GetOptions{}
getOpts.ApplyOptions(opts)
req := r.Get()
// Overwrites GVK based URL when has URLOption
if getOpts.URLOption != nil {
absPath := getOpts.URLOption.URL()
if getOpts.Workspace != nil {
absPath = append(absPath, "workspaces", getOpts.Workspace.Name)
}
}
if getOpts.URLOption == nil || getOpts.URLOption.AbsPath == "" {
req = req.NamespaceIfScoped(key.Namespace, r.IsNamespaced()).
Resource(r.Resource()).
Name(key.Name)
}
return req.
Do(ctx).
Into(obj)
}
// List implements client.Client
func (c *typedClient) List(ctx context.Context, obj runtime.Object, opts ...client.ListOption) error {
r, err := c.cache.GetResource(obj)
if err != nil {
return err
}
listOpts := client.ListOptions{}
listOpts.ApplyOptions(opts)
req := r.Get()
// Overwrites GVK based URL when has URLOption
if listOpts.URLOption != nil {
absPath := listOpts.URLOption.URL()
if listOpts.Workspace != nil {
absPath = append(absPath, "workspaces", listOpts.Workspace.Name)
}
}
if listOpts.URLOption == nil || listOpts.URLOption.AbsPath == "" {
req = req.NamespaceIfScoped(listOpts.Namespace, r.IsNamespaced()).
Resource(r.Resource()).
VersionedParams(listOpts.AsListOptions(), c.paramCodec)
}
return req.
Do(ctx).
Into(obj)
}
// UpdateStatus used by StatusWriter to write status.
func (c *typedClient) UpdateStatus(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error {
o, err := c.cache.GetObjMeta(obj)
if err != nil {
return err
}
// It will be nice to receive an error saying the object doesn't implement
// status subresource and check CRD definition
return o.Put().
NamespaceIfScoped(o.GetNamespace(), o.IsNamespaced()).
Resource(o.Resource()).
Name(o.GetName()).
SubResource("status").
Body(obj).
VersionedParams((&client.UpdateOptions{}).ApplyOptions(opts).AsUpdateOptions(), c.paramCodec).
Do(ctx).
Into(obj)
}
// PatchStatus used by StatusWriter to write status.
func (c *typedClient) PatchStatus(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOption) error {
o, err := c.cache.GetObjMeta(obj)
if err != nil {
return err
}
data, err := patch.Data(obj)
if err != nil {
return err
}
patchOpts := &client.PatchOptions{}
return o.Patch(patch.Type()).
NamespaceIfScoped(o.GetNamespace(), o.IsNamespaced()).
Resource(o.Resource()).
Name(o.GetName()).
SubResource("status").
Body(data).
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), c.paramCodec).
Do(ctx).
Into(obj)
}
// DeleteAllOf implements client.Client
func (c *typedClient) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...client.DeleteAllOfOption) error {
o, err := c.cache.GetObjMeta(obj)
if err != nil {
return err
}
deleteAllOfOpts := client.DeleteAllOfOptions{}
deleteAllOfOpts.ApplyOptions(opts)
return o.Delete().
NamespaceIfScoped(deleteAllOfOpts.ListOptions.Namespace, o.IsNamespaced()).
Resource(o.Resource()).
VersionedParams(deleteAllOfOpts.AsListOptions(), c.paramCodec).
Body(deleteAllOfOpts.AsDeleteOptions()).
Do(ctx).
Error()
}
/*
Copyright 2020 KubeSphere 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 generic
import (
"context"
"fmt"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"kubesphere.io/client-go/client"
)
// client is a client.Client that reads and writes directly from/to an API server. It lazily initializes
// new clients at the time they are used, and caches the client.
type unstructuredClient struct {
cache client.ClientCache
paramCodec runtime.ParameterCodec
}
// Create implements client.Client
func (uc *unstructuredClient) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOption) error {
u, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}
gvk := u.GroupVersionKind()
o, err := uc.cache.GetObjMeta(obj)
if err != nil {
return err
}
createOpts := &client.CreateOptions{}
createOpts.ApplyOptions(opts)
result := o.Post().
NamespaceIfScoped(o.GetNamespace(), o.IsNamespaced()).
Resource(o.Resource()).
Body(obj).
VersionedParams(createOpts.AsCreateOptions(), uc.paramCodec).
Do(ctx).
Into(obj)
u.SetGroupVersionKind(gvk)
return result
}
// Update implements client.Client
func (uc *unstructuredClient) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error {
u, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}
gvk := u.GroupVersionKind()
o, err := uc.cache.GetObjMeta(obj)
if err != nil {
return err
}
updateOpts := client.UpdateOptions{}
updateOpts.ApplyOptions(opts)
result := o.Put().
NamespaceIfScoped(o.GetNamespace(), o.IsNamespaced()).
Resource(o.Resource()).
Name(o.GetName()).
Body(obj).
VersionedParams(updateOpts.AsUpdateOptions(), uc.paramCodec).
Do(ctx).
Into(obj)
u.SetGroupVersionKind(gvk)
return result
}
// Delete implements client.Client
func (uc *unstructuredClient) Delete(ctx context.Context, obj runtime.Object, opts ...client.DeleteOption) error {
_, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}
o, err := uc.cache.GetObjMeta(obj)
if err != nil {
return err
}
deleteOpts := client.DeleteOptions{}
deleteOpts.ApplyOptions(opts)
return o.Delete().
NamespaceIfScoped(o.GetNamespace(), o.IsNamespaced()).
Resource(o.Resource()).
Name(o.GetName()).
Body(deleteOpts.AsDeleteOptions()).
Do(ctx).
Error()
}
// DeleteAllOf implements client.Client
func (uc *unstructuredClient) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...client.DeleteAllOfOption) error {
_, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}
o, err := uc.cache.GetObjMeta(obj)
if err != nil {
return err
}
deleteAllOfOpts := client.DeleteAllOfOptions{}
deleteAllOfOpts.ApplyOptions(opts)
return o.Delete().
NamespaceIfScoped(deleteAllOfOpts.ListOptions.Namespace, o.IsNamespaced()).
Resource(o.Resource()).
VersionedParams(deleteAllOfOpts.AsListOptions(), uc.paramCodec).
Body(deleteAllOfOpts.AsDeleteOptions()).
Do(ctx).
Error()
}
// Patch implements client.Client
func (uc *unstructuredClient) Patch(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOption) error {
_, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}
o, err := uc.cache.GetObjMeta(obj)
if err != nil {
return err
}
data, err := patch.Data(obj)
if err != nil {
return err
}
patchOpts := &client.PatchOptions{}
return o.Patch(patch.Type()).
NamespaceIfScoped(o.GetNamespace(), o.IsNamespaced()).
Resource(o.Resource()).
Name(o.GetName()).
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), uc.paramCodec).
Body(data).
Do(ctx).
Into(obj)
}
// Get implements client.Client
func (uc *unstructuredClient) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object, opts ...client.GetOption) error {
u, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}
gvk := u.GroupVersionKind()
r, err := uc.cache.GetResource(obj)
if err != nil {
return err
}
result := r.Get().
NamespaceIfScoped(key.Namespace, r.IsNamespaced()).
Resource(r.Resource()).
Name(key.Name).
Do(ctx).
Into(obj)
u.SetGroupVersionKind(gvk)
return result
}
// List implements client.Client
func (uc *unstructuredClient) List(ctx context.Context, obj runtime.Object, opts ...client.ListOption) error {
u, ok := obj.(*unstructured.UnstructuredList)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}
gvk := u.GroupVersionKind()
if strings.HasSuffix(gvk.Kind, "List") {
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
}
listOpts := client.ListOptions{}
listOpts.ApplyOptions(opts)
r, err := uc.cache.GetResource(obj)
if err != nil {
return err
}
return r.Get().
NamespaceIfScoped(listOpts.Namespace, r.IsNamespaced()).
Resource(r.Resource()).
VersionedParams(listOpts.AsListOptions(), uc.paramCodec).
Do(ctx).
Into(obj)
}
func (uc *unstructuredClient) UpdateStatus(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error {
_, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}
o, err := uc.cache.GetObjMeta(obj)
if err != nil {
return err
}
return o.Put().
NamespaceIfScoped(o.GetNamespace(), o.IsNamespaced()).
Resource(o.Resource()).
Name(o.GetName()).
SubResource("status").
Body(obj).
VersionedParams((&client.UpdateOptions{}).ApplyOptions(opts).AsUpdateOptions(), uc.paramCodec).
Do(ctx).
Into(obj)
}
func (uc *unstructuredClient) PatchStatus(ctx context.Context, obj runtime.Object, patch client.Patch, opts ...client.PatchOption) error {
u, ok := obj.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("unstructured client did not understand object: %T", obj)
}
gvk := u.GroupVersionKind()
o, err := uc.cache.GetObjMeta(obj)
if err != nil {
return err
}
data, err := patch.Data(obj)
if err != nil {
return err
}
patchOpts := &client.PatchOptions{}
result := o.Patch(patch.Type()).
NamespaceIfScoped(o.GetNamespace(), o.IsNamespaced()).
Resource(o.Resource()).
Name(o.GetName()).
SubResource("status").
Body(data).
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), uc.paramCodec).
Do(ctx).
Into(u)
u.SetGroupVersionKind(gvk)
return result
}
/*
Copyright 2020 KubeSphere 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 client
import (
"context"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
)
// ObjectKey identifies a Kubernetes Object.
type ObjectKey = types.NamespacedName
// ObjectKeyFromObject returns the ObjectKey given a runtime.Object
func ObjectKeyFromObject(obj runtime.Object) (ObjectKey, error) {
accessor, err := meta.Accessor(obj)
if err != nil {
return ObjectKey{}, err
}
return ObjectKey{Namespace: accessor.GetNamespace(), Name: accessor.GetName()}, nil
}
// Patch is a patch that can be applied to a Kubernetes object.
type Patch interface {
// Type is the PatchType of the patch.
Type() types.PatchType
// Data is the raw data representing the patch.
Data(obj runtime.Object) ([]byte, error)
}
// Reader knows how to read and list Kubernetes objects.
type Reader interface {
// Get retrieves an obj for the given object key from the Kubernetes Cluster.
// obj must be a struct pointer so that obj can be updated with the response
// returned by the Server.
Get(ctx context.Context, key ObjectKey, obj runtime.Object, opts ...GetOption) error
// List retrieves list of objects for a given namespace and list options. On a
// successful call, Items field in the list will be populated with the
// result returned from the server.
List(ctx context.Context, list runtime.Object, opts ...ListOption) error
}
// Writer knows how to create, delete, and update Kubernetes objects.
type Writer interface {
// Create saves the object obj in the Kubernetes cluster.
Create(ctx context.Context, obj runtime.Object, opts ...CreateOption) error
// Delete deletes the given obj from Kubernetes cluster.
Delete(ctx context.Context, obj runtime.Object, opts ...DeleteOption) error
// Update updates the given obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
Update(ctx context.Context, obj runtime.Object, opts ...UpdateOption) error
// Patch patches the given obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
Patch(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOption) error
// DeleteAllOf deletes all objects of the given type matching the given options.
DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...DeleteAllOfOption) error
}
// StatusClient knows how to create a client which can update status subresource
// for kubernetes objects.
type StatusClient interface {
Status() StatusWriter
}
// StatusWriter knows how to update status subresource of a Kubernetes object.
type StatusWriter interface {
// Update updates the fields corresponding to the status subresource for the
// given obj. obj must be a struct pointer so that obj can be updated
// with the content returned by the Server.
Update(ctx context.Context, obj runtime.Object, opts ...UpdateOption) error
// Patch patches the given object's subresource. obj must be a struct
// pointer so that obj can be updated with the content returned by the
// Server.
Patch(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOption) error
}
// Client knows how to perform CRUD operations on Kubernetes objects.
type Client interface {
Reader
Writer
StatusClient
}
// IgnoreNotFound returns nil on NotFound errors.
// All other values that are not NotFound errors or nil are returned unmodified.
func IgnoreNotFound(err error) error {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
此差异已折叠。
/*
Copyright 2020 KubeSphere 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 resource
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"kubesphere.io/client-go/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)
// client is a client.Reader that reads directly from an KubeSphere API server. It lazily initializes
// new clients at the time they are used, and caches the client.
// TODO(Roland): Paging and sorting parameter should be supported.
type typedClient struct {
cache client.ClientCache
paramCodec runtime.ParameterCodec
}
func New(config *rest.Config, options client.Options) (client.Reader, error) {
if config == nil {
return nil, fmt.Errorf("must provide non-nil rest.Config to client.New")
}
// Init a scheme if none provided
if options.Scheme == nil {
options.Scheme = scheme.Scheme
}
// Init a Mapper if none provided
if options.Mapper == nil {
var err error
options.Mapper, err = apiutil.NewDynamicRESTMapper(config)
if err != nil {
return nil, err
}
}
clientcache := client.NewClientCache(config, options)
typedClient := &typedClient{
cache: clientcache,
paramCodec: runtime.NewParameterCodec(options.Scheme),
}
return typedClient, nil
}
// Get implements client.Client
func (c *typedClient) Get(ctx context.Context, key client.ObjectKey, obj runtime.Object, opts ...client.GetOption) error {
r, err := c.cache.GetResource(obj)
if err != nil {
return err
}
// {ksapi}/namespaces/{namespace}/{resources}/{name}
// {ksapi}/{resources}/{name}
return r.Get().
AbsPath("kapis", "resources.kubesphere.io", "v1alpha3").
NamespaceIfScoped(key.Namespace, r.IsNamespaced()).
Resource(r.Resource()).
Name(key.Name).
Do(ctx).
Into(obj)
}
// List implements client.Client
func (c *typedClient) List(ctx context.Context, obj runtime.Object, opts ...client.ListOption) error {
r, err := c.cache.GetResource(obj)
if err != nil {
return err
}
listOpts := client.ListOptions{}
listOpts.ApplyOptions(opts)
// {ksapi}/namespaces/{namespace}/{resources}
// {ksapi}/{resources}
return r.Get().
AbsPath("kapis", "resources.kubesphere.io", "v1alpha3").
NamespaceIfScoped(listOpts.Namespace, r.IsNamespaced()).
Resource(r.Resource()).
VersionedParams(listOpts.AsListOptions(), c.paramCodec).
Do(ctx).
Into(obj)
}
// This is a generated file. Do not edit directly.
// Run hack/pin-dependency.sh to change pinned dependency versions.
// Run hack/update-vendor.sh to update go.mod files and the vendor directory.
module kubesphere.io/client-go
go 1.13
require (
k8s.io/apimachinery v0.18.6
k8s.io/client-go v0.18.6
sigs.k8s.io/controller-runtime v0.6.4
)
此差异已折叠。
/*
Copyright 2020 KubeSphere 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 e2e
const (
DefaultWorkspaceRoleAdmin = "%v-admin"
)
/*
Copyright 2020 KubeSphere 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 e2e
import (
"testing"
"github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/config"
"github.com/onsi/gomega"
"k8s.io/klog"
"kubesphere.io/kubesphere/test/e2e/framework/ginkgowrapper"
)
// RunE2ETests checks configuration parameters (specified through flags) and then runs
// E2E tests using the Ginkgo runner.
// This function is called on each Ginkgo node in parallel mode.
func RunE2ETests(t *testing.T) {
gomega.RegisterFailHandler(ginkgowrapper.Fail)
klog.Infof("Starting e2e run on Ginkgo node %d", config.GinkgoConfig.ParallelNode)
ginkgo.RunSpecs(t, "KubeSphere e2e suite")
}
......@@ -14,160 +14,20 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e_test
package e2e
import (
"context"
"time"
"os"
"testing"
"k8s.io/klog"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes/scheme"
"kubesphere.io/kubesphere/pkg/apis/network/v1alpha1"
"kubesphere.io/kubesphere/pkg/test"
"sigs.k8s.io/controller-runtime/pkg/client"
"kubesphere.io/kubesphere/test/e2e/framework"
)
var simpleDeployYaml = `apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: production
labels:
name: nginx
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
labels:
name: nginx
app: nginx
color : red
spec:
containers:
- image: nginx:alpine
name: nginx
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: "20m"
memory: "55M"
env:
- name: ENVVARNAME
value: ENVVARVALUE
ports:
- containerPort: 80
name: http
restartPolicy: Always`
var simpleNPYaml = `apiVersion: network.kubesphere.io/v1alpha1
kind: NamespaceNetworkPolicy
metadata:
name: allow-icmp-only
namespace: production
spec:
selector: color == 'red'
ingress:
- action: Allow
protocol: ICMP
source:
selector: color == 'blue'
namespaceSelector: all()`
var simpleJobYaml = `apiVersion: batch/v1
kind: Job
metadata:
name: test-connect
namespace: production
spec:
template:
metadata:
labels:
color : blue
spec:
containers:
- name: test-connect
image: alpine
command: ["ping", "1.1.1.1"]
restartPolicy: Never
backoffLimit: 1`
var testNs = "production"
var _ = Describe("E2e for network policy", func() {
BeforeEach(func() {
Expect(test.EnsureNamespace(ctx.Client, testNs)).ShouldNot(HaveOccurred())
})
AfterEach(func() {
Expect(test.DeleteNamespace(ctx.Client, testNs)).ShouldNot(HaveOccurred())
ns := &corev1.Namespace{}
ns.Name = testNs
Expect(test.WaitForDeletion(ctx.Client, ns, time.Second*5, time.Minute)).ShouldNot(HaveOccurred())
})
It("Should work well in simple namespaceNetworkPolicy", func() {
decode := scheme.Codecs.UniversalDeserializer().Decode
obj, _, err := decode([]byte(simpleDeployYaml), nil, nil)
Expect(err).ShouldNot(HaveOccurred(), "Failed to parse yaml")
deploy := obj.(*appsv1.Deployment)
Expect(ctx.Client.Create(context.TODO(), obj)).ShouldNot(HaveOccurred())
Expect(test.WaitForController(ctx.Client, deploy.Namespace, deploy.Name, *deploy.Spec.Replicas, time.Second*2, time.Minute)).ShouldNot(HaveOccurred())
defer func() {
Expect(ctx.Client.Delete(context.TODO(), deploy)).ShouldNot(HaveOccurred())
}()
obj, _, err = decode([]byte(simpleNPYaml), nil, nil)
Expect(err).ShouldNot(HaveOccurred(), "Failed to parse networkpolicy yaml")
np := obj.(*v1alpha1.NamespaceNetworkPolicy)
Expect(ctx.Client.Create(context.TODO(), np)).ShouldNot(HaveOccurred())
defer func() {
Expect(ctx.Client.Delete(context.TODO(), np)).ShouldNot(HaveOccurred())
Expect(test.WaitForDeletion(ctx.Client, np, time.Second*2, time.Minute)).ShouldNot(HaveOccurred())
}()
obj, _, err = decode([]byte(simpleJobYaml), nil, nil)
Expect(err).ShouldNot(HaveOccurred(), "Failed to parse job yaml")
//create a job to test
job := obj.(*batchv1.Job)
selector, _ := labels.Parse("app=nginx")
podlist := &corev1.PodList{}
Expect(ctx.Client.List(context.TODO(), podlist, &client.ListOptions{
Namespace: deploy.Namespace,
LabelSelector: selector,
})).ShouldNot(HaveOccurred())
Expect(podlist.Items).To(HaveLen(int(*deploy.Spec.Replicas)))
podip := podlist.Items[0].Status.PodIP
job.Spec.Template.Spec.Containers[0].Command = []string{"ping", "-c", "4", podip}
job.Spec.Template.Labels["color"] = "yellow"
orginalJob := job.DeepCopy()
Expect(ctx.Client.Create(context.TODO(), job)).ShouldNot(HaveOccurred())
defer func() {
Expect(ctx.Client.Delete(context.TODO(), job)).ShouldNot(HaveOccurred())
}()
klog.Infoln("sleep 10s to wait for controller creating np")
time.Sleep(time.Second * 10)
Expect(test.WaitForJobFail(ctx.Client, job.Namespace, job.Name, time.Second*3, time.Minute)).ShouldNot(HaveOccurred(), "Failed to block connection")
func TestMain(m *testing.M) {
framework.ParseFlags()
os.Exit(m.Run())
}
//change job color
job = orginalJob.DeepCopy()
Expect(ctx.Client.Delete(context.TODO(), job)).ShouldNot(HaveOccurred())
Expect(test.WaitForDeletion(ctx.Client, job, time.Second*2, time.Minute)).ShouldNot(HaveOccurred())
job.Spec.Template.Labels["color"] = "blue"
Expect(ctx.Client.Create(context.TODO(), job)).ShouldNot(HaveOccurred())
Expect(test.WaitForJobSucceed(ctx.Client, job.Namespace, job.Name, time.Second*3, time.Minute)).ShouldNot(HaveOccurred(), "Connection failed")
})
})
func TestE2E(t *testing.T) {
RunE2ETests(t)
}
/*
Copyright 2020 KubeSphere 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 framework
import (
"github.com/onsi/gomega"
)
// ExpectEqual expects the specified two are the same, otherwise an exception raises
func ExpectEqual(actual interface{}, extra interface{}, explain ...interface{}) {
gomega.ExpectWithOffset(1, actual).To(gomega.Equal(extra), explain...)
}
// ExpectNotEqual expects the specified two are not the same, otherwise an exception raises
func ExpectNotEqual(actual interface{}, extra interface{}, explain ...interface{}) {
gomega.ExpectWithOffset(1, actual).NotTo(gomega.Equal(extra), explain...)
}
// ExpectError expects an error happens, otherwise an exception raises
func ExpectError(err error, explain ...interface{}) {
gomega.ExpectWithOffset(1, err).To(gomega.HaveOccurred(), explain...)
}
// ExpectNoError checks if "err" is set, and if so, fails assertion while logging the error.
func ExpectNoError(err error, explain ...interface{}) {
ExpectNoErrorWithOffset(1, err, explain...)
}
// ExpectNoErrorWithOffset checks if "err" is set, and if so, fails assertion while logging the error at "offset" levels above its caller
// (for example, for call chain f -> g -> ExpectNoErrorWithOffset(1, ...) error would be logged for "f").
func ExpectNoErrorWithOffset(offset int, err error, explain ...interface{}) {
gomega.ExpectWithOffset(1+offset, err).NotTo(gomega.HaveOccurred(), explain...)
}
// ExpectConsistOf expects actual contains precisely the extra elements. The ordering of the elements does not matter.
func ExpectConsistOf(actual interface{}, extra interface{}, explain ...interface{}) {
gomega.ExpectWithOffset(1, actual).To(gomega.ConsistOf(extra), explain...)
}
// ExpectHaveKey expects the actual map has the key in the keyset
func ExpectHaveKey(actual interface{}, key interface{}, explain ...interface{}) {
gomega.ExpectWithOffset(1, actual).To(gomega.HaveKey(key), explain...)
}
// ExpectEmpty expects actual is empty
func ExpectEmpty(actual interface{}, explain ...interface{}) {
gomega.ExpectWithOffset(1, actual).To(gomega.BeEmpty(), explain...)
}
/*
Copyright 2020 KubeSphere 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 framework
import (
"context"
"fmt"
"github.com/onsi/ginkgo" //nolint:stylecheck
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"kubesphere.io/client-go/client"
"kubesphere.io/client-go/client/generic"
"kubesphere.io/kubesphere/pkg/apis"
"kubesphere.io/kubesphere/pkg/constants"
"kubesphere.io/kubesphere/test/e2e/framework/workspace"
)
type Framework struct {
BaseName string
Workspace string
Namespaces []string
Scheme *runtime.Scheme
}
// KubeSphereFramework provides an interface to a test control plane so
// that the implementation can vary without affecting tests.
type KubeSphereFramework interface {
GenericClient(userAgent string) client.Client
KubeSphereSystemNamespace() string
// Name of the workspace for the current test to target
TestWorkSpaceName() string
// Create a Namespace under current Worksapce
CreateNamespace(name string) string
// Get Names of the namespaces for the current test to target
GetNamespaceNames() []string
}
func NewKubeSphereFramework(baseName string) KubeSphereFramework {
sch := runtime.NewScheme()
if err := apis.AddToScheme(sch); err != nil {
Failf("unable add KubeSphere APIs to scheme: %v", err)
}
if err := scheme.AddToScheme(sch); err != nil {
Failf("unable add Kubernetes APIs to scheme: %v", err)
}
f := &Framework{
BaseName: baseName,
Scheme: sch,
}
ginkgo.AfterEach(f.AfterEach)
ginkgo.BeforeEach(f.BeforeEach)
return f
}
// BeforeEach
func (f *Framework) BeforeEach() {
}
// AfterEach
func (f *Framework) AfterEach() {
}
func (f *Framework) TestWorkSpaceName() string {
if f.Workspace == "" {
f.Workspace = CreateTestWorkSpace(f.GenericClient(f.BaseName), f.BaseName)
}
return f.Workspace
}
func CreateTestWorkSpace(client client.Client, baseName string) string {
ginkgo.By("Creating a WorkSpace to execute the test in")
wspt := workspace.NewWorkspaceTemplate("", "admin")
wspt.GenerateName = fmt.Sprintf("e2e-tests-%v-", baseName)
wspt, err := workspace.CreateWorkspace(client, wspt)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
ginkgo.By(fmt.Sprintf("Created test namespace %s", wspt.Name))
return wspt.Name
}
func (f *Framework) GetNamespaceNames() []string {
return f.Namespaces
}
func (f *Framework) CreateNamespace(name string) string {
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
constants.WorkspaceLabelKey: f.TestWorkSpaceName(),
},
},
}
opts := &client.URLOptions{
Group: "tenant.kubesphere.io",
Version: "v1alpha2",
}
err := f.GenericClient(f.BaseName).Create(context.TODO(), ns, opts, &client.WorkspaceOptions{Name: f.TestWorkSpaceName()})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
return ns.Name
}
func (f *Framework) KubeSphereSystemNamespace() string {
return "Kubesphere-system"
}
func (f *Framework) GenericClient(userAgent string) client.Client {
ctx := TestContext
config := &rest.Config{
Host: "127.0.0.1:9090",
Username: "admin",
Password: "P@88w0rd",
}
if ctx.Host != "" {
config = &rest.Config{
Host: ctx.Host,
Username: ctx.Username,
Password: ctx.Password,
}
}
rest.AddUserAgent(config, userAgent)
return generic.NewForConfigOrDie(config, client.Options{Scheme: f.Scheme})
}
/*
Copyright 2020 KubeSphere 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 ginkgowrapper wraps Ginkgo Fail and Skip functions to panic
// with structured data instead of a constant string.
package ginkgowrapper
import (
"bufio"
"bytes"
"regexp"
"runtime"
"runtime/debug"
"strings"
"github.com/onsi/ginkgo"
)
// FailurePanic is the value that will be panicked from Fail.
type FailurePanic struct {
Message string // The failure message passed to Fail
Filename string // The filename that is the source of the failure
Line int // The line number of the filename that is the source of the failure
FullStackTrace string // A full stack trace starting at the source of the failure
}
// String makes FailurePanic look like the old Ginkgo panic when printed.
func (FailurePanic) String() string { return ginkgo.GINKGO_PANIC }
// Fail wraps ginkgo.Fail so that it panics with more useful
// information about the failure. This function will panic with a
// FailurePanic.
func Fail(message string, callerSkip ...int) {
skip := 1
if len(callerSkip) > 0 {
skip += callerSkip[0]
}
_, file, line, _ := runtime.Caller(skip)
fp := FailurePanic{
Message: message,
Filename: file,
Line: line,
FullStackTrace: pruneStack(skip),
}
defer func() {
e := recover()
if e != nil {
panic(fp)
}
}()
ginkgo.Fail(message, skip)
}
// ginkgo adds a lot of test running infrastructure to the stack, so
// we filter those out
var stackSkipPattern = regexp.MustCompile(`onsi/ginkgo`)
func pruneStack(skip int) string {
skip += 2 // one for pruneStack and one for debug.Stack
stack := debug.Stack()
scanner := bufio.NewScanner(bytes.NewBuffer(stack))
var prunedStack []string
// skip the top of the stack
for i := 0; i < 2*skip+1; i++ {
scanner.Scan()
}
for scanner.Scan() {
if stackSkipPattern.Match(scanner.Bytes()) {
scanner.Scan() // these come in pairs
} else {
prunedStack = append(prunedStack, scanner.Text())
scanner.Scan() // these come in pairs
prunedStack = append(prunedStack, scanner.Text())
}
}
return strings.Join(prunedStack, "\n")
}
/*
Copyright 2020 KubeSphere 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 iam
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
iamv1alpha2 "kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2"
"kubesphere.io/client-go/client"
)
var URLOptions = client.URLOptions{
Group: "iam.kubesphere.io",
Version: "v1alpha2",
}
// NewUser returns a User spec with the specified argument.
func NewUser(name, globelRole string) *iamv1alpha2.User {
return &iamv1alpha2.User{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Annotations: map[string]string{
"iam.kubesphere.io/globalrole": globelRole,
},
},
Spec: iamv1alpha2.UserSpec{
Email: fmt.Sprint("%v@kubesphere.io", name),
EncryptedPassword: "P@88w0rd",
},
}
}
// CreateUser uses c to create User. If the returned error is nil, the returned User is valid and has
// been created.
func CreateUser(c client.Client, u *iamv1alpha2.User) (*iamv1alpha2.User, error) {
err := c.Create(context.TODO(), u, &URLOptions)
return u, err
}
// GetUser uses c to get the User by name. If the returned error is nil, the returned User is valid.
func GetUser(c client.Client, name string) (*iamv1alpha2.User, error) {
u := &iamv1alpha2.User{}
err := c.Get(context.TODO(), client.ObjectKey{Name: name}, u, &URLOptions)
if err != nil {
return nil, err
}
return u, nil
}
// NewGroup returns a Group spec with the specified argument.
func NewGroup(name, workspace string) *iamv1alpha2.Group {
return &iamv1alpha2.Group{
ObjectMeta: metav1.ObjectMeta{
GenerateName: name,
},
}
}
// CreateGroup uses c to create Group. If the returned error is nil, the returned Group is valid and has
// been created.
func CreateGroup(c client.Client, u *iamv1alpha2.Group, workspace string) (*iamv1alpha2.Group, error) {
err := c.Create(context.TODO(), u, &URLOptions, &client.WorkspaceOptions{Name: workspace})
return u, err
}
// GetGroup uses c to get the User by name. If the returned error is nil, the returned User is valid.
func GetGroup(c client.Client, name, workspace string) (*iamv1alpha2.Group, error) {
u := &iamv1alpha2.Group{}
err := c.Get(context.TODO(), client.ObjectKey{Name: name}, u, &URLOptions, &client.WorkspaceOptions{Name: workspace})
if err != nil {
return nil, err
}
return u, nil
}
/*
Copyright 2020 KubeSphere 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 framework
import (
"bytes"
"fmt"
"regexp"
"runtime/debug"
"time"
"github.com/onsi/ginkgo"
"kubesphere.io/kubesphere/test/e2e/framework/ginkgowrapper"
)
func nowStamp() string {
return time.Now().Format(time.StampMilli)
}
func log(level string, format string, args ...interface{}) {
fmt.Fprintf(ginkgo.GinkgoWriter, nowStamp()+": "+level+": "+format+"\n", args...)
}
// Logf logs the info.
func Logf(format string, args ...interface{}) {
log("INFO", format, args...)
}
// Failf logs the fail info, including a stack trace.
func Failf(format string, args ...interface{}) {
FailfWithOffset(1, format, args...)
}
// FailfWithOffset calls "Fail" and logs the error with a stack trace that starts at "offset" levels above its caller
// (for example, for call chain f -> g -> FailfWithOffset(1, ...) error would be logged for "f").
func FailfWithOffset(offset int, format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
skip := offset + 1
log("FAIL", "%s\n\nFull Stack Trace\n%s", msg, PrunedStack(skip))
ginkgowrapper.Fail(nowStamp()+": "+msg, skip)
}
// Fail is a replacement for ginkgo.Fail which logs the problem as it occurs
// together with a stack trace and then calls ginkgowrapper.Fail.
func Fail(msg string, callerSkip ...int) {
skip := 1
if len(callerSkip) > 0 {
skip += callerSkip[0]
}
log("FAIL", "%s\n\nFull Stack Trace\n%s", msg, PrunedStack(skip))
ginkgowrapper.Fail(nowStamp()+": "+msg, skip)
}
var codeFilterRE = regexp.MustCompile(`/github.com/onsi/ginkgo/`)
// PrunedStack is a wrapper around debug.Stack() that removes information
// about the current goroutine and optionally skips some of the initial stack entries.
// With skip == 0, the returned stack will start with the caller of PruneStack.
// From the remaining entries it automatically filters out useless ones like
// entries coming from Ginkgo.
//
// This is a modified copy of PruneStack in https://github.com/onsi/ginkgo/blob/f90f37d87fa6b1dd9625e2b1e83c23ffae3de228/internal/codelocation/code_location.go#L25:
// - simplified API and thus renamed (calls debug.Stack() instead of taking a parameter)
// - source code filtering updated to be specific to Kubernetes
// - optimized to use bytes and in-place slice filtering from
// https://github.com/golang/go/wiki/SliceTricks#filter-in-place
func PrunedStack(skip int) []byte {
fullStackTrace := debug.Stack()
stack := bytes.Split(fullStackTrace, []byte("\n"))
// Ensure that the even entries are the method names and the
// the odd entries the source code information.
if len(stack) > 0 && bytes.HasPrefix(stack[0], []byte("goroutine ")) {
// Ignore "goroutine 29 [running]:" line.
stack = stack[1:]
}
// The "+2" is for skipping over:
// - runtime/debug.Stack()
// - PrunedStack()
skip += 2
if len(stack) > 2*skip {
stack = stack[2*skip:]
}
n := 0
for i := 0; i < len(stack)/2; i++ {
// We filter out based on the source code file name.
if !codeFilterRE.Match([]byte(stack[i*2+1])) {
stack[n] = stack[i*2]
stack[n+1] = stack[i*2+1]
n += 2
}
}
stack = stack[:n]
return bytes.Join(stack, []byte("\n"))
}
......@@ -14,43 +14,34 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e_test
package framework
import (
"flag"
"os"
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/klog"
"kubesphere.io/kubesphere/pkg/apis/network/v1alpha1"
"kubesphere.io/kubesphere/pkg/test"
)
var ctx *test.TestCtx
type TestContextType struct {
Host string
InMemoryTest bool
Username string
Password string
}
func TestE2e(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Networking E2e Suite")
func registerFlags(t *TestContextType) {
flag.BoolVar(&t.InMemoryTest, "in-memory-test", false,
"Whether KubeSphere controllers and APIServer be started in memory.")
flag.StringVar(&t.Host, "ks-apiserver", os.Getenv("KS_APISERVER"),
"KubeSphere API Server IP/DNS")
flag.StringVar(&t.Username, "username", os.Getenv("KS_USERNAME"),
"Username to login to KubeSphere API Server")
flag.StringVar(&t.Password, "password", os.Getenv("KS_PASSWORD"),
"Password to login to KubeSphere API Server")
}
var _ = BeforeSuite(func() {
klog.InitFlags(nil)
flag.Set("logtostderr", "false")
flag.Set("alsologtostderr", "false")
flag.Set("v", "4")
var TestContext *TestContextType = &TestContextType{}
func ParseFlags() {
registerFlags(TestContext)
flag.Parse()
klog.SetOutput(GinkgoWriter)
ctx = test.NewTestCtx(nil, os.Getenv("TEST_NAMESPACE"))
Expect(ctx.Setup(os.Getenv("YAML_PATH"), "", v1alpha1.AddToScheme)).ShouldNot(HaveOccurred())
deployName := os.Getenv("DEPLOY_NAME")
Expect(test.WaitForController(ctx.Client, ctx.Namespace, deployName, 1, time.Second*5, time.Minute)).ShouldNot(HaveOccurred(), "Controlller failed to start")
klog.Infoln("Controller is up, begin to test ")
})
var _ = AfterSuite(func() {
ctx.Cleanup(nil)
})
}
/*
Copyright 2020 KubeSphere 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 workspace
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
tenantv1alpha1 "kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha1"
tenantv1alpha2 "kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha2"
fedb1 "kubesphere.io/kubesphere/pkg/apis/types/v1beta1"
"context"
"kubesphere.io/client-go/client"
)
var URLOptions = client.URLOptions{
Group: "tenant.kubesphere.io",
Version: "v1alpha2",
}
// NewWorkspaceTemplate returns a WorkspaceTemplate spec with the specified argument.
func NewWorkspaceTemplate(name string, manager string, hosts ...string) *tenantv1alpha2.WorkspaceTemplate {
clusters := []fedb1.GenericClusterReference{}
if hosts != nil {
for _, h := range hosts {
clusters = append(clusters, fedb1.GenericClusterReference{Name: h})
}
}
return &tenantv1alpha2.WorkspaceTemplate{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: fedb1.FederatedWorkspaceSpec{
Placement: fedb1.GenericPlacementFields{
Clusters: clusters,
},
Template: fedb1.WorkspaceTemplate{
Spec: tenantv1alpha1.WorkspaceSpec{
Manager: manager,
},
},
},
}
}
// CreateWorkSpace uses c to create Workspace. If the returned error is nil, the returned Workspace is valid and has
// been created.
func CreateWorkspace(c client.Client, w *tenantv1alpha2.WorkspaceTemplate) (*tenantv1alpha2.WorkspaceTemplate, error) {
opts := &client.URLOptions{
AbsPath: "kapis/tenant.kubesphere.io/v1alpha2/workspaces",
}
err := c.Create(context.TODO(), w, opts)
return w, err
}
// GetJob uses c to get the Workspace by name. If the returned error is nil, the returned Workspace is valid.
func GetWorkspace(c client.Client, name string) (*tenantv1alpha1.Workspace, error) {
wsp := &tenantv1alpha1.Workspace{}
err := c.Get(context.TODO(), client.ObjectKey{Name: name}, wsp, &URLOptions)
if err != nil {
return nil, err
}
return wsp, nil
}
// DeleteWorkspace uses c to delete the Workspace by name. If the returned error is nil, the returned Workspace is valid.
func DeleteWorkspace(c client.Client, name string, opts ...client.DeleteOption) (*tenantv1alpha1.Workspace, error) {
wsp := &tenantv1alpha1.Workspace{
ObjectMeta: metav1.ObjectMeta{Name: name},
}
opts = append(opts, &URLOptions)
err := c.Delete(context.TODO(), wsp, opts...)
if err != nil {
return nil, err
}
return wsp, nil
}
/*
Copyright 2020 KubeSphere 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 e2e
import (
. "github.com/onsi/ginkgo" //nolint:stylecheck
. "github.com/onsi/gomega" //nolint:stylecheck
"kubesphere.io/client-go/client"
"kubesphere.io/kubesphere/test/e2e/framework"
"kubesphere.io/kubesphere/test/e2e/framework/iam"
)
var _ = Describe("Worksspace", func() {
f := framework.NewKubeSphereFramework("group")
var wsName = ""
var gclient client.Client
BeforeEach(func() {
gclient = f.GenericClient("group")
})
It("Should create group successfully", func() {
By("Expecting to create workspace thronght workspace template")
wsName = f.TestWorkSpaceName()
group, err := iam.CreateGroup(gclient, iam.NewGroup("group1", wsName), wsName)
framework.ExpectNoError(err)
Eventually(func() bool {
expGroup, err := iam.GetGroup(gclient, group.Name, wsName)
return err == nil && expGroup.Name == group.Name
}, timeout, interval).Should(BeTrue())
})
})
package e2e
import (
"context"
. "github.com/onsi/ginkgo" //nolint:stylecheck
. "github.com/onsi/gomega" //nolint:stylecheck
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"kubesphere.io/client-go/client"
"kubesphere.io/kubesphere/test/e2e/framework"
"kubesphere.io/kubesphere/test/e2e/framework/workspace"
)
var _ = Describe("API Test", func() {
f := framework.NewKubeSphereFramework("worksspace")
var gclient client.Client
BeforeEach(func() {
gclient = f.GenericClient("worksspace")
})
It("Should list Kubernetes objects through ks proxy", func() {
results := &corev1.NamespaceList{}
Expect(gclient.List(context.TODO(), results)).Should(Succeed())
if len(results.Items) < 1 {
framework.Failf("Test Failed caused no Cluster role was found")
}
framework.Logf("Response is %v", results)
})
It("Should get Kubernetes objects through ks proxy", func() {
deploy := &appsv1.Deployment{}
Expect(gclient.Get(context.TODO(), client.ObjectKey{Namespace: "kubesphere-system", Name: "ks-apiserver"}, deploy)).Should(Succeed())
Expect(deploy.Name).To(Equal("ks-apiserver"))
})
It("Should list Kubernetes objects through ksapi", func() {
results := &corev1.NamespaceList{}
opts := workspace.URLOptions
err := gclient.List(context.TODO(), results, &opts)
framework.ExpectNoError(err)
framework.Logf("Response is %v", results)
})
It("Should list Kubernetes objects through ksapi by workspaces", func() {
results := &corev1.NamespaceList{}
opts := workspace.URLOptions
err := gclient.List(context.TODO(), results, &opts, &client.WorkspaceOptions{Name: "kubesphere-system"})
framework.ExpectNoError(err)
framework.Logf("Response is %v", results)
})
})
/*
Copyright 2020 KubeSphere 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 e2e
import (
"context"
"time"
. "github.com/onsi/ginkgo" //nolint:stylecheck
. "github.com/onsi/gomega"
"kubesphere.io/client-go/client"
iamv1alpha2 "kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2"
"kubesphere.io/kubesphere/test/e2e/framework"
"kubesphere.io/kubesphere/test/e2e/framework/iam"
"kubesphere.io/kubesphere/test/e2e/framework/workspace"
)
const timeout = time.Second * 30
const interval = time.Second * 1
var _ = Describe("Worksspace", func() {
f := framework.NewKubeSphereFramework("worksspace")
var wsName = ""
var gclient client.Client
BeforeEach(func() {
gclient = f.GenericClient("worksspace")
})
It("Should create workspace and initial workspace settings", func() {
By("Expecting to create workspace thronght workspace template")
wsName = f.TestWorkSpaceName()
By("Expecting to create workspace successfully")
Eventually(func() bool {
wsp, err := workspace.GetWorkspace(gclient, wsName)
return err == nil && wsp.Name == wsName
}, timeout, interval).Should(BeTrue())
By("Expecting initial workspace roles successfully")
wspr := &iamv1alpha2.WorkspaceRoleList{}
Eventually(func() bool {
opts := iam.URLOptions
err := gclient.List(context.TODO(), wspr, &opts)
return err == nil && len(wspr.Items) > 0
}, timeout, interval).Should(BeTrue())
_, err := workspace.DeleteWorkspace(gclient, wsName)
framework.ExpectNoError(err)
framework.Logf("Workspace created successfully")
})
})
../../staging/src/kubesphere.io/client-go
\ No newline at end of file
......@@ -1532,6 +1532,9 @@ k8s.io/utils/net
k8s.io/utils/path
k8s.io/utils/pointer
k8s.io/utils/trace
# kubesphere.io/client-go v0.0.0 => ./staging/src/kubesphere.io/client-go
kubesphere.io/client-go/client
kubesphere.io/client-go/client/generic
# openpitrix.io/openpitrix v0.4.9-0.20200611125425-ae07f141e797 => openpitrix.io/openpitrix v0.4.9-0.20200611125425-ae07f141e797
openpitrix.io/openpitrix/pkg/config
openpitrix.io/openpitrix/pkg/constants
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册