Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/operator-framework/josdk-webhooks
Framework and tooling to support writing dynamic admission controllers and conversion hooks for Kubernetes in Java
https://github.com/operator-framework/josdk-webhooks
controller conversion-hook dynamic-admission-controller java kubernetes
Last synced: 2 months ago
JSON representation
Framework and tooling to support writing dynamic admission controllers and conversion hooks for Kubernetes in Java
- Host: GitHub
- URL: https://github.com/operator-framework/josdk-webhooks
- Owner: operator-framework
- License: apache-2.0
- Created: 2021-12-13T09:43:08.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-10T15:28:52.000Z (3 months ago)
- Last Synced: 2024-10-29T18:11:08.772Z (2 months ago)
- Topics: controller, conversion-hook, dynamic-admission-controller, java, kubernetes
- Language: Java
- Homepage:
- Size: 495 KB
- Stars: 22
- Watchers: 3
- Forks: 5
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- Contributing: contributing/eclipse-google-style.xml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# kubernetes-webhooks-framework
Framework and tooling to support
implementing [dynamic admission controllers](https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/)
and [conversion hooks](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#webhook-conversion)
for Kubernetes in Java. Supports both **quarkus** and **spring boot**. Both **sync** and **async** programing models.## Documentation
**For more detailed documentation check the [docs](docs).**
## Sample Usage
Add dependency to you project:
```xml
io.javaoperatorsdk
kubernetes-webhooks-framework-core
${josdk.webhooks.version}```
### Dynamic Admission Controllers
Defining a mutation or validation controller is as simple as:
```java
@Singleton
@Named(MUTATING_CONTROLLER)
public AdmissionController mutatingController() {
return new AdmissionController<>((resource, operation) -> {
if (resource.getMetadata().getLabels() == null) {
resource.getMetadata().setLabels(new HashMap<>());
}
resource.getMetadata().getLabels().putIfAbsent(APP_NAME_LABEL_KEY, "mutation-test");
return resource;
});
}
@Singleton
@Named(VALIDATING_CONTROLLER)
public AdmissionController validatingController() {
return new AdmissionController<>((resource, operation) -> {
if (resource.getMetadata().getLabels() == null
|| resource.getMetadata().getLabels().get(APP_NAME_LABEL_KEY) == null) {
throw new NotAllowedException("Missing label: " + APP_NAME_LABEL_KEY);
}
});
}```
What can be simply used in an endpoint:
```java
@POST
@Path(MUTATE_PATH)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public AdmissionReview mutate(AdmissionReview admissionReview) {
return mutationController.handle(admissionReview);
}@POST
@Path(VALIDATE_PATH)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public AdmissionReview validate(AdmissionReview admissionReview) {
return validationController.handle(admissionReview);
}
```See samples also for details.
### Conversion Hooks
Conversion hooks follows the same patter described
in [Kuberbuilder](https://book.kubebuilder.io/multiversion-tutorial/conversion-concepts.html), thus first converts the
custom resource from actual version to a hub, and as next step from the hub to the target resource version.To create the controller
register [mappers](https://github.com/java-operator-sdk/kubernetes-webhooks-framework/blob/main/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Mapper.java)
:```java
@Singleton
public ConversionController conversionController() {
var controller = new ConversionController();
controller.registerMapper(new V1Mapper());
controller.registerMapper(new V2Mapper());
return controller;
}
```and use the controllers in the endpoint:
```java
@PostMapping(CONVERSION_PATH)
@ResponseBody
public ConversionReview convert(@RequestBody ConversionReview conversionReview) {
return conversionController.handle(conversionReview);
}
```