Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auto-naming bug for computed names #1618

Merged
merged 1 commit into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Add skipAwait option to YAML SDKs. (https://github.com/pulumi/pulumi-kubernetes/pull/1610)
- [sdk/go] `ConfigGroup` now respects explicit provider instances when parsing YAML. (https://github.com/pulumi/pulumi-kubernetes/pull/1601)
- Fix hanging updates for deployment await logic (https://github.com/pulumi/pulumi-kubernetes/pull/1596)
- Fix auto-naming bug for computed names (https://github.com/pulumi/pulumi-kubernetes/pull/1618)

## 3.3.1 (June 8, 2021)

Expand Down
13 changes: 11 additions & 2 deletions provider/pkg/metadata/naming.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016-2019, Pulumi Corporation.
// Copyright 2016-2021, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@ import (
"math/rand"
"time"

"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -28,8 +29,16 @@ var dns1123Alphabet = []rune("abcdefghijklmnopqrstuvwxyz0123456789")

// AssignNameIfAutonamable generates a name for an object. Uses DNS-1123-compliant characters.
// All auto-named resources get the annotation `pulumi.com/autonamed` for tooling purposes.
func AssignNameIfAutonamable(obj *unstructured.Unstructured, base tokens.QName) {
func AssignNameIfAutonamable(obj *unstructured.Unstructured, propMap resource.PropertyMap, base tokens.QName) {
contract.Assert(base != "")

// Check if the .metadata.name is set and is a computed value. If so, do not auto-name.
if md, ok := propMap["metadata"].V.(resource.PropertyMap); ok {
if name, ok := md["name"]; ok && name.IsComputed() {
return
}
}

if obj.GetName() == "" {
obj.SetName(fmt.Sprintf("%s-%s", base, randString(8)))
SetAnnotationTrue(obj, AnnotationAutonamed)
Expand Down
27 changes: 24 additions & 3 deletions provider/pkg/metadata/naming_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016-2019, Pulumi Corporation.
// Copyright 2016-2021, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,8 @@ import (
"strings"
"testing"

"github.com/pulumi/pulumi/sdk/v3/go/common/resource"

"github.com/stretchr/testify/assert"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -26,17 +28,36 @@ import (
func TestAssignNameIfAutonamable(t *testing.T) {
// o1 has no name, so autonaming succeeds.
o1 := &unstructured.Unstructured{}
AssignNameIfAutonamable(o1, "foo")
pm1 := resource.NewPropertyMap(struct{}{})
AssignNameIfAutonamable(o1, pm1, "foo")
assert.True(t, IsAutonamed(o1))
assert.True(t, strings.HasPrefix(o1.GetName(), "foo-"))

// o2 has a name, so autonaming fails.
o2 := &unstructured.Unstructured{
Object: map[string]interface{}{"metadata": map[string]interface{}{"name": "bar"}},
}
AssignNameIfAutonamable(o2, "foo")
pm2 := resource.PropertyMap{
"metadata": resource.NewObjectProperty(resource.PropertyMap{
"name": resource.NewStringProperty("bar"),
}),
}
AssignNameIfAutonamable(o2, pm2, "foo")
assert.False(t, IsAutonamed(o2))
assert.Equal(t, "bar", o2.GetName())

// o3 has a computed name, so autonaming fails.
o3 := &unstructured.Unstructured{
Object: map[string]interface{}{"metadata": map[string]interface{}{"name": "[Computed]"}},
}
pm3 := resource.PropertyMap{
"metadata": resource.NewObjectProperty(resource.PropertyMap{
"name": resource.MakeComputed(resource.NewStringProperty("bar")),
}),
}
AssignNameIfAutonamable(o3, pm3, "foo")
assert.False(t, IsAutonamed(o3))
assert.Equal(t, "[Computed]", o3.GetName())
}

func TestAdoptName(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ func (k *kubeProvider) Check(ctx context.Context, req *pulumirpc.CheckRequest) (
}
}
} else {
metadata.AssignNameIfAutonamable(newInputs, urn.Name())
metadata.AssignNameIfAutonamable(newInputs, news, urn.Name())

// Set a "managed-by: pulumi" label on all created k8s resources.
_, err = metadata.TrySetManagedByLabel(newInputs)
Expand Down