Skip to content

Commit

Permalink
properly handle the remove event for the file provider
Browse files Browse the repository at this point in the history
Signed-off-by: shawnh2 <[email protected]>
  • Loading branch information
shawnh2 committed Aug 25, 2024
1 parent ec357fa commit 4052618
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions internal/provider/file/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,20 @@ func newResourcesStore(name string, resources *message.ProviderResources, logger
func (r *resourcesStore) HandleEvent(event fsnotify.Event, files, dirs []string) {
r.logger.Info("receive an event", "name", event.Name, "op", event.Op.String())

Check warning on line 32 in internal/provider/file/store.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/file/store.go#L31-L32

Added lines #L31 - L32 were not covered by tests

// TODO(sh2): Find a better way to process the event. For now,
// it only simply reload all the resources from files and
// directories despite the event type.
if err := r.LoadAndStore(files, dirs); err != nil {
r.logger.Error(err, "error processing resources")
// TODO(sh2): Support multiple GatewayClass.
switch event.Op {
case fsnotify.Write:
if err := r.LoadAndStore(files, dirs); err != nil {
r.logger.Error(err, "failed to load and store resources")

Check warning on line 38 in internal/provider/file/store.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/file/store.go#L35-L38

Added lines #L35 - L38 were not covered by tests
}
case fsnotify.Remove:

Check warning on line 40 in internal/provider/file/store.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/file/store.go#L40

Added line #L40 was not covered by tests
// Under our current assumption, one file only contains one GatewayClass and
// all its other related resources, so we can remove them safely.
r.resources.GatewayAPIResources.Delete(r.name)

Check warning on line 43 in internal/provider/file/store.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/file/store.go#L43

Added line #L43 was not covered by tests
}
}

// LoadAndStore loads and stores resources from files and directories.
// LoadAndStore loads and stores all resources from files and directories.
func (r *resourcesStore) LoadAndStore(files, dirs []string) error {
rs, err := loadFromFilesAndDirs(files, dirs)
if err != nil {
Expand All @@ -53,11 +58,21 @@ func (r *resourcesStore) LoadAndStore(files, dirs []string) error {
// in each provider. The ideal case is two different providers share the same resources process logic.
//
// - This issue is tracked by https://github.com/envoyproxy/gateway/issues/3213
//
// So here we just simply Store each gatewayapi.Resources.
var gwcResources gatewayapi.ControllerResources = rs
r.resources.GatewayAPIResources.Store(r.name, &gwcResources)

// We cannot make sure by the time the Write event was triggerd, whether the GatewayClass exist,

Check failure on line 62 in internal/provider/file/store.go

View workflow job for this annotation

GitHub Actions / lint

triggerd ==> triggered
// so here we just simply Store the first gatewayapi.Resources that has GatewayClass.
gwcResources := make(gatewayapi.ControllerResources, 0, 1)
for _, resource := range rs {
if resource.GatewayClass != nil {
gwcResources = append(gwcResources, resource)

Check warning on line 67 in internal/provider/file/store.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/file/store.go#L64-L67

Added lines #L64 - L67 were not covered by tests
}
}
if len(gwcResources) == 0 {
return nil

Check warning on line 71 in internal/provider/file/store.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/file/store.go#L70-L71

Added lines #L70 - L71 were not covered by tests
}

r.resources.GatewayAPIResources.Store(r.name, &gwcResources)
r.logger.Info("loaded and stored resources successfully")

Check warning on line 75 in internal/provider/file/store.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/file/store.go#L74-L75

Added lines #L74 - L75 were not covered by tests

return nil

Check warning on line 77 in internal/provider/file/store.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/file/store.go#L77

Added line #L77 was not covered by tests
}

0 comments on commit 4052618

Please sign in to comment.