Documentation
¶
Overview ¶
Package argocd implements the stack.Workflow interface for Argo CD. It generates one Argo CD Application (argoproj.io/v1alpha1) for each directory of the written layout that renders bundles, as unstructured objects, and places them in an argocd/ directory inside the root layout.
Importing the package for its side effect registers the "argocd" workflow with the stack package:
import _ "github.com/go-kure/kure/pkg/stack/argocd"
Engine returns an engine whose RepoURL is the placeholder https://github.com/example/manifests.git; set the real repository with SetRepoURL before generating.
Bootstrap is not implemented: GenerateBootstrap returns an error when the configuration is enabled. The Flux engine in package fluxcd generates bootstrap manifests.
Example ¶
package main
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/go-kure/kure/pkg/kubernetes"
"github.com/go-kure/kure/pkg/stack"
"github.com/go-kure/kure/pkg/stack/layout"
)
// configMapApp is a minimal stack.ApplicationConfig: one ConfigMap named after
// the application.
type configMapApp struct{}
func (configMapApp) Generate(app *stack.Application) ([]*client.Object, error) {
var obj client.Object = kubernetes.CreateConfigMap(app.Name, app.Namespace)
return []*client.Object{&obj}, nil
}
// exampleCluster is the cluster the examples generate from: one node with one
// bundle holding one application.
func exampleCluster() *stack.Cluster {
cluster, err := stack.NewClusterBuilder("prod").
WithNode("apps").
WithBundle("web").
WithApplication("web", configMapApp{}).
End().
End().
Build()
if err != nil {
panic(err)
}
return cluster
}
// printFiles prints every file below dir, relative to it.
func printFiles(dir string) {
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err == nil && !d.IsDir() {
rel, _ := filepath.Rel(dir, path)
fmt.Println(rel)
}
return err
})
if err != nil {
panic(err)
}
}
func main() {
cluster := exampleCluster()
dir, err := os.MkdirTemp("", "kure-argocd-example")
if err != nil {
panic(err)
}
defer func() { _ = os.RemoveAll(dir) }()
// Use via the stack.Workflow registry
wf, err := stack.NewWorkflow("argocd")
if err != nil {
panic(err)
}
ml, err := wf.CreateLayoutWithResources(cluster, layout.LayoutRules{})
if err != nil {
panic(err)
}
if err := ml.WriteToDisk(filepath.Join(dir, "clusters/prod")); err != nil {
panic(err)
}
printFiles(dir)
}
Output: clusters/prod/apps/argocd/argocd-application-web.yaml clusters/prod/apps/argocd/kustomization.yaml clusters/prod/apps/kustomization.yaml clusters/prod/apps/web/cluster-configmap-web.yaml clusters/prod/apps/web/kustomization.yaml
Example (AgentsWorkflow) ¶
cluster := exampleCluster()
// The provider registers itself from its init, so the package has to be
// imported: import _ "github.com/go-kure/kure/pkg/stack/argocd"
wf, err := stack.NewWorkflow("argocd")
if err != nil {
panic(err)
}
// Application paths are the directories WalkCluster writes with the rules
// you pass, so pass the ones you write the tree with.
apps, err := wf.GenerateFromCluster(cluster, layout.DefaultLayoutRules())
if err != nil {
panic(err)
}
fmt.Println(len(apps), apps[0].GetObjectKind().GroupVersionKind().Kind, apps[0].GetName())
Output: 1 Application web
Index ¶
- type WorkflowEngine
- func (w *WorkflowEngine) CreateLayoutWithResources(c *stack.Cluster, rulesInterface stack.LayoutRulesProvider) (stack.ManifestLayoutResult, error)
- func (w *WorkflowEngine) GenerateBootstrap(config *stack.BootstrapConfig, rootNode *stack.Node, ...) ([]client.Object, error)
- func (w *WorkflowEngine) GenerateFromCluster(c *stack.Cluster, rulesInterface stack.LayoutRulesProvider) ([]client.Object, error)
- func (w *WorkflowEngine) GetName() string
- func (w *WorkflowEngine) GetVersion() string
- func (w *WorkflowEngine) IntegrateWithLayout(ml *layout.ManifestLayout, c *stack.Cluster, rules layout.LayoutRules) error
- func (w *WorkflowEngine) SetDefaultNamespace(namespace string)
- func (w *WorkflowEngine) SetRepoURL(repoURL string)
- func (w *WorkflowEngine) SupportedBootstrapModes() []string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type WorkflowEngine ¶
type WorkflowEngine struct {
// RepoURL is used as the source repo for generated Applications
RepoURL string
// DefaultNamespace is the default namespace for ArgoCD Applications
DefaultNamespace string
}
WorkflowEngine implements the stack.Workflow interface for ArgoCD.
func Engine ¶
func Engine() *WorkflowEngine
Engine creates an ArgoCD workflow engine.
Example ¶
package main
import (
"fmt"
"github.com/go-kure/kure/pkg/stack/argocd"
)
func main() {
// Direct construction (bypasses registry)
engine := argocd.Engine()
// Configure source repository and namespace
engine.SetRepoURL("https://github.com/example/manifests.git")
engine.SetDefaultNamespace("argocd")
fmt.Println(engine.GetName(), engine.RepoURL, engine.DefaultNamespace)
}
Output: ArgoCD Workflow Engine https://github.com/example/manifests.git argocd
func (*WorkflowEngine) CreateLayoutWithResources ¶
func (w *WorkflowEngine) CreateLayoutWithResources(c *stack.Cluster, rulesInterface stack.LayoutRulesProvider) (stack.ManifestLayoutResult, error)
CreateLayoutWithResources creates a new layout that includes ArgoCD Applications.
Example ¶
package main
import (
"fmt"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/go-kure/kure/pkg/kubernetes"
"github.com/go-kure/kure/pkg/stack"
"github.com/go-kure/kure/pkg/stack/argocd"
"github.com/go-kure/kure/pkg/stack/layout"
)
// configMapApp is a minimal stack.ApplicationConfig: one ConfigMap named after
// the application.
type configMapApp struct{}
func (configMapApp) Generate(app *stack.Application) ([]*client.Object, error) {
var obj client.Object = kubernetes.CreateConfigMap(app.Name, app.Namespace)
return []*client.Object{&obj}, nil
}
// exampleCluster is the cluster the examples generate from: one node with one
// bundle holding one application.
func exampleCluster() *stack.Cluster {
cluster, err := stack.NewClusterBuilder("prod").
WithNode("apps").
WithBundle("web").
WithApplication("web", configMapApp{}).
End().
End().
Build()
if err != nil {
panic(err)
}
return cluster
}
func main() {
cluster := exampleCluster()
engine := argocd.Engine()
// Create layout with Applications placed in an argocd/ subdirectory
result, err := engine.CreateLayoutWithResources(cluster, layout.LayoutRules{})
if err != nil {
panic(err)
}
ml := result.(*layout.ManifestLayout)
// Integrate Applications into an existing layout (a no-op for ArgoCD)
if err := engine.IntegrateWithLayout(ml, cluster, layout.LayoutRules{}); err != nil {
panic(err)
}
for _, child := range ml.Children {
fmt.Println(child.FullRepoPath(), len(child.Resources))
}
}
Output: apps/web 1 apps/argocd 1
func (*WorkflowEngine) GenerateBootstrap ¶
func (w *WorkflowEngine) GenerateBootstrap(config *stack.BootstrapConfig, rootNode *stack.Node, _ stack.LayoutRulesProvider) ([]client.Object, error)
GenerateBootstrap creates bootstrap resources for setting up ArgoCD. It takes the layout rules because stack.Workflow passes them (the Flux bootstrap points Flux at the top directory of the tree they write), and reads nothing from them: neither their type nor their values are checked. Rules that are nil, of another type than layout.LayoutRules, or invalid change nothing here, where the Flux engine's GenerateBootstrap refuses all three.
func (*WorkflowEngine) GenerateFromCluster ¶
func (w *WorkflowEngine) GenerateFromCluster(c *stack.Cluster, rulesInterface stack.LayoutRulesProvider) ([]client.Object, error)
GenerateFromCluster creates ArgoCD Applications from a cluster definition: it walks the cluster with the rules and generates from that layout (see generateFromLayout), so each source.path is the directory a walk with those rules writes the bundle to. The rules must be the ones the caller writes the tree with, and are held to what CreateLayoutWithResources accepts (see layoutRules, then the walk's layout.LayoutRules.Validate). An absent or empty cluster is walked too, so invalid rules are an error whatever the cluster is; with valid rules it yields nothing.
Example ¶
package main
import (
"fmt"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/go-kure/kure/pkg/kubernetes"
"github.com/go-kure/kure/pkg/stack"
"github.com/go-kure/kure/pkg/stack/argocd"
"github.com/go-kure/kure/pkg/stack/layout"
)
// configMapApp is a minimal stack.ApplicationConfig: one ConfigMap named after
// the application.
type configMapApp struct{}
func (configMapApp) Generate(app *stack.Application) ([]*client.Object, error) {
var obj client.Object = kubernetes.CreateConfigMap(app.Name, app.Namespace)
return []*client.Object{&obj}, nil
}
// exampleCluster is the cluster the examples generate from: one node with one
// bundle holding one application.
func exampleCluster() *stack.Cluster {
cluster, err := stack.NewClusterBuilder("prod").
WithNode("apps").
WithBundle("web").
WithApplication("web", configMapApp{}).
End().
End().
Build()
if err != nil {
panic(err)
}
return cluster
}
func main() {
cluster := exampleCluster()
engine := argocd.Engine()
// Generate ArgoCD Applications from a cluster: each source.path is a
// directory a walk with the rules you pass writes
objects, err := engine.GenerateFromCluster(cluster, layout.DefaultLayoutRules())
if err != nil {
panic(err)
}
for _, obj := range objects {
path, _, _ := unstructured.NestedString(obj.(*unstructured.Unstructured).Object, "spec", "source", "path")
fmt.Println(obj.GetObjectKind().GroupVersionKind().Kind, obj.GetName(), path)
}
}
Output: Application web apps/web
func (*WorkflowEngine) GetName ¶
func (w *WorkflowEngine) GetName() string
GetName returns a human-readable name for this workflow engine.
func (*WorkflowEngine) GetVersion ¶
func (w *WorkflowEngine) GetVersion() string
GetVersion returns the version of this workflow engine.
func (*WorkflowEngine) IntegrateWithLayout ¶
func (w *WorkflowEngine) IntegrateWithLayout(ml *layout.ManifestLayout, c *stack.Cluster, rules layout.LayoutRules) error
IntegrateWithLayout adds ArgoCD Applications to an existing manifest layout. For ArgoCD, this is typically not needed as Applications reference external repos. It adds nothing to ml, and refuses what it would not honour: a delivery intent set by an application the layout records (see refuseDeliveryIntent) or by one of c's applications (a layout the caller built records none, so c is read as well), and a node that sets KustomizationName, DependsOn or NamedDependsOn, as in generation (checkNodeKustomizationFields).
func (*WorkflowEngine) SetDefaultNamespace ¶
func (w *WorkflowEngine) SetDefaultNamespace(namespace string)
SetDefaultNamespace configures the default namespace for ArgoCD Applications.
func (*WorkflowEngine) SetRepoURL ¶
func (w *WorkflowEngine) SetRepoURL(repoURL string)
SetRepoURL configures the repository URL for generated Applications.
func (*WorkflowEngine) SupportedBootstrapModes ¶
func (w *WorkflowEngine) SupportedBootstrapModes() []string
SupportedBootstrapModes returns the bootstrap modes supported by ArgoCD.