Skip to content

rev-CR-10299 #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.0.317
VERSION=v0.0.318

OUT_DIR=dist
YEAR?=$(shell date +"%Y")
Expand Down
176 changes: 50 additions & 126 deletions cmd/commands/git-source.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"strings"
"time"

"github.com/Masterminds/semver/v3"
apcmd "github.com/argoproj-labs/argocd-autopilot/cmd/commands"
"github.com/argoproj-labs/argocd-autopilot/pkg/application"
"github.com/argoproj-labs/argocd-autopilot/pkg/fs"
Expand All @@ -40,8 +39,8 @@ import (
"github.com/codefresh-io/cli-v2/pkg/store"
"github.com/codefresh-io/cli-v2/pkg/util"
apu "github.com/codefresh-io/cli-v2/pkg/util/aputil"
eventsutil "github.com/codefresh-io/cli-v2/pkg/util/events"
ingressutil "github.com/codefresh-io/cli-v2/pkg/util/ingress"
eventsutil "github.com/codefresh-io/cli-v2/pkg/util/events"
wfutil "github.com/codefresh-io/cli-v2/pkg/util/workflow"
billyUtils "github.com/go-git/go-billy/v5/util"
"github.com/juju/ansiterm"
Expand Down Expand Up @@ -104,8 +103,6 @@ type (
}
)

var versionOfGitSourceByAppProxyRefactor = semver.MustParse("0.0.317")

func NewGitSourceCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "git-source",
Expand Down Expand Up @@ -208,56 +205,37 @@ func NewGitSourceCreateCommand() *cobra.Command {
}

func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error {
version, err := getRuntimeVersion(ctx, opts.RuntimeName)
// upsert git-source repo

gsRepo, gsFs, err := opts.GsCloneOpts.GetRepo(ctx)
if err != nil {
return err
return fmt.Errorf("failed to clone git-source repo: %w", err)
}

if !version.LessThan(versionOfGitSourceByAppProxyRefactor) {
appProxy, err := cfConfig.NewClient().AppProxy(ctx, opts.RuntimeName, store.Get().InsecureIngressHost)
if err != nil {
return err
}

appSpecifier := opts.GsCloneOpts.Repo
isInternal := util.StringIndexOf(store.Get().CFInternalGitSources, opts.GsName) > -1

err = appProxy.AppProxyGitSources().Create(ctx, opts.GsName, appSpecifier, opts.RuntimeName, opts.RuntimeName, isInternal)
if err != nil {
return fmt.Errorf("failed to create git-source: %w", err)
if opts.CreateDemoResources {
if err := createDemoResources(ctx, opts, gsRepo, gsFs); err != nil {
return fmt.Errorf("failed to create git-source demo resources: %w", err)
}
} else {
log.G(ctx).Infof("runtime \"%s\" is using a depracated git-source api. Versions %s and up use the app-proxy for this command", opts.RuntimeName, minAddClusterSupportedVersion)
// upsert git-source repo
gsRepo, gsFs, err := opts.GsCloneOpts.GetRepo(ctx)
if err != nil {
return fmt.Errorf("failed to clone git-source repo: %w", err)
}

if opts.CreateDemoResources {
if err := createDemoResources(ctx, opts, gsRepo, gsFs); err != nil {
return fmt.Errorf("failed to create git-source demo resources: %w", err)
}
} else {
if err := createPlaceholderIfNeeded(ctx, opts, gsRepo, gsFs); err != nil {
return fmt.Errorf("failed to create a git-source placeholder: %w", err)
}
if err := createPlaceholderIfNeeded(ctx, opts, gsRepo, gsFs); err != nil {
return fmt.Errorf("failed to create a git-source placeholder: %w", err)
}
}

appDef := &runtime.AppDef{
Name: opts.GsName,
Type: application.AppTypeDirectory,
URL: opts.GsCloneOpts.Repo,
}
appDef := &runtime.AppDef{
Name: opts.GsName,
Type: application.AppTypeDirectory,
URL: opts.GsCloneOpts.Repo,
}

appDef.IsInternal = util.StringIndexOf(store.Get().CFInternalGitSources, appDef.Name) > -1
appDef.IsInternal = util.StringIndexOf(store.Get().CFInternalGitSources, appDef.Name) > -1

if err := appDef.CreateApp(ctx, nil, opts.InsCloneOpts, opts.RuntimeName, store.Get().CFGitSourceType, opts.Include, ""); err != nil {
return fmt.Errorf("failed to create git-source application. Err: %w", err)
}
if err := appDef.CreateApp(ctx, nil, opts.InsCloneOpts, opts.RuntimeName, store.Get().CFGitSourceType, opts.Include, ""); err != nil {
return fmt.Errorf("failed to create git-source application. Err: %w", err)
}

log.G(ctx).Infof("Successfully created git-source: \"%s\"", opts.GsName)

return nil
}

Expand Down Expand Up @@ -390,7 +368,7 @@ func createCronExampleEventSource() *eventsourcev1alpha1.EventSource {
Name: store.Get().CronExampleEventSourceName,
},
Spec: eventsourcev1alpha1.EventSourceSpec{
Template: tpl,
Template: tpl,
EventBusName: store.Get().EventBusName,
Calendar: map[string]eventsourcev1alpha1.CalendarEventSource{
store.Get().CronExampleEventName: {
Expand All @@ -412,7 +390,7 @@ func createCronExampleSensor(triggers []sensorsv1alpha1.Trigger) (*sensorsv1alph

tpl := &sensorsv1alpha1.Template{
ServiceAccountName: "argo-server",
Container: &corev1.Container{},
Container: &corev1.Container{},
}

if store.Get().SetDefaultResources {
Expand All @@ -429,7 +407,7 @@ func createCronExampleSensor(triggers []sensorsv1alpha1.Trigger) (*sensorsv1alph
},
Spec: sensorsv1alpha1.SensorSpec{
EventBusName: "codefresh-eventbus",
Template: tpl,
Template: tpl,
Dependencies: dependencies,
Triggers: triggers,
},
Expand Down Expand Up @@ -602,36 +580,19 @@ func NewGitSourceDeleteCommand() *cobra.Command {
}

func RunGitSourceDelete(ctx context.Context, opts *GitSourceDeleteOptions) error {
version, err := getRuntimeVersion(ctx, opts.RuntimeName)
if err != nil {
return err
}

if !version.LessThan(versionOfGitSourceByAppProxyRefactor) {
appProxy, err := cfConfig.NewClient().AppProxy(ctx, opts.RuntimeName, store.Get().InsecureIngressHost)
if err != nil {
return err
}

err = appProxy.AppProxyGitSources().Delete(ctx, opts.GsName)
if err != nil {
return fmt.Errorf("failed to delete git-source: %w", err)
}
} else {
log.G(ctx).Infof("runtime \"%s\" is using a depracated git-source api. Versions %s and up use the app-proxy for this command", opts.RuntimeName, minAddClusterSupportedVersion)
err = apcmd.RunAppDelete(ctx, &apcmd.AppDeleteOptions{
CloneOpts: opts.InsCloneOpts,
ProjectName: opts.RuntimeName,
AppName: opts.GsName,
Global: false,
})
err := apcmd.RunAppDelete(ctx, &apcmd.AppDeleteOptions{
CloneOpts: opts.InsCloneOpts,
ProjectName: opts.RuntimeName,
AppName: opts.GsName,
Global: false,
})

if err != nil {
return fmt.Errorf("failed to delete the git-source %s. Err: %w", opts.GsName, err)
}
if err != nil {
return fmt.Errorf("failed to delete the git-source %s. Err: %w", opts.GsName, err)
}

log.G(ctx).Infof("Successfully deleted the git-source: %s", opts.GsName)

return nil
}

Expand Down Expand Up @@ -701,53 +662,29 @@ func NewGitSourceEditCommand() *cobra.Command {
func RunGitSourceEdit(ctx context.Context, opts *GitSourceEditOptions) error {
repo, fs, err := opts.InsCloneOpts.GetRepo(ctx)
if err != nil {
return err
return fmt.Errorf("failed to clone the installation repo, attemptint to edit git-source %s. Err: %w", opts.GsName, err)
}

version, err := getRuntimeVersion(ctx, opts.RuntimeName)
c := &dirConfig{}
fileName := fs.Join(apstore.Default.AppsDir, opts.GsName, opts.RuntimeName, "config_dir.json")
err = fs.ReadJson(fileName, c)
if err != nil {
return err
return fmt.Errorf("failed to read the %s of git-source: %s. Err: %w", fileName, opts.GsName, err)
}

if !version.LessThan(versionOfGitSourceByAppProxyRefactor) {
appProxy, err := cfConfig.NewClient().AppProxy(ctx, opts.RuntimeName, store.Get().InsecureIngressHost)
if err != nil {
return err
}

err = appProxy.AppProxyGitSources().Edit(ctx, opts.GsName, opts.GsCloneOpts.Repo)
if err != nil {
return fmt.Errorf("failed to edit git-source: %w", err)
}
} else {
log.G(ctx).Infof("runtime \"%s\" is using a depracated git-source api. Versions %s and up use the app-proxy for this command", opts.RuntimeName, minAddClusterSupportedVersion)

if err != nil {
return fmt.Errorf("failed to clone the installation repo, attemptint to edit git-source %s. Err: %w", opts.GsName, err)
}
c := &dirConfig{}
fileName := fs.Join(apstore.Default.AppsDir, opts.GsName, opts.RuntimeName, "config_dir.json")
err = fs.ReadJson(fileName, c)
if err != nil {
return fmt.Errorf("failed to read the %s of git-source: %s. Err: %w", fileName, opts.GsName, err)
}

c.Config.SrcPath = opts.GsCloneOpts.Path()
c.Config.SrcRepoURL = opts.GsCloneOpts.URL()
c.Config.SrcTargetRevision = opts.GsCloneOpts.Revision()
c.Config.SrcPath = opts.GsCloneOpts.Path()
c.Config.SrcRepoURL = opts.GsCloneOpts.URL()
c.Config.SrcTargetRevision = opts.GsCloneOpts.Revision()

err = fs.WriteJson(fileName, c)
if err != nil {
return fmt.Errorf("failed to write the updated %s of git-source: %s. Err: %w", fileName, opts.GsName, err)
}
err = fs.WriteJson(fileName, c)
if err != nil {
return fmt.Errorf("failed to write the updated %s of git-source: %s. Err: %w", fileName, opts.GsName, err)
}

log.G(ctx).Info("Pushing updated GitSource to the installation repo")
if err := apu.PushWithMessage(ctx, repo, fmt.Sprintf("Persisted an updated git-source \"%s\"", opts.GsName)); err != nil {
return fmt.Errorf("failed to persist the updated git-source: %s. Err: %w", opts.GsName, err)
}
log.G(ctx).Info("Pushing updated GitSource to the installation repo")
if err := apu.PushWithMessage(ctx, repo, fmt.Sprintf("Persisted an updated git-source \"%s\"", opts.GsName)); err != nil {
return fmt.Errorf("failed to persist the updated git-source: %s. Err: %w", opts.GsName, err)
}

log.G(ctx).Infof("Successfully created git-source: \"%s\"", opts.GsName)
return nil
}

Expand Down Expand Up @@ -908,7 +845,7 @@ func createGithubExampleEventSource(repoURL string, ingressHost string, runtimeN
},
Spec: eventsourcev1alpha1.EventSourceSpec{
EventBusName: store.Get().EventBusName,
Template: tpl,
Template: tpl,
Service: &eventsourcev1alpha1.Service{
Ports: []corev1.ServicePort{
{
Expand Down Expand Up @@ -1022,7 +959,7 @@ func createGithubExampleSensor() *sensorsv1alpha1.Sensor {
}

tpl := &sensorsv1alpha1.Template{
Container: &corev1.Container{},
Container: &corev1.Container{},
ServiceAccountName: store.Get().WorkflowTriggerServiceAccount,
}

Expand All @@ -1040,7 +977,7 @@ func createGithubExampleSensor() *sensorsv1alpha1.Sensor {
},
Spec: sensorsv1alpha1.SensorSpec{
EventBusName: store.Get().EventBusName,
Template: tpl,
Template: tpl,
Dependencies: dependencies,
Triggers: triggers,
},
Expand Down Expand Up @@ -1220,16 +1157,3 @@ func unMarshalCustomObject(obj interface{}) (map[string]interface{}, error) {
}
return crd, nil
}

func getRuntimeVersion(ctx context.Context, runtimeName string) (*semver.Version, error) {
rt, err := cfConfig.NewClient().V2().Runtime().Get(ctx, runtimeName)
if err != nil {
return nil, err
}

if rt.RuntimeVersion == nil {
return nil, fmt.Errorf("runtime \"%s\" has no version", runtimeName)
}

return semver.MustParse(*rt.RuntimeVersion), nil
}
20 changes: 10 additions & 10 deletions cmd/commands/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func runtimeInstallCommandPreRunHandler(cmd *cobra.Command, opts *RuntimeInstall
var err error
handleCliStep(reporter.InstallPhasePreCheckStart, "Starting pre checks", nil, true, false)

opts.Version, err = getVersionIfExists(opts.versionStr)
opts.Version, err = getVersionIfExists(opts)
handleCliStep(reporter.InstallStepPreCheckValidateRuntimeVersion, "Validating runtime version", err, true, false)
if err != nil {
return err
Expand Down Expand Up @@ -2357,19 +2357,19 @@ func validateRuntimeName(runtime string) error {
return err
}

func getVersionIfExists(opts *RuntimeInstallOptions) (*semver.Version, error) {
if opts.versionStr != "" {
log.G().Infof("vesionStr: %s", opts.versionStr)
return semver.NewVersion(opts.versionStr)
}

return nil, nil
}

func initializeGitSourceCloneOpts(opts *RuntimeInstallOptions) {
opts.GsCloneOpts.Provider = opts.InsCloneOpts.Provider
opts.GsCloneOpts.Auth = opts.InsCloneOpts.Auth
opts.GsCloneOpts.Progress = opts.InsCloneOpts.Progress
host, orgRepo, _, _, _, suffix, _ := aputil.ParseGitUrl(opts.InsCloneOpts.Repo)
opts.GsCloneOpts.Repo = host + orgRepo + "_git-source" + suffix + "/resources" + "_" + opts.RuntimeName
}

func getVersionIfExists(versionStr string) (*semver.Version, error) {
if versionStr != "" {
log.G().Infof("vesionStr: %s", versionStr)
return semver.NewVersion(versionStr)
}

return nil, nil
}
4 changes: 2 additions & 2 deletions docs/releases/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cf version

```bash
# download and extract the binary
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.317/cf-linux-amd64.tar.gz | tar zx
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.318/cf-linux-amd64.tar.gz | tar zx

# move the binary to your $PATH
mv ./cf-linux-amd64 /usr/local/bin/cf
Expand All @@ -36,7 +36,7 @@ cf version

```bash
# download and extract the binary
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.317/cf-darwin-amd64.tar.gz | tar zx
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.318/cf-darwin-amd64.tar.gz | tar zx

# move the binary to your $PATH
mv ./cf-darwin-amd64 /usr/local/bin/cf
Expand Down
2 changes: 1 addition & 1 deletion manifests/runtime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: "{{ namespace }}"
spec:
defVersion: 1.0.1
version: 0.0.317
version: 0.0.318
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
components:
- name: events
Expand Down
1 change: 0 additions & 1 deletion pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ type Store struct {
SccName string
CFInternalGitSources []string
CFInternalReporters []string
VersionOfGitSourceAppProxyRefactor string
}

// Get returns the global store
Expand Down