|
| 1 | +package io.javaoperatorsdk.webhook.sample.commons; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.concurrent.CompletableFuture; |
| 5 | + |
| 6 | +import io.fabric8.kubernetes.api.model.Pod; |
| 7 | +import io.javaoperatorsdk.webhook.admission.AdmissionController; |
| 8 | +import io.javaoperatorsdk.webhook.admission.AsyncAdmissionController; |
| 9 | +import io.javaoperatorsdk.webhook.admission.NotAllowedException; |
| 10 | +import io.javaoperatorsdk.webhook.admission.mutation.AsyncMutator; |
| 11 | +import io.javaoperatorsdk.webhook.admission.mutation.Mutator; |
| 12 | +import io.javaoperatorsdk.webhook.admission.validation.Validator; |
| 13 | + |
| 14 | +public class AdmissionControllers { |
| 15 | + |
| 16 | + public static final String ERROR_MESSAGE = "Some error happened"; |
| 17 | + public static final String APP_NAME_LABEL_KEY = "app.kubernetes.io/name"; |
| 18 | + |
| 19 | + public static AdmissionController<Pod> mutatingController() { |
| 20 | + return new AdmissionController<>((resource, operation) -> { |
| 21 | + if (resource.getMetadata().getLabels() == null) { |
| 22 | + resource.getMetadata().setLabels(new HashMap<>()); |
| 23 | + } |
| 24 | + |
| 25 | + resource.getMetadata().getLabels().putIfAbsent(APP_NAME_LABEL_KEY, "mutation-test"); |
| 26 | + return resource; |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + public static 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 | + public static AsyncAdmissionController<Pod> asyncMutatingController() { |
| 40 | + return new AsyncAdmissionController<>( |
| 41 | + (AsyncMutator<Pod>) (resource, operation) -> CompletableFuture.supplyAsync(() -> { |
| 42 | + if (resource.getMetadata().getLabels() == null) { |
| 43 | + resource.getMetadata().setLabels(new HashMap<>()); |
| 44 | + } |
| 45 | + resource.getMetadata().getLabels().putIfAbsent(APP_NAME_LABEL_KEY, "mutation-test"); |
| 46 | + return resource; |
| 47 | + })); |
| 48 | + } |
| 49 | + |
| 50 | + public static AsyncAdmissionController<Pod> asyncValidatingController() { |
| 51 | + return new AsyncAdmissionController<>((resource, operation) -> { |
| 52 | + if (resource.getMetadata().getLabels() == null |
| 53 | + || resource.getMetadata().getLabels().get(APP_NAME_LABEL_KEY) == null) { |
| 54 | + throw new NotAllowedException("Missing label: " + APP_NAME_LABEL_KEY); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + public static AdmissionController<Pod> errorMutatingController() { |
| 61 | + return new AdmissionController<>((Validator<Pod>) (resource, operation) -> { |
| 62 | + throw new IllegalStateException(ERROR_MESSAGE); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + public static AdmissionController<Pod> errorValidatingController() { |
| 67 | + return new AdmissionController<>((Mutator<Pod>) (resource, operation) -> { |
| 68 | + throw new IllegalStateException(ERROR_MESSAGE); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + public static AsyncAdmissionController<Pod> errorAsyncMutatingController() { |
| 73 | + return new AsyncAdmissionController<>((AsyncMutator<Pod>) (resource, operation) -> { |
| 74 | + throw new IllegalStateException(ERROR_MESSAGE); |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + public static AsyncAdmissionController<Pod> errorAsyncValidatingController() { |
| 79 | + return new AsyncAdmissionController<>((Validator<Pod>) (resource, operation) -> { |
| 80 | + throw new IllegalStateException(ERROR_MESSAGE); |
| 81 | + }); |
| 82 | + } |
| 83 | +} |
0 commit comments