mirror of
https://github.com/hanzoai/deploy.git
synced 2026-10-07 08:44:50 +00:00
* feat(api): hard-fork CRD API group argoproj.io -> deploy.hanzo.ai
Hanzo CD hard-fork, phase 1.1. Single source of truth is
pkg/apis/application/register.go's Group constant (Application/AppProject/
ApplicationSet all derive their FullName from it); every other Go call-site
that hardcoded the literal group string is updated to match: the 3 generated
informer GVRs, the app-of-apps nested-Application detector in types.go, the
resource-tree Application-kind cache populator, the 3 NotFound error
constructors in server/application, and the applicationset-controller's
leader-election Lease name/ID (kept in lockstep across Go source + RBAC
resourceNames, or leader election would get RBAC-denied on startup).
CRD YAMLs (application/appproject/applicationset) and every RBAC apiGroups
rule that actually governs our 3 CRDs are repointed to deploy.hanzo.ai, across
both manifests/base/* (namespaced Roles) and manifests/cluster-rbac/* (cluster
scope). Two adjacent things that share the "argoproj.io" string but are NOT
ours are deliberately left untouched: gitops-engine's health-check dispatch
for Argo Workflows' `Workflow` kind, and argocd-server-clusterrole's second
apiGroups block granting read on Argo Workflows' `workflows` resource — both
belong to a separate, unrelated Argoproj-family project we are not forking.
Also untouched, per explicit scope: annotation/label keys (argocd.argoproj.io/*)
and finalizers (*.argoproj.io) that track already-applied live resources, and
argocd-cm.yaml's resource.customizations.ignoreResourceUpdates keys (a narrow
app-of-apps diffing nicety, not build- or RBAC-relevant).
CGO_ENABLED=0 go build ./... verified green.
* ci: wire canonical hanzo.yml + cicd.yml (Hanzo CD image build)
Phase 1.3. Follows the same one-way pattern every hanzo/lux/zoo repo uses:
hanzo.yml declares the image (root Dockerfile -> ghcr.io/hanzoai/deploy) and a
fast go-build sanity test (the multi-stage Dockerfile build is the slow, real
gate); cicd.yml is the ~10-line trigger shim importing hanzoai/ci's reusable
build workflow on the arc pool, secrets from KMS only.
No `deploy:` block yet, matching the hanzoai/cms build-only-first-cutover
pattern: this repo's own live hanzo-cd namespace runs the whole Hanzo fleet's
GitOps, so the prod image/CRD swap is a deliberate separate action (see the
[HELD] universe PR), never an automatic rollout on merge. Every PR/push still
builds and pushes an immutable ghcr.io/hanzoai/deploy:sha-<7>-amd64 image.
* feat(ui): rebrand Argo CD -> Hanzo CD (title, favicon, logo, sidebar)
Phase 1.2. User-facing "Argo CD"/"ArgoCD" text replaced with "Hanzo CD"
across the login page, help modal, settings screens, version-info panel,
sync-options warning, fullscreen-logs title, app.tsx page titles, and
index.html's <title>/noscript text (dropped the now-wrong upstream CLI
doc link rather than invent a hanzo.ai URL that doesn't exist).
Logo/favicon assets replaced with the canonical Hanzo mark (never
hand-drawn, copied from ~/work/hanzo/logo/dist): the two real usages of
Argo's logo collapse onto one Hanzo glyph (mono + white variants) at the
same 5 call sites (login icon + footer, login.scss $logoPath/
$logoNegativePath, sidebar text-logo), preserving the existing CSS
invert() filters so both the light login panel and dark sidebar still
render correctly. Old argologo.svg/argo_o.svg/argo.png deleted (no
longer referenced). favicon-{16x16,32x32}.png added at
ui/src/assets/favicon/ (index.html's links to that path were previously
dead - the directory didn't exist in this checkout). package.json name
argo-cd-ui -> hanzo-cd-ui.
Left alone, deliberately: the two Kubernetes resource-kind icon sets
under ui/src/assets/images/resources/ (unrelated to branding), and
upstream doc/project links (argoproj.io, argo-cd.readthedocs.io) since
we have no equivalent hanzo.ai docs to point to yet - except the login
footer logo's outbound link, which now points to hanzo.ai instead of
argoproj.io (a Hanzo-branded mark linking to the upstream project it
was forked from read as an obvious leftover).
* feat(module): rename Go module github.com/argoproj/argo-cd/v3 -> github.com/hanzoai/deploy
Phase 2. No version suffix on the new path (org policy: never publish our
own Go modules above v1.x.x / with a v2+ suffix) — this fork starts a fresh
v0/v1 module identity rather than carrying the v3 the upstream fork point
had reached.
Mechanical, symmetric rewrite: 704 .go files (import statements only; two
disjoint substitutions applied specific-first so the nested gitops-engine
module path never double-matches), plus go.mod (module line, the
gitops-engine require+replace pair, version reset to v0.0.0-... since a
suffix-less path can't carry a v3 semver), gitops-engine/go.mod, Makefile
(ldflags PACKAGE + 2 TEST_MODULE filters), .golangci.yaml, .goreleaser.yaml,
.mockery.yaml (+ gitops-engine's), Procfile, the codegen/proto generation
scripts (hack/generate-proto.sh, update-codegen.sh, update-openapi.sh), and
20 .proto files' go_package options. Also one OpenTelemetry tracer name
string in gitops-engine/pkg/sync/sync_context.go (a cosmestic label, not an
import, but worth keeping consistent).
CGO_ENABLED=0 go build ./... verified green for both the root module and
the nested gitops-engine module; go vet clean on both; go mod tidy run
(network available) to reconcile go.sum, dependency set unchanged.
Left alone, deliberately: doc-only mentions (docs/snyk/*.html scan reports,
gitops-engine/README.md), a comment-only reference in
.github/workflows/release.yaml, and one Renovate bot config line — none are
build- or codegen-load-bearing. Also left alone: comments citing upstream
GitHub issue URLs (github.com/argoproj/argo-cd/issues/NNNNN) and a handful
of test fixtures using "https://github.com/argoproj/argo-cd.git" as a
representative example repo URL — both are accurate historical/test-data
references, not stale self-references.
---------
Co-authored-by: Hanzo Dev <[email protected]>
580 lines
21 KiB
Go
580 lines
21 KiB
Go
package commands
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"text/tabwriter"
|
|
|
|
"go.yaml.in/yaml/v3"
|
|
|
|
"github.com/hanzoai/deploy/util/templates"
|
|
|
|
"github.com/hanzoai/deploy/cmd/argocd/commands/utils"
|
|
"github.com/hanzoai/deploy/cmd/util"
|
|
"github.com/hanzoai/deploy/pkg/apis/application/v1alpha1"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
"github.com/hanzoai/deploy/cmd/argocd/commands/headless"
|
|
argocdclient "github.com/hanzoai/deploy/pkg/apiclient"
|
|
applicationpkg "github.com/hanzoai/deploy/pkg/apiclient/application"
|
|
"github.com/hanzoai/deploy/util/argo"
|
|
"github.com/hanzoai/deploy/util/errors"
|
|
utilio "github.com/hanzoai/deploy/util/io"
|
|
)
|
|
|
|
// NewApplicationGetResourceCommand returns a new instance of the `app get-resource` command
|
|
func NewApplicationGetResourceCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
|
var (
|
|
resourceName string
|
|
kind string
|
|
group string
|
|
project string
|
|
filteredFields []string
|
|
showManagedFields bool
|
|
output string
|
|
appNamespace string
|
|
)
|
|
command := &cobra.Command{
|
|
Use: "get-resource APPNAME",
|
|
Short: "Get details about the live Kubernetes manifests of a resource in an application. The filter-fields flag can be used to only display fields you want to see.",
|
|
Example: `
|
|
# Get a specific resource, Pod my-app-pod, in 'my-app' by name in wide format
|
|
argocd app get-resource my-app --kind Pod --resource-name my-app-pod
|
|
|
|
# Get a specific resource, Pod my-app-pod, in 'my-app' by name in yaml format
|
|
argocd app get-resource my-app --kind Pod --resource-name my-app-pod -o yaml
|
|
|
|
# Get a specific resource, Pod my-app-pod, in 'my-app' by name in json format
|
|
argocd app get-resource my-app --kind Pod --resource-name my-app-pod -o json
|
|
|
|
# Get details about all Pods in the application
|
|
argocd app get-resource my-app --kind Pod
|
|
|
|
# Get a specific resource with managed fields, Pod my-app-pod, in 'my-app' by name in wide format
|
|
argocd app get-resource my-app --kind Pod --resource-name my-app-pod --show-managed-fields
|
|
|
|
# Get the details of a specific field in a resource in 'my-app' in the wide format
|
|
argocd app get-resource my-app --kind Pod --filter-fields status.podIP
|
|
|
|
# Get the details of multiple specific fields in a specific resource in 'my-app' in the wide format
|
|
argocd app get-resource my-app --kind Pod --resource-name my-app-pod --filter-fields status.podIP,status.hostIP`,
|
|
}
|
|
|
|
command.Run = func(c *cobra.Command, args []string) {
|
|
ctx := c.Context()
|
|
|
|
if len(args) != 1 {
|
|
c.HelpFunc()(c, args)
|
|
os.Exit(1)
|
|
}
|
|
|
|
appName, appNs := argo.ParseFromQualifiedName(args[0], appNamespace)
|
|
|
|
conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie()
|
|
defer utilio.Close(conn)
|
|
|
|
tree, err := appIf.ResourceTree(ctx, &applicationpkg.ResourcesQuery{
|
|
ApplicationName: &appName,
|
|
AppNamespace: &appNs,
|
|
})
|
|
errors.CheckError(err)
|
|
|
|
// Get manifests of resources
|
|
// If resource name is "" find all resources of that kind
|
|
var resources []unstructured.Unstructured
|
|
var resourceNames []string
|
|
for _, r := range tree.Nodes {
|
|
if (resourceName != "" && r.Name != resourceName) || (group != "" && r.Group != group) || r.Kind != kind {
|
|
continue
|
|
}
|
|
resource, err := appIf.GetResource(ctx, &applicationpkg.ApplicationResourceRequest{
|
|
Name: &appName,
|
|
AppNamespace: &appNs,
|
|
Group: &r.Group,
|
|
Kind: &r.Kind,
|
|
Namespace: &r.Namespace,
|
|
Project: &project,
|
|
ResourceName: &r.Name,
|
|
Version: &r.Version,
|
|
})
|
|
errors.CheckError(err)
|
|
manifest := resource.GetManifest()
|
|
|
|
var obj *unstructured.Unstructured
|
|
err = json.Unmarshal([]byte(manifest), &obj)
|
|
errors.CheckError(err)
|
|
|
|
if !showManagedFields {
|
|
unstructured.RemoveNestedField(obj.Object, "metadata", "managedFields")
|
|
}
|
|
|
|
if len(filteredFields) != 0 {
|
|
obj = filterFieldsFromObject(obj, filteredFields)
|
|
}
|
|
|
|
resourceNames = append(resourceNames, obj.GetName())
|
|
resources = append(resources, *obj)
|
|
}
|
|
fetchedStr := strings.Join(resourceNames, ", ")
|
|
printManifests(&resources, len(filteredFields) > 0, resourceName == "", output)
|
|
log.Infof("Resources '%s' fetched", fetchedStr)
|
|
}
|
|
command.Flags().StringVarP(&appNamespace, "app-namespace", "N", "", "Namespace of the application")
|
|
command.Flags().StringVar(&resourceName, "resource-name", "", "Name of resource, if none is included will output details of all resources with specified kind")
|
|
command.Flags().StringVar(&kind, "kind", "", "Kind of resource [REQUIRED]")
|
|
err := command.MarkFlagRequired("kind")
|
|
errors.CheckError(err)
|
|
command.Flags().StringVar(&group, "group", "", "Group")
|
|
command.Flags().StringVar(&project, "project", "", "Project of resource")
|
|
command.Flags().StringSliceVar(&filteredFields, "filter-fields", nil, "A comma separated list of fields to display, if not provided will output the entire manifest")
|
|
command.Flags().BoolVar(&showManagedFields, "show-managed-fields", false, "Show managed fields in the output manifest")
|
|
command.Flags().StringVarP(&output, "output", "o", "wide", "Format of the output, wide, yaml, or json")
|
|
return command
|
|
}
|
|
|
|
// filterFieldsFromObject creates a new unstructured object containing only the specified fields from the source object.
|
|
func filterFieldsFromObject(obj *unstructured.Unstructured, filteredFields []string) *unstructured.Unstructured {
|
|
var filteredObj unstructured.Unstructured
|
|
filteredObj.Object = make(map[string]any)
|
|
|
|
for _, f := range filteredFields {
|
|
fields := strings.Split(f, ".")
|
|
|
|
value, exists, err := unstructured.NestedFieldCopy(obj.Object, fields...)
|
|
if exists {
|
|
errors.CheckError(err)
|
|
err = unstructured.SetNestedField(filteredObj.Object, value, fields...)
|
|
errors.CheckError(err)
|
|
} else {
|
|
// If doesn't exist assume its a nested inside a list of objects
|
|
value := extractNestedItem(obj.Object, fields, 0)
|
|
filteredObj.Object = value
|
|
}
|
|
}
|
|
filteredObj.SetName(obj.GetName())
|
|
return &filteredObj
|
|
}
|
|
|
|
// extractNestedItem recursively extracts an item that may be nested inside a list of objects.
|
|
func extractNestedItem(obj map[string]any, fields []string, depth int) map[string]any {
|
|
if depth >= len(fields) {
|
|
return nil
|
|
}
|
|
|
|
value, exists, _ := unstructured.NestedFieldCopy(obj, fields[:depth+1]...)
|
|
list, ok := value.([]any)
|
|
if !exists || !ok {
|
|
return extractNestedItem(obj, fields, depth+1)
|
|
}
|
|
|
|
extractedItems := extractItemsFromList(list, fields[depth+1:])
|
|
if len(extractedItems) == 0 {
|
|
for _, e := range list {
|
|
if o, ok := e.(map[string]any); ok {
|
|
result := extractNestedItem(o, fields[depth+1:], 0)
|
|
extractedItems = append(extractedItems, result)
|
|
}
|
|
}
|
|
}
|
|
|
|
filteredObj := reconstructObject(extractedItems, fields, depth)
|
|
return filteredObj
|
|
}
|
|
|
|
// extractItemsFromList processes a list of objects and extracts specific fields from each item.
|
|
func extractItemsFromList(list []any, fields []string) []any {
|
|
var extratedObjs []any
|
|
for _, e := range list {
|
|
extractedObj := make(map[string]any)
|
|
if o, ok := e.(map[string]any); ok {
|
|
value, exists, _ := unstructured.NestedFieldCopy(o, fields...)
|
|
if !exists {
|
|
continue
|
|
}
|
|
err := unstructured.SetNestedField(extractedObj, value, fields...)
|
|
errors.CheckError(err)
|
|
extratedObjs = append(extratedObjs, extractedObj)
|
|
}
|
|
}
|
|
return extratedObjs
|
|
}
|
|
|
|
// reconstructObject rebuilds the original object structure by placing extracted items back into their proper nested location.
|
|
func reconstructObject(extracted []any, fields []string, depth int) map[string]any {
|
|
obj := make(map[string]any)
|
|
err := unstructured.SetNestedField(obj, extracted, fields[:depth+1]...)
|
|
errors.CheckError(err)
|
|
return obj
|
|
}
|
|
|
|
// printManifests outputs resource manifests in the specified format (wide, JSON, or YAML).
|
|
func printManifests(objs *[]unstructured.Unstructured, filteredFields bool, showName bool, output string) {
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
|
if showName {
|
|
fmt.Fprint(w, "FIELD\tRESOURCE NAME\tVALUE\n")
|
|
} else {
|
|
fmt.Fprint(w, "FIELD\tVALUE\n")
|
|
}
|
|
|
|
for i, o := range *objs {
|
|
if output == "json" || output == "yaml" {
|
|
var formattedManifest []byte
|
|
var err error
|
|
if output == "json" {
|
|
formattedManifest, err = json.MarshalIndent(o.Object, "", " ")
|
|
} else {
|
|
formattedManifest, err = yaml.Marshal(o.Object)
|
|
}
|
|
errors.CheckError(err)
|
|
|
|
fmt.Println(string(formattedManifest))
|
|
if len(*objs) > 1 && i != len(*objs)-1 {
|
|
fmt.Println("---")
|
|
}
|
|
} else {
|
|
name := o.GetName()
|
|
if filteredFields {
|
|
unstructured.RemoveNestedField(o.Object, "metadata", "name")
|
|
}
|
|
|
|
printManifestAsTable(w, name, showName, o.Object, "")
|
|
}
|
|
}
|
|
|
|
if output != "json" && output != "yaml" {
|
|
err := w.Flush()
|
|
errors.CheckError(err)
|
|
}
|
|
}
|
|
|
|
// printManifestAsTable recursively prints a manifest object as a tabular view with nested fields flattened.
|
|
func printManifestAsTable(w *tabwriter.Writer, name string, showName bool, obj map[string]any, parentField string) {
|
|
for key, value := range obj {
|
|
field := parentField + key
|
|
switch v := value.(type) {
|
|
case map[string]any:
|
|
printManifestAsTable(w, name, showName, v, field+".")
|
|
case []any:
|
|
for i, e := range v {
|
|
index := "[" + strconv.Itoa(i) + "]"
|
|
|
|
if innerObj, ok := e.(map[string]any); ok {
|
|
printManifestAsTable(w, name, showName, innerObj, field+index+".")
|
|
} else {
|
|
if showName {
|
|
fmt.Fprintf(w, "%v\t%v\t%v\n", field+index, name, e)
|
|
} else {
|
|
fmt.Fprintf(w, "%v\t%v\n", field+index, e)
|
|
}
|
|
}
|
|
}
|
|
default:
|
|
if showName {
|
|
fmt.Fprintf(w, "%v\t%v\t%v\n", field, name, v)
|
|
} else {
|
|
fmt.Fprintf(w, "%v\t%v\n", field, v)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func NewApplicationPatchResourceCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
|
var (
|
|
patch string
|
|
patchType string
|
|
resourceName string
|
|
namespace string
|
|
kind string
|
|
group string
|
|
all bool
|
|
project string
|
|
appNamespace string
|
|
)
|
|
command := &cobra.Command{
|
|
Use: "patch-resource APPNAME",
|
|
Short: "Patch resource in an application",
|
|
}
|
|
command.Flags().StringVarP(&appNamespace, "app-namespace", "N", "", "Namespace of the application")
|
|
command.Flags().StringVar(&patch, "patch", "", "Patch")
|
|
err := command.MarkFlagRequired("patch")
|
|
errors.CheckError(err)
|
|
command.Flags().StringVar(&patchType, "patch-type", string(types.MergePatchType), "Which Patching strategy to use: 'application/json-patch+json', 'application/merge-patch+json', or 'application/strategic-merge-patch+json'. Defaults to 'application/merge-patch+json'")
|
|
command.Flags().StringVar(&resourceName, "resource-name", "", "Name of resource")
|
|
command.Flags().StringVar(&kind, "kind", "", "Kind")
|
|
err = command.MarkFlagRequired("kind")
|
|
errors.CheckError(err)
|
|
command.Flags().StringVar(&group, "group", "", "Group")
|
|
command.Flags().StringVar(&namespace, "namespace", "", "Namespace")
|
|
command.Flags().BoolVar(&all, "all", false, "Indicates whether to patch multiple matching of resources")
|
|
command.Flags().StringVar(&project, "project", "", `The name of the application's project - specifying this allows the command to report "not found" instead of "permission denied" if the app does not exist`)
|
|
command.Run = func(c *cobra.Command, args []string) {
|
|
ctx := c.Context()
|
|
|
|
if len(args) != 1 {
|
|
c.HelpFunc()(c, args)
|
|
os.Exit(1)
|
|
}
|
|
appName, appNs := argo.ParseFromQualifiedName(args[0], appNamespace)
|
|
|
|
conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie()
|
|
defer utilio.Close(conn)
|
|
resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{
|
|
ApplicationName: &appName,
|
|
AppNamespace: &appNs,
|
|
})
|
|
errors.CheckError(err)
|
|
objectsToPatch, err := util.FilterResources(command.Flags().Changed("group"), resources.Items, group, kind, namespace, resourceName, all)
|
|
errors.CheckError(err)
|
|
for i := range objectsToPatch {
|
|
obj := objectsToPatch[i]
|
|
gvk := obj.GroupVersionKind()
|
|
_, err = appIf.PatchResource(ctx, &applicationpkg.ApplicationResourcePatchRequest{
|
|
Name: &appName,
|
|
AppNamespace: &appNs,
|
|
Namespace: new(obj.GetNamespace()),
|
|
ResourceName: new(obj.GetName()),
|
|
Version: new(gvk.Version),
|
|
Group: new(gvk.Group),
|
|
Kind: new(gvk.Kind),
|
|
Patch: new(patch),
|
|
PatchType: new(patchType),
|
|
Project: new(project),
|
|
})
|
|
errors.CheckError(err)
|
|
log.Infof("Resource '%s' patched", obj.GetName())
|
|
}
|
|
}
|
|
|
|
return command
|
|
}
|
|
|
|
func NewApplicationDeleteResourceCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
|
var (
|
|
resourceName string
|
|
namespace string
|
|
kind string
|
|
group string
|
|
force bool
|
|
orphan bool
|
|
all bool
|
|
project string
|
|
appNamespace string
|
|
)
|
|
command := &cobra.Command{
|
|
Use: "delete-resource APPNAME",
|
|
Short: "Delete resource in an application",
|
|
}
|
|
|
|
command.Flags().StringVar(&resourceName, "resource-name", "", "Name of resource")
|
|
command.Flags().StringVarP(&appNamespace, "app-namespace", "N", "", "Namespace of the application")
|
|
command.Flags().StringVar(&kind, "kind", "", "Kind")
|
|
err := command.MarkFlagRequired("kind")
|
|
errors.CheckError(err)
|
|
command.Flags().StringVar(&group, "group", "", "Group")
|
|
command.Flags().StringVar(&namespace, "namespace", "", "Namespace")
|
|
command.Flags().BoolVar(&force, "force", false, "Indicates whether to force delete the resource")
|
|
command.Flags().BoolVar(&orphan, "orphan", false, "Indicates whether to orphan the dependents of the deleted resource")
|
|
command.Flags().BoolVar(&all, "all", false, "Indicates whether to patch multiple matching of resources")
|
|
command.Flags().StringVar(&project, "project", "", `The name of the application's project - specifying this allows the command to report "not found" instead of "permission denied" if the app does not exist`)
|
|
command.Run = func(c *cobra.Command, args []string) {
|
|
ctx := c.Context()
|
|
|
|
if len(args) != 1 {
|
|
c.HelpFunc()(c, args)
|
|
os.Exit(1)
|
|
}
|
|
appName, appNs := argo.ParseFromQualifiedName(args[0], appNamespace)
|
|
|
|
conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie()
|
|
defer utilio.Close(conn)
|
|
resources, err := appIf.ManagedResources(ctx, &applicationpkg.ResourcesQuery{
|
|
ApplicationName: &appName,
|
|
AppNamespace: &appNs,
|
|
})
|
|
errors.CheckError(err)
|
|
objectsToDelete, err := util.FilterResources(command.Flags().Changed("group"), resources.Items, group, kind, namespace, resourceName, all)
|
|
errors.CheckError(err)
|
|
|
|
promptUtil := utils.NewPrompt(clientOpts.PromptsEnabled)
|
|
|
|
for i := range objectsToDelete {
|
|
obj := objectsToDelete[i]
|
|
gvk := obj.GroupVersionKind()
|
|
|
|
canDelete := promptUtil.Confirm(fmt.Sprintf("Are you sure you want to delete %s/%s %s/%s ? [y/n]", gvk.Group, gvk.Kind, obj.GetNamespace(), obj.GetName()))
|
|
if canDelete {
|
|
_, err = appIf.DeleteResource(ctx, &applicationpkg.ApplicationResourceDeleteRequest{
|
|
Name: &appName,
|
|
AppNamespace: &appNs,
|
|
Namespace: new(obj.GetNamespace()),
|
|
ResourceName: new(obj.GetName()),
|
|
Version: new(gvk.Version),
|
|
Group: new(gvk.Group),
|
|
Kind: new(gvk.Kind),
|
|
Force: &force,
|
|
Orphan: &orphan,
|
|
Project: new(project),
|
|
})
|
|
errors.CheckError(err)
|
|
log.Infof("Resource '%s' deleted", obj.GetName())
|
|
} else {
|
|
fmt.Printf("The command to delete %s/%s %s/%s was cancelled.\n", gvk.Group, gvk.Kind, obj.GetNamespace(), obj.GetName())
|
|
}
|
|
}
|
|
}
|
|
|
|
return command
|
|
}
|
|
|
|
func parentChildInfo(nodes []v1alpha1.ResourceNode) (map[string]v1alpha1.ResourceNode, map[string][]string, map[string]struct{}) {
|
|
mapUIDToNode := make(map[string]v1alpha1.ResourceNode)
|
|
mapParentToChild := make(map[string][]string)
|
|
parentNode := make(map[string]struct{})
|
|
|
|
for _, node := range nodes {
|
|
mapUIDToNode[node.UID] = node
|
|
|
|
if len(node.ParentRefs) > 0 {
|
|
_, ok := mapParentToChild[node.ParentRefs[0].UID]
|
|
if !ok {
|
|
var temp []string
|
|
mapParentToChild[node.ParentRefs[0].UID] = temp
|
|
}
|
|
mapParentToChild[node.ParentRefs[0].UID] = append(mapParentToChild[node.ParentRefs[0].UID], node.UID)
|
|
} else {
|
|
parentNode[node.UID] = struct{}{}
|
|
}
|
|
}
|
|
return mapUIDToNode, mapParentToChild, parentNode
|
|
}
|
|
|
|
func printDetailedTreeViewAppResourcesNotOrphaned(nodeMapping map[string]v1alpha1.ResourceNode, parentChildMapping map[string][]string, parentNodes map[string]struct{}, w *tabwriter.Writer) {
|
|
for uid := range parentNodes {
|
|
detailedTreeViewAppResourcesNotOrphaned("", nodeMapping, parentChildMapping, nodeMapping[uid], w)
|
|
}
|
|
}
|
|
|
|
func printDetailedTreeViewAppResourcesOrphaned(nodeMapping map[string]v1alpha1.ResourceNode, parentChildMapping map[string][]string, parentNodes map[string]struct{}, w *tabwriter.Writer) {
|
|
for uid := range parentNodes {
|
|
detailedTreeViewAppResourcesOrphaned("", nodeMapping, parentChildMapping, nodeMapping[uid], w)
|
|
}
|
|
}
|
|
|
|
func printTreeViewAppResourcesNotOrphaned(nodeMapping map[string]v1alpha1.ResourceNode, parentChildMapping map[string][]string, parentNodes map[string]struct{}, w *tabwriter.Writer) {
|
|
for uid := range parentNodes {
|
|
treeViewAppResourcesNotOrphaned("", nodeMapping, parentChildMapping, nodeMapping[uid], w)
|
|
}
|
|
}
|
|
|
|
func printTreeViewAppResourcesOrphaned(nodeMapping map[string]v1alpha1.ResourceNode, parentChildMapping map[string][]string, parentNodes map[string]struct{}, w *tabwriter.Writer) {
|
|
for uid := range parentNodes {
|
|
treeViewAppResourcesOrphaned("", nodeMapping, parentChildMapping, nodeMapping[uid], w)
|
|
}
|
|
}
|
|
|
|
func printResources(listAll bool, orphaned bool, appResourceTree *v1alpha1.ApplicationTree, output string) {
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
|
switch output {
|
|
case "tree=detailed":
|
|
fmt.Fprint(w, "GROUP\tKIND\tNAMESPACE\tNAME\tORPHANED\tAGE\tHEALTH\tREASON\n")
|
|
|
|
if !orphaned || listAll {
|
|
mapUIDToNode, mapParentToChild, parentNode := parentChildInfo(appResourceTree.Nodes)
|
|
printDetailedTreeViewAppResourcesNotOrphaned(mapUIDToNode, mapParentToChild, parentNode, w)
|
|
}
|
|
|
|
if orphaned || listAll {
|
|
mapUIDToNode, mapParentToChild, parentNode := parentChildInfo(appResourceTree.OrphanedNodes)
|
|
printDetailedTreeViewAppResourcesOrphaned(mapUIDToNode, mapParentToChild, parentNode, w)
|
|
}
|
|
case "tree":
|
|
fmt.Fprint(w, "GROUP\tKIND\tNAMESPACE\tNAME\tORPHANED\n")
|
|
|
|
if !orphaned || listAll {
|
|
mapUIDToNode, mapParentToChild, parentNode := parentChildInfo(appResourceTree.Nodes)
|
|
printTreeViewAppResourcesNotOrphaned(mapUIDToNode, mapParentToChild, parentNode, w)
|
|
}
|
|
|
|
if orphaned || listAll {
|
|
mapUIDToNode, mapParentToChild, parentNode := parentChildInfo(appResourceTree.OrphanedNodes)
|
|
printTreeViewAppResourcesOrphaned(mapUIDToNode, mapParentToChild, parentNode, w)
|
|
}
|
|
default:
|
|
headers := []any{"GROUP", "KIND", "NAMESPACE", "NAME", "ORPHANED"}
|
|
fmtStr := "%s\t%s\t%s\t%s\t%s\n"
|
|
_, _ = fmt.Fprintf(w, fmtStr, headers...)
|
|
if !orphaned || listAll {
|
|
for _, res := range appResourceTree.Nodes {
|
|
if len(res.ParentRefs) == 0 {
|
|
_, _ = fmt.Fprintf(w, fmtStr, res.Group, res.Kind, res.Namespace, res.Name, "No")
|
|
}
|
|
}
|
|
}
|
|
if orphaned || listAll {
|
|
for _, res := range appResourceTree.OrphanedNodes {
|
|
_, _ = fmt.Fprintf(w, fmtStr, res.Group, res.Kind, res.Namespace, res.Name, "Yes")
|
|
}
|
|
}
|
|
}
|
|
err := w.Flush()
|
|
errors.CheckError(err)
|
|
}
|
|
|
|
func NewApplicationListResourcesCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
|
|
var (
|
|
orphaned bool
|
|
output string
|
|
project string
|
|
appNamespace string
|
|
)
|
|
command := &cobra.Command{
|
|
Use: "resources APPNAME",
|
|
Short: "List resources of application",
|
|
Example: templates.Examples(`
|
|
# List first-level resources of application
|
|
argocd app resources my-app --refresh
|
|
|
|
# List only the orphaned resources of application
|
|
argocd app resources my-app --orphaned
|
|
|
|
# Shows resource hierarchy with parent-child relationships
|
|
argocd app resources my-app --output tree
|
|
|
|
# Shows resource hierarchy with parent-child relationships including information about age, health and reason
|
|
argocd app resources my-app --output tree=detailed
|
|
`),
|
|
Run: func(c *cobra.Command, args []string) {
|
|
ctx := c.Context()
|
|
if len(args) != 1 {
|
|
c.HelpFunc()(c, args)
|
|
os.Exit(1)
|
|
}
|
|
listAll := !c.Flag("orphaned").Changed
|
|
appName, appNs := argo.ParseFromQualifiedName(args[0], appNamespace)
|
|
conn, appIf := headless.NewClientOrDie(clientOpts, c).NewApplicationClientOrDie()
|
|
defer utilio.Close(conn)
|
|
appResourceTree, err := appIf.ResourceTree(ctx, &applicationpkg.ResourcesQuery{
|
|
ApplicationName: &appName,
|
|
AppNamespace: &appNs,
|
|
Project: &project,
|
|
})
|
|
errors.CheckError(err)
|
|
printResources(listAll, orphaned, appResourceTree, output)
|
|
},
|
|
}
|
|
command.Flags().BoolVar(&orphaned, "orphaned", false, "Lists only orphaned resources")
|
|
command.Flags().StringVarP(&appNamespace, "app-namespace", "N", "", "Namespace of the application")
|
|
command.Flags().StringVar(&output, "output", "", `Output format. One of: tree|tree=detailed.
|
|
tree: Shows resource hierarchy with parent-child relationships
|
|
tree=detailed: Same as tree, but includes AGE, HEALTH, and REASON columns`)
|
|
command.Flags().StringVar(&project, "project", "", `The name of the application's project - specifying this allows the command to report "not found" instead of "permission denied" if the app does not exist`)
|
|
return command
|
|
}
|