Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restoring disabled informers in beyla-k8s-cache #1264

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/grafana/beyla

go 1.23
go 1.23.0

require (
github.com/AlessandroPomponio/go-gibberish v0.0.0-20191004143433-a2d4156f0396
Expand All @@ -13,7 +13,7 @@ require (
github.com/goccy/go-json v0.10.2
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.1
github.com/grafana/beyla-k8s-cache v0.0.0-20241016170428-8b4efb83c725
github.com/grafana/beyla-k8s-cache v0.0.0-20241017152938-23a6d2229caf
github.com/grafana/go-offsets-tracker v0.1.7
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/mariomac/guara v0.0.0-20230621100729-42bd7716e524
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grafana/beyla-k8s-cache v0.0.0-20241016170428-8b4efb83c725 h1:auLp/060bWP2uWVKH3MIAzL61ttGujsNXry6NI8Un/M=
github.com/grafana/beyla-k8s-cache v0.0.0-20241016170428-8b4efb83c725/go.mod h1:+Y9gUSD7rptXXZ0OL0aRcBLAgzBm+nE4OH1QB9LqHYc=
github.com/grafana/beyla-k8s-cache v0.0.0-20241017152938-23a6d2229caf h1:OQnoOTdx+7aCdNZqYGRZMNNdmyToL6U3DG3IoZJOeX8=
github.com/grafana/beyla-k8s-cache v0.0.0-20241017152938-23a6d2229caf/go.mod h1:SMZM7CaUstB0UGj+Wf4PWHPRCJ6cO/ax9ebUyb0Dt1o=
github.com/grafana/go-offsets-tracker v0.1.7 h1:2zBQ7iiGzvyXY7LA8kaaSiEqH/Yx82UcfRabbY5aOG4=
github.com/grafana/go-offsets-tracker v0.1.7/go.mod h1:qcQdu7zlUKIFNUdBJlLyNHuJGW0SKWKjkrN6jtt+jds=
github.com/grafana/opentelemetry-go v1.28.0-grafana.3 h1:vExZiZKDZTdDi7fP1GG3GOGuoZ0GNu76408tNXfsnD0=
Expand Down
53 changes: 31 additions & 22 deletions pkg/internal/kube/informer_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ type MetadataProvider struct {
metadata *Store
informer *meta.Informers

kubeConfigPath string
syncTimeout time.Duration
resyncPeriod time.Duration

enable kubeflags.EnableFlag
cfg *MetadataConfig
}

func NewMetadataProvider(config MetadataConfig) *MetadataProvider {
Expand All @@ -58,50 +54,45 @@ func NewMetadataProvider(config MetadataConfig) *MetadataProvider {
if config.ResyncPeriod == 0 {
config.ResyncPeriod = defaultResyncTime
}
mp := &MetadataProvider{
kubeConfigPath: config.KubeConfigPath,
syncTimeout: config.SyncTimeout,
resyncPeriod: config.ResyncPeriod,
enable: config.Enable,
}
mp := &MetadataProvider{cfg: &config}
return mp
}

func (mp *MetadataProvider) IsKubeEnabled() bool {
if mp == nil {
if mp == nil || mp.cfg == nil {
return false
}
mp.mt.Lock()
defer mp.mt.Unlock()
switch strings.ToLower(string(mp.enable)) {
switch strings.ToLower(string(mp.cfg.Enable)) {
case string(kubeflags.EnabledTrue):
return true
case string(kubeflags.EnabledFalse), "": // empty value is disabled
return false
case string(kubeflags.EnabledAutodetect):
// We autodetect that we are in a kubernetes if we can properly load a K8s configuration file
_, err := loadKubeConfig(mp.kubeConfigPath)
_, err := loadKubeConfig(mp.cfg.KubeConfigPath)
if err != nil {
klog().Debug("kubeconfig can't be detected. Assuming we are not in Kubernetes", "error", err)
mp.enable = kubeflags.EnabledFalse
mp.cfg.Enable = kubeflags.EnabledFalse
return false
}
mp.enable = kubeflags.EnabledTrue
mp.cfg.Enable = kubeflags.EnabledTrue
return true
default:
klog().Warn("invalid value for Enable value. Ignoring stage", "value", mp.enable)
klog().Warn("invalid value for Enable value. Ignoring stage", "value", mp.cfg.Enable)
return false
}
}

func (mp *MetadataProvider) ForceDisable() {
mp.mt.Lock()
defer mp.mt.Unlock()
mp.enable = kubeflags.EnabledFalse
mp.cfg.Enable = kubeflags.EnabledFalse
}

func (mp *MetadataProvider) KubeClient() (kubernetes.Interface, error) {
restCfg, err := loadKubeConfig(mp.kubeConfigPath)
restCfg, err := loadKubeConfig(mp.cfg.KubeConfigPath)
if err != nil {
return nil, fmt.Errorf("kubeconfig can't be detected: %w", err)
}
Expand Down Expand Up @@ -172,16 +163,19 @@ func (mp *MetadataProvider) initInformers(ctx context.Context) (*meta.Informers,
var informers *meta.Informers
go func() {
var err error
if informers, err = meta.InitInformers(ctx, mp.kubeConfigPath, defaultResyncTime); err != nil {
opts := append(disabledInformerOpts(mp.cfg.DisabledInformers),
meta.WithResyncPeriod(mp.cfg.ResyncPeriod),
meta.WithKubeConfigPath(mp.cfg.KubeConfigPath))
if informers, err = meta.InitInformers(ctx, opts...); err != nil {
done <- err
}
close(done)
}()

select {
case <-time.After(mp.syncTimeout):
case <-time.After(mp.cfg.SyncTimeout):
klog().Warn("kubernetes cache has not been synced after timeout. The kubernetes attributes might be incomplete."+
" Consider increasing the BEYLA_KUBE_INFORMERS_SYNC_TIMEOUT value", "timeout", mp.syncTimeout)
" Consider increasing the BEYLA_KUBE_INFORMERS_SYNC_TIMEOUT value", "timeout", mp.cfg.SyncTimeout)
case err, ok := <-done:
if ok {
return nil, fmt.Errorf("failed to initialize Kubernetes informers: %w", err)
Expand Down Expand Up @@ -216,3 +210,18 @@ func loadKubeConfig(kubeConfigPath string) (*rest.Config, error) {
}
return config, nil
}

func disabledInformerOpts(disabledInformers []string) []meta.InformerOption {
var opts []meta.InformerOption
for _, di := range disabledInformers {
switch strings.ToLower(di) {
case "node", "nodes":
opts = append(opts, meta.WithoutNodes())
case "service", "services":
opts = append(opts, meta.WithoutServices())
default:
klog().Warn("invalid value for DisableInformers. Ignoring", "value", di)
}
}
return opts
}
38 changes: 22 additions & 16 deletions vendor/github.com/grafana/beyla-k8s-cache/pkg/meta/informers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading