From fe0097e905b8ff221ed6efb0628f82a12508ceba Mon Sep 17 00:00:00 2001 From: Han Qiao Date: Wed, 16 Oct 2024 08:31:23 +0800 Subject: [PATCH] fix: skip upserting disabled function (#2770) * chore: refactor function enabled check * fix: skip upserting disabled function --- internal/functions/deploy/deploy.go | 24 +++++------------------- internal/functions/serve/serve.go | 9 +++++---- pkg/config/config.go | 5 +++++ pkg/function/batch.go | 4 ++++ 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/internal/functions/deploy/deploy.go b/internal/functions/deploy/deploy.go index fbed1313c..25ad3372a 100644 --- a/internal/functions/deploy/deploy.go +++ b/internal/functions/deploy/deploy.go @@ -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 { @@ -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)) @@ -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) { @@ -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 -} diff --git a/internal/functions/serve/serve.go b/internal/functions/serve/serve.go index 316f7c4a6..15320171d 100644 --- a/internal/functions/serve/serve.go +++ b/internal/functions/serve/serve.go @@ -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 diff --git a/pkg/config/config.go b/pkg/config/config.go index 5fbd4661f..82b9d0a46 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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) diff --git a/pkg/function/batch.go b/pkg/function/batch.go index 424a8a8cd..cf5a2f161 100644 --- a/pkg/function/batch.go +++ b/pkg/function/batch.go @@ -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