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 panics during preview when metadata is a computed value #572

Merged
merged 2 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 0.23.2 (Unreleased)

### Improvements

- Fix an issue where the provider would panic during preview if and object's
`metadata` property ended up being computed from one or more unknown
values. (Fixes [#559](https://github.com/pulumi/pulumi-kubernetes/issues/559)).

## 0.23.1 (May 10, 2019)

### Supported Kubernetes versions
Expand Down
14 changes: 14 additions & 0 deletions pkg/metadata/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package metadata
import (
"strings"

"github.com/pulumi/pulumi/pkg/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

Expand Down Expand Up @@ -48,9 +49,17 @@ func IsInternalAnnotation(key string) bool {
// SetAnnotation sets the specified key, value annotation on the provided Unstructured object.
func SetAnnotation(obj *unstructured.Unstructured, key, value string) {
// Note: Cannot use obj.GetAnnotations() here because it doesn't properly handle computed values from preview.
// during preview, our strategy for if metdata or annotations end up being computed values, is to just not
// apply an annotiation (since there's no way to insert data into the computed object)
metadataRaw := obj.Object["metadata"]
if isComputedValue(metadataRaw) {
return
}
metadata := metadataRaw.(map[string]interface{})
annotationsRaw, ok := metadata["annotations"]
if isComputedValue(annotationsRaw) {
return
}
var annotations map[string]interface{}
if !ok {
annotations = make(map[string]interface{})
Expand Down Expand Up @@ -79,3 +88,8 @@ func GetAnnotationValue(obj *unstructured.Unstructured, key string) string {
annotations := obj.GetAnnotations()
return annotations[key]
}

func isComputedValue(v interface{}) bool {
_, isComputed := v.(resource.Computed)
return isComputed
}
45 changes: 45 additions & 0 deletions pkg/metadata/annotations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2016-2019, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metadata

import (
"testing"

"github.com/pulumi/pulumi/pkg/resource"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func TestSetAnnotationMetadataComputed(t *testing.T) {
obj := &unstructured.Unstructured{Object: map[string]interface{}{
"metadata": resource.Computed{Element: resource.NewObjectProperty(nil)},
}}

// Since metadata is a computed property, we can't really set an annotation of the object, but we should not fail
// as the metadata property of an object could be computed during previews.
SetAnnotation(obj, "foo", "bar")
}

func TestSetAnnotationMetadataAnnotationsComputed(t *testing.T) {
obj := &unstructured.Unstructured{Object: map[string]interface{}{
"metadata": map[string]interface{}{
"annotations": resource.Computed{Element: resource.NewObjectProperty(nil)},
},
}}

// Since metadata is a computed property, we can't really set an annotation of the object, but we should not fail
// as the metadata property of an object could be computed during previews.
SetAnnotation(obj, "foo", "bar")
}
8 changes: 8 additions & 0 deletions pkg/metadata/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ import (
// SetLabel sets the specified key/value pair as a label on the provided Unstructured object.
func SetLabel(obj *unstructured.Unstructured, key, value string) {
// Note: Cannot use obj.GetLabels() here because it doesn't properly handle computed values from preview.
// during preview, our strategy for if metdata or annotations end up being computed values, is to just not
// apply an annotiation (since there's no way to insert data into the computed object)
metadataRaw := obj.Object["metadata"]
if isComputedValue(metadataRaw) {
return
}
metadata := metadataRaw.(map[string]interface{})
labelsRaw, ok := metadata["labels"]
if isComputedValue(labelsRaw) {
return
}
var labels map[string]interface{}
if !ok {
labels = make(map[string]interface{})
Expand Down
45 changes: 45 additions & 0 deletions pkg/metadata/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2016-2019, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metadata

import (
"testing"

"github.com/pulumi/pulumi/pkg/resource"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

func TestSetLabelMetadataComputed(t *testing.T) {
obj := &unstructured.Unstructured{Object: map[string]interface{}{
"metadata": resource.Computed{Element: resource.NewObjectProperty(nil)},
}}

// Since metadata is a computed property, we can't really set an annotation of the object, but we should not fail
// as the metadata property of an object could be computed during previews.
SetLabel(obj, "foo", "bar")
}

func TestSetLabelMetadataLabelsComputed(t *testing.T) {
obj := &unstructured.Unstructured{Object: map[string]interface{}{
"metadata": map[string]interface{}{
"labels": resource.Computed{Element: resource.NewObjectProperty(nil)},
},
}}

// Since metadata is a computed property, we can't really set an annotation of the object, but we should not fail
// as the metadata property of an object could be computed during previews.
SetLabel(obj, "foo", "bar")
}