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

Support manifest service parameters #3699

Merged
merged 1 commit into from
Jan 10, 2025
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
62 changes: 33 additions & 29 deletions api/actions/manifest/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,12 @@ func (a *Applier) deleteAppDestinations(
existingAppRoutes map[string]repositories.RouteRecord,
) error {
for _, route := range existingAppRoutes {
existingDestinations := route.Destinations

for _, destination := range route.Destinations {
if destination.AppGUID != appGUID {
continue
}

var err error
existingDestinations, err = a.deleteAppDestination(ctx, authInfo, route, destination.GUID, existingDestinations)
err := a.deleteAppDestination(ctx, authInfo, route, destination.GUID)
if err != nil {
return err
}
Expand All @@ -192,65 +189,62 @@ func (a *Applier) deleteAppDestinations(
return nil
}

func (a *Applier) deleteAppDestination(ctx context.Context, authInfo authorization.Info, route repositories.RouteRecord, destinationGUID string, existingDestinations []repositories.DestinationRecord) ([]repositories.DestinationRecord, error) {
route, err := a.routeRepo.RemoveDestinationFromRoute(ctx, authInfo, repositories.RemoveDestinationMessage{
func (a *Applier) deleteAppDestination(ctx context.Context, authInfo authorization.Info, route repositories.RouteRecord, destinationGUID string) error {
_, err := a.routeRepo.RemoveDestinationFromRoute(ctx, authInfo, repositories.RemoveDestinationMessage{
RouteGUID: route.GUID,
SpaceGUID: route.SpaceGUID,
GUID: destinationGUID,
})
if err != nil {
return nil, err
}

return route.Destinations, nil
return err
}

func (a *Applier) applyServices(ctx context.Context, authInfo authorization.Info, appInfo payloads.ManifestApplication, appState AppState) error {
desiredServiceNames := map[string]bool{}
manifestServiceNames := map[string]bool{}
for _, s := range appInfo.Services {
desiredServiceNames[s.Name] = true
manifestServiceNames[s.Name] = true
}
for serviceName := range appState.ServiceBindings {
delete(desiredServiceNames, serviceName)
delete(manifestServiceNames, serviceName)
}

if len(desiredServiceNames) == 0 {
if len(manifestServiceNames) == 0 {
return nil
}

serviceInstances, err := a.serviceInstanceRepo.ListServiceInstances(ctx, authInfo, repositories.ListServiceInstanceMessage{
Names: slices.Collect(maps.Keys(desiredServiceNames)),
Names: slices.Collect(maps.Keys(manifestServiceNames)),
})
if err != nil {
return err
}

serviceNameToServiceInstance := map[string]repositories.ServiceInstanceRecord{}
serviceGUIDToInstanceRecord := map[string]repositories.ServiceInstanceRecord{}
for _, serviceInstance := range serviceInstances {
serviceNameToServiceInstance[serviceInstance.Name] = serviceInstance
serviceGUIDToInstanceRecord[serviceInstance.Name] = serviceInstance
}

serviceNameToServiceBinding := map[string]*string{}
for _, manifestService := range appInfo.Services {
serviceNameToServiceBinding[manifestService.Name] = manifestService.BindingName
}

for serviceName := range desiredServiceNames {
serviceInstance, ok := serviceNameToServiceInstance[serviceName]
for manifestServiceName := range manifestServiceNames {
serviceInstanceRecord, ok := serviceGUIDToInstanceRecord[manifestServiceName]
if !ok {
return apierrors.NewNotFoundError(
nil,
repositories.ServiceInstanceResourceType,
"application", appInfo.Name,
"service", serviceName,
"service", manifestServiceName,
)
}

_, err := a.serviceBindingRepo.CreateServiceBinding(ctx, authInfo, repositories.CreateServiceBindingMessage{
Name: serviceNameToServiceBinding[serviceName],
ServiceInstanceGUID: serviceInstance.GUID,
manifestService, err := getManifestService(appInfo, manifestServiceName)
if err != nil {
return apierrors.NewUnknownError(err)
}

_, err = a.serviceBindingRepo.CreateServiceBinding(ctx, authInfo, repositories.CreateServiceBindingMessage{
Name: manifestService.BindingName,
ServiceInstanceGUID: serviceInstanceRecord.GUID,
AppGUID: appState.App.GUID,
SpaceGUID: appState.App.SpaceGUID,
Parameters: manifestService.Parameters,
})
if err != nil {
return err
Expand All @@ -260,6 +254,16 @@ func (a *Applier) applyServices(ctx context.Context, authInfo authorization.Info
return nil
}

func getManifestService(manifestApp payloads.ManifestApplication, serviceName string) (payloads.ManifestApplicationService, error) {
for _, manifestService := range manifestApp.Services {
if manifestService.Name == serviceName {
return manifestService, nil
}
}

return payloads.ManifestApplicationService{}, fmt.Errorf("service %q not found in app %q manifest", serviceName, manifestApp.Name)
}

func splitRoute(route string) (string, string, string) {
parts := strings.SplitN(route, ".", 2)
hostName := parts[0]
Expand Down
24 changes: 24 additions & 0 deletions api/actions/manifest/applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,30 @@ var _ = Describe("Applier", func() {
})
})

When("the manifest service has parameters", func() {
BeforeEach(func() {
appInfo.Services = []payloads.ManifestApplicationService{
{
Name: "service-name",
Parameters: map[string]any{
"binding-param": "binding-param-value",
},
},
{Name: "already-bound-service-name"},
}
})

It("uses them when creating the binding", func() {
Expect(applierErr).NotTo(HaveOccurred())

Expect(serviceBindingRepo.CreateServiceBindingCallCount()).To(Equal(1))
_, _, createMsg := serviceBindingRepo.CreateServiceBindingArgsForCall(0)
Expect(createMsg.Parameters).To(Equal(map[string]any{
"binding-param": "binding-param-value",
}))
})
})

When("listing service instances fails", func() {
BeforeEach(func() {
serviceInstanceRepo.ListServiceInstancesReturns(nil, errors.New("list-services-err"))
Expand Down
15 changes: 15 additions & 0 deletions api/handlers/space_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ var _ = Describe("SpaceManifest", func() {
Memory: tools.PtrTo("256M"),
Timeout: tools.PtrTo[int32](10),
}},
Services: []payloads.ManifestApplicationService{
{
Name: "my-service",
Parameters: map[string]any{
"binding-param": "binding-param-value",
},
},
},
}},
})
})
Expand Down Expand Up @@ -104,6 +112,13 @@ var _ = Describe("SpaceManifest", func() {
Expect(payload.Applications[0].Processes[0].Instances).To(PointTo(BeEquivalentTo(1)))
Expect(payload.Applications[0].Processes[0].Memory).To(PointTo(Equal("256M")))
Expect(payload.Applications[0].Processes[0].Timeout).To(PointTo(Equal(int32(10))))

Expect(payload.Applications[0].Services).To(ConsistOf(payloads.ManifestApplicationService{
Name: "my-service",
Parameters: map[string]any{
"binding-param": "binding-param-value",
},
}))
})

When("the manifest is invalid", func() {
Expand Down
5 changes: 3 additions & 2 deletions api/payloads/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ type ManifestApplicationProcess struct {
}

type ManifestApplicationService struct {
Name string `json:"name" yaml:"name"`
BindingName *string `json:"binding_name" yaml:"binding_name"`
Name string `json:"name" yaml:"name"`
BindingName *string `json:"binding_name" yaml:"binding_name"`
Parameters map[string]any `json:"parameters" yaml:"parameters"`
}

func (s *ManifestApplicationService) UnmarshalYAML(value *yaml.Node) error {
Expand Down
11 changes: 10 additions & 1 deletion tests/assets/sample-broker-golang/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package main

import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -218,7 +220,14 @@ func getOperation(r *http.Request) (string, error) {
}

func logRequest(r *http.Request) {
log(fmt.Sprintf("%s %v", r.Method, r.URL))
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
log(fmt.Sprintf("failed to read request %s %v body: %v", r.Method, r.URL, err))
}

r.Body = io.NopCloser(bytes.NewReader(bodyBytes))

log(fmt.Sprintf("%s %v\nBody: %s", r.Method, r.URL, string(bodyBytes)))
}

func log(s string) {
Expand Down
Loading