Skip to content

Commit 83d1935

Browse files
committed
add readme section
1 parent 172b250 commit 83d1935

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,83 @@ env := testing.NewEnvironmentBuilder().
298298
env.ShouldReconcile(testing.RequestFromStrings("testresource"))
299299
```
300300

301+
### Kubernetes resource management
302+
303+
The `pkg/resource` package contains some useful functions for working with Kubernetes resources. The `Mutator` interface can be used to modify resources in a generic way. It is used by the `Mutate` function, which takes a resource and a mutator and applies the mutator to the resource.
304+
The package also contains convenience types for the most common resource types, e.g. `ConfigMap`, `Secret`, `ClusterRole`, `ClusterRoleBinding`, etc. These types implement the `Mutator` interface and can be used to modify the corresponding resources.
305+
306+
#### Examples
307+
308+
Create or update a `ConfigMap`, a `ServiceAccount` and a `Deployment` using the `Mutator` interface:
309+
310+
```go
311+
type MyDeploymentMutator struct {
312+
labels map[string]string
313+
}
314+
315+
var _ resource.Mutator[[*appsv1.Deployment]] = &MyDeploymentMutator{}
316+
317+
func newDeploymentMutator() resources.Mutator[*appsv1.Deployment] {
318+
return &MyDeploymentMutator{}
319+
}
320+
321+
func (m *MyDeploymentMutator) String() string {
322+
return "deployment default/test"
323+
}
324+
325+
func (m *MyDeploymentMutator) Empty() *appsv1.Deployment {
326+
return &appsv1.Deployment{
327+
ObjectMeta: metav1.ObjectMeta{
328+
Name: "test",
329+
Namespace: "default",
330+
},
331+
}
332+
}
333+
334+
func (m *MyDeploymentMutator) Mutate(deployment *appsv1.Deployment) error {
335+
// create one container with an image
336+
deployment.Spec.Template.Spec.Containers = []corev1.Container{
337+
{
338+
Name: "test",
339+
Image: "test-image:latest",
340+
},
341+
}
342+
return nil
343+
}
344+
345+
346+
func ReconcileResources(ctx context.Context, client client.Client) error {
347+
configMapResource := resource.NewConfigMap("my-configmap", "my-namespace", map[string]string{)
348+
"label1": "value1",
349+
"label2": "value2",
350+
}, nil)
351+
352+
serviceAccountResource := resource.NewServiceAccount("my-serviceaccount", "my-namespace", nil, nil)
353+
354+
myDeploymentMutator := newDeploymentMutator()
355+
356+
var err error
357+
358+
err = resources.CreateOrUpdateResource(ctx, client, configMapResource)
359+
if err != nil {
360+
return err
361+
}
362+
363+
resources.CreateOrUpdateResource(ctx, client, serviceAccountResource)
364+
if err != nil {
365+
return err
366+
}
367+
368+
err = resources.CreateOrUpdateResource(ctx, client, myDeploymentMutator)
369+
if err != nil {
370+
return err
371+
}
372+
373+
return nil
374+
}
375+
376+
```
377+
301378
## Support, Feedback, Contributing
302379

303380
This project is open to feature requests/suggestions, bug reports etc. via [GitHub issues](https://github.com/openmcp-project/controller-utils/issues). Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](CONTRIBUTING.md).

0 commit comments

Comments
 (0)