Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2005-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.fabric8.maven;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.fabric8.kubernetes.api.Annotations;
import io.fabric8.kubernetes.api.KubernetesHelper;
import io.fabric8.kubernetes.api.extensions.Templates;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.client.utils.Utils;
import io.fabric8.maven.support.Commandline;
import io.fabric8.maven.support.JsonSchema;
import io.fabric8.maven.support.JsonSchemaProperty;
import io.fabric8.maven.support.VolumeType;
import io.fabric8.openshift.api.model.DeploymentConfig;
import io.fabric8.openshift.api.model.DeploymentConfigBuilder;
import io.fabric8.openshift.api.model.ImageStream;
import io.fabric8.openshift.api.model.ImageStreamBuilder;
import io.fabric8.openshift.api.model.ParameterBuilder;
import io.fabric8.openshift.api.model.Template;
import io.fabric8.openshift.api.model.TemplateBuilder;
import io.fabric8.utils.Base64Encoder;
import io.fabric8.utils.Files;
import io.fabric8.utils.Objects;
import io.fabric8.utils.PropertiesHelper;
import io.fabric8.utils.Strings;
import io.fabric8.utils.URLUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.model.Scm;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static io.fabric8.kubernetes.api.KubernetesHelper.getName;
import static io.fabric8.kubernetes.api.KubernetesHelper.setName;
import static io.fabric8.utils.Files.guessMediaType;
import static io.fabric8.utils.PropertiesHelper.findPropertiesWithPrefix;
import static io.fabric8.utils.PropertiesHelper.getInteger;
/**
* Generates or copies the Kubernetes JSON file and attaches it to the build so its
* installed and released to maven repositories like other build artifacts.
*/
@Mojo(name = "json", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
public class JsonMojo extends AbstractFabric8Mojo {
public static final String FABRIC8_PORT_HOST_PREFIX = "docker.port.host.";
public static final String FABRIC8_PORT_CONTAINER_PREFIX = "docker.port.container.";
public static final String FABRIC8_PORT_SERVICE = "fabric8.service.port";
public static final String FABRIC8_CONTAINER_PORT_SERVICE = "fabric8.service.containerPort";
public static final String FABRIC8_NODE_PORT_SERVICE = "fabric8.service.nodePort";
public static final String FABRIC8_PROTOCOL_SERVICE = "fabric8.service.protocol";
public static final String FABRIC8_METRICS_PREFIX = "fabric8.metrics.";
public static final String FABRIC8_METRICS_SCRAPE = FABRIC8_METRICS_PREFIX + "scrape";
public static final String FABRIC8_METRICS_SCRAPE_ANNOTATION = FABRIC8_METRICS_SCRAPE + ".annotation";
public static final String FABRIC8_METRICS_PORT = FABRIC8_METRICS_PREFIX + "port";
public static final String FABRIC8_METRICS_PORT_ANNOTATION = FABRIC8_METRICS_PORT + ".annotation";
public static final String FABRIC8_METRICS_SCHEME = FABRIC8_METRICS_PREFIX + "scheme";
public static final String FABRIC8_METRICS_SCHEME_ANNOTATION = FABRIC8_METRICS_SCHEME + ".annotation";
public static final String FABRIC8_ICON_URL_ANNOTATION = "fabric8.io/iconUrl";
private static final String SERVICE_REGEX = "^fabric8\\.service\\.(?[^. ]+)\\..+$";
private static final Pattern SERVICE_PATTERN = Pattern.compile(SERVICE_REGEX);
private static final String NAME = "name";
private static final String ATTRIBUTE_TYPE = "attributeType";
private static final String VOLUME_MOUNT_PATH = "mountPath";
private static final String VOLUME_REGEX = "fabric8.volume.(?[^. ]*).(?[^. ]*)";
private static final Pattern VOLUME_PATTERN = Pattern.compile(VOLUME_REGEX);
private static final String PARAM_REGEX = "fabric8.parameter.(?[^. ]*)(.)?(?[^ ]*)";
private static final Pattern PARAM_PATTERN = Pattern.compile(PARAM_REGEX);
private static final String TEMPLATE_NAME = "fabric8.template";
private static final String PARAMETER_PREFIX = "fabric8.parameter";
private static final String PARAMETER_NAME_PREFIX = PARAMETER_PREFIX + ".%s";
private static final String PARAMETER_PROPERTY = PARAMETER_NAME_PREFIX + ".%s";
private static final String GENERATE = "generate";
private static final String FROM = "from";
private static final String VALUE = "value";
private static final String DESCRIPTION = "description";
@Component
private MavenProjectHelper projectHelper;
/**
* The artifact type for attaching the generated kubernetes json file to the project
*/
@Parameter(property = "fabric8.kubernetes.artifactType", defaultValue = "json")
private String artifactType = "json";
/**
* The artifact classifier for attaching the generated kubernetes json file to the project
*/
@Parameter(property = "fabric8.kubernetes.artifactClassifier", defaultValue = "kubernetes")
private String artifactClassifier = "kubernetes";
/**
* Whether or not we should generate the Kubernetes JSON file using the MVEL template if there is not one specified
* in the build (usually in src/main/resources/kubernetes.json)
*/
@Parameter(property = "fabric8.generateJson", defaultValue = "true")
private boolean generateJson;
/**
* Should we fail the build if no json files could be found
*/
@Parameter(property = "fabric8.failOnMissingJsonFiles", defaultValue = "true")
private boolean failOnMissingJsonFiles;
/**
* Should we generate any required SecurityContextConstraints DTOs in the generated json
*/
@Parameter(property = "fabric8.generateSecurityContextConstraints", defaultValue = "false")
private boolean generateSecurityContextConstraints;
/**
* Whether we should include the namespace in the containers' env vars
*/
@Parameter(property = "fabric8.includeNamespaceEnvVar", defaultValue = "true")
private boolean includeNamespaceEnvVar;
/**
* The name of the env var to add that will contain the namespace at container runtime
*/
@Parameter(property = "fabric8.namespaceEnvVar", defaultValue = "KUBERNETES_NAMESPACE")
private String kubernetesNamespaceEnvVar;
/**
* Whether we should include the namespace in the containers' env vars
*/
@Parameter(property = "fabric8.includePodEnvVar", defaultValue = "false")
private boolean includePodEnvVar;
/**
* The name of the env var to add that will contain the pod name at container runtime
*/
@Parameter(property = "fabric8.podEnvVar", defaultValue = "KUBERNETES_POD")
private String kubernetesPodEnvVar;
/**
* The provider to include as a label. Set to empty to disable.
*/
@Parameter(property = "fabric8.provider", defaultValue = "fabric8")
private String provider;
/**
* The labels passed into the generated Kubernetes JSON template.
*
* If no value is explicitly configured in the maven plugin then we use all maven properties starting with "fabric8.label."
*/
@Parameter()
private Map labels;
/**
* The annotations for the PodSpec
*/
@Parameter()
private Map podSpecAnnotations;
/**
* The annotations for the ReplicationController
*/
@Parameter()
private Map rcAnnotations;
/**
* The annotations for the Template
*/
@Parameter()
private Map templateAnnotations;
/**
* The annotations for the Service
*/
@Parameter()
private Map serviceAnnotations;
/**
* The environment variables passed into the generated Kubernetes JSON template.
*
* If no value is explicitly configured in the maven plugin then we use all maven properties starting with "fabric8.env."
*/
@Parameter()
private List environmentVariables;
/**
* The container ports passed into the generated Kubernetes JSON template.
*/
@Parameter()
private List containerPorts;
/**
* Maps the port names to the default container port numbers
*/
@Parameter()
private Map defaultContainerPortMap;
/**
* The service ports passed into the generated Kubernetes JSON template.
*/
@Parameter()
private List servicePorts;
/**
* The ID prefix used in the generated Kubernetes JSON template
*/
@Parameter(property = "fabric8.replicas", defaultValue = "1")
private Integer replicaCount;
/**
* Should we wrap the generated ReplicationController objects in a DeploymentConfig
*/
// TODO lets disable by default until its working :)
@Parameter(property = "fabric8.useDeploymentConfig", defaultValue = "false")
private boolean useDeploymentConfig;
/**
* The last triggered image tag if generating a DeploymentConfig
*/
@Parameter(property = "fabric8.lastTriggeredImageTag", defaultValue = "latest")
private String lastTriggeredImageTag;
/**
* The strategy name for the DeploymentConfig
*/
@Parameter(property = "fabric8.deploymentStrategy", defaultValue = "Recreate")
private String deploymentStrategy;
/**
* The extra additional kubernetes JSON file for things like services
*/
@Parameter(property = "fabric8.extra.json", defaultValue = "${basedir}/target/classes/kubernetes-extra.json")
private File kubernetesExtraJson;
/**
* Temporary directory used for creating the template annotations
*/
@Parameter(property = "fabric8.templateTempDir", defaultValue = "${basedir}/target/fabric8/template-workdir")
private File templateTempDir;
/**
* The URL to use to link to the icon in the generated Template.
*
* For using a common set of icons, see the {@link #iconRef} option.
*/
@Parameter(property = "fabric8.iconUrl")
private String iconUrl;
/**
* The URL prefix added to the relative path of the icon file
*/
@Parameter(property = "fabric8.iconUrlPrefix")
private String iconUrlPrefix;
/**
* The SCM branch used when creating a URL to the icon file
*/
@Parameter(property = "fabric8.iconBranch", defaultValue = "master")
private String iconBranch;
/**
* The replication controller name used in the generated Kubernetes JSON template
*/
@Parameter(property = "fabric8.replicationController.name", defaultValue = "${project.artifactId}")
private String replicationControllerName;
/**
* The project label used in the generated Kubernetes JSON template
*/
@Parameter(property = "fabric8.label.project", defaultValue = "${project.artifactId}")
private String projectName;
/**
* The project label used in the generated Kubernetes JSON dependencies template
*/
@Parameter(property = "fabric8.combineJson.project", defaultValue = "${project.artifactId}")
private String combineProjectName;
/**
* The group label used in the generated Kubernetes JSON template
*/
@Parameter(property = "fabric8.label.group", defaultValue = "${project.groupId}")
private String groupName;
/**
* The name label used in the generated Kubernetes JSON template
*/
@Parameter(property = "fabric8.container.name", defaultValue = "${project.artifactId}")
private String kubernetesContainerName;
/**
* The service name
*/
@Parameter(property = "fabric8.service.name", defaultValue = "${project.artifactId}")
private String serviceName;
/**
* Should we generate headless services (services with no ports)
*/
// TODO for now lets default to not creating headless services as it barfs when used with kubernetes...
//@Parameter(property = "fabric8.service.headless", defaultValue = "true")
@Parameter(property = "fabric8.service.headless", defaultValue = "false")
private boolean headlessService;
/**
* The Type of the service. Set to
* "LoadBalancer" if you wish an
* external load balancer to be created
*/
@Parameter(property = "fabric8.service.type")
private String serviceType;
/**
* Annotation value to add for metrics scraping.
*/
@Parameter(property = FABRIC8_METRICS_SCRAPE, defaultValue = "false")
private boolean metricsScrape;
/**
* Annotation to add for metrics scraping.
*/
@Parameter(property = FABRIC8_METRICS_SCRAPE_ANNOTATION, defaultValue = "prometheus.io/scrape")
private String metricsScrapeAnnotation;
/**
* Annotation value to add for metrics port.
*/
@Parameter(property = FABRIC8_METRICS_PORT)
private Integer metricsPort;
/**
* Annotation value to add for metrics port.
*/
@Parameter(property = FABRIC8_METRICS_PORT_ANNOTATION, defaultValue = "prometheus.io/port")
private String metricsPortAnnotation;
/**
* Annotation value to add for metrics scheme.
*/
@Parameter(property = FABRIC8_METRICS_SCHEME)
private String metricsScheme;
/**
* Annotation to add for metrics scheme.
*/
@Parameter(property = FABRIC8_METRICS_SCHEME_ANNOTATION, defaultValue = "prometheus.io/scheme")
private String metricsSchemeAnnotation;
/**
* The service port
*/
@Parameter(property = FABRIC8_PORT_SERVICE)
private Integer servicePort;
/**
* The service container port
*/
@Parameter(property = FABRIC8_CONTAINER_PORT_SERVICE)
private String serviceContainerPort;
/**
* The service node port
*/
@Parameter(property = FABRIC8_NODE_PORT_SERVICE)
private Integer serviceNodePort;
/**
* The service protocol
*/
@Parameter(property = FABRIC8_PROTOCOL_SERVICE, defaultValue = "TCP")
private String serviceProtocol;
/**
* The docker image pull policy for non-snapshots
*/
@Parameter(property = "fabric8.imagePullPolicy")
private String imagePullPolicy;
/**
* The docker image pull policy for snapshot releases (which should pull always)
*/
@Parameter(property = "fabric8.imagePullPolicySnapshot")
private String imagePullPolicySnapshot;
/**
* Whether the plugin should discover all the environment variable json schema files in the classpath and export those into the generated kubernetes JSON
*/
@Parameter(property = "fabric8.includeAllEnvironmentVariables", defaultValue = "true")
private boolean includeAllEnvironmentVariables;
@Parameter(property = "fabric8.containerPrivileged")
protected Boolean containerPrivileged;
@Parameter(property = "fabric8.serviceAccount")
protected String serviceAccount;
/**
* Should we create the ServiceAccount resource as part of the build
*/
@Parameter(property = "fabric8.serviceAccountCreate")
private boolean createServiceAccount;
/**
* The properties file used to specify the OpenShift Template parameter values and descriptions. The properties file should be of the form
*
*
* FOO.value = ABC
* FOO.description = this is the description of FOO
*
*
*/
@Parameter(property = "fabric8.templateParametersFile", defaultValue = "${basedir}/src/main/fabric8/templateParameters.properties")
protected File templateParametersPropertiesFile;
/**
* The properties file used to specify the annotations to be added to the generated PodSpec
*
*
* acme.com/cheese = SOMETHING
*
*
*/
@Parameter(property = "fabric8.podSpecAnnotationsFile", defaultValue = "${basedir}/src/main/fabric8/podSpecAnnotations.properties")
protected File podSpecAnnotationsFile;
/**
* The properties file used to specify the annotations to be added to the generated ReplicationController
*
*
* acme.com/cheese = SOMETHING
*
*
*/
@Parameter(property = "fabric8.rcAnnotationsFile", defaultValue = "${basedir}/src/main/fabric8/rcAnnotations.properties")
protected File rcAnnotationsFile;
/**
* The properties file used to specify the annotations to be added to the generated Template
*
*
* acme.com/cheese = SOMETHING
*
*
*/
@Parameter(property = "fabric8.templateAnnotationsFile", defaultValue = "${basedir}/src/main/fabric8/templateAnnotations.properties")
protected File templateAnnotationsFile;
/**
* The properties file used to specify the annotations to be added to the generated Service
*
*
* acme.com/cheese = SOMETHING
*
*
*/
@Parameter(property = "fabric8.serviceAnnotationsFile", defaultValue = "${basedir}/src/main/fabric8/serviceAnnotations.properties")
protected File serviceAnnotationsFile;
/**
* Defines the maximum size in kilobytes that the data encoded URL of the icon should be before we defer
* and try to use an external URL
*/
@Parameter(property = "fabric8.maximumDataUrlSizeK", defaultValue = "2")
private int maximumDataUrlSizeK;
@Component
protected ArtifactResolver resolver;
@Parameter(property = "localRepository", readonly = true, required = true)
protected ArtifactRepository localRepository;
@Parameter(property = "project.remoteArtifactRepositories")
protected List remoteRepositories;
/**
* The default requests storage size for a PersistenceVolumeClaim if its created for a persistent volume via a claim
*/
@Parameter(property = "fabric8.defaultPersistentVolumeClaimRequestsStorage", defaultValue = "20")
private String defaultPersistentVolumeClaimRequestsStorage;
/**
* Should we remove the version label from the service selector?
*/
@Parameter(property = "fabric8.removeVersionLabelFromServiceSelector", defaultValue = "true")
private boolean removeVersionLabelFromServiceSelector;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
File json = getKubernetesJson();
getLog().info("Configured with file: " + json);
if (json == null) {
throw new MojoExecutionException("No kubernetes json file is specified!");
}
if (shouldGenerateForThisProject()) {
if (!isIgnoreProject() || combineDependencies) {
if (generateJson) {
generateKubernetesJson(json);
if (combineDependencies) {
combineDependentJsonFiles(getKubernetesCombineJson() == null ? json : getKubernetesCombineJson());
}
if (kubernetesExtraJson != null && kubernetesExtraJson.exists()) {
combineJsonFiles(json, kubernetesExtraJson);
}
}
if (json.exists() && json.isFile()) {
if (useDeploymentConfig) {
wrapInDeploymentConfigs(json);
}
addEnvironmentAnnotations(json);
}
}
}
}
@Override
protected boolean shouldGenerateForThisProject() {
return super.shouldGenerateForThisProject() || combineDependencies;
}
protected void combineDependentJsonFiles(File json) throws MojoExecutionException {
try {
MavenProject project = getProject();
Set jsonFiles = new LinkedHashSet<>();
Set dependencyArtifacts = project.getDependencyArtifacts();
for (Artifact artifact : dependencyArtifacts) {
String classifier = artifact.getClassifier();
String type = artifact.getType();
File file = artifact.getFile();
if (isKubernetesJsonArtifact(classifier, type)) {
if (file != null) {
System.out.println("Found kubernetes JSON dependency: " + artifact);
jsonFiles.add(file);
} else {
Set artifacts = resolveArtifacts(artifact);
for (Artifact resolvedArtifact : artifacts) {
classifier = resolvedArtifact.getClassifier();
type = resolvedArtifact.getType();
file = resolvedArtifact.getFile();
if (isKubernetesJsonArtifact(classifier, type) && file != null) {
System.out.println("Resolved kubernetes JSON dependency: " + artifact);
jsonFiles.add(file);
}
}
}
}
}
List