Skip to content

Commit

Permalink
CR-18810-fix-create-gs (#695)
Browse files Browse the repository at this point in the history
## What
* fixed `cf git-source create` command to not crash (and handle runtime
namespace correctly)
* fixed `cf runtime uninstall` to get runtime namespace from platform

## Why
the `cf git-source create` command was crashing, the `cf runtime
uninstall` was working (even when runtimeName !== runtimeNamespace), but
in a less safe way
  • Loading branch information
ATGardner authored May 23, 2023
1 parent 7ab21e9 commit 7e5ecfe
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 138 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.1.46
VERSION=v0.1.47

OUT_DIR=dist
YEAR?=$(shell date +"%Y")
Expand Down
10 changes: 5 additions & 5 deletions cmd/commands/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
"github.com/codefresh-io/cli-v2/pkg/util"
kubeutil "github.com/codefresh-io/cli-v2/pkg/util/kube"
kustutil "github.com/codefresh-io/cli-v2/pkg/util/kust"
platmodel "github.com/codefresh-io/go-sdk/pkg/codefresh/model"
"github.com/ghodss/yaml"

"github.com/Masterminds/semver/v3"
"github.com/argoproj-labs/argocd-autopilot/pkg/kube"
apkube "github.com/argoproj-labs/argocd-autopilot/pkg/kube"
platmodel "github.com/codefresh-io/go-sdk/pkg/codefresh/model"
"github.com/ghodss/yaml"
"github.com/juju/ansiterm"
"github.com/spf13/cobra"
kusttypes "sigs.k8s.io/kustomize/api/types"
Expand All @@ -51,7 +51,7 @@ type (
tag string
dryRun bool
skipTLSValidation bool
kubeFactory kube.Factory
kubeFactory apkube.Factory
}

ClusterRemoveOptions struct {
Expand Down Expand Up @@ -147,7 +147,7 @@ func newClusterAddCommand() *cobra.Command {
cmd.Flags().StringVar(&opts.tag, "tag", "", "[dev only] - use a specific tag of the csdp-add-cluster image")

util.Die(cmd.Flags().MarkHidden("tag"))
opts.kubeFactory = kube.AddFlags(cmd.Flags())
opts.kubeFactory = apkube.AddFlags(cmd.Flags())

return cmd
}
Expand Down
24 changes: 2 additions & 22 deletions cmd/commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ import (
"github.com/codefresh-io/cli-v2/pkg/util/kube"
routingutil "github.com/codefresh-io/cli-v2/pkg/util/routing"

"github.com/argoproj-labs/argocd-autopilot/pkg/fs"
"github.com/argoproj-labs/argocd-autopilot/pkg/git"
apfs "github.com/argoproj-labs/argocd-autopilot/pkg/fs"
apgit "github.com/argoproj-labs/argocd-autopilot/pkg/git"
aputil "github.com/argoproj-labs/argocd-autopilot/pkg/util"
platmodel "github.com/codefresh-io/go-sdk/pkg/codefresh/model"
Expand Down Expand Up @@ -487,7 +486,6 @@ func getKubeContextName(context, kubeconfig *pflag.Flag) (string, error) {

return contextName, context.Value.Set(contextName)
}

type SelectItem struct {
Value string
Label string
Expand Down Expand Up @@ -744,24 +742,6 @@ func suggestIscRepo(ctx context.Context, suggestedSharedConfigRepo string) (stri
return setIscRepoResponse, nil
}

func isRuntimeManaged(ctx context.Context, runtimeName string) (bool, error) {
rt, err := getRuntime(ctx, runtimeName)
if err != nil {
return false, err
}

return rt.Managed, nil
}

func getRuntimeInstallationType(ctx context.Context, runtimeName string) (*platmodel.InstallationType, error) {
rt, err := getRuntime(ctx, runtimeName)
if err != nil {
return nil, err
}

return &rt.InstallationType, nil
}

func ensureRuntimeOnKubeContext(ctx context.Context, kubeconfig string, runtimeName string, kubeContextName string) error {
rt, err := getRuntime(ctx, runtimeName)
if err != nil {
Expand Down Expand Up @@ -797,7 +777,7 @@ func getRuntime(ctx context.Context, runtimeName string) (*platmodel.Runtime, er
return rt, nil
}

func patchRuntimeRepo(ctx context.Context, cloneOpts *git.CloneOptions, msg string, f func(fs fs.FS) error) error {
func patchRuntimeRepo(ctx context.Context, cloneOpts *apgit.CloneOptions, msg string, f func(fs apfs.FS) error) error {
r, fs, err := cloneOpts.GetRepo(ctx)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/commands/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

cfgit "github.com/codefresh-io/cli-v2/pkg/git"
"github.com/codefresh-io/cli-v2/pkg/store"

"github.com/stretchr/testify/assert"
)

Expand Down
47 changes: 26 additions & 21 deletions cmd/commands/git-source.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
routingutil "github.com/codefresh-io/cli-v2/pkg/util/routing"
wfutil "github.com/codefresh-io/cli-v2/pkg/util/workflow"

"github.com/argoproj-labs/argocd-autopilot/pkg/application"
"github.com/argoproj-labs/argocd-autopilot/pkg/fs"
"github.com/argoproj-labs/argocd-autopilot/pkg/git"
apapp "github.com/argoproj-labs/argocd-autopilot/pkg/application"
apfs "github.com/argoproj-labs/argocd-autopilot/pkg/fs"
apgit "github.com/argoproj-labs/argocd-autopilot/pkg/git"
aputil "github.com/argoproj-labs/argocd-autopilot/pkg/util"
apicommon "github.com/argoproj/argo-events/pkg/apis/common"
eventsourcereg "github.com/argoproj/argo-events/pkg/apis/eventsource"
Expand All @@ -53,8 +53,8 @@ import (

type (
GitSourceCreateOptions struct {
InsCloneOpts *git.CloneOptions
GsCloneOpts *git.CloneOptions
InsCloneOpts *apgit.CloneOptions
GsCloneOpts *apgit.CloneOptions
GsName string
RuntimeName string
RuntimeNamespace string
Expand Down Expand Up @@ -82,22 +82,22 @@ type (
GitSourceEditOptions struct {
RuntimeName string
GsName string
GsCloneOpts *git.CloneOptions
GsCloneOpts *apgit.CloneOptions
Include *string
Exclude *string
}

gitSourceCalendarDemoPipelineOptions struct {
runtimeName string
gsCloneOpts *git.CloneOptions
gsFs fs.FS
gsCloneOpts *apgit.CloneOptions
gsFs apfs.FS
}

gitSourceGitDemoPipelineOptions struct {
runtimeName string
gsCloneOpts *git.CloneOptions
gsCloneOpts *apgit.CloneOptions
gitProvider cfgit.Provider
gsFs fs.FS
gsFs apfs.FS
hostName string
ingressHost string
skipIngress bool
Expand Down Expand Up @@ -132,7 +132,7 @@ func NewGitSourceCommand() *cobra.Command {

func NewGitSourceCreateCommand() *cobra.Command {
var (
gsCloneOpts *git.CloneOptions
gsCloneOpts *apgit.CloneOptions
gitProvider cfgit.Provider
createRepo bool
include string
Expand Down Expand Up @@ -188,17 +188,22 @@ func NewGitSourceCreateCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

runtimeNamespace := args[0]
namespace := cmd.Flag("namespace").Value.String()
if namespace != "" {
runtimeNamespace = namespace
runtimeName := args[0]
runtime, err := getRuntime(ctx, runtimeName)
if err != nil {
return err
}

runtimeNamespace := runtimeName
if runtime.Metadata.Namespace != nil {
runtimeNamespace = *runtime.Metadata.Namespace
}

return RunGitSourceCreate(ctx, &GitSourceCreateOptions{
GsCloneOpts: gsCloneOpts,
GitProvider: gitProvider,
GsName: args[1],
RuntimeName: args[0],
RuntimeName: runtimeName,
RuntimeNamespace: runtimeNamespace,
CreateDemoResources: false,
Include: include,
Expand Down Expand Up @@ -245,7 +250,7 @@ func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error
return nil
}

func ensureGitSourceDirectory(ctx context.Context, opts *GitSourceCreateOptions, gsRepo git.Repository, gsFs fs.FS) error {
func ensureGitSourceDirectory(ctx context.Context, opts *GitSourceCreateOptions, gsRepo apgit.Repository, gsFs apfs.FS) error {
fi, err := gsFs.ReadDir(".")
if err != nil {
return fmt.Errorf("failed to read files in git-source repo. Err: %w", err)
Expand Down Expand Up @@ -411,7 +416,7 @@ func RunGitSourceDelete(ctx context.Context, opts *GitSourceDeleteOptions) error

func NewGitSourceEditCommand() *cobra.Command {
var (
gsCloneOpts *git.CloneOptions
gsCloneOpts *apgit.CloneOptions
include string
exclude string
)
Expand Down Expand Up @@ -492,7 +497,7 @@ func RunGitSourceEdit(ctx context.Context, opts *GitSourceEditOptions) error {
return nil
}

func createDemoResources(ctx context.Context, opts *GitSourceCreateOptions, gsRepo git.Repository, gsFs fs.FS) error {
func createDemoResources(ctx context.Context, opts *GitSourceCreateOptions, gsRepo apgit.Repository, gsFs apfs.FS) error {
fi, err := gsFs.ReadDir(".")
if err != nil {
return fmt.Errorf("failed to read files in git-source repo. Err: %w", err)
Expand Down Expand Up @@ -1391,7 +1396,7 @@ func deleteCommonRedundantFields(crd map[string]interface{}) {
}

func writeObjectToYaml[Object any](
gsFs fs.FS,
gsFs apfs.FS,
filePath string,
object Object,
cleanUpFunc func(Object) (map[string]interface{}, error),
Expand Down Expand Up @@ -1425,7 +1430,7 @@ func legacyGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) er

appDef := &runtime.AppDef{
Name: opts.GsName,
Type: application.AppTypeDirectory,
Type: apapp.AppTypeDirectory,
URL: opts.GsCloneOpts.Repo,
Include: opts.Include,
Exclude: opts.Exclude,
Expand Down
3 changes: 2 additions & 1 deletion cmd/commands/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"net/http"
"testing"

kubemocks "github.com/argoproj-labs/argocd-autopilot/pkg/kube/mocks"
cfgit "github.com/codefresh-io/cli-v2/pkg/git"
gitmocks "github.com/codefresh-io/cli-v2/pkg/git/mocks"

kubemocks "github.com/argoproj-labs/argocd-autopilot/pkg/kube/mocks"
platmodel "github.com/codefresh-io/go-sdk/pkg/codefresh/model"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
Expand Down
Loading

0 comments on commit 7e5ecfe

Please sign in to comment.