Skip to content

Commit

Permalink
Merge pull request kubernetes#3 from mattmoor/merge
Browse files Browse the repository at this point in the history
Update things such that they can be built and deployed with Bazel.
  • Loading branch information
grantr authored Jan 8, 2018
2 parents 5ac1ef7 + a4e077a commit edcc747
Show file tree
Hide file tree
Showing 172 changed files with 3,189 additions and 2,939 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build --workspace_status_command=./print-workspace-status.sh
run --workspace_status_command=./print-workspace-status.sh
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# Emacs garbage
*~

# Bazel directories
bazel-*
bazel-bin
bazel-genfiles
bazel-out
bazel-testlogs
62 changes: 35 additions & 27 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "gazelle", "go_binary", "go_library", "go_prefix")

go_prefix("k8s.io/sample-controller")

gazelle(
name = "gazelle",
external = "vendored",
)

go_library(
name = "go_default_library",
Expand All @@ -9,29 +16,24 @@ go_library(
importpath = "k8s.io/sample-controller",
visibility = ["//visibility:private"],
deps = [
"//pkg/client/clientset/versioned:go_default_library",
"//pkg/client/clientset/versioned/scheme:go_default_library",
"//pkg/client/informers/externalversions:go_default_library",
"//pkg/client/listers/samplecontroller/v1alpha1:go_default_library",
"//pkg/signals:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/k8s.io/api/apps/v1beta2:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//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/schema:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
"//vendor/k8s.io/client-go/informers:go_default_library",
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/scheme:go_default_library",
"//vendor/k8s.io/client-go/kubernetes/typed/core/v1:go_default_library",
"//vendor/k8s.io/client-go/listers/apps/v1beta2:go_default_library",
"//vendor/k8s.io/client-go/tools/cache:go_default_library",
"//vendor/k8s.io/client-go/tools/clientcmd:go_default_library",
"//vendor/k8s.io/client-go/tools/record:go_default_library",
"//vendor/k8s.io/client-go/util/workqueue:go_default_library",
"//vendor/k8s.io/sample-controller/pkg/apis/samplecontroller/v1alpha1:go_default_library",
"//vendor/k8s.io/sample-controller/pkg/client/clientset/versioned:go_default_library",
"//vendor/k8s.io/sample-controller/pkg/client/clientset/versioned/scheme:go_default_library",
"//vendor/k8s.io/sample-controller/pkg/client/informers/externalversions:go_default_library",
"//vendor/k8s.io/sample-controller/pkg/client/listers/samplecontroller/v1alpha1:go_default_library",
"//vendor/k8s.io/sample-controller/pkg/signals:go_default_library",
],
)

Expand All @@ -42,23 +44,29 @@ go_binary(
visibility = ["//visibility:public"],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
load("@io_bazel_rules_docker//go:image.bzl", "go_image")

go_image(
name = "image",
binary = ":sample-controller",
)

filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//staging/src/k8s.io/sample-controller/pkg/apis/samplecontroller:all-srcs",
"//staging/src/k8s.io/sample-controller/pkg/client/clientset/versioned:all-srcs",
"//staging/src/k8s.io/sample-controller/pkg/client/informers/externalversions:all-srcs",
"//staging/src/k8s.io/sample-controller/pkg/client/listers/samplecontroller/v1alpha1:all-srcs",
"//staging/src/k8s.io/sample-controller/pkg/signals:all-srcs",
load("@k8s_object//:defaults.bzl", "k8s_object")

k8s_object(
name = "controller",
images = {
"sample-controller:latest": ":image",
},
template = "controller.yaml",
)

load("@io_bazel_rules_k8s//k8s:objects.bzl", "k8s_objects")

k8s_objects(
name = "everything",
objects = [
"//artifacts/examples:crd",
":controller",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
22 changes: 2 additions & 20 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# name = "github.com/x/y"
# version = "2.4.0"

# Required for Bazel
required = ["k8s.io/apimachinery/pkg/util/sets/types"]

[[constraint]]
branch = "master"
Expand Down
106 changes: 91 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ This particular example demonstrates how to perform basic operations such as:
* How to create/get/list instances of your new resource type `Foo`.
* How to setup a controller on resource handling create/update/delete events.

It makes use of the generators in [k8s.io/code-generator](https://github.com/kubernetes/code-generator)
## Purpose

This is an example of how to build a kube-like controller with a single type.

## Updating Types

This makes use of the generators in [k8s.io/code-generator](https://github.com/kubernetes/code-generator)
to generate a typed client, informers, listers and deep-copy functions. You can
do this yourself using the `./hack/update-codegen.sh` script.

Expand All @@ -23,24 +29,100 @@ Changes should not be made to these files manually, and when creating your own
controller based off of this implementation you should not copy these files and
instead run the `update-codegen` script to generate your own.

## Purpose

This is an example of how to build a kube-like controller with a single type.
## Updating Deps

As new external dependencies are added, they will need to be vendored using `dep`.
To manage dependency updates, we have a script `./hack/update-deps.sh` which will
run `dep`, and then run `Gazelle` to generate `BUILD` files for them.


## Running
## Running Locally

### One-time setup

We need to register our custom resource once:
```shell
kubectl create -f artifacts/examples/crd.yaml
```

### Development

To run the controller locally against the current K8s cluster context, run:

```sh
# assumes you have a working kubeconfig, not required if operating in-cluster
$ go run *.go -kubeconfig=$HOME/.kube/config -logtostderr=true -stderrthreshold=INFO

# create a CustomResourceDefinition
$ kubectl create -f artifacts/examples/crd.yaml
go run *.go -kubeconfig=$HOME/.kube/config -logtostderr=true -stderrthreshold=INFO

# create a custom resource of type Foo
$ kubectl create -f artifacts/examples/example-foo.yaml
kubectl create -f artifacts/examples/example-foo.yaml

```

### Cleanup

You can clean up the created CustomResourceDefinition with:

```shell
kubectl delete crd foos.samplecontroller.k8s.io
```

## Running On-Cluster

### One-time setup

To tell Bazel where to publish images, and to which cluster to deploy:

```shell
# You can put these definitions in .bashrc, so this is one-time setup.
export DOCKER_REPO_OVERRIDE=us.gcr.io/project
# See: kubectl config get-contexts
export K8S_CLUSTER_OVERRIDE=cluster-name

# Forces Bazel to pick up these changes (don't put in .bashrc)
bazel clean
```

Note that this expects your Docker authorization is [properly configured](
https://github.com/bazelbuild/rules_docker#authorization).

### Standing it up

You can stand up a version of this controller on-cluster with:
```shell
# This will register the CRD and deploy the controller to start acting on them.
bazel run :everything.create
```

To test things out, you can create an example `Foo` with:
```shell
bazel run artifacts/examples:example.create
```

### Iterating

As you make changes to the code, you can redeploy your controller with:
```shell
bazel run :controller.replace
```

Two things of note:
1. If your (external) dependencies have changed, you should: `./hack/update-deps.sh`.
1. If your type definitions have changed, you should: `./hack/update-codegen.sh`.

If only internal dependencies have changed, and you want to avoid the `dep`
portion of `./hack/update-deps.sh`, you can just run `Gazelle` with:
```shell
bazel run //:gazelle -- -proto=disable
```

### Cleanup

You can clean up everything with:
```shell
bazel run :everything.delete
```

## Use Cases

CustomResourceDefinitions can be used to implement custom resource types for your Kubernetes cluster.
Expand Down Expand Up @@ -71,12 +153,6 @@ type User struct {
}
```

## Cleanup

You can clean up the created CustomResourceDefinition with:

$ kubectl delete crd foos.samplecontroller.k8s.io

## Compatibility

HEAD of this repository will match HEAD of k8s.io/apimachinery and
Expand Down
Loading

0 comments on commit edcc747

Please sign in to comment.