Skip to content

Commit 8bb04da

Browse files
Merge pull request #978 from tejasparikh/support-storageclasses-869
2 parents 78b7d24 + 7ca1129 commit 8bb04da

File tree

12 files changed

+510
-0
lines changed

12 files changed

+510
-0
lines changed

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/DefaultKubernetesClient.java

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import io.fabric8.kubernetes.client.dsl.internal.SecurityContextConstraintsOperationsImpl;
5555
import io.fabric8.kubernetes.client.dsl.internal.ServiceAccountOperationsImpl;
5656
import io.fabric8.kubernetes.client.dsl.internal.ServiceOperationsImpl;
57+
import io.fabric8.kubernetes.client.dsl.internal.StorageClassOperationsImpl;
5758
import io.fabric8.kubernetes.client.utils.Serialization;
5859
import io.fabric8.openshift.api.model.DoneableSecurityContextConstraints;
5960
import io.fabric8.openshift.api.model.SecurityContextConstraints;
@@ -217,6 +218,11 @@ public MixedOperation<LimitRange, LimitRangeList, DoneableLimitRange, Resource<L
217218
return new LimitRangeOperationsImpl(httpClient, getConfiguration(), getNamespace());
218219
}
219220

221+
@Override
222+
public MixedOperation<StorageClass, StorageClassList, DoneableStorageClass, Resource<StorageClass, DoneableStorageClass>> storageClasses() {
223+
return new StorageClassOperationsImpl(httpClient, getConfiguration());
224+
}
225+
220226
@Override
221227
public NonNamespaceOperation<CustomResourceDefinition, CustomResourceDefinitionList, DoneableCustomResourceDefinition, Resource<CustomResourceDefinition, DoneableCustomResourceDefinition>> customResourceDefinitions() {
222228
return new CustomResourceDefinitionOperationsImpl(httpClient, getConfiguration());

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/KubernetesClient.java

+3
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,7 @@ public interface KubernetesClient extends Client {
103103
MixedOperation<ConfigMap, ConfigMapList, DoneableConfigMap, Resource<ConfigMap, DoneableConfigMap>> configMaps();
104104

105105
MixedOperation<LimitRange, LimitRangeList, DoneableLimitRange, Resource<LimitRange, DoneableLimitRange>> limitRanges();
106+
107+
MixedOperation<StorageClass, StorageClassList, DoneableStorageClass, Resource<StorageClass, DoneableStorageClass>> storageClasses();
108+
106109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.kubernetes.client.dsl.internal;
17+
18+
import io.fabric8.kubernetes.api.model.DoneableStorageClass;
19+
import io.fabric8.kubernetes.api.model.StorageClass;
20+
import io.fabric8.kubernetes.api.model.StorageClassList;
21+
import io.fabric8.kubernetes.client.Config;
22+
import io.fabric8.kubernetes.client.dsl.Resource;
23+
import io.fabric8.kubernetes.client.dsl.base.HasMetadataOperation;
24+
import okhttp3.OkHttpClient;
25+
26+
import java.util.Map;
27+
import java.util.TreeMap;
28+
29+
public class StorageClassOperationsImpl extends HasMetadataOperation<StorageClass, StorageClassList, DoneableStorageClass, Resource<StorageClass, DoneableStorageClass>> {
30+
public StorageClassOperationsImpl(OkHttpClient client, Config config) {
31+
this(client, config, null, null, null, true, null, null, false, -1, new TreeMap<String, String>(), new TreeMap<String, String>(), new TreeMap<String, String[]>(), new TreeMap<String, String[]>(), new TreeMap<String, String>());
32+
}
33+
34+
public StorageClassOperationsImpl(OkHttpClient client, Config config, String apiVersion, String namespace, String name, Boolean cascading, StorageClass item, String resourceVersion, Boolean reloadingFromServer, long gracePeriodSeconds, Map<String, String> labels, Map<String, String> labelsNot, Map<String, String[]> labelsIn, Map<String, String[]> labelsNotIn, Map<String, String> fields) {
35+
super(client, config, "storage.k8s.io", apiVersion, "storageclasses", namespace, name, cascading, item, resourceVersion, reloadingFromServer, gracePeriodSeconds, labels, labelsNot, labelsIn, labelsNotIn, fields);
36+
}
37+
38+
@Override
39+
public boolean isResourceNamespaced() {
40+
return false;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.kubernetes.client.handlers;
17+
18+
import io.fabric8.kubernetes.api.model.StorageClass;
19+
import io.fabric8.kubernetes.api.model.StorageClassBuilder;
20+
import io.fabric8.kubernetes.client.Config;
21+
import io.fabric8.kubernetes.client.ResourceHandler;
22+
import io.fabric8.kubernetes.client.Watch;
23+
import io.fabric8.kubernetes.client.Watcher;
24+
import io.fabric8.kubernetes.client.dsl.internal.StorageClassOperationsImpl;
25+
import okhttp3.OkHttpClient;
26+
import org.apache.felix.scr.annotations.Component;
27+
import org.apache.felix.scr.annotations.Service;
28+
29+
import java.util.TreeMap;
30+
import java.util.concurrent.TimeUnit;
31+
32+
@Component
33+
@Service
34+
public class StorageClassHandler implements ResourceHandler<StorageClass, StorageClassBuilder> {
35+
@Override
36+
public String getKind() {
37+
return StorageClass.class.getSimpleName();
38+
}
39+
40+
@Override
41+
public StorageClass create(OkHttpClient client, Config config, String namespace, StorageClass item) {
42+
return new StorageClassOperationsImpl(client, config, null, namespace, null, true, item, null, false, -1, new TreeMap<String, String>(), new TreeMap<String, String>(), new TreeMap<String, String[]>(), new TreeMap<String, String[]>(), new TreeMap<String, String>()).create();
43+
}
44+
45+
@Override
46+
public StorageClass replace(OkHttpClient client, Config config, String namespace, StorageClass item) {
47+
return new StorageClassOperationsImpl(client, config, null, namespace, null, true, item, null, true, -1, new TreeMap<String, String>(), new TreeMap<String, String>(), new TreeMap<String, String[]>(), new TreeMap<String, String[]>(), new TreeMap<String, String>()).replace(item);
48+
}
49+
50+
@Override
51+
public StorageClass reload(OkHttpClient client, Config config, String namespace, StorageClass item) {
52+
return new StorageClassOperationsImpl(client, config, null, namespace, null, true, item, null, false, -1, new TreeMap<String, String>(), new TreeMap<String, String>(), new TreeMap<String, String[]>(), new TreeMap<String, String[]>(), new TreeMap<String, String>()).fromServer().get();
53+
}
54+
55+
@Override
56+
public StorageClassBuilder edit(StorageClass item) {
57+
return new StorageClassBuilder(item);
58+
}
59+
60+
@Override
61+
public Boolean delete(OkHttpClient client, Config config, String namespace, StorageClass item) {
62+
return new StorageClassOperationsImpl(client, config, null, namespace, null, true, item, null, false, -1, new TreeMap<String, String>(), new TreeMap<String, String>(), new TreeMap<String, String[]>(), new TreeMap<String, String[]>(), new TreeMap<String, String>()).delete(item);
63+
}
64+
65+
@Override
66+
public Watch watch(OkHttpClient client, Config config, String namespace, StorageClass item, Watcher<StorageClass> watcher) {
67+
return new StorageClassOperationsImpl(client, config, null, namespace, null, true, item, null, false, -1, new TreeMap<String, String>(), new TreeMap<String, String>(), new TreeMap<String, String[]>(), new TreeMap<String, String[]>(), new TreeMap<String, String>()).watch(watcher);
68+
}
69+
70+
@Override
71+
public Watch watch(OkHttpClient client, Config config, String namespace, StorageClass item, String resourceVersion, Watcher<StorageClass> watcher) {
72+
return new StorageClassOperationsImpl(client, config, null, namespace, null, true, item, null, false, -1, new TreeMap<String, String>(), new TreeMap<String, String>(), new TreeMap<String, String[]>(), new TreeMap<String, String[]>(), new TreeMap<String, String>()).watch(resourceVersion, watcher);
73+
}
74+
75+
@Override
76+
public StorageClass waitUntilReady(OkHttpClient client, Config config, String namespace, StorageClass item, long amount, TimeUnit timeUnit) throws InterruptedException {
77+
return new StorageClassOperationsImpl(client, config, null, namespace, null, true, item, null, false, -1, new TreeMap<String, String>(), new TreeMap<String, String>(), new TreeMap<String, String[]>(), new TreeMap<String, String[]>(), new TreeMap<String, String>()).waitUntilReady(amount, timeUnit);
78+
}
79+
80+
}

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/osgi/ManagedKubernetesClient.java

+4
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ public MixedOperation<Secret, SecretList, DoneableSecret, Resource<Secret, Donea
272272
return delegate.secrets();
273273
}
274274

275+
public MixedOperation<StorageClass, StorageClassList, DoneableStorageClass, Resource<StorageClass, DoneableStorageClass>> storageClasses() {
276+
return delegate.storageClasses();
277+
}
278+
275279
@Override
276280
public RootPaths rootPaths() {
277281
return delegate.rootPaths();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.kubernetes.examples;
17+
18+
import io.fabric8.kubernetes.api.model.*;
19+
import io.fabric8.kubernetes.client.Config;
20+
import io.fabric8.kubernetes.client.*;
21+
import org.slf4j.*;
22+
23+
public class ListStorageClassExample {
24+
25+
private static final Logger logger = LoggerFactory.getLogger(ListStorageClassExample.class);
26+
27+
public static void main(String[] args) {
28+
29+
String master = "http://localhost:8080/";
30+
31+
if (args.length == 1) {
32+
master = args[0];
33+
}
34+
35+
io.fabric8.kubernetes.client.Config config = new io.fabric8.kubernetes.client.ConfigBuilder().withMasterUrl(master).build();
36+
37+
try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
38+
39+
StorageClassList storageClassList = client.storageClasses().list();
40+
41+
logger.info(storageClassList.toString());
42+
43+
} catch (KubernetesClientException e) {
44+
logger.error(e.getMessage(), e);
45+
}
46+
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Copyright (C) 2015 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.fabric8.kubernetes.examples;
17+
18+
import io.fabric8.kubernetes.api.model.ObjectMeta;
19+
import io.fabric8.kubernetes.api.model.StorageClass;
20+
import io.fabric8.kubernetes.api.model.StorageClassBuilder;
21+
import io.fabric8.kubernetes.api.model.StorageClassList;
22+
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
23+
import io.fabric8.kubernetes.client.KubernetesClient;
24+
import io.fabric8.kubernetes.client.KubernetesClientException;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
import java.util.UUID;
31+
32+
public class StorageClassExamples {
33+
34+
private static final Logger logger = LoggerFactory.getLogger(StorageClassExamples.class);
35+
36+
public static void main(String[] args) {
37+
38+
String master = "http://localhost:8080/";
39+
40+
if (args.length == 1) {
41+
master = args[0];
42+
}
43+
44+
io.fabric8.kubernetes.client.Config config = new io.fabric8.kubernetes.client.ConfigBuilder().withMasterUrl(master).build();
45+
46+
try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
47+
48+
//list all storage classes
49+
StorageClassList storageClassList = client.storageClasses().list();
50+
logger.info("List of storage classes: {}", storageClassList.toString());
51+
52+
//create new storage class
53+
54+
String name = UUID.randomUUID().toString();
55+
ObjectMeta metadata = new ObjectMeta();
56+
metadata.setName(name);
57+
58+
Map<String, String> parameters = new HashMap<>();
59+
parameters.put("resturl", "http://192.168.10.100:8080");
60+
parameters.put("restuser", "");
61+
parameters.put("secretNamespace", "");
62+
parameters.put("secretName", "");
63+
parameters.put("key", "value1");
64+
65+
StorageClass storageClass = new StorageClassBuilder().withApiVersion("storage.k8s.io/v1")
66+
.withKind("StorageClass")
67+
.withMetadata(metadata)
68+
.withParameters(parameters)
69+
.withProvisioner("k8s.io/minikube-hostpath")
70+
.build();
71+
72+
storageClass = client.storageClasses().create(storageClass);
73+
logger.info("Newly created storage class details: {}", storageClass.toString());
74+
75+
//list all storage classes
76+
storageClassList = client.storageClasses().list();
77+
logger.info("List of storage classes: {}", storageClassList.toString());
78+
79+
//update storage class. add label
80+
storageClass = client.storageClasses().withName(name).edit().editMetadata().addToLabels("testLabel", "testLabelValue").endMetadata().done();
81+
82+
//list all storage classes
83+
storageClassList = client.storageClasses().list();
84+
logger.info("List of storage classes: {}", storageClassList.toString());
85+
86+
87+
//delete storage class
88+
boolean isDeleteSuccessful = client.storageClasses().delete(storageClass);
89+
logger.info("Storage Class resource successfully deleted: {}", isDeleteSuccessful);
90+
91+
92+
} catch (KubernetesClientException e) {
93+
logger.error(e.getMessage(), e);
94+
}
95+
96+
}
97+
}

0 commit comments

Comments
 (0)