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

Add vendoring to Google Cloud Functions again #21070

Merged
merged 4 commits into from
Sep 14, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ https:/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

- Fix timeout option of GCP functions. {issue}16282[16282] {pull}16287[16287]
- Do not need Google credentials if not required for the operation. {issue}17329[17329] {pull}21072[21072]
- Fix dependency issues of GCP functions. {issue}20830[20830] {pull}21070[21070]

==== Added

Expand Down
19 changes: 19 additions & 0 deletions dev-tools/mage/gotool/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ func ListDeps(pkg string) ([]string, error) {
return getLines(callGo(nil, "list", "-deps", "-f", tmpl, pkg))
}

// ListDepsLocation calls `go list -dep` for every package spec given.
func ListDepsLocation(pkg string) (map[string]string, error) {
const tmpl = `{{if not .Standard}}{{.ImportPath}};{{.Dir}}{{end}}`

lines, err := getLines(callGo(nil, "list", "-deps", "-f", tmpl, pkg))
if err != nil {
return nil, err
}
deps := make(map[string]string, len(lines))
for _, l := range lines {
parts := strings.Split(l, ";")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid number of parts")
}
deps[parts[0]] = parts[1]
}
return deps, nil
}

// ListTestFiles lists all go and cgo test files available in a package.
func ListTestFiles(pkg string) ([]string, error) {
const tmpl = `{{ range .TestGoFiles }}{{ printf "%s\n" . }}{{ end }}` +
Expand Down
6 changes: 6 additions & 0 deletions x-pack/functionbeat/dev-tools/packaging/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ shared:
pkg/functionbeat-aws:
source: 'provider/aws/build/golang-crossbuild/aws-linux-amd64'
mode: 0755
pkg/pubsub/vendor:
source: 'provider/gcp/build/pubsub/vendor'
mode: 0644
pkg/storage/vendor:
source: 'provider/gcp/build/storage/vendor'
mode: 0644
pkg/pubsub/pubsub.go:
source: 'provider/gcp/pubsub/pubsub.go'
mode: 0655
Expand Down
8 changes: 5 additions & 3 deletions x-pack/functionbeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ func BuildPkgForFunctions() error {
err := os.RemoveAll("pkg")

filesToCopy := map[string]string{
filepath.Join("provider", "aws", "functionbeat-aws"): filepath.Join("pkg", "functionbeat-aws"),
filepath.Join("provider", "gcp", "pubsub", "pubsub.go"): filepath.Join("pkg", "pubsub", "pubsub.go"),
filepath.Join("provider", "gcp", "storage", "storage.go"): filepath.Join("pkg", "storage", "storage.go"),
filepath.Join("provider", "aws", "functionbeat-aws"): filepath.Join("pkg", "functionbeat-aws"),
filepath.Join("provider", "gcp", "pubsub", "pubsub.go"): filepath.Join("pkg", "pubsub", "pubsub.go"),
filepath.Join("provider", "gcp", "storage", "storage.go"): filepath.Join("pkg", "storage", "storage.go"),
filepath.Join("provider", "gcp", "build", "pubsub", "vendor"): filepath.Join("pkg", "pubsub", "vendor"),
filepath.Join("provider", "gcp", "build", "storage", "vendor"): filepath.Join("pkg", "storage", "vendor"),
}
for src, dest := range filesToCopy {
c := &devtools.CopyTask{
Expand Down
3 changes: 2 additions & 1 deletion x-pack/functionbeat/manager/gcp/template_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,6 @@ func zipResources() map[string][]bundle.Resource {

func zipResourcesOfFunc(typeName string) []bundle.Resource {
root := filepath.Join("pkg", typeName)
return []bundle.Resource{&bundle.LocalFile{Path: filepath.Join(root, typeName+".go"), FileMode: 0755}}
vendor := bundle.Folder(filepath.Join("pkg", typeName, "vendor"), filepath.Join("pkg", typeName), 0644)
return append(vendor, &bundle.LocalFile{Path: filepath.Join(root, typeName+".go"), FileMode: 0755})
}
42 changes: 41 additions & 1 deletion x-pack/functionbeat/scripts/mage/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
package mage

import (
"os"
"path/filepath"

"github.com/magefile/mage/mg"

devtools "github.com/elastic/beats/v7/dev-tools/mage"
"github.com/elastic/beats/v7/dev-tools/mage/gotool"
)

// Update target namespace.
Expand All @@ -20,7 +24,7 @@ var Aliases = map[string]interface{}{

// All updates all generated content.
func (Update) All() {
mg.Deps(Update.Fields, Update.IncludeFields, Update.Config, Update.FieldDocs)
mg.Deps(Update.Fields, Update.IncludeFields, Update.Config, Update.FieldDocs, Update.VendorBeats)
}

// Config generates both the short and reference configs.
Expand All @@ -46,3 +50,39 @@ func (Update) IncludeFields() error {

return devtools.GenerateAllInOneFieldsGo()
}

// VendorBeats collects the vendor folder required to deploy the function for GCP.
func (Update) VendorBeats() error {
for _, f := range []string{"pubsub", "storage"} {
gcpVendorPath := filepath.Join("provider", "gcp", "build", f, "vendor")
err := os.RemoveAll(gcpVendorPath)
if err != nil {
return err
}

deps, err := gotool.ListDepsLocation("github.com/elastic/beats/v7/x-pack/functionbeat/provider/gcp/" + f)
if err != nil {
return err
}

for importPath, location := range deps {
cp := &devtools.CopyTask{
Source: location,
Dest: filepath.Join(gcpVendorPath, importPath),
Mode: 0600,
DirMode: os.ModeDir | 0750,
Exclude: []string{
".*_test.go$",
".*.yml",
},
}
err = cp.Execute()
if err != nil {
return err
}
}

}

return nil
}