-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
554 additions
and
238 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
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 |
---|---|---|
@@ -1,87 +1,57 @@ | ||
package watcher | ||
|
||
import ( | ||
"github.com/gardener/gardener/pkg/apis/core/v1beta1" | ||
"github.com/pPrecel/cloudagent/internal/gardener" | ||
"github.com/pPrecel/cloudagent/internal/system" | ||
"context" | ||
|
||
"github.com/pPrecel/cloudagent/pkg/agent" | ||
"github.com/pPrecel/cloudagent/pkg/config" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type watcher struct { | ||
options *Options | ||
cache *agent.ServerCache | ||
type cached struct { | ||
logger *logrus.Entry | ||
configPath string | ||
cache agent.ResourceGetter | ||
|
||
getConfig func(string) (*config.Config, error) | ||
notifyChange func(string) (*system.Notifier, error) | ||
getConfig func(string) (*config.Config, error) | ||
} | ||
|
||
func newCached(cache *agent.ServerCache, o *Options) *watcher { | ||
return &watcher{ | ||
options: o, | ||
cache: cache, | ||
getConfig: config.Read, | ||
notifyChange: system.NotifyChange, | ||
func newCached(cache agent.ResourceGetter, logger *logrus.Entry, configPath string) *cached { | ||
return &cached{ | ||
logger: logger, | ||
configPath: configPath, | ||
cache: cache, | ||
getConfig: config.Read, | ||
} | ||
} | ||
|
||
func (w *watcher) start() error { | ||
w.options.Logger.Debug("starting cached watcher") | ||
watcher, err := w.newWatcher() | ||
func (w *cached) start(ctx context.Context) error { | ||
w.logger.Debug("starting cached watcher") | ||
watcher, err := w.newWatcher(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
defer watcher.Stop() | ||
watcher.Start() | ||
|
||
w.options.Logger.Info("starting config notifier") | ||
n, err := w.notifyChange(w.options.ConfigPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer n.Stop() | ||
|
||
select { | ||
case err := <-n.Errors: | ||
return err | ||
case <-n.IsMotified: | ||
return nil | ||
} | ||
<-ctx.Done() | ||
return nil | ||
} | ||
|
||
func (w *watcher) newWatcher() (*agent.Watcher, error) { | ||
w.options.Logger.Infof("reading config from path: '%s'", w.options.ConfigPath) | ||
config, err := w.getConfig(w.options.ConfigPath) | ||
func (w *cached) newWatcher(ctx context.Context) (*agent.Watcher, error) { | ||
w.logger.Infof("reading config from path: '%s'", w.configPath) | ||
config, err := w.getConfig(w.configPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
w.options.Logger.Infof("starting state watcher with spec: '%s'", config.PersistentSpec) | ||
w.logger.Infof("starting state watcher with spec: '%s'", config.PersistentSpec) | ||
return agent.NewWatcher(agent.WatcherOptions{ | ||
Spec: config.PersistentSpec, | ||
Context: w.options.Context, | ||
Logger: w.options.Logger, | ||
}, parseWatcherFns(w.options.Logger, w.cache.GardenerCache, config)...) | ||
} | ||
|
||
func parseWatcherFns(l *logrus.Entry, gardenerCache agent.Cache[*v1beta1.ShootList], config *config.Config) []agent.WatchFn { | ||
funcs := []agent.WatchFn{} | ||
for i := range config.GardenerProjects { | ||
p := config.GardenerProjects[i] | ||
r := gardenerCache.Register(p.Namespace) | ||
|
||
l.Debugf("creating watcher func for namespace: '%s'", p.Namespace) | ||
l := l.WithFields( | ||
logrus.Fields{ | ||
"provider": "gardener", | ||
"project": p.Namespace, | ||
}, | ||
) | ||
funcs = append(funcs, | ||
gardener.NewWatchFunc(l, r, p.Namespace, p.KubeconfigPath), | ||
) | ||
} | ||
|
||
return funcs | ||
Context: ctx, | ||
Logger: w.logger, | ||
}, parseWatcherFns( | ||
w.logger, | ||
w.cache.GetGardenerCache(), | ||
config)...) | ||
} |
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,30 @@ | ||
package watcher | ||
|
||
import ( | ||
"github.com/gardener/gardener/pkg/apis/core/v1beta1" | ||
"github.com/pPrecel/cloudagent/internal/gardener" | ||
"github.com/pPrecel/cloudagent/pkg/agent" | ||
"github.com/pPrecel/cloudagent/pkg/config" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func parseWatcherFns(l *logrus.Entry, gardenerCache agent.Cache[*v1beta1.ShootList], config *config.Config) []agent.WatchFn { | ||
funcs := []agent.WatchFn{} | ||
for i := range config.GardenerProjects { | ||
p := config.GardenerProjects[i] | ||
r := gardenerCache.Register(p.Namespace) | ||
|
||
l.Debugf("creating watcher func for namespace: '%s'", p.Namespace) | ||
l := l.WithFields( | ||
logrus.Fields{ | ||
"provider": "gardener", | ||
"project": p.Namespace, | ||
}, | ||
) | ||
funcs = append(funcs, | ||
gardener.NewWatchFunc(l, r, p.Namespace, p.KubeconfigPath), | ||
) | ||
} | ||
|
||
return funcs | ||
} |
Oops, something went wrong.