-
Notifications
You must be signed in to change notification settings - Fork 280
/
gitopsconfig.go
72 lines (60 loc) · 1.84 KB
/
gitopsconfig.go
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
package v1alpha1
import (
"errors"
"fmt"
"regexp"
)
const (
GitOpsConfigKind = "GitOpsConfig"
FluxDefaultNamespace = "flux-system"
FluxDefaultBranch = "main"
)
func validateGitOpsConfig(config *GitOpsConfig) error {
if config == nil {
return errors.New("gitOpsRef is specified but GitOpsConfig is not specified")
}
flux := config.Spec.Flux
if len(flux.Github.Owner) <= 0 {
return errors.New("'owner' is not set or empty in gitOps.flux; owner is a required field")
}
if len(flux.Github.Repository) <= 0 {
return errors.New("'repository' is not set or empty in gitOps.flux; repository is a required field")
}
err := validateGitRepoName(flux.Github.Repository)
if err != nil {
return err
}
if len(flux.Github.Branch) > 0 {
err := validateGitBranchName(config.Spec.Flux.Github.Branch)
if err != nil {
return err
}
}
return nil
}
func validateGitBranchName(branchName string) error {
allowedGitBranchNameRegex := regexp.MustCompile(`^([0-9A-Za-z\_\+,]+)\.?\/?([0-9A-Za-z\-\_\+,]+)$`)
if !allowedGitBranchNameRegex.MatchString(branchName) {
return fmt.Errorf("%s is not a valid git branch name, please check with this documentation https://git-scm.com/docs/git-check-ref-format for valid git branch names", branchName)
}
return nil
}
func validateGitRepoName(repoName string) error {
allowedGitRepoName := regexp.MustCompile(`^([0-9A-Za-z-_.]+)$`)
if !allowedGitRepoName.MatchString(repoName) {
return fmt.Errorf("%s is not a valid git repository name, name can contain only letters, digits, '_', '-' and '.'", repoName)
}
return nil
}
func setGitOpsConfigDefaults(gitops *GitOpsConfig) {
if gitops == nil {
return
}
c := &gitops.Spec.Flux
if len(c.Github.FluxSystemNamespace) == 0 {
c.Github.FluxSystemNamespace = FluxDefaultNamespace
}
if len(c.Github.Branch) == 0 {
c.Github.Branch = FluxDefaultBranch
}
}