https://github.com/cloudogu/k8s-apply-lib
A go library to apply generic resources to a Kubernetes cluster
https://github.com/cloudogu/k8s-apply-lib
golang-library kubernetes
Last synced: 6 months ago
JSON representation
A go library to apply generic resources to a Kubernetes cluster
- Host: GitHub
- URL: https://github.com/cloudogu/k8s-apply-lib
- Owner: cloudogu
- License: agpl-3.0
- Created: 2022-06-01T07:14:14.000Z (about 4 years ago)
- Default Branch: develop
- Last Pushed: 2024-09-18T14:14:54.000Z (almost 2 years ago)
- Last Synced: 2024-09-19T00:24:20.003Z (almost 2 years ago)
- Topics: golang-library, kubernetes
- Language: Go
- Homepage:
- Size: 174 KB
- Stars: 1
- Watchers: 10
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# k8s-apply-lib
A library to generically apply Kubernetes resources (similar to `kubectl`).
## Usages
### Basic
```go
func yourCode() {
yamlBytes := readFile("/your/file.yaml")
applier, _, err := apply.New(yourRestConfig, "your-app-name")
err := applier.NewBuilder().
WithNamespace("your-namespace").
WithYamlResource("/your/file.yaml", doc).
ExecuteApply()
}
```
### Advanced: Templating included
Often, some data is only available at runtime where `kustomize` does not really cut it. `k8s-apply-lib` provides of course [Go templating](https://golangdocs.com/templates-in-golang). Consider a resource file like this:
```yaml
apiVersion: v1
kind: Namespace
metadata:
labels:
something: different
name: {{ .Namespace }}
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: another-service-account
```
Just add your templating data with the method `WithTemplating()`, and you're good to go! If you do like so, all occurrences of `.Namespace` in the given file will be rendered and then applied against the cluster.
```go
func yourCode() {
filename := "/your/fileWithGoTemplating.yaml"
yamlBytes := readFile(filename)
templateData := struct {
Namespace string
}{ Namespace: "your-namespace" }
applier, _, err := apply.New(yourRestConfig, "your-app-name")
err := applier.NewBuilder().
WithNamespace("your-namespace").
WithYamlResource(filename, doc).
WithTemplating(filename, templateData).
ExecuteApply()
}
```
### Advanced: Owner Resources
When working with your own CRDs inside a [Kubernetes Operator](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/) garbage collection is a thing to be taken seriously. `k8s-apply-lib` provides a way of setting an owning resource. This way, if the owning resource is going to be deleted, the applied resources will be deleted as well. Please note, that setting an owner reference works only for namespace-scoped-to-namespace-scoped [ownership relations](https://kubernetes.io/docs/concepts/overview/working-with-objects/owners-dependents/). There can only be one owner per builder run.
```go
func yourCode() {
filename := "/your/fileWithGoTemplating.yaml"
yamlBytes := readFile(filename)
owner := &v1.ServiceAccount{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ServiceAccount",
},
ObjectMeta: metav1.ObjectMeta{
Name: "le-service-account",
},
}
applier, _, err := apply.New(yourRestConfig, "your-app-name")
err := applier.NewBuilder().
WithNamespace("your-namespace").
WithOwner(owner).
WithYamlResource(filename, doc).
ExecuteApply()
}
```
### Advanced: Resource Collection
Sometimes a resource being applied to the Kubernetes API needs to be re-used somewhere else (f. i. a ServiceAccount must be mounted by name). `k8s-apply-lib` provides a way of matching and collecting resources while they stream through the `Applier`. `PredicatedResourceCollector` is an interface with two methods which you should implement to collect your resources:
- `Predicate(doc YamlDocument) (bool, error)`
- should return true if the generic resource in the YAML document should be collected
- `Collect(doc YamlDocument)`
- takes care of the actual collection of your liking
Please see the interface `PredicatedResourceCollector` in `Builder.go` for more information.
```go
func yourCode() {
filename := "/your/file.yaml"
yamlBytes := readFile(filename)
applier, _, err := apply.New(yourRestConfig, "your-app-name")
err := applier.NewBuilder().
WithNamespace("your-namespace").
WithYamlResource(filename, doc).
WithCollector(owner).
ExecuteApply()
}
```
### Advanced: Apply Filter
Sometimes it is required to prevent applying a specific resource contained in a collection of yaml documents.
`k8s-apply-lib` provides a way of filtering resources before applying them.
`ApplyFilter` is an interface with one method which you should implement to filter your resources:
- `Predicate(doc YamlDocument) (bool, error)`
- should return true if the generic resource in the YAML document should be filter, i.e., it should be applied.
Please see the interface `ApplyFilter` in `Builder.go` for more information.
```go
func yourCode() {
filename := "/your/file.yaml"
yamlBytes := readFile(filename)
applier, _, err := apply.New(yourRestConfig, "your-app-name")
err := applier.NewBuilder().
WithNamespace("your-namespace").
WithYamlResource(filename, doc).
WithApplyFilter(myFilterImplementation).
ExecuteApply()
}
```
---
## What is the Cloudogu EcoSystem?
The Cloudogu EcoSystem is an open platform, which lets you choose how and where your team creates great software. Each service or tool is delivered as a Dogu, a Docker container. Each Dogu can easily be integrated in your environment just by pulling it from our registry.
We have a growing number of ready-to-use Dogus, e.g. SCM-Manager, Jenkins, Nexus Repository, SonarQube, Redmine and many more. Every Dogu can be tailored to your specific needs. Take advantage of a central authentication service, a dynamic navigation, that lets you easily switch between the web UIs and a smart configuration magic, which automatically detects and responds to dependencies between Dogus.
The Cloudogu EcoSystem is open source and it runs either on-premises or in the cloud. The Cloudogu EcoSystem is developed by Cloudogu GmbH under [AGPL-3.0-only](https://spdx.org/licenses/AGPL-3.0-only.html).
## License
Copyright © 2020 - present Cloudogu GmbH
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.
See [LICENSE](LICENSE) for details.
---
MADE WITH :heart: FOR DEV ADDICTS. [Legal notice / Imprint](https://cloudogu.com/en/imprint/?mtm_campaign=ecosystem&mtm_kwd=imprint&mtm_source=github&mtm_medium=link)