-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package grafana | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/crossplane/crossplane-runtime/pkg/fieldpath" | ||
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" | ||
"github.com/crossplane/crossplane-runtime/pkg/resource" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func createDashboardConfigInitializer(client client.Client) managed.Initializer { | ||
return &dashboardConfigInitializer{ | ||
kube: client, | ||
} | ||
} | ||
|
||
// Based on the Tagger: https://github.com/crossplane/upjet/blob/v1.1.0/pkg/config/resource.go#L268 | ||
type dashboardConfigInitializer struct { | ||
kube client.Client | ||
} | ||
|
||
// Replaces ${ with $${ to avoid TF interpolation | ||
func (i *dashboardConfigInitializer) Initialize(ctx context.Context, mg resource.Managed) error { | ||
paved, err := fieldpath.PaveObject(mg) | ||
if err != nil { | ||
return err | ||
} | ||
v, err := paved.GetString("spec.forProvider.configJSON") | ||
if err != nil { | ||
return fmt.Errorf("could not get configJSON: %w", err) | ||
} | ||
if err := paved.SetString("spec.forProvider.configJSON", replaceInterpolation(v)); err != nil { | ||
return fmt.Errorf("could not set configJSON: %w", err) | ||
} | ||
pavedByte, err := paved.MarshalJSON() | ||
if err != nil { | ||
return fmt.Errorf("could not marshal modified dashboard spec into JSON: %w", err) | ||
} | ||
if err := json.Unmarshal(pavedByte, mg); err != nil { | ||
return fmt.Errorf("could not unmarshal modified dashboard spec into managed resource interface: %w", err) | ||
} | ||
if err := i.kube.Update(ctx, mg); err != nil { | ||
return fmt.Errorf("could not update managed resource: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
// Replaces ${ with $${ to avoid TF interpolation | ||
// If a string is already escaped, it should not be escaped again | ||
func replaceInterpolation(s string) string { | ||
replaced := strings.ReplaceAll(s, "${", "$${") | ||
return strings.ReplaceAll(replaced, "$$${", "$${") // Unescape already escaped strings | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package grafana | ||
|
||
import "testing" | ||
|
||
func TestReplaceInterpolation(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
name: "no interpolation", | ||
input: "no interpolation", | ||
expected: "no interpolation", | ||
}, | ||
{ | ||
name: "interpolation", | ||
input: "interpolation ${}", | ||
expected: "interpolation $${}", | ||
}, | ||
{ | ||
name: "escaped interpolation", | ||
input: "escaped interpolation $${}", | ||
expected: "escaped interpolation $${}", | ||
}, | ||
{ | ||
name: "multiple interpolations", | ||
input: "multiple ${} interpolations ${}", | ||
expected: "multiple $${} interpolations $${}", | ||
}, | ||
{ | ||
name: "multiple escaped interpolations", | ||
input: "interpolation $${} with escaped $${}", | ||
expected: "interpolation $${} with escaped $${}", | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
actual := replaceInterpolation(tc.input) | ||
if actual != tc.expected { | ||
t.Errorf("expected %s, got %s", tc.expected, actual) | ||
} | ||
}) | ||
} | ||
} |