Skip to content

Commit

Permalink
fix: skip upserting disabled function (#2770)
Browse files Browse the repository at this point in the history
* chore: refactor function enabled check

* fix: skip upserting disabled function
  • Loading branch information
sweatybridge authored Oct 16, 2024
1 parent a64e8d0 commit fe0097e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
24 changes: 5 additions & 19 deletions internal/functions/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

func Run(ctx context.Context, slugs []string, projectRef string, noVerifyJWT *bool, importMapPath string, fsys afero.Fs) error {
// Load function config and project id
var skippedFunctions []string
if err := utils.LoadConfigFS(fsys); err != nil {
return err
} else if len(slugs) > 0 {
Expand All @@ -25,12 +24,9 @@ func Run(ctx context.Context, slugs []string, projectRef string, noVerifyJWT *bo
return err
}
}
} else if slugs, skippedFunctions, err = GetFunctionSlugs(fsys); err != nil {
} else if slugs, err = GetFunctionSlugs(fsys); err != nil {
return err
}
if len(skippedFunctions) > 0 {
fmt.Fprintf(utils.GetDebugLogger(), "Skipped deploying the following functions: %s\n", strings.Join(skippedFunctions, ", "))
}
// TODO: require all functions to be deployed from config for v2
if len(slugs) == 0 {
return errors.Errorf("No Functions specified or found in %s", utils.Bold(utils.FunctionsDir))
Expand All @@ -49,23 +45,19 @@ func Run(ctx context.Context, slugs []string, projectRef string, noVerifyJWT *bo
return nil
}

func GetFunctionSlugs(fsys afero.Fs) (slugs []string, disabledSlugs []string, err error) {
func GetFunctionSlugs(fsys afero.Fs) (slugs []string, err error) {
pattern := filepath.Join(utils.FunctionsDir, "*", "index.ts")
paths, err := afero.Glob(fsys, pattern)
if err != nil {
return nil, nil, errors.Errorf("failed to glob function slugs: %w", err)
return nil, errors.Errorf("failed to glob function slugs: %w", err)
}
for _, path := range paths {
slug := filepath.Base(filepath.Dir(path))
if utils.FuncSlugPattern.MatchString(slug) {
if isFunctionEnabled(slug) {
slugs = append(slugs, slug)
} else {
disabledSlugs = append(disabledSlugs, slug)
}
slugs = append(slugs, slug)
}
}
return slugs, disabledSlugs, nil
return slugs, nil
}

func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool, fsys afero.Fs) (config.FunctionConfig, error) {
Expand Down Expand Up @@ -100,9 +92,3 @@ func GetFunctionConfig(slugs []string, importMapPath string, noVerifyJWT *bool,
}
return functionConfig, nil
}

func isFunctionEnabled(slug string) bool {
functionConfig := utils.Config.Functions[slug]
// If the function config Enabled is not defined, or defined and set to true
return functionConfig.Enabled == nil || *functionConfig.Enabled
}
9 changes: 5 additions & 4 deletions internal/functions/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,20 @@ func parseEnvFile(envFilePath string, fsys afero.Fs) ([]string, error) {
}

func populatePerFunctionConfigs(cwd, importMapPath string, noVerifyJWT *bool, fsys afero.Fs) ([]string, string, error) {
slugs, skippedFunctions, err := deploy.GetFunctionSlugs(fsys)
slugs, err := deploy.GetFunctionSlugs(fsys)
if err != nil {
return nil, "", err
}
if len(skippedFunctions) > 0 {
fmt.Fprintf(utils.GetDebugLogger(), "Skipped serving the following functions: %s\n", strings.Join(skippedFunctions, ", "))
}
functionsConfig, err := deploy.GetFunctionConfig(slugs, importMapPath, noVerifyJWT, fsys)
if err != nil {
return nil, "", err
}
binds := []string{}
for slug, fc := range functionsConfig {
if !fc.IsEnabled() {
fmt.Fprintln(os.Stderr, "Skipped serving Function:", slug)
continue
}
modules, err := deploy.GetBindMounts(cwd, utils.FunctionsDir, "", fc.Entrypoint, fc.ImportMap, fsys)
if err != nil {
return nil, "", err
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,11 @@ type (
}
)

func (f function) IsEnabled() bool {
// If Enabled is not defined, or defined and set to true
return f.Enabled == nil || *f.Enabled
}

func (c *baseConfig) Clone() baseConfig {
copy := *c
copy.Storage.Buckets = maps.Clone(c.Storage.Buckets)
Expand Down
4 changes: 4 additions & 0 deletions pkg/function/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (s *EdgeRuntimeAPI) UpsertFunctions(ctx context.Context, functionConfig con
exists[f.Slug] = struct{}{}
}
for slug, function := range functionConfig {
if !function.IsEnabled() {
fmt.Fprintln(os.Stderr, "Skipped deploying Function:", slug)
continue
}
for _, keep := range filter {
if !keep(slug) {
continue
Expand Down

0 comments on commit fe0097e

Please # to comment.