Skip to content

Commit

Permalink
fix: Moved KubernetesClient implementation of kubectl run to separa…
Browse files Browse the repository at this point in the history
…te package
  • Loading branch information
manusa committed Aug 25, 2020
1 parent b8c9f53 commit 125e9b0
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 137 deletions.
2 changes: 1 addition & 1 deletion doc/CHEATSHEET.md
Expand Up @@ -2304,7 +2304,7 @@ try (KubernetesClient client = new DefaultKubernetesClient()) {
```
try (KubernetesClient client = new DefaultKubernetesClient()) {
client.run().inNamespace("default")
.withRunConfig(new GeneratorRunConfigBuilder()
.withRunConfig(new RunConfigBuilder()
.withName("nginx")
.withImage("nginx:latest")
.withLabels(Collections.singletonMap("foo", "bar"))
Expand Down
Expand Up @@ -64,7 +64,7 @@
import io.fabric8.kubernetes.api.model.ServiceAccount;
import io.fabric8.kubernetes.api.model.ServiceAccountList;
import io.fabric8.kubernetes.api.model.ServiceList;
import io.fabric8.kubernetes.client.utils.PodGeneratorImpl;
import io.fabric8.kubernetes.client.extended.run.RunOperations;
import okhttp3.OkHttpClient;

import java.io.InputStream;
Expand Down Expand Up @@ -263,7 +263,7 @@ public MixedOperation<LimitRange, LimitRangeList, DoneableLimitRange, Resource<L
}

@Override
public PodGeneratorImpl run() {
public RunOperations run() {
return delegate.run();
}

Expand Down
Expand Up @@ -112,8 +112,8 @@
import io.fabric8.kubernetes.client.dsl.internal.core.v1.ServiceAccountOperationsImpl;
import io.fabric8.kubernetes.client.dsl.internal.core.v1.EventOperationsImpl;
import io.fabric8.kubernetes.client.extended.leaderelection.LeaderElectorBuilder;
import io.fabric8.kubernetes.client.utils.GeneratorRunConfigBuilder;
import io.fabric8.kubernetes.client.utils.PodGeneratorImpl;
import io.fabric8.kubernetes.client.extended.run.RunConfigBuilder;
import io.fabric8.kubernetes.client.extended.run.RunOperations;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.kubernetes.client.informers.SharedInformerFactory;
import io.fabric8.kubernetes.client.utils.Utils;
Expand Down Expand Up @@ -443,7 +443,7 @@ public MixedOperation<Lease, LeaseList, DoneableLease, Resource<Lease, DoneableL
}

@Override
public PodGeneratorImpl run() {
return new PodGeneratorImpl(httpClient, getConfiguration(), getNamespace(), new GeneratorRunConfigBuilder());
public RunOperations run() {
return new RunOperations(httpClient, getConfiguration(), getNamespace(), new RunConfigBuilder());
}
}
Expand Up @@ -84,8 +84,8 @@
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.kubernetes.client.dsl.internal.RawCustomResourceOperationsImpl;
import io.fabric8.kubernetes.client.extended.leaderelection.LeaderElectorBuilder;
import io.fabric8.kubernetes.client.extended.run.RunOperations;
import io.fabric8.kubernetes.client.informers.SharedInformerFactory;
import io.fabric8.kubernetes.client.utils.PodGeneratorImpl;

import java.io.InputStream;
import java.util.Collection;
Expand Down Expand Up @@ -519,7 +519,7 @@ public interface KubernetesClient extends Client {
/**
* Run a Pod (core/v1)
*
* @return returns {@link PodGeneratorImpl} that allows you to run a pod based on few parameters(e.g. name, image etc)
* @return returns {@link RunOperations} that allows you to run a pod based on few parameters(e.g. name, image etc)
*/
PodGeneratorImpl run();
RunOperations run();
}
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.utils;
package io.fabric8.kubernetes.client.extended.run;

import io.fabric8.kubernetes.api.model.Quantity;
import io.sundr.builder.annotations.Buildable;
Expand All @@ -23,7 +23,7 @@
/**
* Configuration for client run
*/
public class GeneratorRunConfig {
public class RunConfig {
/**
* Name of resource
*/
Expand Down Expand Up @@ -70,7 +70,7 @@ public class GeneratorRunConfig {
private int port;

@Buildable(builderPackage = "io.fabric8.kubernetes.api.builder")
public GeneratorRunConfig(String name, String image, String imagePullPolicy, String restartPolicy, String serviceAccount, Map<String, String> labels, Map<String, String> env, Map<String, Quantity> limits, Map<String, Quantity> requests, int replicas, int port) {
public RunConfig(String name, String image, String imagePullPolicy, String restartPolicy, String serviceAccount, Map<String, String> labels, Map<String, String> env, Map<String, Quantity> limits, Map<String, Quantity> requests, int replicas, int port) { // NOSONAR
this.name = name;
this.image = image;
this.imagePullPolicy = imagePullPolicy;
Expand Down
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.utils;
package io.fabric8.kubernetes.client.extended.run;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerBuilder;
Expand All @@ -22,12 +22,14 @@
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.api.model.PodSpec;
import io.fabric8.kubernetes.api.model.PodSpecBuilder;
import io.fabric8.kubernetes.client.extended.run.RunConfig;
import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil;

public class GeneratorRunConfigUtil {
public class RunConfigUtil {
private static final String DEFAULT_RESTART_POLICY = "Always";
private GeneratorRunConfigUtil() { }
private RunConfigUtil() { }

public static ObjectMeta getObjectMetadataFromRunConfig(GeneratorRunConfig generatorRunConfig) {
public static ObjectMeta getObjectMetadataFromRunConfig(RunConfig generatorRunConfig) {
ObjectMetaBuilder objectMetaBuilder = new ObjectMetaBuilder();
if (generatorRunConfig.getName() != null) {
objectMetaBuilder.withName(generatorRunConfig.getName());
Expand All @@ -41,7 +43,7 @@ public static ObjectMeta getObjectMetadataFromRunConfig(GeneratorRunConfig gener
return objectMetaBuilder.build();
}

public static PodSpec getPodSpecFromRunConfig(GeneratorRunConfig generatorRunConfig) {
public static PodSpec getPodSpecFromRunConfig(RunConfig generatorRunConfig) {
PodSpecBuilder podSpecBuilder = new PodSpecBuilder();
if (generatorRunConfig.getRestartPolicy() != null) {
podSpecBuilder.withRestartPolicy(generatorRunConfig.getRestartPolicy());
Expand All @@ -58,7 +60,7 @@ public static PodSpec getPodSpecFromRunConfig(GeneratorRunConfig generatorRunCon
return podSpecBuilder.build();
}

public static Container getContainersFromRunConfig(GeneratorRunConfig generatorRunConfig) {
public static Container getContainersFromRunConfig(RunConfig generatorRunConfig) {
ContainerBuilder containerBuilder = new ContainerBuilder();
if (generatorRunConfig.getName() != null) {
containerBuilder.withName(generatorRunConfig.getName());
Expand Down
@@ -0,0 +1,93 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed 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.kubernetes.client.extended.run;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl;
import okhttp3.OkHttpClient;

public class RunOperations {
private final OkHttpClient client;
private final Config config;
private final String namespace;
private final RunConfigBuilder runConfigBuilder;

public RunOperations(OkHttpClient client, Config config, String namespace, RunConfigBuilder runConfigBuilder) {
this.client = client;
this.config = config;
this.namespace = namespace;
this.runConfigBuilder = runConfigBuilder;
}

/**
* Specify namespace for the operation
*
* @param namespace namespace in which resource needs to be created
* @return {@link RunOperations} with injected namespace
*/
public RunOperations inNamespace(String namespace) {
return new RunOperations(client, config, namespace, runConfigBuilder);
}

/**
* Specify image for the Pod
*
* @param image image as a string
* @return {@link RunOperations} with image injected into {@link RunConfig}
*/
public RunOperations withImage(String image) {
return new RunOperations(client, config, namespace, runConfigBuilder.withImage(image));
}

/**
* Specify name for the Pod
*
* @param name name of the pod to be created
* @return {@link RunOperations} with name injected into {@link RunConfig}
*/
public RunOperations withName(String name) {
return new RunOperations(client, config, namespace, runConfigBuilder.withName(name));
}

/**
* Specify complex configuration for Pod creating using {@link RunConfig}
*
* @param generatorRunConfig {@link RunConfig} which allows to provide configuring environment variables, labels, resources, ports etc
* @return {@link RunOperations} with specified configuration
*/
public RunOperations withRunConfig(RunConfig generatorRunConfig) {
return new RunOperations(client, config, namespace, new RunConfigBuilder(generatorRunConfig));
}

/**
* Apply the {@link RunConfig} onto the cluster and create Pod
*
* @return Pod which got created from the operation
*/
public Pod done() {
return new PodOperationsImpl(client, config, namespace).create(convertRunConfigIntoPod());
}

Pod convertRunConfigIntoPod() {
RunConfig finalGeneratorConfig = runConfigBuilder.build();
return new PodBuilder()
.withMetadata(RunConfigUtil.getObjectMetadataFromRunConfig(finalGeneratorConfig))
.withSpec(RunConfigUtil.getPodSpecFromRunConfig(finalGeneratorConfig))
.build();
}
}
Expand Up @@ -123,9 +123,9 @@
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.kubernetes.client.dsl.internal.RawCustomResourceOperationsImpl;
import io.fabric8.kubernetes.client.extended.leaderelection.LeaderElectorBuilder;
import io.fabric8.kubernetes.client.extended.run.RunConfigBuilder;
import io.fabric8.kubernetes.client.extended.run.RunOperations;
import io.fabric8.kubernetes.client.informers.SharedInformerFactory;
import io.fabric8.kubernetes.client.utils.GeneratorRunConfigBuilder;
import io.fabric8.kubernetes.client.utils.PodGeneratorImpl;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
Expand Down Expand Up @@ -561,7 +561,7 @@ public FunctionCallable<NamespacedKubernetesClient> withRequestConfig(RequestCon
}

@Override
public PodGeneratorImpl run() {
return new PodGeneratorImpl(httpClient, getConfiguration(), getNamespace(), new GeneratorRunConfigBuilder());
public RunOperations run() {
return new RunOperations(httpClient, getConfiguration(), getNamespace(), new RunConfigBuilder());
}
}

This file was deleted.

0 comments on commit 125e9b0

Please sign in to comment.