Skip to content

Commit 0e803b3

Browse files
committed
add split command and git auth
1 parent 17f362c commit 0e803b3

6 files changed

Lines changed: 166 additions & 29 deletions

File tree

.vscode/launch.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"name": "Split",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "main.go",
13+
"args": ["split", "/home/schlatter/tmp/argo-apps/out/all.yaml", "--output-dir=out/split"]
14+
},
715
{
816
"name": "ArgoCD",
917
"type": "go",
1018
"request": "launch",
1119
"mode": "auto",
1220
"program": "main.go",
13-
"args": ["argocd", "./testdata/argocd/", "--repoServer=localhost:8081", "--output-dir=out/"]
21+
"args": ["argocd", "./out/split", "--repoServer=localhost:8081", "--output-dir=out/", "-u", "argo", "-i", "/home/schlatter/.ssh/id_rsa"]
1422
},
1523
{
1624
"name": "Kustomize build",

argocd/repoClient.go

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package argocd
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"goff/util"
78
"io/fs"
89
"os"
@@ -16,9 +17,15 @@ import (
1617
"github.com/ghodss/yaml"
1718
)
1819

19-
func Render(dir, repoServerUrl, outputDir string) {
20-
conn := apiclient.NewRepoServerClientset(repoServerUrl, 300, apiclient.TLSConfiguration{StrictValidation: false})
21-
r, b, err := conn.NewRepoServerClient()
20+
type RepoCredentails struct {
21+
Username string
22+
Password string
23+
KeyFile string
24+
}
25+
26+
func Render(dir, repoServerUrl, outputDir string, creds RepoCredentails) {
27+
conn := apiclient.NewRepoServerClientset(repoServerUrl, 600, apiclient.TLSConfiguration{StrictValidation: false})
28+
r, client, err := conn.NewRepoServerClient()
2229
defer r.Close()
2330

2431
if err != nil {
@@ -32,93 +39,124 @@ func Render(dir, repoServerUrl, outputDir string) {
3239
}
3340

3441
for _, file := range files {
35-
renderFile(file, repoServerUrl, outputDir, b)
42+
err = renderFile(file, repoServerUrl, outputDir, client, creds)
43+
if err != nil {
44+
fmt.Println(err)
45+
}
46+
//TODO log errors
3647
}
3748

3849
}
3950

40-
func renderFile(file, repoServerUrl, outputDir string, client apiclient.RepoServerServiceClient) {
51+
func renderFile(file, repoServerUrl, outputDir string, client apiclient.RepoServerServiceClient, creds RepoCredentails) error {
4152

4253
data, err := os.ReadFile(file)
4354
if err != nil {
44-
panic(err)
55+
return err
4556
}
4657

4758
data, err = yaml.YAMLToJSON(data)
4859
if err != nil {
49-
panic(err)
60+
return err
5061
}
5162

5263
app := &v1alpha1.Application{}
5364

5465
err = json.Unmarshal(data, app)
5566
if err != nil {
56-
panic(err)
67+
return err
5768
}
5869

5970
repoDB := &dbmocks.ArgoDB{}
71+
source := v1alpha1.ApplicationSource{}
72+
73+
var privateKey string
74+
if creds.KeyFile != "" {
75+
data, err := os.ReadFile(creds.KeyFile)
76+
if err != nil {
77+
return err
78+
}
79+
privateKey = string(data)
80+
}
6081

6182
if app.Spec.Source != nil {
6283
repoDB.On("GetRepository", context.Background(), app.Spec.Source.RepoURL).Return(&v1alpha1.Repository{
63-
Repo: app.Spec.Source.RepoURL,
84+
Repo: app.Spec.Source.RepoURL,
85+
SSHPrivateKey: privateKey,
86+
Username: creds.Username,
87+
Password: creds.Password,
88+
ForceHttpBasicAuth: true,
6489
}, nil)
90+
source = *app.Spec.Source
6591
}
6692

6793
if app.Spec.Sources != nil {
6894
for i := range app.Spec.Sources {
95+
source = app.Spec.Sources[i]
6996
repo := app.Spec.Sources[i].RepoURL
7097
if repo != "" {
7198
repoDB.On("GetRepository", context.Background(), repo).Return(&v1alpha1.Repository{
72-
Repo: repo,
99+
Repo: repo,
100+
SSHPrivateKey: privateKey,
101+
Username: creds.Username,
102+
Password: creds.Password,
103+
ForceHttpBasicAuth: true,
73104
}, nil)
74105
}
75106
}
76107
}
77108

78109
refSources, err := argo.GetRefSources(context.Background(), app.Spec, repoDB)
79110
req := &apiclient.ManifestRequest{
80-
ApplicationSource: &app.Spec.Sources[0],
111+
ApplicationSource: &source,
81112
AppName: "goff-test",
82113
NoCache: true,
83114
RefSources: refSources,
84115
HasMultipleSources: true,
85-
Revision: app.Spec.Sources[0].TargetRevision,
116+
Revision: source.TargetRevision,
117+
KustomizeOptions: &v1alpha1.KustomizeOptions{
118+
BuildOptions: "--enable-helm",
119+
},
86120
Repo: &v1alpha1.Repository{
87-
Repo: app.Spec.Sources[0].RepoURL,
121+
Repo: source.RepoURL,
122+
SSHPrivateKey: privateKey,
123+
Username: creds.Username,
124+
Password: creds.Password,
125+
ForceHttpBasicAuth: true,
88126
},
89127
}
90128

91129
resp, err := client.GenerateManifest(context.Background(), req)
92130
if err != nil {
93-
panic(err)
131+
return fmt.Errorf("could not process application '%s': %w", app.Name, err)
94132
}
95133

96134
err = os.MkdirAll(outputDir, 0777)
97135
if err != nil {
98-
panic(err)
136+
return err
99137
}
100138

101139
for _, manifest := range resp.Manifests {
102140

103141
fileName, err := util.FileNameFromManifest(manifest)
104142
if err != nil {
105-
panic(err)
143+
return err
106144
}
107145

108146
outputFile := filepath.Join(outputDir, fileName)
109147

110148
yamlManifest, err := yaml.JSONToYAML([]byte(manifest))
111149
if err != nil {
112-
panic(err)
150+
return err
113151
}
114152

115153
err = os.WriteFile(outputFile, yamlManifest, 0777)
116154
if err != nil {
117-
panic(err)
155+
return err
118156
}
119157

120158
}
121-
159+
return nil
122160
}
123161

124162
func findArgoApps(rootDir string) ([]string, error) {

cmd/argocd.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313
var repoServerUrl *string
1414

1515
var argoOutputDir *string
16+
var repoUsername *string
17+
var repoPassword *string
18+
var repoSshKey *string
1619

1720
// argocdCmd represents the argocd command
1821
var argocdCmd = &cobra.Command{
@@ -21,21 +24,20 @@ var argocdCmd = &cobra.Command{
2124
Args: cobra.ExactArgs(1),
2225
Long: `Render manifests from ArgoCD Application`,
2326
Run: func(cmd *cobra.Command, args []string) {
24-
argocd.Render(args[0], *repoServerUrl, *argoOutputDir)
27+
argocd.Render(args[0], *repoServerUrl, *argoOutputDir, argocd.RepoCredentails{
28+
Username: *repoUsername,
29+
Password: *repoPassword,
30+
KeyFile: *repoSshKey,
31+
})
2532
},
2633
}
2734

2835
func init() {
2936
rootCmd.AddCommand(argocdCmd)
3037

31-
// Here you will define your flags and configuration settings.
32-
33-
// Cobra supports Persistent Flags which will work for this command
34-
// and all subcommands, e.g.:
35-
// argocdCmd.PersistentFlags().String("foo", "", "A help for foo")
36-
37-
// Cobra supports local flags which will only run when this command
38-
// is called directly, e.g.:
3938
repoServerUrl = argocdCmd.Flags().String("repoServer", "localhost:8081", "URL to argoCD repo server")
4039
argoOutputDir = argocdCmd.Flags().StringP("output-dir", "o", ".", "Output directory")
40+
repoUsername = argocdCmd.Flags().StringP("username", "u", "", "Repo username")
41+
repoPassword = argocdCmd.Flags().StringP("password", "p", "", "Repo password")
42+
repoSshKey = argocdCmd.Flags().StringP("ssh-key", "i", "", "Repo SSH Key")
4143
}

cmd/split.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"goff/util"
9+
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var outputSplitDir *string
14+
15+
// diffCmd represents the diff command
16+
var splitCmd = &cobra.Command{
17+
Use: "split",
18+
Short: "Split manifests [manifestFile]",
19+
Args: cobra.ExactArgs(1),
20+
Long: `Split multi document yaml`,
21+
Run: func(cmd *cobra.Command, args []string) {
22+
err := util.SplitManifests(args[0], *outputSplitDir)
23+
if err != nil {
24+
panic(err)
25+
}
26+
},
27+
}
28+
29+
func init() {
30+
rootCmd.AddCommand(splitCmd)
31+
outputSplitDir = splitCmd.Flags().StringP("output-dir", "o", ".", "Output directory")
32+
}

docker-compose.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: "3.9"
2+
services:
3+
redis:
4+
image: "redis"
5+
ports:
6+
- "6379:6379"
7+
repos:
8+
image: "quay.io/argoproj/argocd:latest"
9+
command:
10+
- "--redis"
11+
- "redis:6379"
12+
ports:
13+
- "8081:8081"
14+
entrypoint: argocd-repo-server

util/util.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,54 @@
11
package util
22

33
import (
4+
"bytes"
45
"fmt"
6+
"os"
7+
"path/filepath"
58

69
"github.com/ghodss/yaml"
710
)
811

12+
//Multi doc yaml, split and save
13+
func SplitManifests(manifestFile, outDir string) error {
14+
15+
data, err := os.ReadFile(manifestFile)
16+
if err != nil {
17+
return err
18+
}
19+
20+
err = os.MkdirAll(outDir, 0777)
21+
if err != nil {
22+
return err
23+
}
24+
25+
splitted := bytes.Split(data, []byte("---"))
26+
27+
for i := range splitted {
28+
if len(splitted[i]) == 0 {
29+
continue
30+
}
31+
32+
res := &Ressource{}
33+
err := yaml.Unmarshal(splitted[i], res)
34+
if err != nil {
35+
return err
36+
}
37+
38+
filename := fmt.Sprintf("%s-%s.yaml", res.Kind, res.Metadata.Name)
39+
filename = filepath.Join(outDir, filename)
40+
41+
err = os.WriteFile(filename, []byte(splitted[i]), 0777)
42+
if err != nil {
43+
return err
44+
}
45+
fmt.Println("wrote file at: " + filename)
46+
}
47+
48+
return nil
49+
50+
}
51+
952
func FileNameFromManifest(manifest string) (string, error) {
1053
res := &Ressource{}
1154
err := yaml.Unmarshal([]byte(manifest), res)

0 commit comments

Comments
 (0)