Exemplo n.º 1
0
func (i *Informer) notify(v interface{}) {
	// send but do not block for it
	select {
	case i.recvChan <- v:
	default:
		log.Warnf("unable to notify item, discarding it (%v)", v)
	}
}
Exemplo n.º 2
0
func (k2n *KubeToNginx) process(v interface{}) {
	switch vv := v.(type) {
	case *kapi.ServiceList:
		k2n.upstreamsData = make(map[string]string)
		for _, s := range vv.Items {
			k2n.addService(s)
		}
	case *kapi.WatchEvent:
		s, ok := vv.Object.(*kapi.Service)
		if !ok {
			log.Warnf("unknown k8s api object in a watch event was received: %v", vv.Object)
			return
		}

		switch vv.Type {
		case kapi.Added:
			k2n.addService(*s)
		case kapi.Deleted:
			k2n.deleteService(*s)
		case kapi.Modified:
			k2n.updateService(*s)
		}
	default:
		log.Warnf("unknown k8s api object was received: %v", v)
		return
	}

	// mixup everything
	kvs := make(map[string]string)
	for k, v := range k2n.ingressesData {
		kvs[k] = v
	}
	for k, v := range k2n.upstreamsData {
		kvs[k] = v
	}

	// render template
	err := k2n.tmpl.Render(kvs)
	if err != nil {
		log.Error(err)
	}
}
Exemplo n.º 3
0
func (i *Informer) notifyError(err error) {
	// send but do not block for it
	select {
	case i.errChan <- err:
	default:
		log.Warnf("unable to notify error, discarding it (%v)", err)
	}

	// Prevent errors from consuming all resources.
	time.Sleep(1 * time.Second)
}
Exemplo n.º 4
0
// sync compares the staged and dest config files and attempts to sync them
// if they differ. sync will run a config check command if set before
// overwriting the target config file. Finally, sync will run a reload command
// if set to have the application or service pick up the changes.
// It returns an error if any.
func (t *Template) sync(stageFile *os.File, fileMode os.FileMode, doNoOp bool) error {
	stageFileName := stageFile.Name()
	if !t.keepStageFile {
		defer os.Remove(stageFileName)
	}

	log.Debugf("Comparing candidate config to %s", t.config.Dest)
	ok, err := isSameConfig(stageFileName, t.config.Dest)
	if err != nil {
		log.Error(err)
		return err
	}

	if doNoOp {
		log.Warnf("Noop mode enabled. %s will not be modified", t.config.Dest)
		return nil
	}

	if !ok {
		log.Infof("Target config %s out of sync", t.config.Dest)

		if t.config.CheckCmd != "" {
			if err := t.check(stageFileName); err != nil {
				return errors.New("Config check failed: " + err.Error())
			}
		}

		log.Debugf("Overwriting target config %s", t.config.Dest)

		err := os.Rename(stageFileName, t.config.Dest)
		if err != nil {
			if strings.Contains(err.Error(), "device or resource busy") {
				log.Debugf("Rename failed - target is likely a mount.config. Trying to write instead")
				// try to open the file and write to it
				var contents []byte
				var rerr error
				contents, rerr = ioutil.ReadFile(stageFileName)
				if rerr != nil {
					return rerr
				}
				err := ioutil.WriteFile(t.config.Dest, contents, fileMode)
				// make sure owner and group match the temp file, in case the file was created with WriteFile
				os.Chown(t.config.Dest, t.config.Uid, t.config.Gid)
				if err != nil {
					return err
				}
			} else {
				return err
			}
		}

		if t.config.ReloadCmd != "" {
			if err := t.reload(); err != nil {
				return err
			}
		}

		log.Infof("Target config %s has been updated", t.config.Dest)
	} else {
		log.Debugf("Target config %s in sync", t.config.Dest)
	}

	return nil
}