func()

in pkg/api/v1/config_cache.go [98:149]


func (s *Server) getConfig(ctx context.Context, log *logrus.Entry, scope string) (*cachedConfig, error) {
	configPath, isDefault, err := s.configPath(scope)
	if err != nil || configPath == nil {
		return nil, errScopeNotProvided
	}

	if isDefault {
		if s.defaultCache == nil || s.defaultCache.Config == nil {
			log = log.WithField("config-path", configPath.String())
			configChanged, err := snapshot.Observe(ctx, log, s.Client, *configPath, time.NewTicker(reobservationTime).C)
			if err != nil {
				log.WithError(err).Errorf("Can't read default config; check permissions")
				return nil, fmt.Errorf("Could not read config at %q", configPath.String())
			}

			s.defaultCache = &cachedConfig{
				Config: <-configChanged,
			}
			s.defaultCache.generateNormalCache()

			log.Info("Observing default config")
			go func(ctx context.Context) {
				for {
					select {
					case newCfg := <-configChanged:
						s.defaultCache.Mutex.Lock()
						s.defaultCache.Config = newCfg
						s.defaultCache.generateNormalCache()
						log.Info("Observed config updated")
						s.defaultCache.Mutex.Unlock()
					case <-ctx.Done():
						return
					}
				}
			}(ctx)

		}
		return s.defaultCache, nil
	}

	cfgChan, err := snapshot.Observe(ctx, log, s.Client, *configPath, nil)
	if err != nil {
		// Do not log; invalid requests will write useless logs.
		return nil, fmt.Errorf("Could not read config at %q", configPath.String())
	}

	result := cachedConfig{
		Config: <-cfgChan,
	}
	result.generateNormalCache()
	return &result, nil
}