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

Set VCAP_APPLICATION env var in apps #2187

Merged
merged 8 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ generate-fakes:

fmt: install-gofumpt install-shfmt
$(GOFUMPT) -w .
$(SHFMT) -w -i 2 -ci .
$(SHFMT) -f . | grep -v '^tests/vendor' | xargs $(SHFMT) -w -i 2 -ci

vet: ## Run go vet against code.
go vet ./...
Expand Down Expand Up @@ -69,3 +69,4 @@ install-vendir:

vendir-update-dependencies: install-vendir
$(VENDIR) sync --chdir tests
cd tests/vendor/testApps/dora; zip -r ../../../e2e/assets/dora .
1 change: 1 addition & 0 deletions api/handlers/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2596,6 +2596,7 @@ var _ = Describe("App", func() {
SpaceGUID: spaceGUID,
EnvironmentVariables: map[string]string{"VAR": "VAL"},
SystemEnv: map[string]interface{}{},
AppEnv: map[string]interface{}{},
}, nil)

req = createHttpRequest("GET", "/v3/apps/"+appGUID+"/env", nil)
Expand Down
12 changes: 6 additions & 6 deletions api/presenter/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ func ForAppEnvVars(record repositories.AppEnvVarsRecord, baseURL url.URL) AppEnv
}

type AppEnvResponse struct {
EnvironmentVariables map[string]string `json:"environment_variables"`
StagingEnvJSON map[string]string `json:"staging_env_json"`
RunningEnvJSON map[string]string `json:"running_env_json"`
SystemEnvJSON map[string]interface{} `json:"system_env_json"`
ApplicationEnvJSON map[string]string `json:"application_env_json"`
EnvironmentVariables map[string]string `json:"environment_variables"`
StagingEnvJSON map[string]string `json:"staging_env_json"`
RunningEnvJSON map[string]string `json:"running_env_json"`
SystemEnvJSON map[string]any `json:"system_env_json"`
ApplicationEnvJSON map[string]any `json:"application_env_json"`
}

func ForAppEnv(envVarRecord repositories.AppEnvRecord) AppEnvResponse {
Expand All @@ -185,6 +185,6 @@ func ForAppEnv(envVarRecord repositories.AppEnvRecord) AppEnvResponse {
StagingEnvJSON: map[string]string{},
RunningEnvJSON: map[string]string{},
SystemEnvJSON: envVarRecord.SystemEnv,
ApplicationEnvJSON: map[string]string{},
ApplicationEnvJSON: envVarRecord.AppEnv,
}
}
67 changes: 56 additions & 11 deletions api/repositories/app_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type AppRecord struct {
IsStaged bool
envSecretName string
vcapServiceSecretName string
vcapAppSecretName string
}

type DesiredState string
Expand All @@ -101,6 +102,7 @@ type AppEnvRecord struct {
SpaceGUID string
EnvironmentVariables map[string]string
SystemEnv map[string]interface{}
AppEnv map[string]interface{}
}

type CurrentDropletRecord struct {
Expand Down Expand Up @@ -573,21 +575,43 @@ func (f *AppRepo) GetAppEnv(ctx context.Context, authInfo authorization.Info, ap
appEnvVarMap = convertByteSliceValuesToStrings(appEnvVarSecret.Data)
}

systemEnvMap := map[string]interface{}{}
systemEnvMap, err := getSystemEnv(ctx, userClient, app)
if err != nil {
return AppEnvRecord{}, err
}

appEnvMap, err := getAppEnv(ctx, userClient, app)
if err != nil {
return AppEnvRecord{}, err
}

appEnvRecord := AppEnvRecord{
AppGUID: appGUID,
SpaceGUID: app.SpaceGUID,
EnvironmentVariables: appEnvVarMap,
SystemEnv: systemEnvMap,
AppEnv: appEnvMap,
}

return appEnvRecord, nil
}

func getSystemEnv(ctx context.Context, userClient client.Client, app AppRecord) (map[string]any, error) {
systemEnvMap := map[string]any{}
if app.vcapServiceSecretName != "" {
vcapServiceSecret := new(corev1.Secret)
err = userClient.Get(ctx, types.NamespacedName{Name: app.vcapServiceSecretName, Namespace: app.SpaceGUID}, vcapServiceSecret)
err := userClient.Get(ctx, types.NamespacedName{Name: app.vcapServiceSecretName, Namespace: app.SpaceGUID}, vcapServiceSecret)
if err != nil {
return AppEnvRecord{}, fmt.Errorf("error finding VCAP Service Secret %q for App %q: %w",
return map[string]any{}, fmt.Errorf("error finding VCAP Service Secret %q for App %q: %w",
app.vcapServiceSecretName,
app.GUID,
apierrors.FromK8sError(err, AppEnvResourceType))
}

if vcapServicesData, ok := vcapServiceSecret.Data["VCAP_SERVICES"]; ok {
vcapServicesPresenter := new(env.VcapServicesPresenter)
vcapServicesPresenter := new(env.VCAPServices)
if err = json.Unmarshal(vcapServicesData, &vcapServicesPresenter); err != nil {
return AppEnvRecord{}, fmt.Errorf("error unmarshalling VCAP Service Secret %q for App %q: %w",
return map[string]any{}, fmt.Errorf("error unmarshalling VCAP Service Secret %q for App %q: %w",
app.vcapServiceSecretName,
app.GUID,
apierrors.FromK8sError(err, AppEnvResourceType))
Expand All @@ -599,14 +623,34 @@ func (f *AppRepo) GetAppEnv(ctx context.Context, authInfo authorization.Info, ap
}
}

appEnvRecord := AppEnvRecord{
AppGUID: appGUID,
SpaceGUID: app.SpaceGUID,
EnvironmentVariables: appEnvVarMap,
SystemEnv: systemEnvMap,
return systemEnvMap, nil
}

func getAppEnv(ctx context.Context, userClient client.Client, app AppRecord) (map[string]any, error) {
appEnvMap := map[string]any{}
if app.vcapAppSecretName != "" {
vcapAppSecret := new(corev1.Secret)
err := userClient.Get(ctx, types.NamespacedName{Name: app.vcapAppSecretName, Namespace: app.SpaceGUID}, vcapAppSecret)
if err != nil {
return map[string]any{}, fmt.Errorf("error finding VCAP Application Secret %q for App %q: %w",
app.vcapAppSecretName,
app.GUID,
apierrors.FromK8sError(err, AppEnvResourceType))
}

if vcapAppDataBytes, ok := vcapAppSecret.Data["VCAP_APPLICATION"]; ok {
appData := map[string]any{}
if err = json.Unmarshal(vcapAppDataBytes, &appData); err != nil {
return map[string]any{}, fmt.Errorf("error unmarshalling VCAP Application Secret %q for App %q: %w",
app.vcapAppSecretName,
app.GUID,
apierrors.FromK8sError(err, AppEnvResourceType))
}
appEnvMap["VCAP_APPLICATION"] = appData
}
}

return appEnvRecord, nil
return appEnvMap, nil
}

func GenerateEnvSecretName(appGUID string) string {
Expand Down Expand Up @@ -684,6 +728,7 @@ func cfAppToAppRecord(cfApp korifiv1alpha1.CFApp) AppRecord {
IsStaged: meta.IsStatusConditionTrue(cfApp.Status.Conditions, workloads.StatusConditionStaged),
envSecretName: cfApp.Spec.EnvSecretName,
vcapServiceSecretName: cfApp.Status.VCAPServicesSecretName,
vcapAppSecretName: cfApp.Status.VCAPApplicationSecretName,
}
}

Expand Down
46 changes: 43 additions & 3 deletions api/repositories/app_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1352,13 +1352,14 @@ var _ = Describe("AppRepository", func() {
Expect(appEnvRecord.SpaceGUID).To(Equal(cfApp.Namespace))
Expect(appEnvRecord.EnvironmentVariables).To(Equal(envVars))
Expect(appEnvRecord.SystemEnv).To(BeEmpty())
Expect(appEnvRecord.AppEnv).To(BeEmpty())
})

When("the app has a service-binding secret", func() {
var (
vcapServiceSecretDataByte map[string][]byte
vcapServiceSecretData map[string]string
vcapServiceDataPresenter *env.VcapServicesPresenter
vcapServiceDataPresenter *env.VCAPServices
err error
)

Expand All @@ -1367,7 +1368,7 @@ var _ = Describe("AppRepository", func() {
vcapServiceSecretDataByte, err = generateVcapServiceSecretDataByte()
Expect(err).NotTo(HaveOccurred())
vcapServiceSecretData = asMapOfStrings(vcapServiceSecretDataByte)
vcapServiceDataPresenter = new(env.VcapServicesPresenter)
vcapServiceDataPresenter = new(env.VCAPServices)
err = json.Unmarshal(vcapServiceSecretDataByte["VCAP_SERVICES"], vcapServiceDataPresenter)
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -1441,6 +1442,31 @@ var _ = Describe("AppRepository", func() {
})
})

When("the app has a vcap application secret", func() {
BeforeEach(func() {
vcapAppSecretName := prefixedGUID("vcap-app-secret")
Expect(k8sClient.Create(ctx, &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: vcapAppSecretName,
Namespace: cfApp.Namespace,
},
Data: map[string][]byte{
"VCAP_APPLICATION": []byte(`{"foo":"bar"}`),
},
})).To(Succeed())
Expect(k8s.Patch(ctx, k8sClient, cfApp, func() {
cfApp.Status.VCAPApplicationSecretName = vcapAppSecretName
})).To(Succeed())
})

It("returns the env vars stored on the secret", func() {
Expect(getAppEnvErr).NotTo(HaveOccurred())
Expect(appEnvRecord.EnvironmentVariables).To(Equal(envVars))

Expect(appEnvRecord.AppEnv).To(HaveKey("VCAP_APPLICATION"))
})
})

When("the EnvSecret doesn't exist", func() {
BeforeEach(func() {
secretName = "doIReallyExist"
Expand All @@ -1467,6 +1493,20 @@ var _ = Describe("AppRepository", func() {
Expect(getAppEnvErr).To(MatchError(ContainSubstring("Secret")))
})
})

When("the VCAPApplication secret doesn't exist", func() {
BeforeEach(func() {
vcapApplicationSecretName := "doIReallyExist"

ogCFApp := cfApp.DeepCopy()
cfApp.Status.VCAPApplicationSecretName = vcapApplicationSecretName
Expect(k8sClient.Status().Patch(testCtx, cfApp, client.MergeFrom(ogCFApp))).To(Succeed())
})

It("errors", func() {
Expect(getAppEnvErr).To(MatchError(ContainSubstring("Secret")))
})
})
})

When("EnvSecretName is blank", func() {
Expand Down Expand Up @@ -1552,7 +1592,7 @@ func generateVcapServiceSecretDataByte() (map[string][]byte, error) {
VolumeMounts: nil,
}

vcapServicesData, err := json.Marshal(env.VcapServicesPresenter{
vcapServicesData, err := json.Marshal(env.VCAPServices{
UserProvided: []env.ServiceDetails{
serviceDetails,
},
Expand Down
3 changes: 3 additions & 0 deletions controllers/api/v1alpha1/cfapp_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type CFAppStatus struct {

// VCAPServicesSecretName contains the name of the CFApp's VCAP_SERVICES Secret, which should exist in the same namespace
VCAPServicesSecretName string `json:"vcapServicesSecretName"`

// VCAPApplicationSecretName contains the name of the CFApp's VCAP_APPLICATION Secret, which should exist in the same namespace
VCAPApplicationSecretName string `json:"vcapApplicationSecretName"`
}

//+kubebuilder:object:root=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ var _ = Describe("CFServiceBinding", func() {

Expect(k8s.Patch(context.Background(), k8sClient, desiredCFApp, func() {
desiredCFApp.Status = korifiv1alpha1.CFAppStatus{
Conditions: nil,
VCAPServicesSecretName: "foo",
Conditions: nil,
VCAPServicesSecretName: "foo",
VCAPApplicationSecretName: "bar",
}
meta.SetStatusCondition(&desiredCFApp.Status.Conditions, metav1.Condition{
Type: "Ready",
Expand Down
18 changes: 18 additions & 0 deletions controllers/controllers/shared/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const (
IndexServiceBindingAppGUID = "serviceBindingAppGUID"
IndexServiceBindingServiceInstanceGUID = "serviceBindingServiceInstanceGUID"
IndexAppTasks = "appTasks"
IndexSpaceNamespaceName = "spaceNamespace"
IndexOrgNamespaceName = "orgNamespace"
)

func SetupIndexWithManager(mgr manager.Manager) error {
Expand Down Expand Up @@ -45,6 +47,22 @@ func SetupIndexWithManager(mgr manager.Manager) error {
return err
}

err = mgr.GetFieldIndexer().IndexField(context.Background(), &korifiv1alpha1.CFSpace{}, IndexSpaceNamespaceName, func(object client.Object) []string {
space := object.(*korifiv1alpha1.CFSpace)
return []string{space.Status.GUID}
})
if err != nil {
return err
}

err = mgr.GetFieldIndexer().IndexField(context.Background(), &korifiv1alpha1.CFOrg{}, IndexOrgNamespaceName, func(object client.Object) []string {
org := object.(*korifiv1alpha1.CFOrg)
return []string{org.Status.GUID}
})
if err != nil {
return err
}

return nil
}

Expand Down
Loading