-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from szuecs/refactor-controller
Refactor watch and provider logic
- Loading branch information
Showing
12 changed files
with
896 additions
and
710 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"github.com/szuecs/kube-static-egress-controller/provider" | ||
) | ||
|
||
// EgressController is the controller for creating Egress configuration via a | ||
// provider. | ||
type EgressController struct { | ||
interval time.Duration | ||
configsChan <-chan provider.EgressConfig | ||
configsCache map[provider.Resource]map[string]struct{} | ||
provider provider.Provider | ||
} | ||
|
||
// NewEgressController initializes a new EgressController. | ||
func NewEgressController(prov provider.Provider, configsChan <-chan provider.EgressConfig, interval time.Duration) *EgressController { | ||
return &EgressController{ | ||
interval: interval, | ||
configsChan: configsChan, | ||
provider: prov, | ||
configsCache: make(map[provider.Resource]map[string]struct{}), | ||
} | ||
} | ||
|
||
// Run runs the EgressController main loop. | ||
func (c *EgressController) Run(ctx context.Context) { | ||
log.Info("Running controller") | ||
interval := c.interval | ||
for { | ||
select { | ||
case <-time.After(interval): | ||
err := c.provider.Ensure(c.configsCache) | ||
if err != nil { | ||
log.Errorf("Failed to ensure configuration: %v", err) | ||
continue | ||
} | ||
interval = c.interval | ||
case config := <-c.configsChan: | ||
if len(config.IPAddresses) == 0 { | ||
delete(c.configsCache, config.Resource) | ||
} else { | ||
log.Infof("Observed IP Addresses %v for %v", config.IPAddresses, config.Resource) | ||
c.configsCache[config.Resource] = config.IPAddresses | ||
} | ||
|
||
err := c.provider.Ensure(c.configsCache) | ||
if err != nil { | ||
log.Errorf("Failed to ensure configuration: %v", err) | ||
continue | ||
} | ||
interval = c.interval | ||
case <-ctx.Done(): | ||
log.Info("Terminating controller loop.") | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/szuecs/kube-static-egress-controller/provider" | ||
"github.com/szuecs/kube-static-egress-controller/provider/noop" | ||
) | ||
|
||
func TestControllerRun(t *testing.T) { | ||
prov := noop.NewNoopProvider() | ||
configsChan := make(chan provider.EgressConfig) | ||
controller := NewEgressController(prov, configsChan, 0) | ||
|
||
// test adding the an egress config. | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
go controller.Run(ctx) | ||
|
||
configsChan <- provider.EgressConfig{ | ||
Resource: provider.Resource{ | ||
Name: "a", | ||
Namespace: "x", | ||
}, | ||
IPAddresses: map[string]struct{}{ | ||
"10.0.0.1": struct{}{}, | ||
}, | ||
} | ||
|
||
cancel() | ||
require.Len(t, controller.configsCache, 1) | ||
require.Contains(t, controller.configsCache, provider.Resource{ | ||
Name: "a", | ||
Namespace: "x", | ||
}) | ||
|
||
// test removing the config | ||
ctx, cancel = context.WithCancel(context.Background()) | ||
go controller.Run(ctx) | ||
|
||
configsChan <- provider.EgressConfig{ | ||
Resource: provider.Resource{ | ||
Name: "a", | ||
Namespace: "x", | ||
}, | ||
} | ||
|
||
cancel() | ||
require.Len(t, controller.configsCache, 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.