001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.kubernetes;
018
019import org.apache.logging.log4j.Logger;
020import org.apache.logging.log4j.status.StatusLogger;
021
022import io.fabric8.kubernetes.client.Config;
023import io.fabric8.kubernetes.client.ConfigBuilder;
024import io.fabric8.kubernetes.client.DefaultKubernetesClient;
025import io.fabric8.kubernetes.client.KubernetesClient;
026
027/**
028 * Builds a Kubernetes Client.
029 */
030public class KubernetesClientBuilder {
031
032    private static final Logger LOGGER = StatusLogger.getLogger();
033
034    public KubernetesClient createClient() {
035        Config config = kubernetesClientConfig();
036        return config != null ? new DefaultKubernetesClient(config) : null;
037    }
038
039    private Config kubernetesClientConfig() {
040        Config base = null;
041        try {
042            base = Config.autoConfigure(null);
043        } catch (Exception ex) {
044            if (ex instanceof  NullPointerException) {
045                return null;
046            }
047        }
048        KubernetesClientProperties props = new KubernetesClientProperties(base);
049        Config properties = new ConfigBuilder(base)
050                .withApiVersion(props.getApiVersion())
051                .withCaCertData(props.getCaCertData())
052                .withCaCertFile(props.getCaCertFile())
053                .withClientCertData(props.getClientCertData())
054                .withClientCertFile(props.getClientCertFile())
055                .withClientKeyAlgo(props.getClientKeyAlgo())
056                .withClientKeyData(props.getClientKeyData())
057                .withClientKeyFile(props.getClientKeyFile())
058                .withClientKeyPassphrase(props.getClientKeyPassphrase())
059                .withConnectionTimeout(props.getConnectionTimeout())
060                .withHttpProxy(props.getHttpProxy())
061                .withHttpsProxy(props.getHttpsProxy())
062                .withMasterUrl(props.getMasterUrl())
063                .withNamespace(props.getNamespace())
064                .withNoProxy(props.getNoProxy())
065                .withPassword(props.getPassword())
066                .withProxyPassword(props.getProxyPassword())
067                .withProxyUsername(props.getProxyUsername())
068                .withRequestTimeout(props.getRequestTimeout())
069                .withRollingTimeout(props.getRollingTimeout())
070                .withTrustCerts(props.isTrustCerts())
071                .withUsername(props.getUsername())
072                .build();
073        return properties;
074    }
075}