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 empty labels handling in Cluster resource #2441

Merged
merged 6 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion provider/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func fixEmptyLabels(_ context.Context, req shimv2.PlanStateEditRequest) (cty.Val
// effective_labels can include labels read from the cloud provider.
programLabels := property.Map{}

labelsPropertyName := "labels"
if req.TfToken == "google_container_cluster" {
labelsPropertyName = "resourceLabels"
}

// Apply default labels first.
if pConfig := resource.FromResourcePropertyValue(resource.NewProperty(req.ProviderConfig)); pConfig.IsMap() {
l := pConfig.AsMap()["defaultLabels"]
Expand All @@ -33,7 +38,7 @@ func fixEmptyLabels(_ context.Context, req shimv2.PlanStateEditRequest) (cty.Val
}

// Apply labels next, allowing labels to override defaultLabels.
if inputs, ok := (resource.PropertyPath{"labels"}.Get(resource.NewProperty(req.NewInputs))); ok {
if inputs, ok := (resource.PropertyPath{labelsPropertyName}.Get(resource.NewProperty(req.NewInputs))); ok {
if labels := resource.FromResourcePropertyValue(inputs); labels.IsMap() {
for k, v := range labels.AsMap() {
programLabels[k] = v
Expand Down
45 changes: 45 additions & 0 deletions provider/provider_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,51 @@ Resources:
Secret: true,
},
})},
{
// Cluster overloads the labels field and instead uses resourceLabels for GCP labels.
"empty-label-cluster",
autogold.Expect(`Previewing update (test):

+ pulumi:pulumi:Stack empty-label-cluster-test create
+ random:index:RandomString random-account-id create
+ gcp:container:Cluster primary create
+ gcp:serviceaccount:Account serviceAccount create
+ pulumi:pulumi:Stack empty-label-cluster-test create
Outputs:
effectiveLabels: [secret]
labels : {
environment: "dev"
test : ""
}
pulumiLabels : [secret]

Resources:
+ 4 to create

`),
autogold.Expect(auto.OutputMap{
"effectiveLabels": auto.OutputValue{
Value: map[string]interface{}{
"environment": "dev",
"goog-pulumi-provisioned": "true",
"test": "",
},
Secret: true,
},
"labels": auto.OutputValue{Value: map[string]interface{}{
"environment": "dev",
"test": "",
}},
"pulumiLabels": auto.OutputValue{
Value: map[string]interface{}{
"environment": "dev",
"goog-pulumi-provisioned": "true",
"test": "",
},
Secret: true,
},
}),
},
}

for _, tt := range tests {
Expand Down
31 changes: 31 additions & 0 deletions provider/test-programs/empty-label-cluster/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: empty-label-cluster
runtime: yaml
resources:
random-account-id:
type: random:RandomString
properties:
length: 10
special: false
upper: false
number: false
serviceAccount:
type: gcp:serviceaccount:Account
properties:
accountId: ${random-account-id.result}
primary:
type: gcp:container:Cluster
properties:
location: us-central1
# We can't create a cluster with no node pool defined, but we want to only use
# separately managed node pools. So we create the smallest possible default
# node pool and immediately delete it.
removeDefaultNodePool: true
initialNodeCount: 1
deletionProtection: false
resourceLabels:
environment: "dev"
test: ""
outputs:
labels: ${primary.resourceLabels}
effectiveLabels: ${primary.effectiveLabels}
pulumiLabels: ${primary.pulumiLabels}
Loading