Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit 908d191

Browse files
feat: add secret name conversion (#101)
1 parent ed77294 commit 908d191

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

argocd-cmp/cmp.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ spec:
1515
args:
1616
- render
1717
- "."
18-
- --secret-name
19-
- "${ARGOCD_APP_NAME}"
20-
- --secret-namespace
21-
- "${ARGOCD_APP_NAMESPACE}"
2218
- --env-regex
2319
- "^ARGOCD_ENV_.*$"
2420
- --must-decrypt

pkg/config/config.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ import (
1515
)
1616

1717
type Configuration struct {
18-
EnvRegex string `mapstructure:"env-regex"`
19-
RootDirectory string `mapstructure:"root-dir"`
20-
FileRegex string `mapstructure:"file-regex"`
21-
SecretName string `mapstructure:"secret-name"`
22-
SecretNamespace string `mapstructure:"secret-namespace"`
23-
EjsonKey []string `mapstructure:"ejson-key"`
24-
SkipDecrypt bool `mapstructure:"skip-decrypt"`
25-
MustDecrypt bool `mapstructure:"must-decrypt"`
26-
KubectlTimeout time.Duration `mapstructure:"kubectl-timeout"`
27-
Kubeconfig string `mapstructure:"kubeconfig"`
28-
KubeAPI string `mapstructure:"kube-api"`
29-
Output string `mapstructure:"output"`
18+
EnvRegex string `mapstructure:"env-regex"`
19+
RootDirectory string `mapstructure:"root-dir"`
20+
FileRegex string `mapstructure:"file-regex"`
21+
SecretName string `mapstructure:"secret-name"`
22+
SecretNamespace string `mapstructure:"secret-namespace"`
23+
EjsonKey []string `mapstructure:"ejson-key"`
24+
SkipDecrypt bool `mapstructure:"skip-decrypt"`
25+
MustDecrypt bool `mapstructure:"must-decrypt"`
26+
KubectlTimeout time.Duration `mapstructure:"kubectl-timeout"`
27+
Kubeconfig string `mapstructure:"kubeconfig"`
28+
KubeAPI string `mapstructure:"kube-api"`
29+
Output string `mapstructure:"output"`
30+
ConvertSecretname bool `mapstructure:"convert-secret-name"`
3031
}
3132

3233
var (
@@ -82,8 +83,17 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command, directory string) (*C
8283
}
8384

8485
if cfg.SecretName != "" {
85-
regex := regexp.MustCompile(`[^a-zA-Z0-9]+`)
86-
cfg.SecretName = regex.ReplaceAllString(cfg.SecretName, "-")
86+
if cfg.ConvertSecretname {
87+
cfg.SecretName = getValueAfterUnderscore(cfg.SecretName)
88+
89+
} else {
90+
regex := regexp.MustCompile(`[^a-zA-Z0-9]+`)
91+
cfg.SecretName = regex.ReplaceAllString(cfg.SecretName, "-")
92+
}
93+
}
94+
95+
if cfg.SecretNamespace == "" {
96+
cfg.SecretNamespace = os.Getenv("ARGOCD_APP_NAMESPACE")
8797
}
8898

8999
if cfg.SecretName != "" && cfg.SecretNamespace == "" {

pkg/config/utils.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package config
2+
3+
import (
4+
"regexp"
5+
)
6+
7+
func getValueAfterUnderscore(input string) string {
8+
re, _ := regexp.Compile("_(.+)")
9+
10+
matches := re.FindStringSubmatch(input)
11+
if len(matches) < 2 {
12+
// No match found or the part after underscore is missing
13+
return input
14+
}
15+
16+
return matches[1]
17+
}

subst/cmd/render.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func addRenderFlags(flags *flag.FlagSet) {
3838
if flags.Lookup("kube-api") == nil {
3939
flags.String("kube-api", "", "Kubernetes API Url")
4040
}
41+
flags.Bool("convert-secret-name", true, heredoc.Doc(`
42+
Assuming the secret name is derived from ARGOCD_APP_NAME, this option will only use the application name (without project-name_)`))
4143
flags.String("secret-name", "", heredoc.Doc(`
4244
Specify Secret name (each key within the secret will be used as a decryption key)`))
4345
flags.String("secret-namespace", "", heredoc.Doc(`

0 commit comments

Comments
 (0)