Skip to content

Commit

Permalink
Initial Custom Actions Implementation (argoproj#1369)
Browse files Browse the repository at this point in the history
  • Loading branch information
dthomson25 authored Apr 16, 2019
1 parent 97422b4 commit 4541ca6
Show file tree
Hide file tree
Showing 34 changed files with 4,039 additions and 740 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

157 changes: 157 additions & 0 deletions assets/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,93 @@
}
}
},
"/api/v1/applications/{name}/resource/actions": {
"get": {
"tags": [
"ApplicationService"
],
"operationId": "ListResourceActions",
"parameters": [
{
"type": "string",
"name": "name",
"in": "path",
"required": true
},
{
"type": "string",
"name": "namespace",
"in": "query"
},
{
"type": "string",
"name": "resourceName",
"in": "query"
},
{
"type": "string",
"name": "version",
"in": "query"
},
{
"type": "string",
"name": "group",
"in": "query"
},
{
"type": "string",
"name": "kind",
"in": "query"
}
],
"responses": {
"200": {
"description": "(empty)",
"schema": {
"$ref": "#/definitions/applicationResourceActionsListResponse"
}
}
}
}
},
"/api/v1/applications/{name}/resource/{resourceName}/actions": {
"post": {
"tags": [
"ApplicationService"
],
"operationId": "RunResourceAction",
"parameters": [
{
"type": "string",
"name": "name",
"in": "path",
"required": true
},
{
"type": "string",
"name": "resourceName",
"in": "path",
"required": true
},
{
"name": "body",
"in": "body",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "(empty)",
"schema": {
"$ref": "#/definitions/applicationApplicationResponse"
}
}
}
}
},
"/api/v1/applications/{name}/rollback": {
"post": {
"tags": [
Expand Down Expand Up @@ -1450,6 +1537,17 @@
"applicationOperationTerminateResponse": {
"type": "object"
},
"applicationResourceActionsListResponse": {
"type": "object",
"properties": {
"actions": {
"type": "array",
"items": {
"$ref": "#/definitions/v1alpha1ResourceAction"
}
}
}
},
"clusterClusterCreateFromKubeConfigRequest": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -2916,6 +3014,62 @@
}
}
},
"v1alpha1ResourceAction": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"params": {
"type": "array",
"items": {
"$ref": "#/definitions/v1alpha1ResourceActionParam"
}
}
}
},
"v1alpha1ResourceActionDefinition": {
"type": "object",
"properties": {
"actionLua": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
"v1alpha1ResourceActionParam": {
"type": "object",
"properties": {
"default": {
"type": "string"
},
"name": {
"type": "string"
},
"type": {
"type": "string"
},
"value": {
"type": "string"
}
}
},
"v1alpha1ResourceActions": {
"type": "object",
"properties": {
"actionDiscoveryLua": {
"type": "string"
},
"definitions": {
"type": "array",
"items": {
"$ref": "#/definitions/v1alpha1ResourceActionDefinition"
}
}
}
},
"v1alpha1ResourceDiff": {
"type": "object",
"title": "ResourceDiff holds the diff of a live and target resource object",
Expand Down Expand Up @@ -3037,6 +3191,9 @@
"type": "object",
"title": "ResourceOverride holds configuration to customize resource diffing and health assessment",
"properties": {
"actions": {
"$ref": "#/definitions/v1alpha1ResourceActions"
},
"healthLua": {
"type": "string"
},
Expand Down
78 changes: 42 additions & 36 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func NewApplicationCommand(clientOpts *argocdclient.ClientOptions) *cobra.Comman
command.AddCommand(NewApplicationEditCommand(clientOpts))
command.AddCommand(NewApplicationPatchCommand(clientOpts))
command.AddCommand(NewApplicationPatchResourceCommand(clientOpts))
command.AddCommand(NewApplicationResourceActionsCommand(clientOpts))
return command
}

Expand Down Expand Up @@ -1686,6 +1687,43 @@ func NewApplicationPatchCommand(clientOpts *argocdclient.ClientOptions) *cobra.C
return &command
}

func filterResources(command *cobra.Command, resources []*argoappv1.ResourceDiff, group, kind, namespace, resourceName string, all bool) []*unstructured.Unstructured {
liveObjs, err := liveObjects(resources)
errors.CheckError(err)
filteredObjects := make([]*unstructured.Unstructured, 0)
for i := range liveObjs {
obj := liveObjs[i]
gvk := obj.GroupVersionKind()
if command.Flags().Changed("group") && group != gvk.Group {
continue
}
if namespace != "" && namespace != obj.GetNamespace() {
continue
}
if resourceName != "" && resourceName != obj.GetName() {
continue
}
if kind == gvk.Kind {
copy := obj.DeepCopy()
filteredObjects = append(filteredObjects, copy)
}
}
if len(filteredObjects) == 0 {
log.Fatal("No matching resource found")
}
if len(filteredObjects) > 1 && !all {
log.Fatal("Multiple resources match inputs. Use the --all flag to patch multiple resources")
}
firstGroup := filteredObjects[0].GroupVersionKind().Group
for i := range filteredObjects {
obj := filteredObjects[i]
if obj.GroupVersionKind().Group != firstGroup {
log.Fatal("Multiple groups found in objects to patch. Specify which group to patch with --group flag")
}
}
return filteredObjects
}

func NewApplicationPatchResourceCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var patch string
var patchType string
Expand All @@ -1694,7 +1732,7 @@ func NewApplicationPatchResourceCommand(clientOpts *argocdclient.ClientOptions)
var kind string
var group string
var all bool
command := cobra.Command{
command := &cobra.Command{
Use: "patch-resource APPNAME",
Short: "Patch resource in an application",
}
Expand All @@ -1709,7 +1747,7 @@ func NewApplicationPatchResourceCommand(clientOpts *argocdclient.ClientOptions)
errors.CheckError(err)
command.Flags().StringVar(&group, "group", "", "Group")
command.Flags().StringVar(&namespace, "namespace", "", "Namespace")
command.Flags().BoolVar(&all, "all", false, "Indicates where to patch multiple matching of resources")
command.Flags().BoolVar(&all, "all", false, "Indicates whether to patch multiple matching of resources")
command.Run = func(c *cobra.Command, args []string) {
if len(args) != 1 {
c.HelpFunc()(c, args)
Expand All @@ -1722,39 +1760,7 @@ func NewApplicationPatchResourceCommand(clientOpts *argocdclient.ClientOptions)
ctx := context.Background()
resources, err := appIf.ManagedResources(ctx, &application.ResourcesQuery{ApplicationName: &appName})
errors.CheckError(err)
liveObjs, err := liveObjects(resources.Items)
errors.CheckError(err)
objectsToPatch := make([]*unstructured.Unstructured, 0)
for i := range liveObjs {
obj := liveObjs[i]
gvk := obj.GroupVersionKind()
if command.Flags().Changed("group") && group != gvk.Group {
continue
}
if namespace != "" && namespace != obj.GetNamespace() {
continue
}
if resourceName != "" && resourceName != obj.GetName() {
continue
}
if kind == gvk.Kind {
copy := obj.DeepCopy()
objectsToPatch = append(objectsToPatch, copy)
}
}
if len(objectsToPatch) == 0 {
log.Fatal("No matching resource found to patch")
}
if len(objectsToPatch) > 1 && !all {
log.Fatal("Multiple resources match inputs. Use the --all flag to patch multiple resources")
}
group := objectsToPatch[0].GroupVersionKind().Group
for i := range objectsToPatch {
obj := objectsToPatch[i]
if obj.GroupVersionKind().Group != group {
log.Fatal("Multiple groups found in objects to patch. Specify which group to patch with --group flag")
}
}
objectsToPatch := filterResources(command, resources.Items, group, kind, namespace, resourceName, all)
for i := range objectsToPatch {
obj := objectsToPatch[i]
gvk := obj.GroupVersionKind()
Expand All @@ -1773,5 +1779,5 @@ func NewApplicationPatchResourceCommand(clientOpts *argocdclient.ClientOptions)
}
}

return &command
return command
}
Loading

0 comments on commit 4541ca6

Please sign in to comment.