@@ -4,13 +4,16 @@ import ( |
4 | 4 | "errors" |
5 | 5 | "fmt" |
6 | 6 | "io" |
| 7 | + "os" |
| 8 | + "slices" |
7 | 9 | "strconv" |
8 | 10 | "strings" |
9 | 11 |
|
10 | 12 | "github.com/AlecAivazis/survey/v2" |
11 | 13 | "github.com/google/go-containerregistry/pkg/name" |
12 | 14 | "github.com/ory/viper" |
13 | 15 | "github.com/spf13/cobra" |
| 16 | + "github.com/spf13/pflag" |
14 | 17 | "k8s.io/apimachinery/pkg/api/resource" |
15 | 18 | "knative.dev/client/pkg/util" |
16 | 19 | "knative.dev/func/cmd/common" |
@@ -81,6 +84,18 @@ DESCRIPTION |
81 | 84 | eliminating the need for a local container engine. To trigger deployment |
82 | 85 | of a git repository instead of local source, combine with '--source': |
83 | 86 | '{{rootCmdUse}} deploy --remote --source=git.example.com/alice/f.git' |
| 87 | + A branch, tag or commit is given with '--revision': |
| 88 | + '{{rootCmdUse}} deploy --remote --source=git.example.com/alice/f.git --revision=v1.2.0' |
| 89 | + The function is then read from the repository, so no local copy is |
| 90 | + needed, and nothing is written locally: to change the function, clone |
| 91 | + the repository, edit it and deploy the working tree. Choose the |
| 92 | + directory within the repository with '--source-dir'. The function is |
| 93 | + deployed as configured by the func.yaml committed there: of the flags |
| 94 | + which configure a function, only '--builder', '--registry', |
| 95 | + '--registry-insecure' and '--namespace' apply, the latter only if the |
| 96 | + func.yaml names no other namespace. A repository set in the local |
| 97 | + func.yaml (build.source) instead of with '--source' is built with the |
| 98 | + local function's settings. |
84 | 99 |
|
85 | 100 | Domain |
86 | 101 | When deploying, a function's route is automatically generated using the |
@@ -268,53 +283,55 @@ func runDeploy(cmd *cobra.Command, newClient ClientFactory) (err error) { |
268 | 283 | // Initialize config first |
269 | 284 | cfg = newDeployConfig(cmd) |
270 | 285 |
|
271 | | - // Create function object to check if initialized |
272 | | - if f, err = fn.NewFunction(cfg.Path); err != nil { |
273 | | - return |
274 | | - } |
| 286 | + // Only a repository given as a flag or environment variable deploys by |
| 287 | + // reference. One in the local func.yaml (build.source) is built with the |
| 288 | + // local function's settings, as before. |
| 289 | + if cfg.Remote && cfg.Source != "" && flagGiven(cmd, "source") { |
| 290 | + // A function deployed by reference: read from the repository, never |
| 291 | + // from nor to the current directory, so its Root is empty. |
| 292 | + if f, cfg, err = gitFunction(cmd, cfg); err != nil { |
| 293 | + return |
| 294 | + } |
| 295 | + } else { |
| 296 | + // Create function object to check if initialized |
| 297 | + if f, err = fn.NewFunction(cfg.Path); err != nil { |
| 298 | + return |
| 299 | + } |
275 | 300 |
|
276 | | - // Check if function exists BEFORE prompting for config |
277 | | - if !f.Initialized() { |
278 | | - if !cfg.Remote || f.Build.Source.URL == "" { |
279 | | - // Only error if this is not a fully remote build |
| 301 | + // Check if function exists BEFORE prompting for config |
| 302 | + if !f.Initialized() { |
280 | 303 | return NewErrNotInitializedFromPath(f.Root, "deploy") |
281 | | - } else { |
282 | | - // TODO: this case is not supported because the pipeline |
283 | | - // implementation requires the function's name, which is in the |
284 | | - // remote repository. We should inspect the remote repository. |
285 | | - // For now, give a more helpful error. |
286 | | - return errors.New("please ensure the function's source is also available locally") |
287 | 304 | } |
288 | | - } |
289 | 305 |
|
290 | | - // Now that we know function exists, proceed with prompting |
291 | | - if cfg, err = cfg.Prompt(); err != nil { |
292 | | - if errors.Is(err, fn.ErrRegistryRequired) { |
293 | | - return NewErrRegistryRequired(err, "deploy") |
| 306 | + // Now that we know function exists, proceed with prompting |
| 307 | + if cfg, err = cfg.Prompt(); err != nil { |
| 308 | + if errors.Is(err, fn.ErrRegistryRequired) { |
| 309 | + return NewErrRegistryRequired(err, "deploy") |
| 310 | + } |
| 311 | + return |
| 312 | + } |
| 313 | + if err = cfg.Validate(cmd); err != nil { |
| 314 | + return wrapValidateError(err, "deploy") |
294 | 315 | } |
295 | | - return |
296 | | - } |
297 | | - if err = cfg.Validate(cmd); err != nil { |
298 | | - return wrapValidateError(err, "deploy") |
299 | | - } |
300 | 316 |
|
301 | | - // Warn if registry changed but registryInsecure is still true |
302 | | - warnRegistryInsecureChange(cmd.OutOrStderr(), cfg.Registry, f) |
| 317 | + // Warn if registry changed but registryInsecure is still true |
| 318 | + warnRegistryInsecureChange(cmd.OutOrStderr(), cfg.Registry, f) |
303 | 319 |
|
304 | | - // Warn if expose flag is used with deployer where it has no effect |
305 | | - warnExposeIgnore(cmd.OutOrStderr(), cfg.Expose, cfg.Deployer) |
| 320 | + // Warn if expose flag is used with deployer where it has no effect |
| 321 | + warnExposeIgnore(cmd.OutOrStderr(), cfg.Expose, cfg.Deployer) |
306 | 322 |
|
307 | | - // Warn if scale.kpa is set with a deployer that ignores it |
308 | | - warnScaleKpaIgnore(cmd.OutOrStderr(), f.Scale, cfg.Deployer) |
| 323 | + // Warn if scale.kpa is set with a deployer that ignores it |
| 324 | + warnScaleKpaIgnore(cmd.OutOrStderr(), f.Scale, cfg.Deployer) |
309 | 325 |
|
310 | | - // Back-compat: a function deployed before the deployer was recorded has a |
311 | | - // namespace but no deployer, which historically could only mean knative. |
312 | | - if f.Deploy.Namespace != "" && f.Deploy.Deployer == "" { |
313 | | - f.Deploy.Deployer = deployers.Knative |
314 | | - } |
| 326 | + // Back-compat: a function deployed before the deployer was recorded has a |
| 327 | + // namespace but no deployer, which historically could only mean knative. |
| 328 | + if f.Deploy.Namespace != "" && f.Deploy.Deployer == "" { |
| 329 | + f.Deploy.Deployer = deployers.Knative |
| 330 | + } |
315 | 331 |
|
316 | | - if f, err = cfg.Configure(f); err != nil { // Updates f with deploy cfg |
317 | | - return |
| 332 | + if f, err = cfg.Configure(f); err != nil { // Updates f with deploy cfg |
| 333 | + return |
| 334 | + } |
318 | 335 | } |
319 | 336 | if err = f.Validate(); err != nil { |
320 | 337 | return |
@@ -441,6 +458,12 @@ func runDeploy(cmd *cobra.Command, newClient ClientFactory) (err error) { |
441 | 458 | } |
442 | 459 | } |
443 | 460 |
|
| 461 | + // A function deployed by reference is not written: to change it, clone |
| 462 | + // the repository, edit it and deploy the working tree. |
| 463 | + if f.Root == "" { |
| 464 | + return nil |
| 465 | + } |
| 466 | + |
444 | 467 | // Write |
445 | 468 | if err = f.Write(); err != nil { |
446 | 469 | return |
@@ -938,7 +961,8 @@ func printDeployMessages(out io.Writer, f fn.Function) { |
938 | 961 | // ------------------- |
939 | 962 | // When doing a remote build with --revision, warn if the local branch |
940 | 963 | // doesn't match, as this can lead to confusion about which func.yaml is used. |
941 | | - if f.Local.Remote && f.Build.Source.URL != "" && f.Build.Source.Revision != "" { |
| 964 | + // A function read from the repository (no Root) has no local func.yaml. |
| 965 | + if f.Root != "" && f.Local.Remote && f.Build.Source.URL != "" && f.Build.Source.Revision != "" { |
942 | 966 | // Doing a remote build, specified a git repository to pull from, and |
943 | 967 | // specified a reference within that remote. |
944 | 968 | currentBranch, err := common.DefaultCurrentBranch(f.Root) |
@@ -984,3 +1008,107 @@ func warnScaleKpaIgnore(w io.Writer, scale *fn.ScaleOptions, deployer string) { |
984 | 1008 | "supports kpa (concurrency/rps) autoscaling.\n") |
985 | 1009 | } |
986 | 1010 | } |
| 1011 | + |
| 1012 | +// gitFlags are the only flags a deployment from a git repository takes: |
| 1013 | +// those which locate the function, those which the pipeline alone uses or |
| 1014 | +// which depend on where the function is deployed, and those which are not |
| 1015 | +// its settings. The function is otherwise deployed as configured by the |
| 1016 | +// func.yaml committed there. |
| 1017 | +var gitFlags = []string{"remote", "source", "revision", "source-dir", "builder", |
| 1018 | + "registry", "registry-insecure", "registry-authfile", "namespace", "username", |
| 1019 | + "password", "token", "verbose"} |
| 1020 | + |
| 1021 | +// gitFunction returns the function committed to the git repository cfg |
| 1022 | +// names, for a remote pipeline which builds the commit it was read at, and |
| 1023 | +// cfg configured for it. The function is read into memory and has no Root. |
| 1024 | +// Settings it leaves empty default as for any function, never to those of a |
| 1025 | +// function in the current directory. |
| 1026 | +func gitFunction(cmd *cobra.Command, cfg deployConfig) (f fn.Function, _ deployConfig, err error) { |
| 1027 | + var extra []string |
| 1028 | + cmd.Flags().VisitAll(func(fl *pflag.Flag) { |
| 1029 | + if flagGiven(cmd, fl.Name) && !slices.Contains(gitFlags, fl.Name) { |
| 1030 | + extra = append(extra, "--"+fl.Name) |
| 1031 | + } |
| 1032 | + }) |
| 1033 | + if len(extra) > 0 { |
| 1034 | + return f, cfg, fmt.Errorf("%s cannot be used with --source: the function is deployed as "+ |
| 1035 | + "configured by the func.yaml in the repository. To change it, clone the "+ |
| 1036 | + "repository, edit it and deploy the working tree", strings.Join(extra, ", ")) |
| 1037 | + } |
| 1038 | + |
| 1039 | + // The revision may also be given as the URL's fragment (<url>#<revision>), |
| 1040 | + // which then wins, as it does in Configure. |
| 1041 | + source := fn.Source{URL: cfg.Source, Revision: cfg.Revision, Dir: cfg.SourceDir} |
| 1042 | + if parts := strings.SplitN(cfg.Source, "#", 2); len(parts) == 2 { |
| 1043 | + source.URL, source.Revision = parts[0], parts[1] |
| 1044 | + } |
| 1045 | + if f, err = fn.NewFunctionFromGit(cmd.Context(), source); err != nil { |
| 1046 | + return f, cfg, err |
| 1047 | + } |
| 1048 | + f.Local.Remote = true |
| 1049 | + |
| 1050 | + global, err := config.NewDefault() |
| 1051 | + if err != nil { |
| 1052 | + return f, cfg, err |
| 1053 | + } |
| 1054 | + if flagGiven(cmd, "builder") { |
| 1055 | + f.Build.Builder = cfg.Builder |
| 1056 | + } |
| 1057 | + if f.Build.Builder == "" { |
| 1058 | + f.Build.Builder = global.Builder |
| 1059 | + } |
| 1060 | + if flagGiven(cmd, "registry") { |
| 1061 | + f.Registry = cfg.Registry |
| 1062 | + } |
| 1063 | + if f.Registry == "" { |
| 1064 | + f.Registry = global.Registry |
| 1065 | + } |
| 1066 | + if flagGiven(cmd, "registry-insecure") { |
| 1067 | + f.RegistryInsecure = cfg.RegistryInsecure |
| 1068 | + } |
| 1069 | + // The cluster deploys into the namespace the func.yaml names, and only |
| 1070 | + // without one into the namespace the pipeline runs in. |
| 1071 | + if flagGiven(cmd, "namespace") { |
| 1072 | + committed := f.Namespace |
| 1073 | + if committed == "" { |
| 1074 | + committed = f.Deploy.Namespace |
| 1075 | + } |
| 1076 | + if committed != "" && committed != cfg.Namespace { |
| 1077 | + return f, cfg, fmt.Errorf("--namespace %q conflicts with the namespace %q in the "+ |
| 1078 | + "repository's func.yaml, which the function is deployed into", cfg.Namespace, committed) |
| 1079 | + } |
| 1080 | + f.Namespace = cfg.Namespace |
| 1081 | + } else { |
| 1082 | + f.Namespace = defaultNamespace(f, cfg.Verbose) |
| 1083 | + } |
| 1084 | + |
| 1085 | + // Check the namespace and domain the function is deployed with, as a |
| 1086 | + // local deployment checks them in deployConfig.Validate. Function.Validate |
| 1087 | + // checks the rest. |
| 1088 | + if f.Namespace != "" && utils.ValidateNamespace(f.Namespace) != nil { |
| 1089 | + return f, cfg, wrapValidateError(fn.ErrInvalidNamespace, "deploy") |
| 1090 | + } |
| 1091 | + if f.Domain != "" && utils.ValidateDomain(f.Domain) != nil { |
| 1092 | + return f, cfg, wrapValidateError(fn.ErrInvalidDomain, "deploy") |
| 1093 | + } |
| 1094 | + |
| 1095 | + // The client is configured by cfg, whose defaults came from the current |
| 1096 | + // directory. The deployer is the one the cluster deploys with: the |
| 1097 | + // function's, else the one it was last deployed with, else the default. |
| 1098 | + deployer := f.Deployer |
| 1099 | + if deployer == "" { |
| 1100 | + deployer = f.Deploy.Deployer |
| 1101 | + } |
| 1102 | + if deployer == "" { |
| 1103 | + deployer = deployers.Default |
| 1104 | + } |
| 1105 | + cfg.Builder, cfg.Registry, cfg.RegistryInsecure, cfg.Deployer = f.Build.Builder, f.Registry, f.RegistryInsecure, deployer |
| 1106 | + return f, cfg, nil |
| 1107 | +} |
| 1108 | + |
| 1109 | +// flagGiven reports whether the flag was set on the command line or with its |
| 1110 | +// environment variable, as opposed to taking its default. |
| 1111 | +func flagGiven(cmd *cobra.Command, flag string) bool { |
| 1112 | + env := "FUNC_" + strings.ToUpper(strings.ReplaceAll(flag, "-", "_")) |
| 1113 | + return cmd.Flags().Changed(flag) || os.Getenv(env) != "" |
| 1114 | +} |