Skip to content

Commit

Permalink
Simplified resourcePrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
EronWright committed Mar 7, 2024
1 parent d59b800 commit fa95a4e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 10 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
A new MLC-based implementation of `ConfigGroup` is now available in the "yaml/v2" package. This resource is
usable in all Pulumi languages, including Pulumi YAML and in the Java Pulumi SDK.

Notable differences from the "v1" implementations:
- resource prefixes are applied to the Kubernetes object name and to the corresponding resource name.
Notable changes in "v2":
- transformations aren't supported in this release (see https://github.com/pulumi/pulumi/issues/12996).

## 4.9.0 (March 4, 2024)
Expand Down
2 changes: 1 addition & 1 deletion provider/pkg/gen/overlays.go
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ var yamlConfigGroupV2Resource = pschema.ResourceSpec{
TypeSpec: pschema.TypeSpec{
Type: "string",
},
Description: "An optional prefix for the auto-generated resource names. Example: A resource created with resourcePrefix=\"foo\" would produce a resource named \"foo-resourceName\".",
Description: "A prefix for the auto-generated resource names. Defaults to the name of the ConfigGroup. Example: A resource created with resourcePrefix=\"foo\" would produce a resource named \"foo-resourceName\".",
},
"skipAwait": {
TypeSpec: pschema.TypeSpec{
Expand Down
8 changes: 7 additions & 1 deletion provider/pkg/provider/yaml/v2/configgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,15 @@ func (k *ConfigGroupProvider) Construct(ctx *pulumi.Context, typ, name string, i
files, _ := args[0].([]string)
yaml, _ := args[1].([]string)
objects, _ := args[2].([]map[string]any)
resourcePrefix, _ := args[3].(string)
resourcePrefix, hasResourcePrefix := args[3].(string)
skipAwait, _ := args[4].(bool)

if !hasResourcePrefix {
// use the name of the ConfigGroup as the resource prefix to ensure uniqueness
// across multiple instances of the component resource.
resourcePrefix = name
}

objs := make([]unstructured.Unstructured, len(objects))
for idx, obj := range objects {
objs[idx] = unstructured.Unstructured{Object: obj}
Expand Down
38 changes: 36 additions & 2 deletions provider/pkg/provider/yaml/v2/configgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,45 @@ var _ = Describe("Construct", func() {
outputs := unmarshalProperties(GinkgoTB(), resp.State)
Expect(outputs).To(MatchProps(IgnoreExtras, Props{
"resources": MatchArrayValue(HaveExactElements(
MatchResourceReferenceValue("urn:pulumi:stack::project::kubernetes:yaml/v2:ConfigGroup$kubernetes:core/v1:ConfigMap::my-map", "my-map"),
MatchResourceReferenceValue("urn:pulumi:stack::project::kubernetes:yaml/v2:ConfigGroup$kubernetes:stable.example.com/v1:CronTab::my-new-cron-object", "my-new-cron-object"),
MatchResourceReferenceValue("urn:pulumi:stack::project::kubernetes:yaml/v2:ConfigGroup$kubernetes:core/v1:ConfigMap::test-my-map", "test-my-map"),
MatchResourceReferenceValue("urn:pulumi:stack::project::kubernetes:yaml/v2:ConfigGroup$kubernetes:stable.example.com/v1:CronTab::test-my-new-cron-object", "test-my-new-cron-object"),
)),
}))
})

Context("given a resource prefix", func() {
BeforeEach(func() {
inputs["resourcePrefix"] = resource.NewStringProperty("prefixed")
})
It("should use the prefix (instead of the component name)", func(ctx context.Context) {
resp, err := pulumiprovider.Construct(ctx, req, tc.EngineConn(), k.Construct)
Expect(err).ShouldNot(HaveOccurred())
outputs := unmarshalProperties(GinkgoTB(), resp.State)
Expect(outputs).To(MatchProps(IgnoreExtras, Props{
"resources": MatchArrayValue(HaveExactElements(
MatchResourceReferenceValue("urn:pulumi:stack::project::kubernetes:yaml/v2:ConfigGroup$kubernetes:core/v1:ConfigMap::prefixed-my-map", "prefixed-my-map"),
MatchResourceReferenceValue("urn:pulumi:stack::project::kubernetes:yaml/v2:ConfigGroup$kubernetes:stable.example.com/v1:CronTab::prefixed-my-new-cron-object", "prefixed-my-new-cron-object"),
)),
}))
})
})

Context("given a blank resource prefix", func() {
BeforeEach(func() {
inputs["resourcePrefix"] = resource.NewStringProperty("")
})
It("should have no prefix", func(ctx context.Context) {
resp, err := pulumiprovider.Construct(ctx, req, tc.EngineConn(), k.Construct)
Expect(err).ShouldNot(HaveOccurred())
outputs := unmarshalProperties(GinkgoTB(), resp.State)
Expect(outputs).To(MatchProps(IgnoreExtras, Props{
"resources": MatchArrayValue(HaveExactElements(
MatchResourceReferenceValue("urn:pulumi:stack::project::kubernetes:yaml/v2:ConfigGroup$kubernetes:core/v1:ConfigMap::my-map", "my-map"),
MatchResourceReferenceValue("urn:pulumi:stack::project::kubernetes:yaml/v2:ConfigGroup$kubernetes:stable.example.com/v1:CronTab::my-new-cron-object", "my-new-cron-object"),
)),
}))
})
})
}

Describe("yamls", func() {
Expand Down
1 change: 0 additions & 1 deletion provider/pkg/provider/yaml/v2/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ func parseUnstructured(
}
if resourcePrefix != "" {
resourceName = fmt.Sprintf("%s-%s", resourcePrefix, resourceName)
obj.SetName(fmt.Sprintf("%s-%s", resourcePrefix, obj.GetName()))
}

// Apply the skipAwait annotation if necessary.
Expand Down
6 changes: 3 additions & 3 deletions provider/pkg/provider/yaml/v2/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,22 @@ var _ = Describe("ParseDecodeYamlFiles", func() {
BeforeEach(func() {
args.ResourcePrefix = "prefixed"
})
It("should apply the prefix to the resource and to the object", func(ctx context.Context) {
It("should apply the prefix to the resource (but not to the Kubernetes object)", func(ctx context.Context) {
_, err := parse(ctx)
Expect(err).ShouldNot(HaveOccurred())

Expect(tc.monitor.Resources()).To(MatchKeys(IgnoreExtras, Keys{
"urn:pulumi:stack::project::kubernetes:core/v1:ConfigMap::prefixed-my-map": MatchProps(IgnoreExtras, Props{
"state": MatchObject(IgnoreExtras, Props{
"metadata": MatchObject(IgnoreExtras, Props{
"name": MatchValue("prefixed-my-map"),
"name": MatchValue("my-map"),
}),
}),
}),
"urn:pulumi:stack::project::kubernetes:stable.example.com/v1:CronTab::prefixed-my-new-cron-object": MatchProps(IgnoreExtras, Props{
"state": MatchObject(IgnoreExtras, Props{
"metadata": MatchObject(IgnoreExtras, Props{
"name": MatchValue("prefixed-my-new-cron-object"),
"name": MatchValue("my-new-cron-object"),
}),
}),
}),
Expand Down

0 comments on commit fa95a4e

Please sign in to comment.