33Framework and tooling to support
44implementing [ admission controllers] ( https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/ )
55and [ conversion hooks] ( https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#webhook-conversion )
6- for Kubernetes in Java. Supports both ** quarkus** and ** spring boot** . Both Sync and Async programing model.
6+ for Kubernetes in Java. Supports both ** quarkus** and ** spring boot** . Both ** sync ** and ** async ** programing model.
77
88## Sample Usage
99
1010### Admission Controllers
1111
1212Defining a mutation or validation controller is simple as:
1313
14- https://github.com/ java-operator-sdk/kubernetes-webhooks-framework/blob/0946595d941b789caef6a69b34c2e5be8c6b59cf/samples/quarkus/src/main/java/io/javaoperatorsdk/admissioncontroller/sample/quarkus/AdmissionControllerConfig.java#L31-L68
14+ ``` java
1515
16- What can be then simple used in an endpoint:
16+ @Singleton
17+ @Named (MUTATING_CONTROLLER )
18+ public AdmissionController<Pod > mutatingController() {
19+ return new AdmissionController<> ((resource, operation) - > {
20+ if (resource. getMetadata(). getLabels() == null ) {
21+ resource. getMetadata(). setLabels(new HashMap<> ());
22+ }
23+ resource. getMetadata(). getLabels(). putIfAbsent(APP_NAME_LABEL_KEY , " mutation-test" );
24+ return resource;
25+ });
26+ }
27+
28+ @Singleton
29+ @Named (VALIDATING_CONTROLLER )
30+ public AdmissionController<Pod > validatingController() {
31+ return new AdmissionController<> ((resource, operation) - > {
32+ if (resource. getMetadata(). getLabels() == null
33+ || resource. getMetadata(). getLabels(). get(APP_NAME_LABEL_KEY ) == null ) {
34+ throw new NotAllowedException (" Missing label: " + APP_NAME_LABEL_KEY );
35+ }
36+ });
37+ }
38+
39+ ```
40+
41+ What can be simply used in an endpoint:
42+
43+ ``` java
44+ @POST
45+ @Path (MUTATE_PATH )
46+ @Consumes (MediaType . APPLICATION_JSON )
47+ @Produces (MediaType . APPLICATION_JSON )
48+ public AdmissionReview mutate(AdmissionReview admissionReview) {
49+ return mutationController. handle(admissionReview);
50+ }
51+
52+ @POST
53+ @Path (VALIDATE_PATH )
54+ @Consumes (MediaType . APPLICATION_JSON )
55+ @Produces (MediaType . APPLICATION_JSON )
56+ public AdmissionReview validate(AdmissionReview admissionReview) {
57+ return validationController. handle(admissionReview);
58+ }
59+ ```
1760
18- https://github.com/java-operator-sdk/kubernetes-webhooks-framework/blob/0946595d941b789caef6a69b34c2e5be8c6b59cf/samples/quarkus/src/main/java/io/javaoperatorsdk/admissioncontroller/sample/quarkus/AdmissionEndpoint.java#L57-L89
1961
2062See samples also for details.
2163
@@ -29,8 +71,22 @@ To create the controller
2971register [ mappers] ( https://github.com/java-operator-sdk/kubernetes-webhooks-framework/blob/main/core/src/main/java/io/javaoperatorsdk/webhook/conversion/Mapper.java )
3072:
3173
32- https://github.com/java-operator-sdk/kubernetes-webhooks-framework/blob/2a2bce54b49ea3398bef95a9102ee8645e11cf87/samples/quarkus/src/main/java/io/javaoperatorsdk/webhook/admission/sample/quarkus/conversion/ConversionControllerConfig.java#L15-L29
74+ ``` java
75+ @Singleton
76+ public ConversionController conversionController() {
77+ var controller = new ConversionController ();
78+ controller. registerMapper(new V1Mapper ());
79+ controller. registerMapper(new V2Mapper ());
80+ return controller;
81+ }
82+ ```
3383
3484and use the controllers in the endpoint:
3585
36- https://github.com/java-operator-sdk/kubernetes-webhooks-framework/blob/2a2bce54b49ea3398bef95a9102ee8645e11cf87/samples/spring-boot/src/main/java/io/javaoperatorsdk/webhook/sample/springboot/conversion/ConversionEndpoint.java#L29-L40
86+ ``` java
87+ @PostMapping (CONVERSION_PATH )
88+ @ResponseBody
89+ public ConversionReview convert(@RequestBody ConversionReview conversionReview) {
90+ return conversionController. handle(conversionReview);
91+ }
92+ ```
0 commit comments