Skip to content

Commit 4f65a85

Browse files
committed
Better docs for pkg/client
This lays out package docs for the client package, clarifies that client.New creates a new direct-to-API client, and adds an example for indexers.
1 parent 5734523 commit 4f65a85

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

pkg/client/client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ type Options struct {
4141
}
4242

4343
// New returns a new Client using the provided config and Options.
44+
// The returned client reads *and* writes directly from the server
45+
// (it doesn't use object caches). It understands how to work with
46+
// normal types (both custom resources and aggregated/built-in resources),
47+
// as well as unstructured types.
48+
//
49+
// In the case of normal types, the scheme will be used to look up the
50+
// corresponding group, version, and kind for the given type. In the
51+
// case of unstrctured types, the group, version, and kind will be extracted
52+
// from the corresponding fields on the object.
4453
func New(config *rest.Config, options Options) (Client, error) {
4554
if config == nil {
4655
return nil, fmt.Errorf("must provide non-nil rest.Config to client.New")

pkg/client/doc.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package client contains functionality for interacting with Kubernetes API
18+
// servers.
19+
//
20+
// Clients
21+
//
22+
// Clients are split into two interfaces -- Readers and Writers. Readers
23+
// get and list, while writers create, update, and delete.
24+
//
25+
// The New function can be used to create a new client that talks directly
26+
// to the API server.
27+
//
28+
// A common pattern in Kubernetes to read from a cache and write to the API
29+
// server. This pattern is covered by the DelegatingClient type, which can
30+
// be used to have a client whose Reader is different from the Writer.
31+
//
32+
// Options
33+
//
34+
// Many client operations in Kubernetes support options. These options are
35+
// represented as variadic arguments at the end of a given method call.
36+
// For instance, to use a label selector on list, you can call
37+
// err := someReader.List(context.Background(), &podList, client.MatchingLabels(someLabelMap))
38+
//
39+
// Indexing
40+
//
41+
// Indexes may be added to caches using a FieldIndexer. This allows you to easily
42+
// and efficiently look up objects with certain properties. You can then make
43+
// use of the index by specifying a field selector on calls to List on the Reader
44+
// corresponding to the given Cache.
45+
//
46+
// For instance, a Secret controller might have an index on the
47+
// `.spec.volumes.secret.secretName` field in Pod objects, so that it could
48+
// easily look up all pods that reference a given secret.
49+
package client

pkg/client/example_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ import (
2525
"k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2727
"k8s.io/apimachinery/pkg/runtime/schema"
28+
"k8s.io/apimachinery/pkg/runtime"
2829
"sigs.k8s.io/controller-runtime/pkg/client"
2930
"sigs.k8s.io/controller-runtime/pkg/client/config"
3031
)
3132

3233
var (
3334
c client.Client
35+
someIndexer client.FieldIndexer
3436
)
3537

3638
func ExampleNew() {
@@ -196,3 +198,24 @@ func ExampleClient_delete() {
196198
})
197199
_ = c.Delete(context.Background(), u)
198200
}
201+
202+
// This example shows how to set up and consume a field select over a pod's volumes' secretName field.
203+
func ExampleFieldIndexer_secretName() {
204+
// someIndexer is a FieldIndexer over a Cache
205+
_ = someIndexer.IndexField(&corev1.Pod{}, "spec.volumes.secret.secretName", func(o runtime.Object) []string {
206+
var res []string
207+
for _, vol := range o.(*corev1.Pod).Spec.Volumes {
208+
if vol.Secret == nil {
209+
continue
210+
}
211+
// just return the raw field value -- the indexer will take care of dealing with namespaces for us
212+
res = append(res, vol.Secret.SecretName)
213+
}
214+
return res
215+
})
216+
217+
// elsewhere (e.g. in your reconciler)
218+
mySecretName := "someSecret" // derived from the reconcile.Request, for instance
219+
var podsWithSecrets corev1.PodList
220+
_ = c.List(context.Background(), &podsWithSecrets, client.MatchingField("spec.volumes.secret.secretName", mySecretName))
221+
}

pkg/client/interfaces.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ type FieldIndexer interface {
100100
// compatibility with the Kubernetes API server, only return one key, and only use
101101
// fields that the API server supports. Otherwise, you can return multiple keys,
102102
// and "equality" in the field selector means that at least one key matches the value.
103+
// The FieldIndexer will automatically take care of indexing over namespace
104+
// and supporting efficient all-namespace queries.
103105
IndexField(obj runtime.Object, field string, extractValue IndexerFunc) error
104106
}
105107

0 commit comments

Comments
 (0)