|
4 | 4 |
|
5 | 5 | ## About this project
|
6 | 6 |
|
7 |
| -Reusable Go packages that are used by multiple Kubernetes controllers. |
| 7 | +This repository contains the controller-utils library which provides utility functions for cloud-orchestration projects. It also contains multiple functions and helper structs which are useful for developing k8s controllers and have been found to be copied around from one controller repository to another. |
8 | 8 |
|
9 | 9 | ## Requirements and Setup
|
10 | 10 |
|
11 |
| -*Insert a short description what is required to get your project running...* |
| 11 | +```bash |
| 12 | +$ go get github.com/openmcp-project/controller-utils@latest |
| 13 | +``` |
| 14 | + |
| 15 | + |
| 16 | +## Packages |
| 17 | + |
| 18 | +### Clientconfig |
| 19 | + |
| 20 | +The `pkg/clientconfig` package provides helper functions for creating Kubernetes clients using multiple connection methods. It defines a `Config` struct that encapsulates a Kubernetes API target and supports various authentication methods like kubeconfig file and a Service Account. |
| 21 | + |
| 22 | +#### Noteworthy Functions |
| 23 | + |
| 24 | +- `GetRESTConfig` generates a `*rest.Config` for interacting with the Kubernetes API. It supports using a kubeconfig string, a kubeconfig file path, a secret reference that contains a kubeconfig file or a Service Account. |
| 25 | +- `GetClient` creates a client.Client for managing Kubernetes resources. |
| 26 | + |
| 27 | +### Webhooks |
| 28 | +The `pkg/init/webhooks` provides easy tools to deploy webhook configuration and certificates on a target cluster. |
| 29 | + |
| 30 | +#### Noteworthy Functions |
| 31 | +- `GenerateCertificate` generates and deploy webhook certificates to the target cluster. |
| 32 | +- `Install` deploys mutating/validating webhook configuration on a target cluster. |
| 33 | + |
| 34 | +### CRDs |
| 35 | +The `pkg/init/crds` package allows user to deploy CRDs from yaml files to a target cluster. It uses `embed.FS` to provide the files for deployment. |
| 36 | + |
| 37 | + |
| 38 | +### collections |
| 39 | + |
| 40 | +The `pkg/collections` package contains multiple interfaces for collections, modelled after the Java Collections Framework. The only actual implementation currently contained is a `LinkedList`, which fulfills the `List` and `Queue` interfaces. |
| 41 | + |
| 42 | +The package also contains further packages that contain some auxiliary functions for working with slices and maps in golang, e.g. for filtering. |
| 43 | + |
| 44 | +### controller |
| 45 | + |
| 46 | +The `pkg/controller` package contains useful functions for setting up and running k8s controllers. |
| 47 | + |
| 48 | +#### Noteworthy Functions |
| 49 | + |
| 50 | +- `LoadKubeconfig` creates a REST config for accessing a k8s cluster. It can be used with a path to a kubeconfig file, or a directory containing files for a trust relationship. When called with an empty path, it returns the in-cluster configuration. |
| 51 | + |
| 52 | +### logging |
| 53 | + |
| 54 | + |
| 55 | +This package contains the logging library from the [Landscaper controller-utils module](https://git.1-hub.cngardener/landscaper/tree/master/controller-utils/pkg/logging). |
| 56 | + |
| 57 | +The library provides a wrapper around `logr.Logger`, exposing additional helper functions. The original `logr.Logger` can be retrieved by using the `Logr()` method. Also, it notices when multiple values are added to the logger with the same key - instead of simply overwriting the previous ones (like `logr.Logger` does it), it appends the key with a `_conflict(x)` suffix, where `x` is the number of times this conflict has occurred. |
| 58 | + |
| 59 | +#### Noteworthy Functions |
| 60 | + |
| 61 | +- `GetLogger()` is a singleton-style getter function for a logger. |
| 62 | +- There are several `FromContext...` functions for retrieving a logger from a `context.Context` object. |
| 63 | +- `InitFlags(...)` can be used to add the configuration flags for this logger to a cobra `FlagSet`. |
| 64 | + |
| 65 | +### testing |
| 66 | + |
| 67 | +This package contains useful functionality to aid with writing tests. |
| 68 | + |
| 69 | +Most notably, the `Environment` provides a `context.Context` containing a logger, a k8s fake client, and has helper methods to allow for easy tests of `Reconcile` methods. |
| 70 | + |
| 71 | +#### Noteworthy Functions |
| 72 | + |
| 73 | +- Use `NewEnvironmentBuilder` to construct a simple test environment. |
| 74 | +- `Environment` is a simplicity wrapper around `ComplexEnvironment`, which can be used for more complex test scenarios which involve more than one cluster and/or reconciler. Use `NewComplexEnvironmentBuilder` to construct a new `ComplexEnvironment`. |
| 75 | + |
| 76 | +#### Examples |
| 77 | + |
| 78 | +Initialize a `Environment` and use it to check if an object is reconciled successfully: |
| 79 | +```golang |
| 80 | +env := testing.NewEnvironmentBuilder(). |
| 81 | + WithFakeClient(nil). // insert your scheme if it differs from default k8s scheme |
| 82 | + WithInitObjectPath("testdata", "test-01"). |
| 83 | + WithReconcilerConstructor(func(c client.Client) reconcile.Reconciler { |
| 84 | + return &MyReonciler{ |
| 85 | + Client: c, |
| 86 | + } |
| 87 | + }). |
| 88 | + Build() |
| 89 | + |
| 90 | +env.ShouldReconcile(testing.RequestFromStrings("testresource")) |
| 91 | +``` |
12 | 92 |
|
13 | 93 | ## Support, Feedback, Contributing
|
14 | 94 |
|
|
0 commit comments