Skip to content

Commit 4221f36

Browse files
CR-5108 (#50)
* final * bump * git-source * new file * corrections * lint * Delete yarn.lock * Delete .yarn-integrity * after codegen * gs using path param * removed redundant * name * example * without redundant condition * . * added files * bump * . * gitSourceOptions * go.mod no replace * optional * with --git-src-repo check * example in error * Use: * Join * removed redundant * after codegen * tested * bump
1 parent 98b61a1 commit 4221f36

File tree

12 files changed

+289
-76
lines changed

12 files changed

+289
-76
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.55
1+
VERSION=v0.0.56
22
OUT_DIR=dist
33
YEAR?=$(shell date +"%Y")
44

cmd/commands/git-source.go

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// Copyright 2021 The Codefresh Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package commands
16+
17+
import (
18+
"context"
19+
"fmt"
20+
21+
"github.com/codefresh-io/cli-v2/pkg/log"
22+
"github.com/codefresh-io/cli-v2/pkg/runtime"
23+
"github.com/codefresh-io/cli-v2/pkg/store"
24+
"github.com/codefresh-io/cli-v2/pkg/util"
25+
26+
"github.com/argoproj-labs/argocd-autopilot/pkg/application"
27+
"github.com/argoproj-labs/argocd-autopilot/pkg/fs"
28+
"github.com/argoproj-labs/argocd-autopilot/pkg/git"
29+
apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store"
30+
wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
31+
wfv1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
32+
"github.com/go-git/go-billy/v5/memfs"
33+
"github.com/spf13/cobra"
34+
v1 "k8s.io/api/core/v1"
35+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
36+
)
37+
38+
type (
39+
GitSourceCreateOptions struct {
40+
insCloneOpts *git.CloneOptions
41+
gsCloneOpts *git.CloneOptions
42+
gsName string
43+
runtimeName string
44+
fullGsPath string
45+
}
46+
)
47+
48+
func NewGitSourceCommand() *cobra.Command {
49+
cmd := &cobra.Command{
50+
Use: "git-source",
51+
Short: "Manage git-sources of Codefresh runtimes",
52+
Run: func(cmd *cobra.Command, args []string) {
53+
cmd.HelpFunc()(cmd, args)
54+
exit(1)
55+
},
56+
}
57+
58+
cmd.AddCommand(NewGitSourceCreateCommand())
59+
60+
return cmd
61+
}
62+
63+
func NewGitSourceCreateCommand() *cobra.Command {
64+
var (
65+
insCloneOpts *git.CloneOptions
66+
gsCloneOpts *git.CloneOptions
67+
)
68+
69+
cmd := &cobra.Command{
70+
Use: "create runtime_name git-source_name",
71+
Short: "add a new git-source to an existing runtime",
72+
Example: util.Doc(`
73+
<BIN> git-source create runtime_name git-source-name https://github.com/owner/repo-name/my-workflow
74+
`),
75+
PreRun: func(cmd *cobra.Command, args []string) {
76+
ctx := cmd.Context()
77+
78+
if len(args) < 1 {
79+
log.G(ctx).Fatal("must enter runtime name")
80+
}
81+
82+
if len(args) < 2 {
83+
log.G(ctx).Fatal("must enter git-source name")
84+
}
85+
86+
if gsCloneOpts.Repo == "" {
87+
log.G(ctx).Fatal("must enter a valid value to --git-src-repo. Example: https://github.com/owner/repo-name/path/to/workflow")
88+
}
89+
90+
if gsCloneOpts.Auth.Password == "" {
91+
gsCloneOpts.Auth.Password = insCloneOpts.Auth.Password
92+
}
93+
94+
insCloneOpts.Parse()
95+
gsCloneOpts.Parse()
96+
},
97+
RunE: func(cmd *cobra.Command, args []string) error {
98+
ctx := cmd.Context()
99+
100+
return RunCreateGitSource(ctx, &GitSourceCreateOptions{
101+
insCloneOpts: insCloneOpts,
102+
gsCloneOpts: gsCloneOpts,
103+
gsName: args[1],
104+
runtimeName: args[0],
105+
fullGsPath: gsCloneOpts.Path(),
106+
})
107+
},
108+
}
109+
110+
insCloneOpts = git.AddFlags(cmd, &git.AddFlagsOptions{
111+
CreateIfNotExist: true,
112+
FS: memfs.New(),
113+
})
114+
gsCloneOpts = git.AddFlags(cmd, &git.AddFlagsOptions{
115+
Prefix: "git-src",
116+
Optional: true,
117+
CreateIfNotExist: true,
118+
FS: memfs.New(),
119+
})
120+
121+
return cmd
122+
}
123+
124+
func RunCreateGitSource(ctx context.Context, opts *GitSourceCreateOptions) error {
125+
gsRepo, gsFs, err := opts.gsCloneOpts.GetRepo(ctx)
126+
if err != nil {
127+
return err
128+
}
129+
130+
fi, err := gsFs.ReadDir(".")
131+
132+
if err != nil {
133+
return fmt.Errorf("failed to read files in git-source repo. Err: %w", err)
134+
}
135+
136+
if len(fi) == 0 {
137+
if err = createDemoWorkflowTemplate(gsFs, opts.gsName, opts.runtimeName); err != nil {
138+
return fmt.Errorf("failed to create demo workflowTemplate: %w", err)
139+
}
140+
141+
_, err = gsRepo.Persist(ctx, &git.PushOptions{
142+
CommitMsg: fmt.Sprintf("Created demo workflow template in %s Directory", opts.gsCloneOpts.Path()),
143+
})
144+
145+
if err != nil {
146+
return fmt.Errorf("failed to push changes. Err: %w", err)
147+
}
148+
}
149+
150+
appDef := &runtime.AppDef{
151+
Name: opts.gsName,
152+
Type: application.AppTypeDirectory,
153+
URL: opts.gsCloneOpts.Repo,
154+
}
155+
if err := appDef.CreateApp(ctx, nil, opts.insCloneOpts, opts.runtimeName, store.Get().CFGitSourceType, nil); err != nil {
156+
return fmt.Errorf("failed to create git-source application. Err: %w", err)
157+
}
158+
159+
log.G(ctx).Infof("done creating a new git-source: '%s'", opts.gsName)
160+
161+
return nil
162+
}
163+
164+
func createDemoWorkflowTemplate(gsFs fs.FS, gsName, runtimeName string) error {
165+
var err error
166+
167+
gsPath := gsFs.Join(apstore.Default.AppsDir, gsName, runtimeName, "demo-wf-template.yaml")
168+
wfTemplate := &wfv1alpha1.WorkflowTemplate{
169+
TypeMeta: metav1.TypeMeta{
170+
Kind: wf.WorkflowTemplateKind,
171+
APIVersion: wfv1alpha1.SchemeGroupVersion.String(),
172+
},
173+
ObjectMeta: metav1.ObjectMeta{
174+
Name: "demo-workflow-template",
175+
Namespace: runtimeName,
176+
},
177+
Spec: wfv1alpha1.WorkflowTemplateSpec{
178+
WorkflowSpec: wfv1alpha1.WorkflowSpec{
179+
Entrypoint: "whalesay",
180+
Templates: []wfv1alpha1.Template{
181+
{
182+
Name: "whalesay",
183+
Container: &v1.Container{
184+
Image: "docker/whalesay",
185+
Command: []string{"cowsay"},
186+
Args: []string{"Hello World"},
187+
},
188+
},
189+
},
190+
},
191+
},
192+
}
193+
if err = gsFs.WriteYamls(gsPath, wfTemplate); err != nil {
194+
return err
195+
}
196+
197+
return err
198+
}

cmd/commands/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ variables in advanced to simplify the use of those commands.
5151
cmd.AddCommand(NewVersionCommand())
5252
cmd.AddCommand(NewConfigCommand())
5353
cmd.AddCommand(NewRuntimeCommand())
54+
cmd.AddCommand(NewGitSourceCommand())
5455

5556
cobra.OnInitialize(func() { postInitCommands(cmd.Commands()) })
5657

cmd/commands/runtime.go

Lines changed: 11 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ import (
3737
apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store"
3838
aputil "github.com/argoproj-labs/argocd-autopilot/pkg/util"
3939
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
40-
wf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"
41-
wfv1alpha1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
4240
"github.com/ghodss/yaml"
4341
"github.com/go-git/go-billy/v5/memfs"
4442
"github.com/juju/ansiterm"
@@ -127,7 +125,7 @@ func NewRuntimeInstallCommand() *cobra.Command {
127125
insCloneOpts.Parse()
128126
if gsCloneOpts.Repo == "" {
129127
host, orgRepo, _, _, _, suffix, _ := aputil.ParseGitUrl(insCloneOpts.Repo)
130-
gsCloneOpts.Repo = host + orgRepo + "_git_source" + suffix
128+
gsCloneOpts.Repo = host + orgRepo + "_git-source" + suffix + "/workflows"
131129
}
132130

133131
gsCloneOpts.Parse()
@@ -243,12 +241,16 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
243241
return fmt.Errorf("failed to create workflows-reporter: %w", err)
244242
}
245243

246-
if err = createDemoWorkflowTemplate(ctx, opts.gsCloneOpts, store.Get().GitSourceName, opts.RuntimeName); err != nil {
247-
return fmt.Errorf("failed to create demo workflowTemplate: %w", err)
248-
}
244+
gsPath := opts.gsCloneOpts.FS.Join(apstore.Default.AppsDir, store.Get().GitSourceName, opts.RuntimeName)
245+
fullGsPath := opts.gsCloneOpts.FS.Join(opts.gsCloneOpts.FS.Root(), gsPath)[1:]
249246

250-
if err = createGitSource(ctx, opts.insCloneOpts, opts.gsCloneOpts, store.Get().GitSourceName, opts.RuntimeName,
251-
opts.commonConfig.CodefreshBaseURL); err != nil {
247+
if err = RunCreateGitSource(ctx, &GitSourceCreateOptions{
248+
insCloneOpts: opts.insCloneOpts,
249+
gsCloneOpts: opts.gsCloneOpts,
250+
gsName: store.Get().GitSourceName,
251+
runtimeName: opts.RuntimeName,
252+
fullGsPath: fullGsPath,
253+
}); err != nil {
252254
return fmt.Errorf("failed to create `%s`: %w", store.Get().GitSourceName, err)
253255
}
254256

@@ -258,7 +260,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
258260

259261
func NewRuntimeListCommand() *cobra.Command {
260262
cmd := &cobra.Command{
261-
Use: "list ",
263+
Use: "list [runtime_name]",
262264
Short: "List all Codefresh runtimes",
263265
Example: util.Doc(`<BIN> runtime list`),
264266
RunE: func(_ *cobra.Command, _ []string) error {
@@ -759,61 +761,3 @@ func createSensor(repofs fs.FS, name, path, namespace, eventSourceName, trigger,
759761
})
760762
return repofs.WriteYamls(repofs.Join(path, "sensor.yaml"), sensor)
761763
}
762-
763-
func createDemoWorkflowTemplate(ctx context.Context, gsCloneOpts *git.CloneOptions, gsName, runtimeName string) error {
764-
gsRepo, gsFs, err := gsCloneOpts.GetRepo(ctx)
765-
if err != nil {
766-
return err
767-
}
768-
769-
gsPath := gsCloneOpts.FS.Join(apstore.Default.AppsDir, gsName, runtimeName)
770-
wfTemplate := &wfv1alpha1.WorkflowTemplate{
771-
TypeMeta: metav1.TypeMeta{
772-
Kind: wf.WorkflowTemplateKind,
773-
APIVersion: wfv1alpha1.SchemeGroupVersion.String(),
774-
},
775-
ObjectMeta: metav1.ObjectMeta{
776-
Name: "demo-workflow-template",
777-
Namespace: runtimeName,
778-
},
779-
Spec: wfv1alpha1.WorkflowTemplateSpec{
780-
WorkflowSpec: wfv1alpha1.WorkflowSpec{
781-
Entrypoint: "whalesay",
782-
Templates: []wfv1alpha1.Template{
783-
{
784-
Name: "whalesay",
785-
Container: &v1.Container{
786-
Image: "docker/whalesay",
787-
Command: []string{"cowsay"},
788-
Args: []string{"Hello World"},
789-
},
790-
},
791-
},
792-
},
793-
},
794-
}
795-
if err = gsFs.WriteYamls(gsFs.Join(gsPath, "demo-wf-template.yaml"), wfTemplate); err != nil {
796-
return err
797-
}
798-
799-
_, err = gsRepo.Persist(ctx, &git.PushOptions{
800-
CommitMsg: fmt.Sprintf("Created %s Directory", gsPath),
801-
})
802-
return err
803-
}
804-
805-
func createGitSource(ctx context.Context, insCloneOpts *git.CloneOptions, gsCloneOpts *git.CloneOptions, gsName, runtimeName, cfBaseURL string) error {
806-
gsPath := gsCloneOpts.FS.Join(apstore.Default.AppsDir, gsName, runtimeName)
807-
fullGsPath := gsCloneOpts.FS.Join(gsCloneOpts.FS.Root(), gsPath)[1:]
808-
809-
appDef := &runtime.AppDef{
810-
Name: gsName,
811-
Type: application.AppTypeDirectory,
812-
URL: gsCloneOpts.URL() + fullGsPath,
813-
}
814-
if err := appDef.CreateApp(ctx, nil, insCloneOpts, runtimeName, store.Get().CFGitSourceType, nil); err != nil {
815-
return fmt.Errorf("failed to create git-source: %w", err)
816-
}
817-
818-
return nil
819-
}

docs/commands/cli-v2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ cli-v2 [flags]
3232
### SEE ALSO
3333

3434
* [cli-v2 config](cli-v2_config.md) - Manage Codefresh authentication contexts
35+
* [cli-v2 git-source](cli-v2_git-source.md) - Manage git-sources of Codefresh runtimes
3536
* [cli-v2 runtime](cli-v2_runtime.md) - Manage Codefresh runtimes
3637
* [cli-v2 version](cli-v2_version.md) - Show cli version
3738

docs/commands/cli-v2_git-source.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## cli-v2 git-source
2+
3+
Manage git-sources of Codefresh runtimes
4+
5+
```
6+
cli-v2 git-source [flags]
7+
```
8+
9+
### Options
10+
11+
```
12+
-h, --help help for git-source
13+
```
14+
15+
### Options inherited from parent commands
16+
17+
```
18+
--auth-context string Run the next command using a specific authentication context
19+
--cfconfig string Custom path for authentication contexts config file (default "/home/user")
20+
--insecure Disable certificate validation for TLS connections (e.g. to g.codefresh.io)
21+
--request-timeout duration Request timeout (default 30s)
22+
```
23+
24+
### SEE ALSO
25+
26+
* [cli-v2](cli-v2.md) - cli-v2 is used for installing and managing codefresh installations using gitops
27+
* [cli-v2 git-source create](cli-v2_git-source_create.md) - add a new git-source to an existing runtime
28+

0 commit comments

Comments
 (0)