Skip to content

Commit

Permalink
add vcap_application.uris + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
szeort committed Nov 15, 2023
1 parent 3428c46 commit 5cf8c34
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
28 changes: 28 additions & 0 deletions controllers/controllers/workloads/env/vcap_app_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func (b *VCAPApplicationEnvValueBuilder) BuildEnvValue(ctx context.Context, cfAp
return nil, fmt.Errorf("failed retrieving org for CFSpace: %w", err)
}

appURIs, err := b.getAppURIs(ctx, cfApp)
if err != nil {
return nil, fmt.Errorf("failed retrieving app routes: %w", err)
}

vars := b.extraValues
if vars == nil {
vars = map[string]any{}
Expand All @@ -44,6 +49,8 @@ func (b *VCAPApplicationEnvValueBuilder) BuildEnvValue(ctx context.Context, cfAp
vars["organization_name"] = org.Spec.DisplayName
vars["space_id"] = space.Name
vars["space_name"] = space.Spec.DisplayName
vars["uris"] = appURIs
vars["application_uris"] = appURIs

marshalledVars, _ := json.Marshal(vars)

Expand All @@ -52,6 +59,27 @@ func (b *VCAPApplicationEnvValueBuilder) BuildEnvValue(ctx context.Context, cfAp
}, nil
}

func (b *VCAPApplicationEnvValueBuilder) getAppURIs(ctx context.Context, cfApp *korifiv1alpha1.CFApp) ([]string, error) {
var appRoutes korifiv1alpha1.CFRouteList
err := b.k8sClient.List(
ctx,
&appRoutes,
client.InNamespace(cfApp.Namespace),
client.MatchingFields{shared.IndexRouteDestinationAppName: cfApp.Name},
)

if err != nil {
return nil, err
}

uris := make([]string, len(appRoutes.Items))
for i := range appRoutes.Items {
uris[i] = appRoutes.Items[i].Status.URI
}

return uris, nil
}

func (b *VCAPApplicationEnvValueBuilder) getSpaceFromNamespace(ctx context.Context, ns string) (korifiv1alpha1.CFSpace, error) {
spaces := korifiv1alpha1.CFSpaceList{}
if err := b.k8sClient.List(ctx, &spaces, client.MatchingFields{
Expand Down
50 changes: 50 additions & 0 deletions controllers/controllers/workloads/env/vcap_app_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package env_test
import (
"encoding/json"

korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/controllers/controllers/workloads/env"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -38,9 +41,12 @@ var _ = Describe("VCAP_APPLICATION env value builder", func() {
Expect(vcapAppValue).To(HaveKeyWithValue("space_name", cfSpace.Spec.DisplayName))
Expect(vcapAppValue).To(HaveKeyWithValue("organization_id", cfOrg.Name))
Expect(vcapAppValue).To(HaveKeyWithValue("organization_name", cfOrg.Spec.DisplayName))
Expect(vcapAppValue).To(HaveKeyWithValue("uris", BeEmpty()))
Expect(vcapAppValue).To(HaveKeyWithValue("application_uris", BeEmpty()))
})

When("extra values are provided", func() {

BeforeEach(func() {
builder = env.NewVCAPApplicationEnvValueBuilder(controllersClient, map[string]any{
"application_id": "not-the-application-id",
Expand All @@ -63,5 +69,49 @@ var _ = Describe("VCAP_APPLICATION env value builder", func() {
Expect(vcapAppValue).To(HaveKeyWithValue("x", HaveKeyWithValue("y", "z")))
})
})

When("application routes are provided", func() {
var (
appRoute *korifiv1alpha1.CFRoute
routeUri string
)

BeforeEach(func() {
appRoute = &korifiv1alpha1.CFRoute{
ObjectMeta: metav1.ObjectMeta{
Namespace: cfApp.Namespace,
Name: "vcap-application-my-route",
},
Spec: korifiv1alpha1.CFRouteSpec{
Destinations: []korifiv1alpha1.Destination{{
GUID: "destination-123-456",
AppRef: v1.LocalObjectReference{
Name: cfApp.Name,
},
}},
},
}

ensureCreate(appRoute)

routeUri = cfApp.Name + ".mydomain.platform.com"

ensurePatch(appRoute, func(appRoute *korifiv1alpha1.CFRoute) {
appRoute.Status.CurrentStatus = "valid"
appRoute.Status.Description = "test patch status"
appRoute.Status.URI = routeUri
})
})

It("includes application uris", func() {
Expect(buildVCAPApplicationEnvValueErr).ToNot(HaveOccurred())
Expect(vcapApplication).To(HaveKey("VCAP_APPLICATION"))
vcapAppValue := map[string]any{}
Expect(json.Unmarshal([]byte(vcapApplication["VCAP_APPLICATION"]), &vcapAppValue)).To(Succeed())
Expect(vcapAppValue).To(HaveKeyWithValue("application_id", cfApp.Name))
Expect(vcapAppValue).To(HaveKeyWithValue("application_uris", ConsistOf(routeUri)))
Expect(vcapAppValue).To(HaveKeyWithValue("uris", ConsistOf(routeUri)))
})
})
})
})

0 comments on commit 5cf8c34

Please sign in to comment.