Skip to content

Commit

Permalink
add cache helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Scherba <mikhail.scherba@flant.com>

test

Signed-off-by: Mikhail Scherba <mikhail.scherba@flant.com>
  • Loading branch information
miklezzzz committed Oct 21, 2024
1 parent 69de505 commit 9e509c6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
14 changes: 5 additions & 9 deletions cmd/addon-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,18 @@ func run(ctx context.Context, operator *addon_operator.AddonOperator) error {
bk := configmap.New(log.StandardLogger(), operator.KubeClient(), app.Namespace, app.ConfigMapName)
operator.SetupKubeConfigManager(bk)

err := operator.Setup()
if err != nil {
fmt.Printf("Setup is failed: %s\n", err)
os.Exit(1)
if err := operator.Setup(); err != nil {
log.Fatalf("Setup is failed: %s\n", err)
}

err = operator.Start(ctx)
if err != nil {
fmt.Printf("Start is failed: %s\n", err)
os.Exit(1)
if err := operator.Start(ctx); err != nil {
log.Fatalf("Start is failed: %s\n", err)
}

// Block action by waiting signals from OS.
utils_signal.WaitForProcessInterruption(func() {
operator.Stop()
os.Exit(1)
os.Exit(0)
})

return nil
Expand Down
16 changes: 8 additions & 8 deletions pkg/addon-operator/bootstrap.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package addon_operator

import (
"fmt"

log "github.com/sirupsen/logrus"

"github.com/flant/addon-operator/pkg/app"
Expand All @@ -14,25 +16,23 @@ import (

// Bootstrap inits all dependencies for a full-fledged AddonOperator instance.
func (op *AddonOperator) bootstrap() error {
if err := op.HelmResourcesManager.InitCache(); err != nil {
return fmt.Errorf("fatal: init helm resources manager: %v", err)
}
log.Info(sh_app.AppStartMessage)

log.Infof("Search modules in: %s", app.ModulesDir)

log.Infof("Addon-operator namespace: %s", app.Namespace)

// Debug server.
// TODO: rewrite sh_app global variables to the addon-operator ones
var err error
op.DebugServer, err = shell_operator.RunDefaultDebugServer(sh_app.DebugUnixSocket, sh_app.DebugHttpServerAddr)
if err != nil {
log.Errorf("Fatal: start Debug server: %s", err)
return err
return fmt.Errorf("fatal: start Debug server: %v", err)
}

err = op.Assemble(op.DebugServer)
if err != nil {
log.Errorf("Fatal: %s", err)
return err
if err := op.Assemble(op.DebugServer); err != nil {
return fmt.Errorf("fatal: %v", err)
}

return nil
Expand Down
48 changes: 33 additions & 15 deletions pkg/helm_resources_manager/helm_resources_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
)

type HelmResourcesManager interface {
InitCache() error
WithCache(cr_cache.Cache)
WithDefaultNamespace(namespace string)
Stop()
StopMonitors()
Expand Down Expand Up @@ -56,33 +58,49 @@ func NewHelmResourcesManager(ctx context.Context, kclient *klient.Client) (HelmR
return nil, fmt.Errorf("kube client not set")
}

cfg := kclient.RestConfig()
return &helmResourcesManager{
eventCh: make(chan ReleaseStatusEvent),
monitors: make(map[string]*ResourcesMonitor),
ctx: cctx,
cancel: cancel,
kubeClient: kclient,
}, nil
}

func (hm *helmResourcesManager) WithCache(cache cr_cache.Cache) {
hm.cache = cache
}

func (hm *helmResourcesManager) InitCache() error {
if hm.cache != nil {
return nil
}
defaultLabelSelector, err := labels.Parse(app.ExtraLabels)
if err != nil {
return nil, err
return fmt.Errorf("couldn't init cache: %v", err)
}
if hm.kubeClient == nil {
return fmt.Errorf("couldn't init cache without kube client set")
}
cache, err := cr_cache.New(cfg, cr_cache.Options{
cache, err := cr_cache.New(hm.kubeClient.RestConfig(), cr_cache.Options{
DefaultLabelSelector: defaultLabelSelector,
})
if err != nil {
return nil, err
return fmt.Errorf("couldn't init cache: %v", err)
}

if hm.ctx == nil {
return fmt.Errorf("couldn't init cache witouh context set")
}

go cache.Start(cctx)
go cache.Start(hm.ctx)
log.Debug("Helm resource manager: cache's been started")
if synced := cache.WaitForCacheSync(cctx); !synced {
return nil, fmt.Errorf("Couldn't sync helm resource informer cache")
if synced := cache.WaitForCacheSync(hm.ctx); !synced {
return fmt.Errorf("couldn't sync helm resource informer cache")
}
log.Debug("Helm resourcer manager: cache has been synced")

return &helmResourcesManager{
eventCh: make(chan ReleaseStatusEvent),
monitors: make(map[string]*ResourcesMonitor),
ctx: cctx,
cancel: cancel,
kubeClient: kclient,
cache: cache,
}, nil
return nil
}

func (hm *helmResourcesManager) WithDefaultNamespace(namespace string) {
Expand Down
7 changes: 6 additions & 1 deletion pkg/helm_resources_manager/test/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package mock
import (
"context"

cr_cache "sigs.k8s.io/controller-runtime/pkg/cache"

. "github.com/flant/addon-operator/pkg/helm_resources_manager"
. "github.com/flant/addon-operator/pkg/helm_resources_manager/types"
klient "github.com/flant/kube-client/client"
Expand All @@ -17,7 +19,10 @@ func (h *MockHelmResourcesManager) WithContext(_ context.Context) {}

func (h *MockHelmResourcesManager) WithKubeClient(_ *klient.Client) {}

func (h *MockHelmResourcesManager) WithCache(_ context.Context) error {
func (h *MockHelmResourcesManager) WithCache(_ cr_cache.Cache) {
}

func (h *MockHelmResourcesManager) InitCache() error {
return nil
}

Expand Down

0 comments on commit 9e509c6

Please sign in to comment.