1
0
mirror of https://github.com/ko-build/ko.git synced 2025-07-15 23:54:17 +02:00

Remove --watch mode (#585)

This commit is contained in:
Jason Hall
2022-03-03 14:58:34 -05:00
committed by GitHub
parent 967e3ebb2d
commit cd41b3e714
15 changed files with 1 additions and 1078 deletions

View File

@ -19,25 +19,13 @@ import (
"os"
"path/filepath"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
)
const deprecation412 = `NOTICE!
-----------------------------------------------------------------
Watch mode is deprecated and unsupported, and will be removed in
a future release.
For more information see:
https://github.com/google/ko/issues/412
-----------------------------------------------------------------
`
// FilenameOptions is from pkg/kubectl.
type FilenameOptions struct {
Filenames []string
Recursive bool
Watch bool
}
func AddFileArg(cmd *cobra.Command, fo *FilenameOptions) {
@ -46,8 +34,6 @@ func AddFileArg(cmd *cobra.Command, fo *FilenameOptions) {
"Filename, directory, or URL to files to use to create the resource")
cmd.Flags().BoolVarP(&fo.Recursive, "recursive", "R", fo.Recursive,
"Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory.")
cmd.Flags().BoolVarP(&fo.Watch, "watch", "W", fo.Watch,
"Continuously monitor the transitive dependencies of the passed yaml files, and redeploy whenever anything changes. (DEPRECATED)")
}
// Based heavily on pkg/kubectl
@ -56,19 +42,6 @@ func EnumerateFiles(fo *FilenameOptions) chan string {
go func() {
// When we're done enumerating files, close the channel
defer close(files)
// When we are in --watch mode, we set up watches on the filesystem locations
// that we are supplied and continuously stream files, until we are sent an
// interrupt.
var watcher *fsnotify.Watcher
if fo.Watch {
log.Print(deprecation412)
var err error
watcher, err = fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Unexpected error initializing fsnotify: %v", err)
}
defer watcher.Close()
}
for _, paths := range fo.Filenames {
// Just pass through '-' as it is indicative of stdin.
if paths == "-" {
@ -90,9 +63,6 @@ func EnumerateFiles(fo *FilenameOptions) chan string {
if path != paths && !fo.Recursive {
return filepath.SkipDir
}
if watcher != nil {
watcher.Add(path)
}
// We don't stream back directories, we just decide to skip them, or not.
return nil
}
@ -105,14 +75,6 @@ func EnumerateFiles(fo *FilenameOptions) chan string {
default:
return nil
}
// We weren't passed this explicitly, so elide the watch as we
// are already watching the directory.
} else {
// We were passed this directly, and so we may not be watching the
// directory, so watch this file explicitly.
if watcher != nil {
watcher.Add(path)
}
}
files <- path
@ -122,23 +84,6 @@ func EnumerateFiles(fo *FilenameOptions) chan string {
log.Fatalf("Error enumerating files: %v", err)
}
}
// We're done watching the files we were passed and setting up watches.
// Now listen for change events from the watches we set up and resend
// files that change as if we just saw them (so they can be reprocessed).
if watcher != nil {
for {
select {
case event := <-watcher.Events:
switch filepath.Ext(event.Name) {
case ".json", ".yaml":
files <- event.Name
}
case err := <-watcher.Errors:
log.Fatalf("Error watching: %v", err)
}
}
}
}()
return files
}

View File

@ -23,14 +23,12 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"strings"
"sync"
"github.com/google/go-containerregistry/pkg/name"
"github.com/mattmoor/dep-notify/pkg/graph"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v3"
"k8s.io/apimachinery/pkg/labels"
@ -150,9 +148,6 @@ func makeBuilder(ctx context.Context, bo *options.BuildOptions) (*build.Caching,
// - if a valid Build future exists at the time of the request,
// then block on it.
// - if it does not, then initiate and record a Build future.
// - When import paths are "affected" by filesystem changes during a
// Watch, then invalidate their build futures *before* we put the
// affected yaml files onto the channel
//
// This will benefit the following key cases:
// 1. When the same import path is referenced across multiple yaml files
@ -295,38 +290,6 @@ func resolveFilesToWriter(
// This tracks filename -> []importpath
var sm sync.Map
var g graph.Interface
var errCh chan error
var err error
if fo.Watch {
// Start a dep-notify process that on notifications scans the
// file-to-recorded-build map and for each affected file resends
// the filename along the channel.
g, errCh, err = graph.New(func(ss graph.StringSet) {
sm.Range(func(k, v interface{}) bool {
key := k.(string)
value := v.([]string)
for _, ip := range value {
// dep-notify doesn't understand the ko:// prefix
ip := strings.TrimPrefix(ip, build.StrictScheme)
if ss.Has(ip) {
// See the comment above about how "builder" works.
// Always use ko:// for the builder.
builder.Invalidate(build.StrictScheme + ip)
fs <- key
}
}
return true
})
})
if err != nil {
return fmt.Errorf("creating dep-notify graph: %w", err)
}
// Cleanup the fsnotify hooks when we're done.
defer g.Shutdown()
}
// This tracks resolution errors and ensures we cancel other builds if an
// individual build fails.
errs, ctx := errgroup.WithContext(ctx)
@ -376,33 +339,11 @@ func resolveFilesToWriter(
if err != nil {
// This error is sometimes expected during watch mode, so this
// isn't fatal. Just print it and keep the watch open.
err := fmt.Errorf("error processing import paths in %q: %w", f, err)
if fo.Watch {
log.Print(err)
return nil
}
return err
return fmt.Errorf("error processing import paths in %q: %w", f, err)
}
// Associate with this file the collection of binary import paths.
sm.Store(f, recordingBuilder.ImportPaths)
ch <- b
if fo.Watch {
for _, ip := range recordingBuilder.ImportPaths {
// dep-notify doesn't understand the ko:// prefix
ip := strings.TrimPrefix(ip, build.StrictScheme)
// Technically we never remove binary targets from the graph,
// which will increase our graph's watch load, but the
// notifications that they change will result in no affected
// yamls, and no new builds or deploys.
if err := g.Add(ip); err != nil {
// If we're in watch mode, just fail.
err := fmt.Errorf("adding importpath %q to dep graph: %w", ip, err)
errCh <- err
return err
}
}
}
return nil
})
@ -418,9 +359,6 @@ func resolveFilesToWriter(
// be applied.
out.Write(append(b, []byte("\n---\n")...))
}
case err := <-errCh:
return fmt.Errorf("watching dependencies: %w", err)
}
}