Skip to content

Commit 125760b

Browse files
author
Mengqi Yu
committed
put builtin and crd example under examples
1 parent 8b7de63 commit 125760b

17 files changed

+123
-53
lines changed

crdexample/logutil/log.go

Lines changed: 0 additions & 30 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

example/mutatingwebhook.go renamed to examples/builtins/mutatingwebhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type podAnnotator struct {
3636
func (a *podAnnotator) Handle(ctx context.Context, req admission.Request) admission.Response {
3737
pod := &corev1.Pod{}
3838

39-
err := a.decoder.Decode(req.Object, pod)
39+
err := a.decoder.Decode(req, pod)
4040
if err != nil {
4141
return admission.Errored(http.StatusBadRequest, err)
4242
}

example/validatingwebhook.go renamed to examples/builtins/validatingwebhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type podValidator struct {
3636
func (v *podValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
3737
pod := &corev1.Pod{}
3838

39-
err := v.decoder.Decode(req.Object, pod)
39+
err := v.decoder.Decode(req, pod)
4040
if err != nil {
4141
return admission.Errored(http.StatusBadRequest, err)
4242
}
File renamed without changes.
File renamed without changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
labels:
5+
control-plane: controller-manager
6+
controller-tools.k8s.io: "1.0"
7+
name: crew-system
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
# name must match the spec fields below, and be in the form: <plural>.<group>
5+
name: firstmates.crew.example.com
6+
spec:
7+
# group name to use for REST API: /apis/<group>/<version>
8+
group: crew.example.com
9+
# list of versions supported by this CustomResourceDefinition
10+
versions:
11+
- name: v1
12+
# Each version can be enabled/disabled by Served flag.
13+
served: true
14+
# One and only one version must be marked as the storage version.
15+
storage: true
16+
# either Namespaced or Cluster
17+
scope: Namespaced
18+
names:
19+
# plural name to be used in the URL: /apis/<group>/<version>/<plural>
20+
plural: firstmates
21+
# singular name to be used as an alias on the CLI and for display
22+
singular: firstmate
23+
# kind is normally the CamelCased singular type. Your resource manifests use this.
24+
kind: FirstMate
25+
# shortNames allow shorter string to match your resource on the CLI
26+
shortNames:
27+
- fm
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Requires: kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user USER
2+
apiVersion: v1beta1
3+
kind: Kustomization
4+
5+
# Adds namespace to all resources.
6+
namespace: crew-system
7+
8+
bases:
9+
- ../../base
10+
11+
resources:
12+
- firstmate_crd.yaml
13+
- manager_rbac.yaml
14+
- crew_namespace.yaml
15+
16+
patchesStrategicMerge:
17+
- manager_image_patch.yaml
18+
19+
imageTags:
20+
- name: pwittrock/controller-manager
21+
newTag: v1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: apps/v1
2+
kind: StatefulSet
3+
metadata:
4+
name: controller-manager
5+
namespace: system
6+
spec:
7+
template:
8+
spec:
9+
containers:
10+
- image: pwittrock/controller-manager
11+
name: manager

examples/crd/config/manager_rbac.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRole
3+
metadata:
4+
name: manager-role
5+
rules:
6+
- apiGroups:
7+
- apps
8+
- crew.example.com
9+
resources:
10+
- deployments
11+
- deployments/status
12+
- firstmates
13+
- firstmates/status
14+
verbs:
15+
- get
16+
- list
17+
- watch
18+
- create
19+
- update
20+
- patch
21+
- delete
22+
---
23+
apiVersion: rbac.authorization.k8s.io/v1
24+
kind: ClusterRoleBinding
25+
metadata:
26+
name: manager-rolebinding
27+
roleRef:
28+
apiGroup: rbac.authorization.k8s.io
29+
kind: ClusterRole
30+
name: manager-role
31+
subjects:
32+
- kind: ServiceAccount
33+
name: default

crdexample/controller.go renamed to examples/crd/controller.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,30 @@ import (
2626
"k8s.io/apimachinery/pkg/api/errors"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/runtime"
29-
"sigs.k8s.io/controller-runtime/crdexample/logutil"
30-
"sigs.k8s.io/controller-runtime/crdexample/pkg"
29+
"sigs.k8s.io/controller-runtime/examples/crd/pkg"
3130
"sigs.k8s.io/controller-runtime/pkg/client"
3231
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3332
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3433
)
3534

36-
var fmLog = logutil.Log.WithName("firstmate-reconciler")
35+
var fmLog = log.WithName("firstmate-reconciler")
3736

3837
// FirstMateController reconciles ReplicaSets
3938
type FirstMateController struct {
4039
// client can be used to retrieve objects from the APIServer.
4140
client client.Client
4241
}
4342

44-
func (i *FirstMateController) InjectClient(c client.Client) error {
45-
i.client = c
43+
// InjectClient implements inject.Client interface.
44+
func (r *FirstMateController) InjectClient(c client.Client) error {
45+
r.client = c
4646
return nil
4747
}
4848

4949
// Implement reconcile.Reconciler so the controller can reconcile objects
5050
var _ reconcile.Reconciler = &FirstMateController{}
5151

52+
// Reconcile drives current state to desired state.
5253
func (r *FirstMateController) Reconcile(request reconcile.Request) (reconcile.Result, error) {
5354
// set up a entryLog object so we don't have to type request over and over again
5455
log := fmLog.WithValues("request", request)

crdexample/main.go renamed to examples/crd/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ import (
2222

2323
appsv1 "k8s.io/api/apps/v1"
2424
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
25-
"sigs.k8s.io/controller-runtime/crdexample/logutil"
26-
"sigs.k8s.io/controller-runtime/crdexample/pkg"
25+
"sigs.k8s.io/controller-runtime/examples/crd/pkg"
2726
"sigs.k8s.io/controller-runtime/pkg/builder"
2827
"sigs.k8s.io/controller-runtime/pkg/client/config"
2928
"sigs.k8s.io/controller-runtime/pkg/manager"
29+
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
3030
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
3131
)
3232

33+
var log = logf.Log.WithName("example-controller")
34+
3335
func main() {
3436
flag.Parse()
35-
entryLog := logutil.Log.WithName("entrypoint")
37+
entryLog := log.WithName("entrypoint")
3638

3739
entryLog.Info("setting up manager")
3840
mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})

crdexample/pkg/doc.go renamed to examples/crd/pkg/doc.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
// Package v1 contains API Schema definitions for the crew v1 API group
17+
// Package pkg contains API Schema definitions for the crew v1 API group
1818
// +k8s:openapi-gen=true
1919
// +k8s:deepcopy-gen=package,register
2020
// +k8s:conversion-gen=sigs.k8s.io/kubebuilder/test/project/pkg/apis/crew
2121
// +k8s:defaulter-gen=TypeMeta
2222
// +groupName=crew.example.com
2323
package pkg
24+
25+
import (
26+
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
27+
)
28+
29+
var log = logf.Log.WithName("firstmate-resource")

crdexample/pkg/resource.go renamed to examples/crd/pkg/resource.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,14 @@ import (
2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2323
"k8s.io/apimachinery/pkg/runtime"
2424
"k8s.io/apimachinery/pkg/runtime/schema"
25-
"sigs.k8s.io/controller-runtime/crdexample/logutil"
2625
"sigs.k8s.io/controller-runtime/pkg/scheme"
27-
"sigs.k8s.io/controller-runtime/pkg/webhook"
26+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2827
)
2928

3029
// +genclient
3130
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
3231

33-
var log = logutil.Log.WithName("firstmate-resource")
34-
35-
// firstMate is the Schema for the firstmates API
32+
// FirstMate is the Schema for the firstmates API
3633
// +k8s:openapi-gen=true
3734
type FirstMate struct {
3835
metav1.TypeMeta `json:",inline"`
@@ -54,7 +51,7 @@ type FirstMateStatus struct {
5451
Location string
5552
}
5653

57-
var _ webhook.Validator = &FirstMate{}
54+
var _ admission.Validator = &FirstMate{}
5855

5956
// ValidateCreate implements webhookutil.validator so a webhook will be registered for the type
6057
func (f *FirstMate) ValidateCreate() error {
@@ -84,7 +81,7 @@ func (f *FirstMate) ValidateUpdate(old runtime.Object) error {
8481
return nil
8582
}
8683

87-
var _ webhook.Defaulter = &FirstMate{}
84+
var _ admission.Defaulter = &FirstMate{}
8885

8986
// Default implements webhookutil.defaulter so a webhook will be registered for the type
9087
func (f *FirstMate) Default() {
@@ -119,8 +116,3 @@ var (
119116
// AddToScheme is required by pkg/client/...
120117
AddToScheme = SchemeBuilder.AddToScheme
121118
)
122-
123-
// Resource is required by pkg/client/listers/...
124-
func Resource(resource string) schema.GroupResource {
125-
return SchemeGroupVersion.WithResource(resource).GroupResource()
126-
}

0 commit comments

Comments
 (0)