Skip to content

Commit

Permalink
watch for when routability of an app instance changes
Browse files Browse the repository at this point in the history
* bump bbs now with readiness information for app processes
* update cc_client to have AppReadinessChanged function

Signed-off-by: Geoff Franks <[email protected]>
Co-authored-by: Geoff Franks <[email protected]>
Signed-off-by: Amelia Downs <[email protected]>
Co-authored-by: Amelia Downs <[email protected]>
  • Loading branch information
ameowlia and geofffranks committed Jan 10, 2024
1 parent 558a082 commit eaf84cb
Show file tree
Hide file tree
Showing 32 changed files with 3,307 additions and 1,021 deletions.
44 changes: 40 additions & 4 deletions cc_client/cc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cc_client

import (
"bytes"
"code.cloudfoundry.org/lager/v3"
"crypto/tls"
"crypto/x509"
"encoding/json"
Expand All @@ -13,19 +12,23 @@ import (
"net/http"
"time"

"code.cloudfoundry.org/lager/v3"

"code.cloudfoundry.org/runtimeschema/cc_messages"
)

const (
appCrashedPath = "/internal/v4/apps/%s/crashed"
appReschedulingPath = "/internal/v4/apps/%s/rescheduling"
ccRequestTimeout = 5 * time.Second
appCrashedPath = "/internal/v4/apps/%s/crashed"
appReschedulingPath = "/internal/v4/apps/%s/rescheduling"
appReadinessChangedPath = "/internal/v4/apps/%s/readiness_changed"
ccRequestTimeout = 5 * time.Second
)

//go:generate counterfeiter -o fakes/fake_cc_client.go . CcClient
type CcClient interface {
AppCrashed(guid string, appCrashed cc_messages.AppCrashedRequest, logger lager.Logger) error
AppRescheduling(guid string, appRescheduling cc_messages.AppReschedulingRequest, logger lager.Logger) error
AppReadinessChanged(guid string, AppReadinessChanged cc_messages.AppReadinessChangedRequest, logger lager.Logger) error
}

type ccClient struct {
Expand Down Expand Up @@ -158,3 +161,36 @@ func (cc *ccClient) AppRescheduling(guid string, appRescheduling cc_messages.App
logger.Debug("delivered-app-rescheduling-response")
return nil
}

func (cc *ccClient) AppReadinessChanged(guid string, appReadinessChanged cc_messages.AppReadinessChangedRequest, logger lager.Logger) error {
logger = logger.Session("cc-client")
logger.Debug("delivering-app-readiness-changed-response", lager.Data{"app_readiness_changed": appReadinessChanged})

payload, err := json.Marshal(appReadinessChanged)
if err != nil {
return err
}

url := fmt.Sprintf(cc.ccURI+appReadinessChangedPath, guid)
request, err := http.NewRequest("POST", url, bytes.NewReader(payload))
if err != nil {
return err
}

request.Header.Set("content-type", "application/json")

response, err := cc.httpClient.Do(request)
if err != nil {
logger.Error("deliver-app-readiness-changed-response-failed", err)
return err
}

defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return &BadResponseError{response.StatusCode}
}

logger.Debug("delivered-app-readiness-changed-response")
return nil
}
62 changes: 62 additions & 0 deletions cc_client/cc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,40 @@ var _ = Describe("CC Client", func() {
})
})

Describe("Successfully calling the Cloud Controller's readiness changed endpoint", func() {
var expectedBody = []byte(`{"instance":"instance-id","index":3,"cell_id":"id-of-cell","ready":true}`)
var callCount int

BeforeEach(func() {
callCount = 0
fakeCC.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/internal/v4/apps/"+guid+"/readiness_changed"),
ghttp.RespondWith(200, `{}`),
func(w http.ResponseWriter, req *http.Request) {
callCount += 1
body, err := ioutil.ReadAll(req.Body)
defer req.Body.Close()

Expect(err).NotTo(HaveOccurred())
Expect(body).To(Equal(expectedBody))
},
),
)
})

It("sends the request payload to the CC without modification", func() {
err := ccClient.AppReadinessChanged(guid, cc_messages.AppReadinessChangedRequest{
Index: 3,
Instance: "instance-id",
CellID: "id-of-cell",
Ready: true,
}, logger)
Expect(err).NotTo(HaveOccurred())
Expect(callCount).To(Equal(1))
})
})

Describe("Error conditions", func() {
Context("when the request couldn't be completed", func() {
BeforeEach(func() {
Expand All @@ -123,6 +157,14 @@ var _ = Describe("CC Client", func() {
Expect(err).To(HaveOccurred())
Expect(err).To(BeAssignableToTypeOf(&url.Error{}))
})

It("percolates errors calling app readiness changed", func() {
err := ccClient.AppReadinessChanged(guid, cc_messages.AppReadinessChangedRequest{
Index: 1,
}, logger)
Expect(err).To(HaveOccurred())
Expect(err).To(BeAssignableToTypeOf(&url.Error{}))
})
})

Context("when the crashed response code is not StatusOK (200)", func() {
Expand Down Expand Up @@ -164,6 +206,26 @@ var _ = Describe("CC Client", func() {
Expect(err.(*cc_client.BadResponseError).StatusCode).To(Equal(500))
})
})

Context("when the readiness changed response code is not StatusOK (200)", func() {
BeforeEach(func() {
fakeCC.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/internal/v4/apps/"+guid+"/readiness_changed"),
ghttp.RespondWith(500, `{}`),
),
)
})

It("returns an error with the actual status code", func() {
err := ccClient.AppReadinessChanged(guid, cc_messages.AppReadinessChangedRequest{
Index: 1,
}, logger)
Expect(err).To(HaveOccurred())
Expect(err).To(BeAssignableToTypeOf(&cc_client.BadResponseError{}))
Expect(err.(*cc_client.BadResponseError).StatusCode).To(Equal(500))
})
})
})

})
78 changes: 78 additions & 0 deletions cc_client/fakes/fake_cc_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module code.cloudfoundry.org/tps
go 1.20

require (
code.cloudfoundry.org/bbs v0.0.0-20230406145249-41bd09f9f0ca
code.cloudfoundry.org/bbs v0.0.0-20231204223625-b4a8e8d97ad9
code.cloudfoundry.org/clock v1.1.0
code.cloudfoundry.org/debugserver v0.0.0-20230329140605-8c21649a9a42
code.cloudfoundry.org/lager/v3 v3.0.3
code.cloudfoundry.org/localip v0.0.0-20230406154046-f137f65d303d
code.cloudfoundry.org/locket v0.0.0-20230406154009-5e8522d975d2
code.cloudfoundry.org/runtimeschema v0.0.0-20230323223330-5366865eed76
code.cloudfoundry.org/runtimeschema v0.0.0-20231214123546-c95bb31afd3e
code.cloudfoundry.org/workpool v0.0.0-20230406174608-2e26d5d93731
github.com/cloudfoundry/dropsonde v1.1.0
github.com/lib/pq v1.10.9
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
code.cloudfoundry.org/bbs v0.0.0-20230406145249-41bd09f9f0ca h1:SYbT/cML0wx30J368+HaSjyoL96QbXaNJslR+0r3dw0=
code.cloudfoundry.org/bbs v0.0.0-20230406145249-41bd09f9f0ca/go.mod h1:XKlGVVXFi5EcHHMPzw3xgONK9PeEZuUbIC43XNwxD10=
code.cloudfoundry.org/bbs v0.0.0-20231204223625-b4a8e8d97ad9 h1:4smYQWmuzWZhmMoX2vRlGJYcVEckJxzHPvtEoXKAS60=
code.cloudfoundry.org/bbs v0.0.0-20231204223625-b4a8e8d97ad9/go.mod h1:XKlGVVXFi5EcHHMPzw3xgONK9PeEZuUbIC43XNwxD10=
code.cloudfoundry.org/cfhttp/v2 v2.0.1-0.20210513172332-4c5ee488a657 h1:8rnhkeAe8Bnx+8r3unO++S3syBw8P22qPbw3LLFWEoc=
code.cloudfoundry.org/cfhttp/v2 v2.0.1-0.20210513172332-4c5ee488a657/go.mod h1:Fwt0o/haXfwgOHMom4AM96pXCVw9EAiIcSsPb8hWK9s=
code.cloudfoundry.org/clock v1.1.0 h1:XLzC6W3Ah/Y7ht1rmZ6+QfPdt1iGWEAAtIZXgiaj57c=
Expand All @@ -24,8 +24,8 @@ code.cloudfoundry.org/localip v0.0.0-20230406154046-f137f65d303d h1:UKggh2CVl4b5
code.cloudfoundry.org/localip v0.0.0-20230406154046-f137f65d303d/go.mod h1:7CQS2p5BMdlH24kxSUdOUUXYTGHD6N3ikfnerNL77HI=
code.cloudfoundry.org/locket v0.0.0-20230406154009-5e8522d975d2 h1:tUpafuylA0aTl6sdQnf20OsO/ipCvdNSZlWOB1cHhz8=
code.cloudfoundry.org/locket v0.0.0-20230406154009-5e8522d975d2/go.mod h1:AwHLRkdXtttLXNB8RHgLfErJ2kKafH62AR2OClhy6xI=
code.cloudfoundry.org/runtimeschema v0.0.0-20230323223330-5366865eed76 h1:eUDbHh3nW7UAIsYdoShW/KccCbNUshYPlP4FcBd4sNM=
code.cloudfoundry.org/runtimeschema v0.0.0-20230323223330-5366865eed76/go.mod h1:Fk8Yr7+89rZ+FLaG4zNzqqvTrOYyYY7P4QeeiCv+fnk=
code.cloudfoundry.org/runtimeschema v0.0.0-20231214123546-c95bb31afd3e h1:6hOzMD7567RcIQCnVOMCVjYxsVQU49tNCN35M+Q2iqs=
code.cloudfoundry.org/runtimeschema v0.0.0-20231214123546-c95bb31afd3e/go.mod h1:Fk8Yr7+89rZ+FLaG4zNzqqvTrOYyYY7P4QeeiCv+fnk=
code.cloudfoundry.org/tlsconfig v0.0.0-20200131000646-bbe0f8da39b3/go.mod h1:eTbFJpyXRGuFVyg5+oaj9B2eIbIc+0/kZjH8ftbtdew=
code.cloudfoundry.org/tlsconfig v0.0.0-20230320190829-8f91c367795b h1:FjTuGbVBKeaSyvW7WEATlIFCyb0uCpaiuTSaMQXjUyY=
code.cloudfoundry.org/tlsconfig v0.0.0-20230320190829-8f91c367795b/go.mod h1:C8SxvGRSutmgzV2FxH8Zwqz2Q8HsaAITQRQFKhlDzPw=
Expand Down
1 change: 1 addition & 0 deletions vendor/code.cloudfoundry.org/bbs/CODEOWNERS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eaf84cb

Please sign in to comment.