-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ThirdPartyResource example: added watcher example, code cleanup #45463
Merged
k8s-github-robot
merged 1 commit into
kubernetes:master
from
nilebox:nilebox-tpr-watcher-example
May 9, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
staging/src/k8s.io/client-go/examples/third-party-resources/apis/tpr/v1/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
licenses(["notice"]) | ||
|
||
load( | ||
"@io_bazel_rules_go//go:def.bzl", | ||
"go_library", | ||
"go_test", | ||
) | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = ["types_test.go"], | ||
library = ":go_default_library", | ||
tags = ["automanaged"], | ||
deps = [ | ||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", | ||
], | ||
) | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = [ | ||
"register.go", | ||
"types.go", | ||
], | ||
tags = ["automanaged"], | ||
deps = [ | ||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library", | ||
], | ||
) |
56 changes: 56 additions & 0 deletions
56
staging/src/k8s.io/client-go/examples/third-party-resources/apis/tpr/v1/register.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
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 use in this package | ||
const GroupName = "tpr.client-go.k8s.io" | ||
|
||
// SchemeGroupVersion is 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() | ||
} | ||
|
||
func init() { | ||
// We only register manually written functions here. The registration of the | ||
// generated functions takes place in the generated files. The separation | ||
// makes the code compile even when the generated files are missing. | ||
SchemeBuilder.Register(addKnownTypes) | ||
} | ||
|
||
// Adds the list of known types to api.Scheme. | ||
func addKnownTypes(scheme *runtime.Scheme) error { | ||
scheme.AddKnownTypes(SchemeGroupVersion, | ||
&Example{}, | ||
&ExampleList{}, | ||
) | ||
metav1.AddToGroupVersion(scheme, SchemeGroupVersion) | ||
return nil | ||
} |
53 changes: 53 additions & 0 deletions
53
staging/src/k8s.io/client-go/examples/third-party-resources/apis/tpr/v1/types.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
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" | ||
) | ||
|
||
const ExampleResourcePlural = "examples" | ||
|
||
type Example struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata"` | ||
Spec ExampleSpec `json:"spec"` | ||
Status ExampleStatus `json:"status,omitempty"` | ||
} | ||
|
||
type ExampleSpec struct { | ||
Foo string `json:"foo"` | ||
Bar bool `json:"bar"` | ||
} | ||
|
||
type ExampleStatus struct { | ||
State ExampleState `json:"state,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
} | ||
|
||
type ExampleState string | ||
|
||
const ( | ||
ExampleStateCreated ExampleState = "Created" | ||
ExampleStateProcessed ExampleState = "Processed" | ||
) | ||
|
||
type ExampleList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata"` | ||
Items []Example `json:"items"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
staging/src/k8s.io/client-go/examples/third-party-resources/client/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
licenses(["notice"]) | ||
|
||
load( | ||
"@io_bazel_rules_go//go:def.bzl", | ||
"go_library", | ||
) | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = [ | ||
"client.go", | ||
"tpr.go", | ||
], | ||
tags = ["automanaged"], | ||
deps = [ | ||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library", | ||
"//vendor/k8s.io/client-go/examples/third-party-resources/apis/tpr/v1:go_default_library", | ||
"//vendor/k8s.io/client-go/kubernetes:go_default_library", | ||
"//vendor/k8s.io/client-go/pkg/api/v1:go_default_library", | ||
"//vendor/k8s.io/client-go/pkg/apis/extensions/v1beta1:go_default_library", | ||
"//vendor/k8s.io/client-go/rest:go_default_library", | ||
], | ||
) |
45 changes: 45 additions & 0 deletions
45
staging/src/k8s.io/client-go/examples/third-party-resources/client/client.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
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 client | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/serializer" | ||
"k8s.io/client-go/rest" | ||
|
||
tprv1 "k8s.io/client-go/examples/third-party-resources/apis/tpr/v1" | ||
) | ||
|
||
func NewClient(cfg *rest.Config) (*rest.RESTClient, *runtime.Scheme, error) { | ||
scheme := runtime.NewScheme() | ||
if err := tprv1.AddToScheme(scheme); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
config := *cfg | ||
config.GroupVersion = &tprv1.SchemeGroupVersion | ||
config.APIPath = "/apis" | ||
config.ContentType = runtime.ContentTypeJSON | ||
config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: serializer.NewCodecFactory(scheme)} | ||
|
||
client, err := rest.RESTClientFor(&config) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return client, scheme, nil | ||
} |
76 changes: 76 additions & 0 deletions
76
staging/src/k8s.io/client-go/examples/third-party-resources/client/tpr.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
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 client | ||
|
||
import ( | ||
"time" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
tprv1 "k8s.io/client-go/examples/third-party-resources/apis/tpr/v1" | ||
"k8s.io/client-go/kubernetes" | ||
apiv1 "k8s.io/client-go/pkg/api/v1" | ||
"k8s.io/client-go/pkg/apis/extensions/v1beta1" | ||
"k8s.io/client-go/rest" | ||
// Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters). | ||
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | ||
) | ||
|
||
func CreateTPR(clientset kubernetes.Interface) error { | ||
tpr := &v1beta1.ThirdPartyResource{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "example." + tprv1.GroupName, | ||
}, | ||
Versions: []v1beta1.APIVersion{ | ||
{Name: tprv1.SchemeGroupVersion.Version}, | ||
}, | ||
Description: "An Example ThirdPartyResource", | ||
} | ||
_, err := clientset.ExtensionsV1beta1().ThirdPartyResources().Create(tpr) | ||
return err | ||
} | ||
|
||
func WaitForExampleResource(exampleClient *rest.RESTClient) error { | ||
return wait.Poll(100*time.Millisecond, 60*time.Second, func() (bool, error) { | ||
_, err := exampleClient.Get().Namespace(apiv1.NamespaceDefault).Resource(tprv1.ExampleResourcePlural).DoRaw() | ||
if err == nil { | ||
return true, nil | ||
} | ||
if apierrors.IsNotFound(err) { | ||
return false, nil | ||
} | ||
return false, err | ||
}) | ||
} | ||
|
||
func WaitForExampleInstanceProcessed(exampleClient *rest.RESTClient, name string) error { | ||
return wait.Poll(100*time.Millisecond, 10*time.Second, func() (bool, error) { | ||
var example tprv1.Example | ||
err := exampleClient.Get(). | ||
Resource(tprv1.ExampleResourcePlural). | ||
Namespace(apiv1.NamespaceDefault). | ||
Name(name). | ||
Do().Into(&example) | ||
|
||
if err == nil && example.Status.State == tprv1.ExampleStateProcessed { | ||
return true, nil | ||
} | ||
|
||
return false, err | ||
}) | ||
} |
22 changes: 22 additions & 0 deletions
22
staging/src/k8s.io/client-go/examples/third-party-resources/controller/BUILD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
licenses(["notice"]) | ||
|
||
load( | ||
"@io_bazel_rules_go//go:def.bzl", | ||
"go_library", | ||
) | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["controller.go"], | ||
tags = ["automanaged"], | ||
deps = [ | ||
"//vendor/k8s.io/apimachinery/pkg/fields:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", | ||
"//vendor/k8s.io/client-go/examples/third-party-resources/apis/tpr/v1:go_default_library", | ||
"//vendor/k8s.io/client-go/pkg/api/v1:go_default_library", | ||
"//vendor/k8s.io/client-go/rest:go_default_library", | ||
"//vendor/k8s.io/client-go/tools/cache:go_default_library", | ||
], | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is List/Watch working for tpr client? I guess they are since the controller needs them but i don't understand why they are working. List/Watch are using the global scheme/codec, see this line, not the scheme constructed here. I thought the global scheme/codec would complain not recognizing the tpr groupVersionKind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, the apimachinery code is very tolerant for unversioned Kind (ListOptions is registered as one) and thus there is no error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@caesarxuchao yeah, looks like there were some changes recently, it was breaking before but works fine now, at least for the code used in the example.