001/* 002 * VM-Operator 003 * Copyright (C) 2024 Michael N. Lipp 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <https://www.gnu.org/licenses/>. 017 */ 018 019package org.jdrupes.vmoperator.common; 020 021import io.kubernetes.client.Discovery.APIResource; 022import io.kubernetes.client.apimachinery.GroupVersionKind; 023import io.kubernetes.client.openapi.ApiException; 024import io.kubernetes.client.util.generic.options.ListOptions; 025import java.io.Reader; 026import java.util.Collection; 027 028/** 029 * A stub for namespaced custom objects. It uses a dynamic model 030 * (see {@link K8sDynamicModel}) for representing the object's 031 * state and can therefore be used for any kind of object, especially 032 * custom objects. 033 */ 034public class K8sDynamicStub 035 extends K8sDynamicStubBase<K8sDynamicModel, K8sDynamicModels> { 036 037 private static DynamicTypeAdapterFactory<K8sDynamicModel, 038 K8sDynamicModels> taf = new K8sDynamicModelTypeAdapterFactory(); 039 040 /** 041 * Instantiates a new dynamic stub. 042 * 043 * @param client the client 044 * @param context the context 045 * @param namespace the namespace 046 * @param name the name 047 */ 048 public K8sDynamicStub(K8sClient client, 049 APIResource context, String namespace, String name) { 050 super(K8sDynamicModel.class, K8sDynamicModels.class, taf, client, 051 context, namespace, name); 052 } 053 054 /** 055 * Get a dynamic object stub. If the version in parameter 056 * `gvk` is an empty string, the stub refers to the first object with 057 * matching group and kind. 058 * 059 * @param client the client 060 * @param gvk the group, version and kind 061 * @param namespace the namespace 062 * @param name the name 063 * @return the stub if the object exists 064 * @throws ApiException the api exception 065 */ 066 public static K8sDynamicStub get(K8sClient client, 067 GroupVersionKind gvk, String namespace, String name) 068 throws ApiException { 069 return new K8sDynamicStub(client, apiResource(client, gvk), namespace, 070 name); 071 } 072 073 /** 074 * Get a dynamic object stub. 075 * 076 * @param client the client 077 * @param context the context 078 * @param namespace the namespace 079 * @param name the name 080 * @return the stub if the object exists 081 * @throws ApiException the api exception 082 */ 083 public static K8sDynamicStub get(K8sClient client, 084 APIResource context, String namespace, String name) { 085 return new K8sDynamicStub(client, context, namespace, name); 086 } 087 088 /** 089 * Creates a stub from yaml. 090 * 091 * @param client the client 092 * @param context the context 093 * @param yaml the yaml 094 * @return the k 8 s dynamic stub 095 * @throws ApiException the api exception 096 */ 097 public static K8sDynamicStub createFromYaml(K8sClient client, 098 APIResource context, Reader yaml) throws ApiException { 099 var model = new K8sDynamicModel(client.getJSON().getGson(), 100 K8s.yamlToJson(client, yaml)); 101 return K8sGenericStub.create(K8sDynamicModel.class, 102 K8sDynamicModels.class, client, context, model, 103 (c, ns, n) -> new K8sDynamicStub(c, context, ns, n)); 104 } 105 106 /** 107 * Get the stubs for the objects in the given namespace that match 108 * the criteria from the given options. 109 * 110 * @param client the client 111 * @param namespace the namespace 112 * @param options the options 113 * @return the collection 114 * @throws ApiException the api exception 115 */ 116 public static Collection<K8sDynamicStub> list(K8sClient client, 117 APIResource context, String namespace, ListOptions options) 118 throws ApiException { 119 return K8sGenericStub.list(K8sDynamicModel.class, 120 K8sDynamicModels.class, client, context, namespace, options, 121 (c, ns, n) -> new K8sDynamicStub(c, context, ns, n)); 122 } 123 124 /** 125 * Get the stubs for the objects in the given namespace. 126 * 127 * @param client the client 128 * @param namespace the namespace 129 * @return the collection 130 * @throws ApiException the api exception 131 */ 132 public static Collection<K8sDynamicStub> list(K8sClient client, 133 APIResource context, String namespace) 134 throws ApiException { 135 return list(client, context, namespace, new ListOptions()); 136 } 137 138 /** 139 * A factory for creating K8sDynamicModel(s) objects. 140 */ 141 public static class K8sDynamicModelTypeAdapterFactory extends 142 DynamicTypeAdapterFactory<K8sDynamicModel, K8sDynamicModels> { 143 144 /** 145 * Instantiates a new dynamic model type adapter factory. 146 */ 147 public K8sDynamicModelTypeAdapterFactory() { 148 super(K8sDynamicModel.class, K8sDynamicModels.class); 149 } 150 } 151 152}