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 buggy behavior with annotations/labels on preview #461

Merged
merged 1 commit into from
Mar 1, 2019
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

### Bug fixes

- None
- Properly handle computed values in labels and annotations (https://github.com/pulumi/pulumi-kubernetes/pull/461)

## 0.20.3 (February 20, 2019)

Expand Down
15 changes: 11 additions & 4 deletions pkg/metadata/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,19 @@ func IsInternalAnnotation(key string) bool {
}

func SetAnnotation(obj *unstructured.Unstructured, key, value string) {
annotations := obj.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
// Note: Cannot use obj.GetAnnotations() here because it doesn't properly handle computed values from preview.
metadataRaw := obj.Object["metadata"]
metadata := metadataRaw.(map[string]interface{})
annotationsRaw, ok := metadata["annotations"]
var annotations map[string]interface{}
if !ok {
annotations = make(map[string]interface{})
} else {
annotations = annotationsRaw.(map[string]interface{})
}
annotations[key] = value
obj.SetAnnotations(annotations)

metadata["annotations"] = annotations
}

func SetAnnotationTrue(obj *unstructured.Unstructured, key string) {
Expand Down
17 changes: 13 additions & 4 deletions pkg/metadata/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,24 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// SetLabel sets the specified key/value pair as a label on the provided Unstructured object.
func SetLabel(obj *unstructured.Unstructured, key, value string) {
labels := obj.GetLabels()
if labels == nil {
labels = map[string]string{}
// Note: Cannot use obj.GetLabels() here because it doesn't properly handle computed values from preview.
metadataRaw := obj.Object["metadata"]
metadata := metadataRaw.(map[string]interface{})
labelsRaw, ok := metadata["labels"]
var labels map[string]interface{}
if !ok {
labels = make(map[string]interface{})
} else {
labels = labelsRaw.(map[string]interface{})
}
labels[key] = value
obj.SetLabels(labels)

metadata["labels"] = labels
}

// SetManagedByLabel sets the `app.kubernetes.io/managed-by` label to `pulumi`.
func SetManagedByLabel(obj *unstructured.Unstructured) {
SetLabel(obj, "app.kubernetes.io/managed-by", "pulumi")
}
4 changes: 2 additions & 2 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"github.com/golang/glog"
pbempty "github.com/golang/protobuf/ptypes/empty"
"github.com/golang/protobuf/ptypes/struct"
"github.com/pulumi/pulumi-kubernetes/pkg/metadata"
"github.com/pulumi/pulumi-kubernetes/pkg/await"
"github.com/pulumi/pulumi-kubernetes/pkg/clients"
"github.com/pulumi/pulumi-kubernetes/pkg/metadata"
"github.com/pulumi/pulumi-kubernetes/pkg/openapi"
"github.com/pulumi/pulumi/pkg/resource"
"github.com/pulumi/pulumi/pkg/resource/plugin"
Expand Down Expand Up @@ -338,7 +338,7 @@ func (k *kubeProvider) Diff(
}
oldInputs, _ := parseCheckpointObject(oldState)

// Get new resouce inputs. The user is submitting these as an update.
// Get new resource inputs. The user is submitting these as an update.
newResInputs, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.news", label), KeepUnknowns: true, SkipNulls: true,
})
Expand Down