github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/btpmanager/credentials/watcher.go (about)

     1  package btpmgrcreds
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/kyma-project/runtime-watcher/listener/pkg/event"
     8  	"github.com/kyma-project/runtime-watcher/listener/pkg/types"
     9  	"github.com/sirupsen/logrus"
    10  )
    11  
    12  type Watcher struct {
    13  	ctx                context.Context
    14  	listener           *event.SKREventListener
    15  	logs               *logrus.Logger
    16  	btpOperatorManager *Manager
    17  }
    18  
    19  func NewWatcher(ctx context.Context, listenerAddr, componentName string, btpOperatorManager *Manager, logs *logrus.Logger) *Watcher {
    20  	noVerify := func(r *http.Request, watcherEvtObject *types.WatchEvent) error {
    21  		return nil
    22  	}
    23  	listener, _ := event.RegisterListenerComponent(listenerAddr, componentName, noVerify)
    24  	return &Watcher{
    25  		ctx:                ctx,
    26  		listener:           listener,
    27  		btpOperatorManager: btpOperatorManager,
    28  		logs:               logs,
    29  	}
    30  }
    31  
    32  func (s *Watcher) ReactOnSkrEvent() {
    33  	go func() {
    34  		for {
    35  			select {
    36  			case response := <-s.listener.ReceivedEvents:
    37  				kymaName := response.Object.GetName()
    38  				s.logs.Infof("event received for: %s", kymaName)
    39  				instance, err := s.btpOperatorManager.MatchInstance(kymaName)
    40  				if err != nil {
    41  					s.logs.Errorf("while trying to match instance for kyma name : %s, %s", kymaName, err)
    42  					continue
    43  				}
    44  				updated, err := s.btpOperatorManager.ReconcileSecretForInstance(instance)
    45  				if err != nil {
    46  					s.logs.Errorf("while trying to update for instance %s with kyma name : %s, %s", instance.InstanceID, kymaName, err)
    47  					continue
    48  				}
    49  				if updated {
    50  					s.logs.Infof("instance id: %s updated kyma %s with success", instance.InstanceID, kymaName)
    51  				}
    52  			case <-s.ctx.Done():
    53  				s.logs.Info("runtime watcher: context closed")
    54  				return
    55  			}
    56  		}
    57  	}()
    58  
    59  	if err := s.listener.Start(s.ctx); err != nil {
    60  		s.logs.Errorf("cannot start listener: %s", err.Error())
    61  	}
    62  }