Skip to content

Commit

Permalink
Cloud Run Diff Supression (#4186) (#2679)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Nov 5, 2020
1 parent 46005b5 commit 92401df
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/4186.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
run: patched `google_cloud_run_service` to suppress Google generated annotations
```
25 changes: 22 additions & 3 deletions google-beta/resource_cloud_run_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"log"
"reflect"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -34,6 +35,23 @@ func revisionNameCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, v i
return nil
}

const cloudRunGoogleProvidedAnnotation = "serving.knative.dev"

func cloudrunAnnotationDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
// Suppress diffs for the annotations provided by Google
if strings.Contains(k, cloudRunGoogleProvidedAnnotation) && new == "" {
return true
}

// Let diff be determined by annotations (above)
if strings.Contains(k, "annotations.%") {
return true
}

// For other keys, don't suppress diff.
return false
}

func resourceCloudRunService() *schema.Resource {
return &schema.Resource{
Create: resourceCloudRunServiceCreate,
Expand Down Expand Up @@ -485,9 +503,10 @@ and annotations.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"annotations": {
Type: schema.TypeMap,
Computed: true,
Optional: true,
Type: schema.TypeMap,
Computed: true,
Optional: true,
DiffSuppressFunc: cloudrunAnnotationDiffSuppress,
Description: `Annotations is a key value map stored with a resource that
may be set by external tools to store and retrieve arbitrary metadata. More
info: http://kubernetes.io/docs/user-guide/annotations`,
Expand Down
6 changes: 6 additions & 0 deletions google-beta/resource_cloud_run_service_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ resource "google_cloud_run_service" "default" {
}
}
metadata {
annotations = {
generated-by = "magic-modules"
}
}
traffic {
percent = 100
latest_revision = true
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/cloud_run_domain_mapping.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Resource to hold the state and status of a user's domain mapping.

To get more information about DomainMapping, see:

* [API documentation](https://cloud.google.com/run/docs/reference/rest/v1alpha1/projects.locations.domainmappings)
* [API documentation](https://cloud.google.com/run/docs/reference/rest/v1/projects.locations.domainmappings)
* How-to Guides
* [Official Documentation](https://cloud.google.com/run/docs/mapping-custom-domains)

Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/cloud_run_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ resource "google_cloud_run_service" "default" {
}
}
metadata {
annotations = {
generated-by = "magic-modules"
}
}
traffic {
percent = 100
latest_revision = true
Expand Down

0 comments on commit 92401df

Please sign in to comment.