Skip to content

Commit 417f39d

Browse files
committed
patch resources
1 parent 4e4e91d commit 417f39d

12 files changed

+485
-539
lines changed

provider/pkg/gen/java-templates/src/main/java/com/pulumi/kubernetes/apiextensions/CustomResource.java

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,22 @@
44
package com.pulumi.kubernetes.apiextensions;
55

66
import java.lang.annotation.Annotation;
7-
import java.lang.reflect.Field;
8-
import java.lang.reflect.InvocationTargetException;
9-
import java.lang.reflect.Modifier;
10-
import java.util.ArrayList;
11-
import java.util.List;
12-
import java.util.Map;
137
import java.util.Optional;
148
import java.util.concurrent.ExecutionException;
15-
import java.util.stream.Collectors;
16-
import java.util.stream.Stream;
179

1810
import javax.annotation.Nullable;
1911

2012
import com.pulumi.core.Either;
2113
import com.pulumi.core.Output;
2214
import com.pulumi.core.annotations.Export;
23-
import com.pulumi.core.annotations.Import;
2415
import com.pulumi.core.annotations.ResourceType;
2516
import com.pulumi.core.internal.Codegen;
2617
import com.pulumi.core.internal.Internal;
2718
import com.pulumi.core.internal.OutputInternal;
28-
import com.pulumi.core.internal.annotations.ImportMetadata;
2919
import com.pulumi.kubernetes.Utilities;
3020
import com.pulumi.kubernetes.meta.v1.outputs.ObjectMeta;
3121
import com.pulumi.resources.ResourceArgs;
3222

33-
import net.bytebuddy.ByteBuddy;
34-
import net.bytebuddy.description.annotation.AnnotationDescription;
35-
import net.bytebuddy.implementation.FieldAccessor;
36-
import net.bytebuddy.implementation.Implementation;
37-
import net.bytebuddy.implementation.MethodCall;
38-
3923
/**
4024
* CustomResource represents an instance of a CustomResourceDefinition (CRD). For example, the
4125
* CoreOS Prometheus operator exposes a CRD `monitoring.coreos.com/ServiceMonitor`; to
@@ -135,16 +119,6 @@ private static String getOutputValue(OutputInternal<String> o) {
135119
}
136120
}
137121

138-
private static class Field {
139-
public Optional<?> value;
140-
public Either<Annotation,AnnotationDescription> importAnnotation;
141-
142-
public Field(Optional<?> value, Either<Annotation,AnnotationDescription> importAnnotation) {
143-
this.value = value;
144-
this.importAnnotation = importAnnotation;
145-
}
146-
}
147-
148122
private static ResourceArgs makeArgs(@Nullable CustomResourceArgsBase args) {
149123
if (args == null) {
150124
return null;
@@ -153,53 +127,7 @@ private static ResourceArgs makeArgs(@Nullable CustomResourceArgsBase args) {
153127
// optimization: if there are no "other" fields, we can just return the args as-is.
154128
return args;
155129
}
156-
157-
// collect the input properties from the annotated fields of the user-supplied args,
158-
// plus the dynamic input properties within otherFields.
159-
var importFields =
160-
ImportMetadata.of(args.getClass()).values().stream()
161-
.map(info -> new Field(info.getFieldOutput(args), Either.ofLeft(info.getAnnotation())));
162-
var otherFields =
163-
args.otherFields().orElseGet(Map::of).entrySet().stream()
164-
.map(entry -> new Field(Optional.ofNullable(entry.getValue()), Either.ofRight(
165-
AnnotationDescription.Builder.ofType(Import.class).define("name", entry.getKey()).build())));
166-
List<Field> fields =
167-
Stream.concat(importFields, otherFields)
168-
.collect(Collectors.toList());
169-
170-
try {
171-
// define a dynamic subclass of ResourceArgs with a field for each input property
172-
// and a constructor that takes the property values and assigns them to the fields.
173-
var t = new ByteBuddy().subclass(ResourceArgs.class);
174-
Implementation.Composable c = MethodCall.invoke(ResourceArgs.class.getConstructor());
175-
176-
var paramTypes = new ArrayList<Class<?>>();
177-
var paramValues = new ArrayList<Object>();
178-
179-
for (var arg : fields) {
180-
// define a field of type Output with @Import(name="foo")
181-
var fieldName = String.format("f%d", paramTypes.size());
182-
var f = t.defineField(fieldName, Output.class, Modifier.PUBLIC);
183-
t = arg.importAnnotation.either(a -> f.annotateField(a), ad -> f.annotateField(ad));
184-
185-
// bind the field to the corresponding constructor parameter
186-
c = c.andThen(FieldAccessor.ofField(fieldName).setsArgumentAt(paramTypes.size()));
187-
188-
paramTypes.add(Output.class);
189-
paramValues.add(arg.value.map(v -> v instanceof Output ? v : Output.ofNullable(v)).orElse(null));
190-
}
191-
192-
return (ResourceArgs) t
193-
.defineConstructor(Modifier.PUBLIC).withParameters(paramTypes).intercept(c)
194-
.make()
195-
.load(CustomResource.class.getClassLoader())
196-
.getLoaded().getConstructors()[0]
197-
.newInstance(paramValues.toArray());
198-
199-
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException
200-
| InvocationTargetException e) {
201-
throw new RuntimeException(e);
202-
}
130+
return Util.generateResourceArgs(args, args.otherFields().get());
203131
}
204132

205133
private static com.pulumi.resources.CustomResourceOptions makeResourceOptions(
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// *** WARNING: this file was generated by pulumi-java-gen. ***
2+
// *** Do not edit by hand unless you're certain you know what you are doing! ***
3+
4+
package com.pulumi.kubernetes.apiextensions;
5+
6+
import java.util.Optional;
7+
import java.util.concurrent.ExecutionException;
8+
9+
import javax.annotation.Nullable;
10+
11+
import com.pulumi.core.Output;
12+
import com.pulumi.core.annotations.Export;
13+
import com.pulumi.core.annotations.ResourceType;
14+
import com.pulumi.core.internal.Codegen;
15+
import com.pulumi.core.internal.Internal;
16+
import com.pulumi.core.internal.OutputInternal;
17+
import com.pulumi.kubernetes.Utilities;
18+
import com.pulumi.kubernetes.meta.v1.outputs.ObjectMetaPatch;
19+
import com.pulumi.resources.ResourceArgs;
20+
21+
/**
22+
* Patch resources are used to modify existing Kubernetes resources by using
23+
* Server-Side Apply updates. The name of the resource must be specified, but all other properties are optional. More than
24+
* one patch may be applied to the same resource, and a random FieldManager name will be used for each Patch resource.
25+
* Conflicts will result in an error by default, but can be forced using the &#34;pulumi.com/patchForce&#34; annotation. See the
26+
* [Server-Side Apply Docs](https://www.pulumi.com/registry/packages/kubernetes/how-to-guides/managing-resources-with-server-side-apply/) for
27+
* additional information about using Server-Side Apply to manage Kubernetes resources with Pulumi.
28+
*
29+
* CustomResourcePatch represents an instance of a CustomResourceDefinition (CRD). For example, the
30+
* CoreOS Prometheus operator exposes a CRD `monitoring.coreos.com/ServiceMonitor`; to
31+
* instantiate this as a Pulumi resource, one could call `new CustomResourcePatch`, passing the
32+
* `ServiceMonitor` resource definition as an argument.
33+
*/
34+
@ResourceType(type="kubernetes:apiextensions:CustomResourcePatch")
35+
public class CustomResourcePatch extends com.pulumi.resources.CustomResource {
36+
/**
37+
* APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
38+
*
39+
*/
40+
@Export(name="apiVersion", refs={String.class}, tree="[0]")
41+
private Output</* @Nullable */ String> apiVersion;
42+
43+
/**
44+
* @return APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
45+
*
46+
*/
47+
public Output<Optional<String>> apiVersion() {
48+
return Codegen.optional(this.apiVersion);
49+
}
50+
51+
/**
52+
* Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
53+
*
54+
*/
55+
@Export(name="kind", refs={String.class}, tree="[0]")
56+
private Output</* @Nullable */ String> kind;
57+
58+
/**
59+
* @return Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
60+
*
61+
*/
62+
public Output<Optional<String>> kind() {
63+
return Codegen.optional(this.kind);
64+
}
65+
/**
66+
* Standard object&#39;s metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
67+
*
68+
*/
69+
@Export(name="metadata", refs={ObjectMetaPatch.class}, tree="[0]")
70+
private Output</* @Nullable */ ObjectMetaPatch> metadata;
71+
72+
/**
73+
* @return Standard object&#39;s metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
74+
*
75+
*/
76+
public Output<Optional<ObjectMetaPatch>> metadata() {
77+
return Codegen.optional(this.metadata);
78+
}
79+
80+
/**
81+
*
82+
* @param name The _unique_ name of the resulting resource.
83+
*/
84+
public CustomResourcePatch(String name) {
85+
this(name, CustomResourcePatchArgs.Empty);
86+
}
87+
88+
/**
89+
*
90+
* @param name The _unique_ name of the resulting resource.
91+
* @param args The arguments to use to populate this resource's properties.
92+
*/
93+
public CustomResourcePatch(String name, @Nullable CustomResourcePatchArgsBase args) {
94+
this(name, args, null);
95+
}
96+
97+
/**
98+
*
99+
* @param name The _unique_ name of the resulting resource.
100+
* @param args The arguments to use to populate this resource's properties.
101+
* @param options A bag of options that control this resource's behavior.
102+
*/
103+
public CustomResourcePatch(String name, @Nullable CustomResourcePatchArgsBase args, @Nullable com.pulumi.resources.CustomResourceOptions options) {
104+
super(makeType(args), name, makeArgs(args), makeResourceOptions(options, Codegen.empty()));
105+
}
106+
107+
private static String makeType(@Nullable CustomResourcePatchArgsBase args) {
108+
String apiVersion = args.apiVersion().map(Internal::of).map(CustomResourcePatch::getOutputValue).orElse("");
109+
String kind = args.kind().map(Internal::of).map(CustomResourcePatch::getOutputValue).orElse("");
110+
return String.format("kubernetes:%s:%sPatch", apiVersion, kind);
111+
}
112+
113+
private static String getOutputValue(OutputInternal<String> o) {
114+
try {
115+
return o.getValueOrDefault("").get();
116+
} catch (InterruptedException | ExecutionException e) {
117+
throw new RuntimeException(e);
118+
}
119+
}
120+
121+
private static ResourceArgs makeArgs(@Nullable CustomResourcePatchArgsBase args) {
122+
if (args == null) {
123+
return null;
124+
}
125+
if (args.otherFields().isEmpty() || args.otherFields().get().isEmpty()) {
126+
// optimization: if there are no "other" fields, we can just use the args as-is.
127+
// Otherwise we generate a subclass of ResourceArgs that includes the "other" fields.
128+
return args;
129+
}
130+
return Util.generateResourceArgs(args, args.otherFields().get());
131+
}
132+
133+
private static com.pulumi.resources.CustomResourceOptions makeResourceOptions(
134+
@Nullable com.pulumi.resources.CustomResourceOptions options, @Nullable Output<String> id) {
135+
var defaultOptions = com.pulumi.resources.CustomResourceOptions.builder()
136+
.version(Utilities.getVersion())
137+
.build();
138+
return com.pulumi.resources.CustomResourceOptions.merge(defaultOptions, options, id);
139+
}
140+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// *** WARNING: this file was generated by pulumi-java-gen. ***
2+
// *** Do not edit by hand unless you're certain you know what you are doing! ***
3+
4+
package com.pulumi.kubernetes.apiextensions;
5+
6+
/**
7+
* The set of arguments for constructing a CustomResourcePatch resource.
8+
*
9+
* NOTE: This type is fairly loose, since other than `apiVersion` and `kind`,
10+
* there are no fields required across all CRDs. Use otherFields(...) to specify
11+
* additional fields.
12+
*/
13+
public final class CustomResourcePatchArgs extends CustomResourcePatchArgsBase {
14+
15+
public static final CustomResourcePatchArgs Empty = new CustomResourcePatchArgs();
16+
17+
public static Builder builder() {
18+
return new Builder();
19+
}
20+
21+
public static Builder builder(CustomResourcePatchArgs defaults) {
22+
return new Builder(defaults);
23+
}
24+
25+
public static final class Builder extends CustomResourcePatchArgsBase.Builder<CustomResourcePatchArgs, Builder> {
26+
public Builder() {
27+
super(new CustomResourcePatchArgs());
28+
}
29+
30+
public Builder(CustomResourcePatchArgs defaults) {
31+
super(new CustomResourcePatchArgs(), defaults);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)