cmd/ # Main applications for this project
└── engine/ # Same as project name
└── main.go # First running root executable
internal/ # Private application and library code
└── driver/ # Tools with which we can manage resources
└── argocd/ # ArgoCD resource definitions
├── client.go # ArgoCD client connection definitions
├── cluster.go # ArgoCD cluster definitions
├── destination.go # ArgoCD destination definitions
└── project.go # ArgoCD project definitions
.editorconfig # IDE standards config file
.gitignore # Ignored Git files
.golangci.yml # Go linter config file
go.mod # Go dependencies
go.sum # Go dependency checksums
Makefile # Tasks for code compilation and managing
Last active
May 21, 2024 13:51
-
-
Save nemre/803e513f8d226d5c3d3d491af7695539 to your computer and use it in GitHub Desktop.
Manage ArgoCD Resources Programmatically with Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| root = true | |
| [*] | |
| charset = utf-8 | |
| end_of_line = lf | |
| indent_size = 4 | |
| indent_style = tab | |
| insert_final_newline = true | |
| trim_trailing_whitespace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| linters: | |
| enable-all: true | |
| disable: | |
| - exhaustruct | |
| # disabled because of generics | |
| - rowserrcheck | |
| - wastedassign | |
| # disabled because deprecated | |
| - deadcode | |
| - exhaustivestruct | |
| - golint | |
| - ifshort | |
| - interfacer | |
| - maligned | |
| - nosnakecase | |
| - scopelint | |
| - structcheck | |
| - varcheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package argocd | |
| import ( | |
| "fmt" | |
| "github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster" | |
| "github.com/argoproj/argo-cd/v2/pkg/apiclient" | |
| "github.com/argoproj/argo-cd/v2/pkg/apiclient/project" | |
| ) | |
| type Connection struct { | |
| Address string | |
| Token string | |
| } | |
| type Client struct { | |
| projectClient project.ProjectServiceClient | |
| clusterClient cluster.ClusterServiceClient | |
| } | |
| func NewClient(c *Connection) (*Client, error) { | |
| apiClient, err := apiclient.NewClient(&apiclient.ClientOptions{ | |
| ServerAddr: fmt.Sprintf(c.Address), | |
| Insecure: true, | |
| AuthToken: c.Token, | |
| }) | |
| if err != nil { | |
| return nil, err | |
| } | |
| _, projectClient, err := apiClient.NewProjectClient() | |
| if err != nil { | |
| return nil, err | |
| } | |
| _, clusterClient, err := apiClient.NewClusterClient() | |
| if err != nil { | |
| return nil, err | |
| } | |
| return &Client{ | |
| projectClient: projectClient, | |
| clusterClient: clusterClient, | |
| }, nil | |
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package argocd | |
| import ( | |
| "context" | |
| "github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster" | |
| "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | |
| ) | |
| func (c *Client) GetClusters() ([]v1alpha1.Cluster, error) { | |
| cl, err := c.clusterClient.List(context.Background(), &cluster.ClusterQuery{}) | |
| if err != nil { | |
| return nil, err | |
| } | |
| return cl.Items, nil | |
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package argocd | |
| import ( | |
| "context" | |
| "github.com/argoproj/argo-cd/v2/pkg/apiclient/project" | |
| "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | |
| ) | |
| func (c *Client) AddDestination(projectName, server, namespace, name string) error { | |
| p, err := c.GetProject(projectName) | |
| if err != nil { | |
| return err | |
| } | |
| p.Spec.Destinations = []v1alpha1.ApplicationDestination{ | |
| { | |
| Server: server, | |
| Namespace: namespace, | |
| Name: name, | |
| }, | |
| } | |
| _, err = c.projectClient.Update(context.Background(), &project.ProjectUpdateRequest{ | |
| Project: p, | |
| }) | |
| if err != nil { | |
| return err | |
| } | |
| return nil | |
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module engine | |
| go 1.20 | |
| require ( | |
| github.com/argoproj/argo-cd/v2 v2.6.7 | |
| k8s.io/apimachinery v0.24.2 | |
| ) | |
| require ( | |
| cloud.google.com/go v0.99.0 // indirect | |
| github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect | |
| github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd // indirect | |
| github.com/Masterminds/semver/v3 v3.2.0 // indirect | |
| github.com/Microsoft/go-winio v0.6.0 // indirect | |
| github.com/ProtonMail/go-crypto v0.0.0-20230331115716-d34776aa93ec // indirect | |
| github.com/acomagu/bufpipe v1.0.4 // indirect | |
| github.com/argoproj/gitops-engine v0.7.1-0.20221208230615-917f5a0f16d5 // indirect | |
| github.com/argoproj/pkg v0.13.7-0.20221221191914-44694015343d // indirect | |
| github.com/bombsimon/logrusr/v2 v2.0.1 // indirect | |
| github.com/bradleyfalzon/ghinstallation/v2 v2.1.0 // indirect | |
| github.com/cespare/xxhash/v2 v2.1.2 // indirect | |
| github.com/chai2010/gettext-go v0.0.0-20170215093142-bf70f2a70fb1 // indirect | |
| github.com/cloudflare/circl v1.3.2 // indirect | |
| github.com/coreos/go-oidc v2.2.1+incompatible // indirect | |
| github.com/davecgh/go-spew v1.1.1 // indirect | |
| github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect | |
| github.com/docker/distribution v2.8.1+incompatible // indirect | |
| github.com/emicklei/go-restful/v3 v3.8.0 // indirect | |
| github.com/emirpasic/gods v1.18.1 // indirect | |
| github.com/evanphx/json-patch v5.6.0+incompatible // indirect | |
| github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect | |
| github.com/fatih/camelcase v1.0.0 // indirect | |
| github.com/felixge/httpsnoop v1.0.3 // indirect | |
| github.com/fvbommel/sortorder v1.0.1 // indirect | |
| github.com/ghodss/yaml v1.0.0 // indirect | |
| github.com/go-errors/errors v1.0.1 // indirect | |
| github.com/go-git/gcfg v1.5.0 // indirect | |
| github.com/go-git/go-billy/v5 v5.4.1 // indirect | |
| github.com/go-git/go-git/v5 v5.6.1 // indirect | |
| github.com/go-logr/logr v1.2.3 // indirect | |
| github.com/go-logr/stdr v1.2.2 // indirect | |
| github.com/go-openapi/jsonpointer v0.19.5 // indirect | |
| github.com/go-openapi/jsonreference v0.20.0 // indirect | |
| github.com/go-openapi/swag v0.21.1 // indirect | |
| github.com/go-redis/cache/v8 v8.4.2 // indirect | |
| github.com/go-redis/redis/v8 v8.11.5 // indirect | |
| github.com/gobwas/glob v0.2.3 // indirect | |
| github.com/gogo/protobuf v1.3.2 // indirect | |
| github.com/golang-jwt/jwt/v4 v4.4.3 // indirect | |
| github.com/golang/protobuf v1.5.2 // indirect | |
| github.com/google/btree v1.0.1 // indirect | |
| github.com/google/gnostic v0.5.7-v3refs // indirect | |
| github.com/google/go-cmp v0.5.9 // indirect | |
| github.com/google/go-github/v45 v45.2.0 // indirect | |
| github.com/google/go-querystring v1.1.0 // indirect | |
| github.com/google/gofuzz v1.1.0 // indirect | |
| github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect | |
| github.com/google/uuid v1.3.0 // indirect | |
| github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect | |
| github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect | |
| github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect | |
| github.com/hashicorp/go-cleanhttp v0.5.2 // indirect | |
| github.com/hashicorp/go-retryablehttp v0.7.0 // indirect | |
| github.com/imdario/mergo v0.3.15 // indirect | |
| github.com/inconshreveable/mousetrap v1.0.1 // indirect | |
| github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect | |
| github.com/jonboulle/clockwork v0.2.2 // indirect | |
| github.com/josharian/intern v1.0.0 // indirect | |
| github.com/json-iterator/go v1.1.12 // indirect | |
| github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect | |
| github.com/kevinburke/ssh_config v1.2.0 // indirect | |
| github.com/klauspost/compress v1.15.9 // indirect | |
| github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect | |
| github.com/mailru/easyjson v0.7.7 // indirect | |
| github.com/mitchellh/go-wordwrap v1.0.0 // indirect | |
| github.com/moby/spdystream v0.2.0 // indirect | |
| github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect | |
| github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | |
| github.com/modern-go/reflect2 v1.0.2 // indirect | |
| github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect | |
| github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | |
| github.com/opencontainers/go-digest v1.0.0 // indirect | |
| github.com/patrickmn/go-cache v2.1.0+incompatible // indirect | |
| github.com/peterbourgon/diskv v2.0.1+incompatible // indirect | |
| github.com/pjbgf/sha1cd v0.3.0 // indirect | |
| github.com/pkg/errors v0.9.1 // indirect | |
| github.com/pmezard/go-difflib v1.0.0 // indirect | |
| github.com/pquerna/cachecontrol v0.1.0 // indirect | |
| github.com/r3labs/diff v1.1.0 // indirect | |
| github.com/robfig/cron/v3 v3.0.1 // indirect | |
| github.com/russross/blackfriday v1.5.2 // indirect | |
| github.com/sergi/go-diff v1.3.1 // indirect | |
| github.com/sirupsen/logrus v1.9.0 // indirect | |
| github.com/skeema/knownhosts v1.1.0 // indirect | |
| github.com/spf13/cobra v1.6.1 // indirect | |
| github.com/spf13/pflag v1.0.5 // indirect | |
| github.com/stretchr/testify v1.8.1 // indirect | |
| github.com/vmihailenco/go-tinylfu v0.2.1 // indirect | |
| github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect | |
| github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect | |
| github.com/xanzy/ssh-agent v0.3.3 // indirect | |
| github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect | |
| go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.31.0 // indirect | |
| go.opentelemetry.io/otel v1.11.1 // indirect | |
| go.opentelemetry.io/otel/trace v1.11.1 // indirect | |
| go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect | |
| golang.org/x/crypto v0.8.0 // indirect | |
| golang.org/x/exp v0.0.0-20210901193431-a062eea981d2 // indirect | |
| golang.org/x/mod v0.10.0 // indirect | |
| golang.org/x/net v0.9.0 // indirect | |
| golang.org/x/oauth2 v0.0.0-20220608161450-d0670ef3b1eb // indirect | |
| golang.org/x/sync v0.1.0 // indirect | |
| golang.org/x/sys v0.7.0 // indirect | |
| golang.org/x/term v0.7.0 // indirect | |
| golang.org/x/text v0.9.0 // indirect | |
| golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect | |
| golang.org/x/tools v0.8.0 // indirect | |
| google.golang.org/appengine v1.6.7 // indirect | |
| google.golang.org/genproto v0.0.0-20220107163113-42d7afdf6368 // indirect | |
| google.golang.org/grpc v1.51.0 // indirect | |
| google.golang.org/protobuf v1.28.1 // indirect | |
| gopkg.in/inf.v0 v0.9.1 // indirect | |
| gopkg.in/square/go-jose.v2 v2.6.0 // indirect | |
| gopkg.in/warnings.v0 v0.1.2 // indirect | |
| gopkg.in/yaml.v2 v2.4.0 // indirect | |
| gopkg.in/yaml.v3 v3.0.1 // indirect | |
| k8s.io/api v0.24.2 // indirect | |
| k8s.io/apiextensions-apiserver v0.24.2 // indirect | |
| k8s.io/apiserver v0.24.2 // indirect | |
| k8s.io/cli-runtime v0.24.2 // indirect | |
| k8s.io/client-go v0.24.2 // indirect | |
| k8s.io/component-base v0.24.2 // indirect | |
| k8s.io/component-helpers v0.24.2 // indirect | |
| k8s.io/klog/v2 v2.70.1 // indirect | |
| k8s.io/kube-aggregator v0.24.2 // indirect | |
| k8s.io/kube-openapi v0.0.0-20220627174259-011e075b9cb8 // indirect | |
| k8s.io/kubectl v0.24.2 // indirect | |
| k8s.io/kubernetes v1.24.2 // indirect | |
| k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect | |
| sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect | |
| sigs.k8s.io/kustomize/api v0.11.4 // indirect | |
| sigs.k8s.io/kustomize/kyaml v0.13.6 // indirect | |
| sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect | |
| sigs.k8s.io/yaml v1.3.0 // indirect | |
| ) | |
| replace ( | |
| k8s.io/api => k8s.io/api v0.24.2 | |
| k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.24.2 | |
| k8s.io/apimachinery => k8s.io/apimachinery v0.24.2 | |
| k8s.io/apiserver => k8s.io/apiserver v0.24.2 | |
| k8s.io/cli-runtime => k8s.io/cli-runtime v0.24.2 | |
| k8s.io/client-go => k8s.io/client-go v0.24.2 | |
| k8s.io/cloud-provider => k8s.io/cloud-provider v0.24.2 | |
| k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.24.2 | |
| k8s.io/code-generator => k8s.io/code-generator v0.24.2 | |
| k8s.io/component-base => k8s.io/component-base v0.24.2 | |
| k8s.io/component-helpers => k8s.io/component-helpers v0.24.2 | |
| k8s.io/controller-manager => k8s.io/controller-manager v0.24.2 | |
| k8s.io/cri-api => k8s.io/cri-api v0.24.2 | |
| k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.24.2 | |
| k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.24.2 | |
| k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.24.2 | |
| k8s.io/kube-proxy => k8s.io/kube-proxy v0.24.2 | |
| k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.24.2 | |
| k8s.io/kubectl => k8s.io/kubectl v0.24.2 | |
| k8s.io/kubelet => k8s.io/kubelet v0.24.2 | |
| k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.24.2 | |
| k8s.io/metrics => k8s.io/metrics v0.24.2 | |
| k8s.io/mount-utils => k8s.io/mount-utils v0.24.2 | |
| k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.24.2 | |
| k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.24.2 | |
| ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "engine/internal/driver/argocd" | |
| ) | |
| func main() { | |
| connection := argocd.Connection{ | |
| Address: "localhost:8080", | |
| Token: "my-foo-account-token", | |
| } | |
| client, err := argocd.NewClient(&connection) | |
| if err != nil { | |
| panic(err) | |
| } | |
| createProject, err := client.CreateProject("foo") | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Println(createProject.UID) | |
| err = client.AddDestination(createProject.Name, "server", "namespace", "name") | |
| if err != nil { | |
| panic(err) | |
| } | |
| getProject, err := client.GetProject("foo") | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Println(getProject.Namespace) | |
| err = client.DeleteProject(getProject.Name) | |
| if err != nil { | |
| panic(err) | |
| } | |
| clusters, err := client.GetClusters() | |
| if err != nil { | |
| panic(err) | |
| } | |
| for _, cluster := range clusters { | |
| fmt.Println(cluster.Name) | |
| } | |
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| run: | |
| @go run ./cmd/engine | |
| test: | |
| @go test ./... -count=1 | |
| lint: | |
| @golangci-lint run | |
| format: | |
| @gofumpt -l -w -extra . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package argocd | |
| import ( | |
| "context" | |
| "github.com/argoproj/argo-cd/v2/pkg/apiclient/project" | |
| "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | |
| "k8s.io/apimachinery/pkg/apis/meta/v1" | |
| ) | |
| func (c *Client) CreateProject(name string) (*v1alpha1.AppProject, error) { | |
| return c.projectClient.Create(context.Background(), &project.ProjectCreateRequest{ | |
| Project: &v1alpha1.AppProject{ | |
| ObjectMeta: v1.ObjectMeta{ | |
| Name: name, | |
| }, | |
| }, | |
| }) | |
| } | |
| func (c *Client) DeleteProject(name string) error { | |
| _, err := c.projectClient.Delete(context.Background(), &project.ProjectQuery{ | |
| Name: name, | |
| }) | |
| return err | |
| } | |
| func (c *Client) GetProject(name string) (*v1alpha1.AppProject, error) { | |
| return c.projectClient.Get(context.Background(), &project.ProjectQuery{ | |
| Name: name, | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment