GCF deploy with local dependency

240 views Asked by At

Let's say we have a code structure like this:

v1/
---api/
------pets/
---------create/
------------server.go
------------cmd/
---------------main.go
------------go.mod
------------go.sum
------------.gcloudignore
------------vendor/
---------get/
---------update/
---------delete/
------food/
---------create/
---------get/
---pkg/
------constants/
---------constants.go
------model/
---------pet.go
---------food.go
------store/
---------firestore.go
------validate/
---------header.go
------go.mod
------go.sum

every CRUD function directory (pets/get, food/create, etc) is the same structure as pets/create, so they all contain: server.go, cmd/main.go for testing, go.mod, go.sum, .gcloudignore and vendor/ of v1/pkg

v1/api/pets/create/go.mod contains:

module bitbucket.org/company/project/v1/api/pets/create

go 1.14

replace bitbucket.org/company/project/v1/pkg => ../../../pkg

require (
    bitbucket.org/company/project/v1/pkg v0.0.0-00010101000000-000000000000
    github.com/GoogleCloudPlatform/functions-framework-go v1.1.0
)

v1/api/pets/create/server.go contains method that will be deployed to GCF

.gcloudignore contains go.mod and go.sum to ignore them while deploying

v1/pkg is vendored into every CRUD function/directory just like v1/api/pets/create

PROBLEM: project is huge on production app, it contains (of course) much more methods with much more code, everything lags because those vendor dirs are ~4MB

How do I include v1/pkg into each of these CRUD folders? What is the easiest method you guys use?

WHAT WE TRIED:

  • using private repo instead of local path -> not working while deploying

  • using functions.go in root, with all functions defined there -> whole source code is uploaded for each function, we don't want that obviously

  • using code from /pkg to each function separately (c/p) which is not maintainable and doesn't make any sense to do

  • How can I use a sub-packages with Go on Google Cloud Functions? -> this is not our use case, "helperpackage" should be somewhere upper in the tree

1

There are 1 answers

1
guillaume blaquiere On

There is no magic things that you can do. Something like your second try is the best one, but without using a function.go at the root directory

gcloud functions deploy --source=v1/api/pets/create <function_name> --trigger-http --runtime=go113 --entry-point=<method-name>

Uploading the whole project isn't a real problem because, in Go, only the compiled binary is kept and all the source files are trashed. So, no extra file will stay beside the binary.

However, if you really want to limit the uploaded code, you can use the --ignore-file= parameter