This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
repository.go
149 lines (127 loc) · 3.88 KB
/
repository.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package repository
import (
"strings"
"github.com/caos/orbos/internal/operator/boom/api/latest/reconciling"
"github.com/caos/orbos/internal/operator/boom/application/applications/argocd/info"
"github.com/caos/orbos/internal/operator/boom/application/resources"
"github.com/caos/orbos/internal/operator/boom/labels"
helper2 "github.com/caos/orbos/internal/utils/helper"
"github.com/caos/orbos/mntr"
)
type Repository struct {
URL string
UsernameSecret *secret `yaml:"usernameSecret,omitempty"`
PasswordSecret *secret `yaml:"passwordSecret,omitempty"`
SSHPrivateKeySecret *secret `yaml:"sshPrivateKeySecret,omitempty"`
}
const (
cert = "certificate"
user = "username"
pw = "password"
)
type secret struct {
Name string
Key string
}
func getSecretName(name string, ty string) string {
return strings.Join([]string{info.GetName().String(), "repo", name, ty}, "-")
}
func getSecretKey(ty string) string {
return ty
}
func GetSecrets(spec *reconciling.Reconciling) []interface{} {
secrets := make([]interface{}, 0)
namespace := "caos-system"
for _, v := range spec.Repositories {
if helper2.IsCrdSecret(v.Username, v.ExistingUsernameSecret) {
data := map[string]string{
getSecretKey(user): v.Username.Value,
}
conf := &resources.SecretConfig{
Name: getSecretName(v.Name, user),
Namespace: namespace,
Labels: labels.GetAllApplicationLabels(info.GetName()),
Data: data,
}
secretRes := resources.NewSecret(conf)
secrets = append(secrets, secretRes)
}
if helper2.IsCrdSecret(v.Password, v.ExistingPasswordSecret) {
data := map[string]string{
getSecretKey(pw): v.Password.Value,
}
conf := &resources.SecretConfig{
Name: getSecretName(v.Name, pw),
Namespace: namespace,
Labels: labels.GetAllApplicationLabels(info.GetName()),
Data: data,
}
secretRes := resources.NewSecret(conf)
secrets = append(secrets, secretRes)
}
if helper2.IsCrdSecret(v.Certificate, v.ExistingCertificateSecret) {
data := map[string]string{
getSecretKey(cert): v.Certificate.Value,
}
conf := &resources.SecretConfig{
Name: getSecretName(v.Name, cert),
Namespace: namespace,
Labels: labels.GetAllApplicationLabels(info.GetName()),
Data: data,
}
secretRes := resources.NewSecret(conf)
secrets = append(secrets, secretRes)
}
}
return secrets
}
func GetFromSpec(monitor mntr.Monitor, spec *reconciling.Reconciling) []*Repository {
repositories := make([]*Repository, 0)
if spec.Repositories == nil || len(spec.Repositories) == 0 {
return repositories
}
for _, v := range spec.Repositories {
var us, ps, ssh *secret
if helper2.IsCrdSecret(v.Username, v.ExistingUsernameSecret) {
us = &secret{
Name: getSecretName(v.Name, user),
Key: getSecretKey(user),
}
} else if helper2.IsExistentSecret(v.Username, v.ExistingUsernameSecret) {
us = &secret{
Name: v.ExistingUsernameSecret.Name,
Key: v.ExistingUsernameSecret.Key,
}
}
if helper2.IsCrdSecret(v.Password, v.ExistingPasswordSecret) {
ps = &secret{
Name: getSecretName(v.Name, pw),
Key: getSecretKey(pw),
}
} else if helper2.IsExistentSecret(v.Password, v.ExistingPasswordSecret) {
ps = &secret{
Name: v.ExistingPasswordSecret.Name,
Key: v.ExistingPasswordSecret.Key,
}
}
if helper2.IsCrdSecret(v.Certificate, v.ExistingCertificateSecret) {
ssh = &secret{
Name: getSecretName(v.Name, cert),
Key: getSecretKey(cert),
}
} else if helper2.IsExistentSecret(v.Certificate, v.ExistingCertificateSecret) {
ssh = &secret{
Name: v.ExistingCertificateSecret.Name,
Key: v.ExistingCertificateSecret.Key,
}
}
repo := &Repository{
URL: v.URL,
UsernameSecret: us,
PasswordSecret: ps,
SSHPrivateKeySecret: ssh,
}
repositories = append(repositories, repo)
}
return repositories
}