Skip to content

Commit

Permalink
Provide support for setting BuildConfig memory/CPU requests and limits
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia committed Mar 9, 2020
1 parent 0d02f1e commit 6ce537c
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Usage:
```
### 0.2.1-SNAPSHOT
* Fix #53: Renamed plugins to openshift/kubernetes-maven-plugin keeping acronym (oc/k8s) for goal
* Support for setting BuildConfig memory/cpu request and limits, Ported PR fabric8io/fabric8-maven-plugin#1772

### 0.2.0 (05-03-2020)
* Fix #71: script to extract changelog information for notifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
Expand All @@ -37,6 +38,7 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Pattern;

import io.fabric8.kubernetes.api.model.Config;
import io.fabric8.kubernetes.api.model.Container;
Expand All @@ -58,6 +60,7 @@
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.PodSpec;
import io.fabric8.kubernetes.api.model.PodStatus;
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.ReplicationController;
import io.fabric8.kubernetes.api.model.ReplicationControllerSpec;
import io.fabric8.kubernetes.api.model.apps.DaemonSet;
Expand All @@ -84,6 +87,7 @@
import io.fabric8.openshift.api.model.DeploymentConfigSpec;
import io.fabric8.openshift.api.model.Template;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.ResourceFileType;
Expand All @@ -94,6 +98,10 @@
*/
public class KubernetesHelper {
protected static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssX";
private static final String FILENAME_PATTERN_REGEX = "^(?<name>.*?)(-(?<type>[^-]+))?\\.(?<ext>yaml|yml|json)$";
private static final String PROFILES_PATTERN_REGEX = "^profiles?\\.ya?ml$";
private static final Pattern FILENAME_PATTERN = Pattern.compile(FILENAME_PATTERN_REGEX);
private static final Pattern EXCLUDE_PATTERN = Pattern.compile(PROFILES_PATTERN_REGEX);

/**
* Validates that the given value is valid according to the kubernetes ID parsing rules, throwing an exception if not.
Expand Down Expand Up @@ -828,5 +836,74 @@ public static boolean removeEnvVar(List<EnvVar> envVarList, String name) {
}
return removed;
}


/**
* Get a specific resource fragment ending with some suffix
*
* @param resourceDirFinal resource directory
* @param remotes list remote fragments if provided
* @param resourceNameSuffix resource name suffix
* @param log log object
* @return file if present or null
*/
public static File getResourceFragmentFromSource(File resourceDirFinal, List<String> remotes, String resourceNameSuffix, KitLogger log) {
if (resourceDirFinal != null) {
File[] resourceFiles = listResourceFragments(resourceDirFinal, remotes, log);

if (resourceFiles != null) {
for (File file : resourceFiles) {
if (file.getName().endsWith(resourceNameSuffix)) {
return file;
}
}
}
}
return null;
}

/**
* Get requests or limit objects from string hashmaps
*
* @param quantity hashmap of strings
* @return hashmap of string to quantity
*/
public static Map<String, Quantity> getQuantityFromString(Map<String, String> quantity) {
Map<String, Quantity> stringQuantityMap = new HashMap<>();
if (quantity != null && !quantity.isEmpty()) {
for (Map.Entry<String, String> entry : quantity.entrySet()) {
stringQuantityMap.put(entry.getKey(), new Quantity(entry.getValue()));
}
}
return stringQuantityMap;
}

public static File[] listResourceFragments(File localResourceDir, List<String> remotes, KitLogger log) {
File[] resourceFiles = listResourceFragments(localResourceDir);

if(remotes != null) {
File[] remoteResourceFiles = listRemoteResourceFragments(remotes, log);
if (remoteResourceFiles.length > 0) {
resourceFiles = ArrayUtils.addAll(resourceFiles, remoteResourceFiles);
}
}
return resourceFiles;
}

private static File[] listResourceFragments(File resourceDir) {
return resourceDir.listFiles((File dir, String name) -> FILENAME_PATTERN.matcher(name).matches() && !EXCLUDE_PATTERN.matcher(name).matches());
}

private static File[] listRemoteResourceFragments(List<String> remotes, KitLogger log) {
if (remotes != null && !remotes.isEmpty()) {
final File remoteResources = FileUtil.createTempDirectory();
FileUtil.downloadRemotes(remoteResources, remotes, log);

if (remoteResources.isDirectory()) {
return remoteResources.listFiles();
}
}
return new File[0];
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.common.util;

import mockit.Mocked;
import org.eclipse.jkube.kit.common.KitLogger;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class KubernetesHelperTest {
@Mocked
KitLogger logger;

@Test
public void testListResourceFragments() {
File localResourceDir = new File(getClass().getResource("/util/fragments").getPath());

assertLocalFragments(KubernetesHelper.listResourceFragments(localResourceDir, null, logger), 2);
}

@Test
public void testResourceFragmentsWithRemotes() {
List<String> remoteStrList = new ArrayList<>();

remoteStrList.add("https://gist.githubusercontent.com/lordofthejars/ac2823cec7831697d09444bbaa76cd50/raw/e4b43f1b6494766dfc635b5959af7730c1a58a93/deployment.yaml");
remoteStrList.add("https://gist.githubusercontent.com/rohanKanojia/c4ac4ae5533f0bf0dd77d13c905face7/raw/8a7de1e27c1f437c1ccbd186ed247efd967953ee/sa.yml");
File localResourceDir = new File(getClass().getResource("/util/fragments").getPath());

File[] fragments = KubernetesHelper.listResourceFragments(localResourceDir, remoteStrList, logger);
assertLocalFragments(fragments, 4);
assertEquals("sa.yml", fragments[2].getName());
assertEquals("deployment.yaml", fragments[3].getName());
}

private void assertLocalFragments(File[] fragments, int expectedSize) {
assertEquals(expectedSize, fragments.length);
assertEquals("deployment.yml", fragments[0].getName());
assertEquals("service.yml", fragments[1].getName());
}
}
35 changes: 35 additions & 0 deletions jkube-kit/common/src/test/resources/util/fragments/deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at:
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

spec:
replicas: 1
template:
spec:
volumes:
- name: config
gitRepo:
repository: 'https://github.com/jstrachan/sample-springboot-config.git'
revision: 667ee4db6bc842b127825351e5c9bae5a4fb2147
directory: .
containers:
- volumeMounts:
- name: config
mountPath: /app/config
env:
- name: KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
serviceAccount: ribbon
19 changes: 19 additions & 0 deletions jkube-kit/common/src/test/resources/util/fragments/service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at:
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

metadata:
annotations:
api.service.kubernetes.io/path: /hello
spec:
type: LoadBalancer
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.config.resource;

import java.util.Map;

public class OpenshiftBuildConfig {
private Map<String, String> limits;
private Map<String, String> requests;

public Map<String, String> getRequests() {
return requests;
}

public void setRequests(Map<String, String> requests) {
this.requests = requests;
}

public Map<String, String> getLimits() {
return limits;
}

public void setLimits(Map<String, String> resourceLimits) {
this.limits = resourceLimits;
}

public static class Builder {
private OpenshiftBuildConfig openshiftBuildConfig;

public Builder() {
this.openshiftBuildConfig = new OpenshiftBuildConfig();
}

public Builder(OpenshiftBuildConfig openshiftBuildConfig) {
if (openshiftBuildConfig != null) {
this.openshiftBuildConfig.limits = openshiftBuildConfig.limits;
this.openshiftBuildConfig.requests = openshiftBuildConfig.requests;
}
}

public Builder limits(Map<String, String> limits) {
this.openshiftBuildConfig.limits = limits;
return this;
}

public Builder requests(Map<String, String> requests) {
this.openshiftBuildConfig.requests = requests;
return this;
}

public OpenshiftBuildConfig build() {
return this.openshiftBuildConfig;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public class ResourceConfig {

private List<IngressRule> ingressRules;

private OpenshiftBuildConfig openshiftBuildConfig;

public Optional<Map<String, String>> getEnv() {
return Optional.ofNullable(env);
}
Expand Down Expand Up @@ -149,6 +151,10 @@ public List<String> getRemotes() {

public List<IngressRule> getIngressRules() { return ingressRules; }

public OpenshiftBuildConfig getOpenshiftBuildConfig() {
return openshiftBuildConfig;
}

// =============================================================================================

public static class Builder {
Expand Down Expand Up @@ -250,6 +256,11 @@ public Builder withCustomResourceDefinitions(List<String> customResourceDefiniti
return this;
}

public Builder withOpenshiftBuildConfig(OpenshiftBuildConfig openshiftBuildConfig) {
config.openshiftBuildConfig = openshiftBuildConfig;
return this;
}

public ResourceConfig build() {
return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.jkube.kit.build.service.docker.helper.Task;
import org.eclipse.jkube.kit.config.image.build.OpenShiftBuildStrategy;
import org.eclipse.jkube.kit.config.resource.BuildRecreateMode;
import org.eclipse.jkube.kit.config.resource.ResourceConfig;

import java.io.File;

Expand Down Expand Up @@ -71,6 +72,10 @@ class BuildServiceConfig {

private boolean s2iImageStreamLookupPolicyLocal;

private ResourceConfig resourceConfig;

private File resourceDir;

public BuildServiceConfig() {
}

Expand Down Expand Up @@ -126,6 +131,14 @@ public void attachArtifact(String classifier, File destFile) {
}
}

public ResourceConfig getResourceConfig() {
return resourceConfig;
}

public File getResourceDir() {
return resourceDir;
}

public static class Builder {
private BuildServiceConfig config;

Expand Down Expand Up @@ -197,6 +210,16 @@ public Builder imagePullManager(ImagePullManager imagePullManager) {
return this;
}

public Builder resourceConfig(ResourceConfig resourceConfig) {
config.resourceConfig = resourceConfig;
return this;
}

public Builder resourceDir(File resourceDir) {
config.resourceDir = resourceDir;
return this;
}

public BuildServiceConfig build() {
return config;
}
Expand Down
Loading

0 comments on commit 6ce537c

Please sign in to comment.