Skip to content

Commit

Permalink
Merge branch 'master' into dev1.8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jinghzhu authored Dec 13, 2017
2 parents 8f5a0bd + 1d0fdae commit f75848f
Show file tree
Hide file tree
Showing 26 changed files with 725 additions and 457 deletions.
49 changes: 49 additions & 0 deletions apis/test/v1/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2017 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 v1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

var (
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
AddToScheme = SchemeBuilder.AddToScheme
)

// GroupName is the group name used in this package.
const GroupName = "cr.test.io"

// SchemeGroupVersion is the group version used to register these objects.
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: "v1"}

// Resource takes an unqualified resource and returns a Group-qualified GroupResource.
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

// addKnownTypes adds the set of types defined in this package to the supplied scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&Example{},
&ExampleList{},
)
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
}
11 changes: 6 additions & 5 deletions apis/test/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import (
const (
TestResourcePlural string = "tests"
// GroupName is the group name used in this package.
GroupName string = "test.io"
TestCRDName string = TestResourcePlural + "." + GroupName
version string = "v1"
StateCreated string = "Created"
StateProcessed string = "Processed"
GroupName string = "test.io"
TestCRDName string = TestResourcePlural + "." + GroupName
version string = "v1"
StateCreated string = "Created"
StateProcessed string = "Processed"
DefaultNamespace string = "default"
)

var (
Expand Down
79 changes: 79 additions & 0 deletions bk/apis/test0.io/v1/crd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package v1

import (
"fmt"
"reflect"
"time"

logger "github.com/jinghzhu/GoUtils/logger"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/wait"
)

func CreateCustomResourceDefinition(clientSet apiextensionsclient.Interface) (*apiextensionsv1beta1.CustomResourceDefinition, error) {
crd := &apiextensionsv1beta1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: TestCRDName,
},
Spec: apiextensionsv1beta1.CustomResourceDefinitionSpec{
Group: GroupName,
Version: SchemeGroupVersion.Version,
Scope: apiextensionsv1beta1.NamespaceScoped,
Names: apiextensionsv1beta1.CustomResourceDefinitionNames{
Plural: TestResourcePlural,
Kind: reflect.TypeOf(Test{}).Name(),
},
},
}
_, err := clientSet.ApiextensionsV1beta1().CustomResourceDefinitions().Create(crd)
if err != nil {
logger.Error("Fail to create CRD: " + err.Error())
return nil, err
}

// Wait for CRD creation.
err = wait.Poll(500*time.Millisecond, 60*time.Second, func() (bool, error) {
crd, err = clientSet.ApiextensionsV1beta1().CustomResourceDefinitions().Get(TestCRDName, metav1.GetOptions{})
if err != nil {
logger.Error("Fail to wait for CRD creation: " + err.Error())
return false, err
}
for _, cond := range crd.Status.Conditions {
switch cond.Type {
case apiextensionsv1beta1.Established:
if cond.Status == apiextensionsv1beta1.ConditionTrue {
return true, err
}
case apiextensionsv1beta1.NamesAccepted:
if cond.Status == apiextensionsv1beta1.ConditionFalse {
logger.Error(fmt.Sprintf("Name conflict while wait for CRD creation: %v, %v", cond.Reason, err))
}
}
}
return false, err
})
if err != nil {
deleteErr := clientSet.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(TestCRDName, nil)
if deleteErr != nil {
logger.Error("Fail to delete CRD: " + deleteErr.Error())
return nil, errors.NewAggregate([]error{err, deleteErr})
}
return nil, err
}
return crd, nil
}

// addKnownTypes adds the set of types defined in this package to the supplied scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&Test{},
&TestList{},
)
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)

return nil
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// +k8s:deepcopy-gen=package,register

// Package v1 is the v1 version of the API.
// +groupName=testio
// +groupName=test0.io
package v1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
const (
TestResourcePlural string = "tests"
// GroupName is the group name used in this package.
GroupName string = "testio"
GroupName string = "test0.io"
TestCRDName string = TestResourcePlural + "." + GroupName
version string = "v1"
StateCreated string = "Created"
Expand Down
152 changes: 152 additions & 0 deletions bk/apis/test0.io/v1/zz_generated.deepcopy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// +build !ignore_autogenerated

/*
Copyright 2017 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.
*/

// This file was autogenerated by deepcopy-gen. Do not edit it manually!

package v1

import (
reflect "reflect"

conversion "k8s.io/apimachinery/pkg/conversion"
runtime "k8s.io/apimachinery/pkg/runtime"
)

func init() {
SchemeBuilder.Register(RegisterDeepCopies)
}

// RegisterDeepCopies adds deep-copy functions to the given scheme. Public
// to allow building arbitrary schemes.
//
// Deprecated: deepcopy registration will go away when static deepcopy is fully implemented.
func RegisterDeepCopies(scheme *runtime.Scheme) error {
return scheme.AddGeneratedDeepCopyFuncs(
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*Test).DeepCopyInto(out.(*Test))
return nil
}, InType: reflect.TypeOf(&Test{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*TestList).DeepCopyInto(out.(*TestList))
return nil
}, InType: reflect.TypeOf(&TestList{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*TestSpec).DeepCopyInto(out.(*TestSpec))
return nil
}, InType: reflect.TypeOf(&TestSpec{})},
conversion.GeneratedDeepCopyFunc{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*TestStatus).DeepCopyInto(out.(*TestStatus))
return nil
}, InType: reflect.TypeOf(&TestStatus{})},
)
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Test) DeepCopyInto(out *Test) {
*out = *in
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
out.Spec = in.Spec
out.Status = in.Status
return
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Test.
func (in *Test) DeepCopy() *Test {
if in == nil {
return nil
}
out := new(Test)
in.DeepCopyInto(out)
return out
}

// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *Test) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
} else {
return nil
}
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *TestList) DeepCopyInto(out *TestList) {
*out = *in
out.TypeMeta = in.TypeMeta
out.ListMeta = in.ListMeta
if in.Items != nil {
in, out := &in.Items, &out.Items
*out = make([]Test, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TestList.
func (in *TestList) DeepCopy() *TestList {
if in == nil {
return nil
}
out := new(TestList)
in.DeepCopyInto(out)
return out
}

// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
func (in *TestList) DeepCopyObject() runtime.Object {
if c := in.DeepCopy(); c != nil {
return c
} else {
return nil
}
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *TestSpec) DeepCopyInto(out *TestSpec) {
*out = *in
return
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TestSpec.
func (in *TestSpec) DeepCopy() *TestSpec {
if in == nil {
return nil
}
out := new(TestSpec)
in.DeepCopyInto(out)
return out
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *TestStatus) DeepCopyInto(out *TestStatus) {
*out = *in
return
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TestStatus.
func (in *TestStatus) DeepCopy() *TestStatus {
if in == nil {
return nil
}
out := new(TestStatus)
in.DeepCopyInto(out)
return out
}
51 changes: 51 additions & 0 deletions bk/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package client

import (
"time"

"github.com/jinghzhu/GoUtils/logger"
crd "github.com/jinghzhu/k8scrd/apis/test0.io/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/rest"
)

func NewClient(cfg *rest.Config) (*rest.RESTClient, *runtime.Scheme, error) {
scheme := runtime.NewScheme()
if err := crd.AddToScheme(scheme); err != nil {
return nil, nil, err
}

config := *cfg
config.GroupVersion = &crd.SchemeGroupVersion
config.APIPath = "/apis"
config.ContentType = runtime.ContentTypeJSON
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: serializer.NewCodecFactory(scheme)}

client, err := rest.RESTClientFor(&config)
if err != nil {
logger.Error("Fail to generate REST client: " + err.Error())
return nil, nil, err
}

return client, scheme, nil
}

func WaitForInstanceProcessed(testClient *rest.RESTClient, name string) error {
return wait.Poll(100*time.Millisecond, 10*time.Second, func() (bool, error) {
var instance crd.Test
err := testClient.Get().
Resource(crd.TestResourcePlural).
Namespace(corev1.NamespaceDefault).
Name(name).
Do().Into(&instance)

if err == nil && instance.Status.State == crd.StateProcessed {
return true, nil
}

return false, err
})
}
Loading

0 comments on commit f75848f

Please sign in to comment.