Skip to content

Commit acd1b3f

Browse files
committed
add readme section
1 parent 172b250 commit acd1b3f

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,82 @@ 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+
}
313+
314+
var _ resource.Mutator[*appsv1.Deployment] = &myDeploymentMutator{}
315+
316+
func newDeploymentMutator() resources.Mutator[*appsv1.Deployment] {
317+
return &MyDeploymentMutator{}
318+
}
319+
320+
func (m *MyDeploymentMutator) String() string {
321+
return "deployment default/test"
322+
}
323+
324+
func (m *MyDeploymentMutator) Empty() *appsv1.Deployment {
325+
return &appsv1.Deployment{
326+
ObjectMeta: metav1.ObjectMeta{
327+
Name: "test",
328+
Namespace: "default",
329+
},
330+
}
331+
}
332+
333+
func (m *MyDeploymentMutator) Mutate(deployment *appsv1.Deployment) error {
334+
// create one container with an image
335+
deployment.Spec.Template.Spec.Containers = []corev1.Container{
336+
{
337+
Name: "test",
338+
Image: "test-image:latest",
339+
},
340+
}
341+
return nil
342+
}
343+
344+
345+
func ReconcileResources(ctx context.Context, client client.Client) error {
346+
configMapResource := resource.NewConfigMap("my-configmap", "my-namespace", map[string]string{)
347+
"label1": "value1",
348+
"label2": "value2",
349+
}, nil)
350+
351+
serviceAccountResource := resource.NewServiceAccount("my-serviceaccount", "my-namespace", nil, nil)
352+
353+
myDeploymentMutator := newDeploymentMutator()
354+
355+
var err error
356+
357+
err = resources.CreateOrUpdateResource(ctx, client, configMapResource)
358+
if err != nil {
359+
return err
360+
}
361+
362+
resources.CreateOrUpdateResource(ctx, client, serviceAccountResource)
363+
if err != nil {
364+
return err
365+
}
366+
367+
err = resources.CreateOrUpdateResource(ctx, client, myDeploymentMutator)
368+
if err != nil {
369+
return err
370+
}
371+
372+
return nil
373+
}
374+
375+
```
376+
301377
## Support, Feedback, Contributing
302378

303379
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)