Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlabsourceconfig.go « gitlabsourceconfig « domains « source « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ebc8b485955dc3d6ff7ec3574a4bde7cf3fd03ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package gitlabsourceconfig

import (
	"bytes"
	"io/ioutil"
	"os"
	"time"

	log "github.com/sirupsen/logrus"
	"gopkg.in/yaml.v2"
)

// GitlabSourceDomains holds the domains to be used with the gitlab source
type GitlabSourceDomains struct {
	Enabled []string
	Broken  string
	Rollout GitlabSourceRollout
}

// GitlabSourceRollout holds the rollout strategy and percentage
type GitlabSourceRollout struct {
	Stickiness string
	Percentage int
}

// GitlabSourceConfig holds the configuration for the gitlab source
type GitlabSourceConfig struct {
	Domains GitlabSourceDomains
}

// UpdateFromYaml updates the config
// We use new variable here (instead of using `config` directly)
// because if `content` is empty `yaml.Unmarshal` does not update
// the fields already set.
func (config *GitlabSourceConfig) UpdateFromYaml(content []byte) error {
	updated := GitlabSourceConfig{}

	err := yaml.Unmarshal(content, &updated)
	if err != nil {
		return err
	}

	*config = updated

	log.WithFields(log.Fields{
		"Enabled domains":    config.Domains.Enabled,
		"Broken domain":      config.Domains.Broken,
		"Rollout %":          config.Domains.Rollout.Percentage,
		"Rollout stickiness": config.Domains.Rollout.Stickiness,
	}).Info("gitlab source config updated")

	return nil
}

// WatchForGitlabSourceConfigChange polls the filesystem and updates test domains if needed.
func WatchForGitlabSourceConfigChange(config *GitlabSourceConfig, interval time.Duration) {
	var lastContent []byte

	gitlabSourceConfigFile := os.Getenv("GITLAB_SOURCE_CONFIG_FILE")
	if gitlabSourceConfigFile == "" {
		gitlabSourceConfigFile = ".gitlab-source-config.yml"
	}

	for {
		content, err := readConfig(gitlabSourceConfigFile)
		if err != nil {
			log.WithError(err).Warn("Failed to read gitlab source config file")

			time.Sleep(interval)
			continue
		}

		if !bytes.Equal(lastContent, content) {
			lastContent = content

			err = config.UpdateFromYaml(content)
			if err != nil {
				log.WithError(err).Warn("Failed to update gitlab source config")
			}
		}

		time.Sleep(interval)
	}
}

func readConfig(configfile string) ([]byte, error) {
	content, err := ioutil.ReadFile(configfile)

	if err != nil && !os.IsNotExist(err) {
		return nil, err
	}

	return content, nil
}