Skip to content

Commit 1a4249c

Browse files
authored
provide new 'baseDomainPort' variable for templates (#82)
On-behalf-of: @SAP [email protected] Signed-off-by: Angel Kafazov <[email protected]>
1 parent 842a11b commit 1a4249c

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

pkg/subroutines/deployment_test.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (s *DeployTestSuite) Test_applyReleaseWithValues() {
6363
}
6464

6565
// mocks
66-
s.clientMock.EXPECT().Get(mock.Anything, types.NamespacedName{Namespace: "default", Name: "rebac-authz-webhook-cert"}, mock.Anything).Return(nil).Once()
66+
s.clientMock.EXPECT().Get(mock.Anything, types.NamespacedName{Namespace: "default", Name: "rebac-authz-webhook-cert"}, mock.Anything).Return(nil).Twice()
6767
s.clientMock.EXPECT().Patch(mock.Anything, mock.Anything, mock.Anything, mock.Anything).RunAndReturn(
6868
func(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
6969
// Simulate a successful patch operation
@@ -86,7 +86,7 @@ func (s *DeployTestSuite) Test_applyReleaseWithValues() {
8686
specJSON, ok := specValues.(apiextensionsv1.JSON)
8787
s.Require().True(ok, "spec.values should be of type apiextensionsv1.JSON")
8888

89-
expected := `{"baseDomain":"portal.dev.local","iamWebhookCA":"","port":"8443","protocol":"https","services":{"services":{"platform-mesh-operator":{"version":"v1.0.0"}}}}`
89+
expected := `{"baseDomain":"portal.dev.local","baseDomainPort":"portal.dev.local:8443","iamWebhookCA":"","port":"8443","protocol":"https","services":{"services":{"platform-mesh-operator":{"version":"v1.0.0"}}}}`
9090
s.Require().Equal(expected, string(specJSON.Raw), "spec.values.Raw should match expected JSON string")
9191

9292
return nil
@@ -109,4 +109,48 @@ func (s *DeployTestSuite) Test_applyReleaseWithValues() {
109109

110110
err = s.testObj.ApplyReleaseWithValues(ctx, "../../manifests/k8s/platform-mesh-operator-components/release.yaml", s.clientMock, mergedValues)
111111
s.Assert().NoError(err, "ApplyReleaseWithValues should not return an error")
112+
113+
// switch to standard port 443
114+
inst.Spec.Exposure = &v1alpha1.ExposureConfig{
115+
Port: 443,
116+
}
117+
118+
templateVars, err = subroutines.TemplateVars(ctx, inst, s.clientMock)
119+
s.Assert().NoError(err, "TemplateVars should not return an error")
120+
121+
s.clientMock.EXPECT().Patch(mock.Anything, mock.Anything, mock.Anything, mock.Anything).RunAndReturn(
122+
func(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
123+
// Simulate a successful patch operation
124+
hr := obj.(*unstructured.Unstructured)
125+
126+
// Extract .spec
127+
spec, found, err := unstructured.NestedFieldNoCopy(hr.Object, "spec")
128+
s.Require().NoError(err, "should be able to get spec")
129+
s.Require().True(found, "spec should be present")
130+
131+
// Check if spec is a map
132+
specMap, ok := spec.(map[string]interface{})
133+
s.Require().True(ok, "spec should be a map[string]interface{}")
134+
135+
// Extract .spec.values
136+
specValues, found, err := unstructured.NestedFieldNoCopy(specMap, "values")
137+
s.Require().NoError(err, "should be able to get spec.values")
138+
s.Require().True(found, "spec.values should be present")
139+
140+
specJSON, ok := specValues.(apiextensionsv1.JSON)
141+
s.Require().True(ok, "spec.values should be of type apiextensionsv1.JSON")
142+
143+
expected := `{"baseDomain":"portal.dev.local","baseDomainPort":"portal.dev.local","iamWebhookCA":"","port":"443","protocol":"https","services":{"services":{"platform-mesh-operator":{"version":"v1.0.0"}}}}`
144+
s.Require().Equal(expected, string(specJSON.Raw), "spec.values.Raw should match expected JSON string")
145+
146+
return nil
147+
},
148+
).Once()
149+
150+
mergedValues, err = subroutines.MergeValuesAndServices(instance, templateVars)
151+
s.Assert().NoError(err, "MergeValuesAndServices should not return an error")
152+
153+
err = s.testObj.ApplyReleaseWithValues(ctx, "../../manifests/k8s/platform-mesh-operator-components/release.yaml", s.clientMock, mergedValues)
154+
s.Assert().NoError(err, "ApplyReleaseWithValues should not return an error")
155+
112156
}

pkg/subroutines/subroutine_helpers.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ func TemplateVars(ctx context.Context, inst *v1alpha1.PlatformMesh, cl client.Cl
259259
port := 8443
260260
baseDomain := "portal.dev.local"
261261
protocol := "https"
262+
baseDomainPort := ""
262263

263264
if inst.Spec.Exposure != nil {
264265
if inst.Spec.Exposure.Port != 0 {
@@ -272,6 +273,12 @@ func TemplateVars(ctx context.Context, inst *v1alpha1.PlatformMesh, cl client.Cl
272273
}
273274
}
274275

276+
if port == 80 || port == 443 {
277+
baseDomainPort = baseDomain
278+
} else {
279+
baseDomainPort = fmt.Sprintf("%s:%d", baseDomain, port)
280+
}
281+
275282
var secret corev1.Secret
276283
err := cl.Get(ctx, client.ObjectKey{
277284
Name: "rebac-authz-webhook-cert",
@@ -282,10 +289,11 @@ func TemplateVars(ctx context.Context, inst *v1alpha1.PlatformMesh, cl client.Cl
282289
}
283290

284291
values := map[string]interface{}{
285-
"iamWebhookCA": base64.StdEncoding.EncodeToString(secret.Data["ca.crt"]),
286-
"baseDomain": baseDomain,
287-
"protocol": protocol,
288-
"port": fmt.Sprintf("%d", port),
292+
"iamWebhookCA": base64.StdEncoding.EncodeToString(secret.Data["ca.crt"]),
293+
"baseDomain": baseDomain,
294+
"protocol": protocol,
295+
"port": fmt.Sprintf("%d", port),
296+
"baseDomainPort": baseDomainPort,
289297
}
290298

291299
result := apiextensionsv1.JSON{}

0 commit comments

Comments
 (0)