-
Notifications
You must be signed in to change notification settings - Fork 274
/
gitopsconfig.go
81 lines (70 loc) 路 2.33 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
73
74
75
76
77
78
79
80
81
package v1alpha1
import (
"errors"
"fmt"
"regexp"
)
const GitOpsConfigKind = "GitOpsConfig"
func GetAndValidateGitOpsConfig(fileName string, refName string, clusterConfig *Cluster) (*GitOpsConfig, error) {
config, err := getGitOpsConfig(fileName)
if err != nil {
return nil, err
}
err = validateGitOpsConfig(config, refName, clusterConfig)
if err != nil {
return nil, err
}
return config, nil
}
func getGitOpsConfig(fileName string) (*GitOpsConfig, error) {
var config GitOpsConfig
err := ParseClusterConfig(fileName, &config)
if err != nil {
return nil, err
}
return &config, nil
}
func validateGitOpsConfig(config *GitOpsConfig, refName string, clusterConfig *Cluster) error {
if config == nil {
return errors.New("gitOpsRef is specified but GitOpsConfig is not specified")
}
if config.Name != refName {
return fmt.Errorf("GitOpsConfig retrieved with name %s does not match name (%s) specified in "+
"gitOpsRef", config.Name, refName)
}
if config.Namespace != clusterConfig.Namespace {
return errors.New("GitOpsConfig and Cluster objects must have the same namespace 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
}