Skip to content

Commit

Permalink
feat(app): add Name to ApplicationSource
Browse files Browse the repository at this point in the history
  • Loading branch information
CefBoud committed Oct 20, 2024
1 parent fdf2503 commit 220edee
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 5 deletions.
95 changes: 90 additions & 5 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ func printHeader(acdClient argocdclient.Client, app *argoappv1.Application, ctx
}
}

// getSourceNameToPositionMap returns a map of source name to position
func getSourceNameToPositionMap(app *argoappv1.Application) map[string]int64 {
sourceNameToPosition := make(map[string]int64)
for i, s := range app.Spec.Sources {
if s.Name != "" {
sourceNameToPosition[s.Name] = i + 1

Check failure on line 322 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Build & cache Go code

cannot use i + 1 (value of type int) as int64 value in assignment

Check failure on line 322 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Lint Go code

cannot use i + 1 (value of type int) as int64 value in assignment

Check failure on line 322 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Check changes to generated code

cannot use i + 1 (value of type int) as int64 value in assignment
}
}
return sourceNameToPosition
}

// NewApplicationGetCommand returns a new instance of an `argocd app get` command
func NewApplicationGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var (
Expand All @@ -324,6 +335,7 @@ func NewApplicationGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com
showOperation bool
appNamespace string
sourcePosition int
sourceName string
)
command := &cobra.Command{
Use: "get APPNAME",
Expand All @@ -347,6 +359,9 @@ func NewApplicationGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com
# Show application parameters and overrides for a source at position 1 under spec.sources of app my-app
argocd app get my-app --show-params --source-position 1
# Show application parameters and overrides for a source named "test"
argocd app get my-app --show-params --source-name test
# Refresh application data when retrieving
argocd app get my-app --refresh
Expand Down Expand Up @@ -379,6 +394,19 @@ func NewApplicationGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com
})
errors.CheckError(err)

if sourceName != "" && sourcePosition != -1 {
errors.CheckError(fmt.Errorf("Only one of source-position and source-name can be specified."))
}

if len(sourceName) != "" {

Check failure on line 401 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Build & cache Go code

invalid operation: len(sourceName) != "" (mismatched types int and untyped string)

Check failure on line 401 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Lint Go code

invalid operation: len(sourceName) != "" (mismatched types int and untyped string)

Check failure on line 401 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Check changes to generated code

invalid operation: len(sourceName) != "" (mismatched types int and untyped string)
sourceNameToPosition := getSourceNameToPositionMap(app)
if pos, ok := sourceNameToPosition[sourceName]; !ok {
log.Fatal("Unknown source name '%s'", sourceName)
} else {
sourcePosition = pos

Check failure on line 406 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Build & cache Go code

cannot use pos (variable of type int64) as int value in assignment

Check failure on line 406 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Lint Go code

cannot use pos (variable of type int64) as int value in assignment

Check failure on line 406 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Check changes to generated code

cannot use pos (variable of type int64) as int value in assignment
}
}

if app.Spec.HasMultipleSources() {
if sourcePosition <= 0 {
errors.CheckError(fmt.Errorf("Source position should be specified and must be greater than 0 for applications with multiple sources"))
Expand Down Expand Up @@ -433,6 +461,7 @@ func NewApplicationGetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com
command.Flags().BoolVar(&hardRefresh, "hard-refresh", false, "Refresh application data as well as target manifests cache")
command.Flags().StringVarP(&appNamespace, "app-namespace", "N", "", "Only get application from namespace")
command.Flags().IntVar(&sourcePosition, "source-position", -1, "Position of the source from the list of sources of the app. Counting starts at 1.")
command.Flags().StringVar(&sourceName, "source-name", "", "Name of the source from the list of sources of the app.")
return command
}

Expand Down Expand Up @@ -762,6 +791,7 @@ func NewApplicationSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com
appOpts cmdutil.AppOptions
appNamespace string
sourcePosition int
sourceName string
)
command := &cobra.Command{
Use: "set APPNAME",
Expand All @@ -776,6 +806,9 @@ func NewApplicationSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com
# Set and override application parameters for a source at position 1 under spec.sources of app my-app. source-position starts at 1.
argocd app set my-app --source-position 1 --repo https://github.com/argoproj/argocd-example-apps.git
# Set and override application parameters for a source named "test" under spec.sources of app my-app.
argocd app set my-app --source-name test --repo https://github.com/argoproj/argocd-example-apps.git
# Set application parameters and specify the namespace
argocd app set my-app --parameter key1=value1 --parameter key2=value2 --namespace my-namespace
`),
Expand All @@ -794,6 +827,19 @@ func NewApplicationSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com
app, err := appIf.Get(ctx, &application.ApplicationQuery{Name: &appName, AppNamespace: &appNs})
errors.CheckError(err)

if sourceName != "" && sourcePosition != -1 {
errors.CheckError(fmt.Errorf("Only one of source-position and source-name can be specified."))
}

if len(sourceName) != "" {

Check failure on line 834 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Build & cache Go code

invalid operation: len(sourceName) != "" (mismatched types int and untyped string)

Check failure on line 834 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Lint Go code

invalid operation: len(sourceName) != "" (mismatched types int and untyped string)

Check failure on line 834 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Check changes to generated code

invalid operation: len(sourceName) != "" (mismatched types int and untyped string)
sourceNameToPosition := getSourceNameToPositionMap(app)
if pos, ok := sourceNameToPosition[sourceName]; !ok {
log.Fatal("Unknown source name '%s'", sourceName)
} else {
sourcePosition = pos

Check failure on line 839 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Build & cache Go code

cannot use pos (variable of type int64) as int value in assignment

Check failure on line 839 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Lint Go code

cannot use pos (variable of type int64) as int value in assignment

Check failure on line 839 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Check changes to generated code

cannot use pos (variable of type int64) as int value in assignment
}
}

if app.Spec.HasMultipleSources() {
if sourcePosition <= 0 {
errors.CheckError(fmt.Errorf("Source position should be specified and must be greater than 0 for applications with multiple sources"))
Expand Down Expand Up @@ -823,6 +869,7 @@ func NewApplicationSetCommand(clientOpts *argocdclient.ClientOptions) *cobra.Com
cmdutil.AddAppFlags(command, &appOpts)
command.Flags().StringVarP(&appNamespace, "app-namespace", "N", "", "Set application parameters in namespace")
command.Flags().IntVar(&sourcePosition, "source-position", -1, "Position of the source from the list of sources of the app. Counting starts at 1.")
command.Flags().StringVar(&sourceName, "source-name", "", "Name of the source from the list of sources of the app.")
return command
}

Expand Down Expand Up @@ -872,6 +919,9 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
# Unset kustomize override suffix for source at position 1 under spec.sources of app my-app. source-position starts at 1.
argocd app unset my-app --source-position 1 --namesuffix
# Unset kustomize override suffix for source named "test" under spec.sources of app my-app.
argocd app unset my-app --source-name test --namesuffix
# Unset parameter override
argocd app unset my-app -p COMPONENT=PARAM`,

Expand All @@ -889,6 +939,19 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
app, err := appIf.Get(ctx, &application.ApplicationQuery{Name: &appName, AppNamespace: &appNs})
errors.CheckError(err)

if sourceName != "" && sourcePosition != -1 {

Check failure on line 942 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Build & cache Go code

undefined: sourceName

Check failure on line 942 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Lint Go code

undefined: sourceName

Check failure on line 942 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Check changes to generated code

undefined: sourceName
errors.CheckError(fmt.Errorf("Only one of source-position and source-name can be specified."))
}

if len(sourceName) != "" {

Check failure on line 946 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Build & cache Go code

undefined: sourceName

Check failure on line 946 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Lint Go code

undefined: sourceName

Check failure on line 946 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Check changes to generated code

undefined: sourceName
sourceNameToPosition := getSourceNameToPositionMap(app)
if pos, ok := sourceNameToPosition[sourceName]; !ok {

Check failure on line 948 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Build & cache Go code

undefined: sourceName

Check failure on line 948 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Lint Go code

undefined: sourceName

Check failure on line 948 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Check changes to generated code

undefined: sourceName
log.Fatal("Unknown source name '%s'", sourceName)

Check failure on line 949 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Build & cache Go code

undefined: sourceName

Check failure on line 949 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Lint Go code

undefined: sourceName

Check failure on line 949 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Check changes to generated code

undefined: sourceName
} else {
sourcePosition = pos

Check failure on line 951 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Build & cache Go code

cannot use pos (variable of type int64) as int value in assignment

Check failure on line 951 in cmd/argocd/commands/app.go

View workflow job for this annotation

GitHub Actions / Check changes to generated code

cannot use pos (variable of type int64) as int value in assignment
}
}

if app.Spec.HasMultipleSources() {
if sourcePosition <= 0 {
errors.CheckError(fmt.Errorf("Source position should be specified and must be greater than 0 for applications with multiple sources"))
Expand Down Expand Up @@ -934,6 +997,7 @@ func NewApplicationUnsetCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
command.Flags().BoolVar(&opts.passCredentials, "pass-credentials", false, "Unset passCredentials")
command.Flags().BoolVar(&opts.ref, "ref", false, "Unset ref on the source")
command.Flags().IntVar(&sourcePosition, "source-position", -1, "Position of the source from the list of sources of the app. Counting starts at 1.")
command.Flags().StringVar(&sourceName, "source-name", "", "Name of the source from the list of sources of the app.")
return command
}

Expand Down Expand Up @@ -2816,6 +2880,7 @@ func NewApplicationManifestsCommand(clientOpts *argocdclient.ClientOptions) *cob
revision string
revisions []string
sourcePositions []int64
sourceNames []string
local string
localRepoRoot string
)
Expand All @@ -2829,6 +2894,9 @@ func NewApplicationManifestsCommand(clientOpts *argocdclient.ClientOptions) *cob
# Get manifests for an application at a specific revision
argocd app manifests my-app --revision 0.0.1
# Get manifests for a multi-source application at specific revisions for specific sources
argocd app manifests my-app --revisions 0.0.1 --source-names src-base --revisions 0.0.2 --source-names src-values
# Get manifests for a multi-source application at specific revisions for specific sources
argocd app manifests my-app --revisions 0.0.1 --source-positions 1 --revisions 0.0.2 --source-positions 2
`),
Expand All @@ -2840,8 +2908,12 @@ func NewApplicationManifestsCommand(clientOpts *argocdclient.ClientOptions) *cob
os.Exit(1)
}

if len(revisions) != len(sourcePositions) {
errors.CheckError(fmt.Errorf("While using revisions and source-positions, length of values for both flags should be same."))
if len(sourceNames) > 0 && len(sourcePositions) > 0 {
errors.CheckError(fmt.Errorf("Only one of source-position and source-name can be specified."))
}

if len(revisions) != len(sourcePositions) || len(revisions) != len(sourceNames) {
errors.CheckError(fmt.Errorf("While using revisions and either source-positions or source-names, length of values for both flags should be same."))
}

for _, pos := range sourcePositions {
Expand All @@ -2855,6 +2927,21 @@ func NewApplicationManifestsCommand(clientOpts *argocdclient.ClientOptions) *cob
conn, appIf := clientset.NewApplicationClientOrDie()
defer argoio.Close(conn)

app, err := appIf.Get(context.Background(), &application.ApplicationQuery{Name: &appName})
errors.CheckError(err)

if len(sourceNames) > 0 {
sourceNameToPosition := getSourceNameToPositionMap(app)

for _, name := range sourceNames {
if pos, ok := sourceNameToPosition[name]; !ok {
log.Fatal("Unknown source name '%s'", name)
} else {
sourcePositions = append(sourcePositions, pos)
}
}
}

resources, err := appIf.ManagedResources(ctx, &application.ResourcesQuery{
ApplicationName: &appName,
AppNamespace: &appNs,
Expand All @@ -2865,9 +2952,6 @@ func NewApplicationManifestsCommand(clientOpts *argocdclient.ClientOptions) *cob
switch source {
case "git":
if local != "" {
app, err := appIf.Get(context.Background(), &application.ApplicationQuery{Name: &appName})
errors.CheckError(err)

settingsConn, settingsIf := clientset.NewSettingsClientOrDie()
defer argoio.Close(settingsConn)
argoSettings, err := settingsIf.Get(context.Background(), &settings.SettingsQuery{})
Expand Down Expand Up @@ -2936,6 +3020,7 @@ func NewApplicationManifestsCommand(clientOpts *argocdclient.ClientOptions) *cob
command.Flags().StringVar(&revision, "revision", "", "Show manifests at a specific revision")
command.Flags().StringArrayVar(&revisions, "revisions", []string{}, "Show manifests at specific revisions for the source at position in source-positions")
command.Flags().Int64SliceVar(&sourcePositions, "source-positions", []int64{}, "List of source positions. Default is empty array. Counting start at 1.")
command.Flags().Int64SliceVar(&sourceNames, "source-names", []string{}, "List of source names. Default is an empty array.")
command.Flags().StringVar(&local, "local", "", "If set, show locally-generated manifests. Value is the absolute path to app manifests within the manifest repo. Example: '/home/username/apps/env/app-1'.")
command.Flags().StringVar(&localRepoRoot, "local-repo-root", ".", "Path to the local repository root. Used together with --local allows setting the repository root. Example: '/home/username/apps'.")
return command
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ type ApplicationSource struct {
Chart string `json:"chart,omitempty" protobuf:"bytes,12,opt,name=chart"`
// Ref is reference to another source within sources field. This field will not be used if used with a `source` tag.
Ref string `json:"ref,omitempty" protobuf:"bytes,13,opt,name=ref"`
// Name is used to refer to a source and is displayed in the UI.
Name string `json:"name,omitempty" protobuf:"bytes,14,opt,name=name"`
}

// ApplicationSources contains list of required information about the sources of an application
Expand Down

0 comments on commit 220edee

Please sign in to comment.