io.jshift.maven.enricher.api.visitor.MetadataVisitor Maven / Gradle / Ivy
The newest version!
/**
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.jshift.maven.enricher.api.visitor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import io.fabric8.kubernetes.api.builder.TypedVisitor;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder;
import io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder;
import io.fabric8.kubernetes.api.model.batch.JobBuilder;
import io.fabric8.kubernetes.api.model.extensions.IngressBuilder;
import io.jshift.kit.config.resource.MetaDataConfig;
import io.jshift.kit.config.resource.ProcessorConfig;
import io.jshift.kit.config.resource.ResourceConfig;
import io.jshift.maven.enricher.api.Enricher;
import io.jshift.maven.enricher.api.Kind;
import io.fabric8.openshift.api.model.BuildBuilder;
import io.fabric8.openshift.api.model.BuildConfigBuilder;
import io.fabric8.openshift.api.model.DeploymentConfigBuilder;
import io.fabric8.openshift.api.model.ImageStreamBuilder;
/**
* Visitor which adds labels and annotations
*
* @author roland
* @since 02/05/16
*/
public abstract class MetadataVisitor extends TypedVisitor {
private static ThreadLocal configHolder = new ThreadLocal<>();
private final Map labelsFromConfig;
private final Map annotationFromConfig;
private List enrichers = null;
private MetadataVisitor(ResourceConfig resourceConfig) {
if (resourceConfig != null) {
labelsFromConfig = getMapFromConfiguration(resourceConfig.getLabels(), getKind());
annotationFromConfig = getMapFromConfiguration(resourceConfig.getAnnotations(), getKind());
} else {
labelsFromConfig = new HashMap<>();
annotationFromConfig = new HashMap<>();
}
this.enrichers = enrichers;
}
public static void setProcessorConfig(ProcessorConfig config) {
configHolder.set(config);
}
public static void clearProcessorConfig() {
configHolder.set(null);
}
private ProcessorConfig getProcessorConfig() {
ProcessorConfig config = configHolder.get();
if (config == null) {
throw new IllegalArgumentException("No ProcessorConfig set");
}
return config;
}
public void visit(T item) {
ProcessorConfig config = getProcessorConfig();
ObjectMeta metadata = getOrCreateMetadata(item);
updateLabels(metadata);
updateAnnotations(metadata);
}
private void updateLabels(ObjectMeta metadata) {
overlayMap(metadata.getLabels(),labelsFromConfig);
}
private void updateAnnotations(ObjectMeta metadata) {
overlayMap(metadata.getAnnotations(),annotationFromConfig);
}
private Map getMapFromConfiguration(MetaDataConfig config, Kind kind) {
if (config == null) {
return new HashMap<>();
}
Map ret;
if (kind == Kind.SERVICE) {
ret = propertiesToMap(config.getService());
} else if (kind == Kind.DEPLOYMENT || kind == Kind.DEPLOYMENT_CONFIG) {
ret = propertiesToMap(config.getDeployment());
} else if (kind == Kind.REPLICATION_CONTROLLER || kind == Kind.REPLICA_SET) {
ret = propertiesToMap(config.getReplicaSet());
} else if (kind == Kind.POD_SPEC) {
ret = propertiesToMap(config.getPod());
} else if (kind == Kind.INGRESS) {
ret = propertiesToMap(config.getIngress());
} else {
ret = new HashMap<>();
}
if (config.getAll() != null) {
ret.putAll(propertiesToMap(config.getAll()));
}
return ret;
}
private Map propertiesToMap(Properties properties) {
Map propertyMap = new HashMap<>();
if(properties != null) {
for (Map.Entry
© 2015 - 2024 Weber Informatics LLC | Privacy Policy