|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.flink.kubernetes.operator.api.bluegreen; |
| 19 | + |
| 20 | +import org.apache.flink.util.Preconditions; |
| 21 | + |
| 22 | +import io.fabric8.kubernetes.api.model.ConfigMap; |
| 23 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 24 | +import io.fabric8.kubernetes.client.KubernetesClientBuilder; |
| 25 | +import io.fabric8.kubernetes.client.informers.ResourceEventHandler; |
| 26 | +import lombok.Getter; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +import java.io.Serializable; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +/** Simple Kubernetes service proxy for Gate operations. */ |
| 34 | +public class GateKubernetesService implements Serializable { |
| 35 | + |
| 36 | + private static final Logger logger = LoggerFactory.getLogger(GateKubernetesService.class); |
| 37 | + |
| 38 | + @Getter private final KubernetesClient kubernetesClient; |
| 39 | + |
| 40 | + private final String namespace; |
| 41 | + private final String configMapName; |
| 42 | + |
| 43 | + public GateKubernetesService(String namespace, String configMapName) { |
| 44 | + Preconditions.checkNotNull(namespace); |
| 45 | + Preconditions.checkNotNull(configMapName); |
| 46 | + |
| 47 | + try { |
| 48 | + kubernetesClient = new KubernetesClientBuilder().build(); |
| 49 | + } catch (Exception e) { |
| 50 | + logger.error("Error instantiating Kubernetes Client", e); |
| 51 | + throw e; |
| 52 | + } |
| 53 | + |
| 54 | + this.namespace = namespace; |
| 55 | + this.configMapName = configMapName; |
| 56 | + } |
| 57 | + |
| 58 | + public void setInformers(ResourceEventHandler<ConfigMap> resourceEventHandler) { |
| 59 | + kubernetesClient |
| 60 | + .configMaps() |
| 61 | + .inNamespace(namespace) |
| 62 | + .withName(configMapName) |
| 63 | + .inform(resourceEventHandler, 0); |
| 64 | + } |
| 65 | + |
| 66 | + public void updateConfigMapEntries(Map<String, String> kvps) { |
| 67 | + var configMap = parseConfigMap(); |
| 68 | + |
| 69 | + kvps.forEach((key, value) -> configMap.getData().put(key, value)); |
| 70 | + |
| 71 | + try { |
| 72 | + kubernetesClient.configMaps().inNamespace(namespace).resource(configMap).update(); |
| 73 | + } catch (Exception e) { |
| 74 | + logger.error("Failed to UPDATE the ConfigMap", e); |
| 75 | + throw e; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public ConfigMap parseConfigMap() { |
| 80 | + try { |
| 81 | + return kubernetesClient |
| 82 | + .configMaps() |
| 83 | + .inNamespace(namespace) |
| 84 | + .withName(configMapName) |
| 85 | + .get(); |
| 86 | + } catch (Exception e) { |
| 87 | + logger.error("Failed to GET the ConfigMap", e); |
| 88 | + throw e; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments