-
Notifications
You must be signed in to change notification settings - Fork 274
/
gitops.go
103 lines (87 loc) 路 2.33 KB
/
gitops.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package cluster
import (
"context"
"path"
anywherev1 "github.com/aws/eks-anywhere/pkg/api/v1alpha1"
)
func gitOpsEntry() *ConfigManagerEntry {
return &ConfigManagerEntry{
APIObjectMapping: map[string]APIObjectGenerator{
anywherev1.GitOpsConfigKind: func() APIObject {
return &anywherev1.GitOpsConfig{}
},
},
Processors: []ParsedProcessor{processGitOps},
Defaulters: []Defaulter{
setGitOpsDefaults,
SetDefaultFluxGitHubConfigPath,
},
Validations: []Validation{
validateGitOps,
validateGitOpsNamespace,
},
}
}
func processGitOps(c *Config, objects ObjectLookup) {
if c.Cluster.Spec.GitOpsRef == nil {
return
}
if c.Cluster.Spec.GitOpsRef.Kind == anywherev1.GitOpsConfigKind {
gitOps := objects.GetFromRef(c.Cluster.APIVersion, *c.Cluster.Spec.GitOpsRef)
if gitOps == nil {
return
}
// GitOpsConfig will be deprecated.
// During the deprecation window, FluxConfig will be used internally
// GitOpsConfig will preserved it was in the original spec
gitOpsConf := gitOps.(*anywherev1.GitOpsConfig)
c.GitOpsConfig = gitOpsConf
c.FluxConfig = gitOpsConf.ConvertToFluxConfig()
}
}
func validateGitOps(c *Config) error {
if c.GitOpsConfig != nil {
return c.GitOpsConfig.Validate()
}
return nil
}
func validateGitOpsNamespace(c *Config) error {
if c.GitOpsConfig != nil {
if err := validateSameNamespace(c, c.GitOpsConfig); err != nil {
return err
}
}
return nil
}
func setGitOpsDefaults(c *Config) error {
if c.GitOpsConfig != nil {
c.GitOpsConfig.SetDefaults()
}
return nil
}
func SetDefaultFluxGitHubConfigPath(c *Config) error {
if c.GitOpsConfig == nil {
return nil
}
gitops := c.GitOpsConfig
if gitops.Spec.Flux.Github.ClusterConfigPath != "" {
return nil
}
if c.Cluster.IsSelfManaged() {
gitops.Spec.Flux.Github.ClusterConfigPath = path.Join("clusters", c.Cluster.Name)
} else {
gitops.Spec.Flux.Github.ClusterConfigPath = path.Join("clusters", c.Cluster.ManagedBy())
}
return nil
}
func getGitOps(ctx context.Context, client Client, c *Config) error {
if c.Cluster.Spec.GitOpsRef == nil || c.Cluster.Spec.GitOpsRef.Kind != anywherev1.GitOpsConfigKind {
return nil
}
gitOps := &anywherev1.GitOpsConfig{}
if err := client.Get(ctx, c.Cluster.Spec.GitOpsRef.Name, c.Cluster.Namespace, gitOps); err != nil {
return err
}
c.GitOpsConfig = gitOps
return nil
}