diff --git a/README.md b/README.md index c27848b..fe31f50 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/rumstead/gitops-toolkit)](https://goreportcard.com/badge/github.com/rumstead/gitops-toolkit) # gitops-toolkit Helpful manifests, scripts, and tools for gitops. Currently, the go binary supports creating N of clusters and allowing a GitOps engine to manage them. -The first use case was to setup an environment to test [Argo CD Application Sets](https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/#introduction-to-applicationset-controller), +The first use case was to set up an environment to test [Argo CD Application Sets](https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/#introduction-to-applicationset-controller), specifically with [cluster generators](https://argo-cd.readthedocs.io/en/stable/operator-manual/applicationset/Generators-Cluster/). +## Troubleshooting +See the [troubleshooting guide](TROUBLESHOOTING.md). + ## Shell Script The first version of the toolkit was done via [shell scripts](hack/multiple-clusters/README.md). @@ -17,10 +20,10 @@ make build go install github.com/rumstead/gitops-toolkit ``` ### Configuring -The script reads a json configuration file to create clusters. Order of the clusters matters because of how K3d updates DNS. The bottom most cluster can +The script reads a json/yaml configuration file to create clusters. Order of the clusters matters because of how K3d updates DNS. The bottom cluster can address all cluster above it. #### Schema -A json schema file can be found [here](pkg/config/v1alpha1/schema.json) with a [sample](pkg/config/testdata/clusters.json). +A json schema file can be found [here](pkg/config/v1alpha1/schema.json) with a [sample](pkg/config/testdata/clusters.json). Similarly, a yaml example file is [here](pkg/config/testdata/clusters.yaml). #### Generating a configuration file You can use the [proto structs](pkg/config/v1alpha1/cluster-config.pb.go) to write your configuration in code and dump them out as json. ## What is happening under the covers? diff --git a/TROUBLESHOOTING.md b/TROUBLESHOOTING.md new file mode 100644 index 0000000..ea9792f --- /dev/null +++ b/TROUBLESHOOTING.md @@ -0,0 +1,10 @@ +# FAQ/Troubleshooting + +## I want to pass in different K3s/K3d args +You can pass in any `k3s` argument or any `k3d` argument via the `additionalArgs` array. + +It is a great way to pass in a different k8s version. + +## level=fatal msg="dial tcp: lookup host.docker.internal..." +You can control the container gateway via the `CRI_GATEWAY` environment variable. +Ie, for podman `CRI_GATEWAY=containers` diff --git a/pkg/gitops/argocd/argocd.go b/pkg/gitops/argocd/argocd.go index 647f166..7504d37 100644 --- a/pkg/gitops/argocd/argocd.go +++ b/pkg/gitops/argocd/argocd.go @@ -33,6 +33,7 @@ func NewGitOpsEngine(binaries map[string]string) gitops.Engine { } func (a *Agent) Deploy(ctx context.Context, ops *kubernetes.Cluster) error { + logging.Log().Infoln("Deploying Argo CD") if _, err := os.Stat(ops.KubeConfigPath); err != nil { return err } @@ -181,8 +182,16 @@ func (a *Agent) AddCluster(_ context.Context, ops, workload *kubernetes.Cluster) clusterName := fmt.Sprintf("CLUSTER=%s", workload.RequestCluster.GetName()) labels := generateArgs(clusterArgLabels, workload.GetLabels()) annotations := generateArgs(clusterArgAnnotations, workload.GetAnnotations()) - cmd := exec.Command(a.cmd.CR, "run", "--network", ops.GetNetwork(), "--rm", "-e", argoUser, "-e", argoPasswd, "-e", kubeConfig, "-e", contextName, - "-e", clusterName, "-v", workDirVolume, "quay.io/argoproj/argocd:latest", "/hack/addCluster.sh", labels+annotations) + cmd := exec.Command(a.cmd.CR, "run", "--network", ops.GetNetwork(), "--rm", + "-e", argoUser, + "-e", argoPasswd, + "-e", kubeConfig, + "-e", contextName, + "-e", clusterName, + "-e", "CRI_GATEWAY", + "-v", workDirVolume, + "quay.io/argoproj/argocd:latest", "/hack/addCluster.sh", labels+annotations) + logging.Log().Debugf("%s\n", cmd.String()) if output, err := tkexec.RunCommand(cmd); err != nil { return fmt.Errorf("error adding cluster to gitops agent: %s: %v", string(output), err) } diff --git a/pkg/gitops/argocd/embed/addClusters.sh b/pkg/gitops/argocd/embed/addClusters.sh index 85dac87..bee1892 100755 --- a/pkg/gitops/argocd/embed/addClusters.sh +++ b/pkg/gitops/argocd/embed/addClusters.sh @@ -2,9 +2,11 @@ set -e set -o pipefail +# support podman or any other non-docker gateway +CRI_GATEWAY="${CRI_GATEWAY:-"docker"}" # login # https://docs.docker.com/desktop/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host -argocd login host.docker.internal:8080 --insecure --username "$ARGOUSER" --password "$ARGOPASSWD" +argocd login "host.$CRI_GATEWAY.internal:8080" --insecure --username "$ARGOUSER" --password "$ARGOPASSWD" # don't quote $1 so it globs argocd cluster add -y --upsert "$CONTEXT" --insecure --name "$CLUSTER" --kubeconfig "$KUBECONFIG" $1 \ No newline at end of file diff --git a/pkg/kubernetes/k3d/k3d.go b/pkg/kubernetes/k3d/k3d.go index fa63c2d..b39d7f8 100644 --- a/pkg/kubernetes/k3d/k3d.go +++ b/pkg/kubernetes/k3d/k3d.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "os" k3dcluster "github.com/k3d-io/k3d/v5/cmd/cluster" k3dclient "github.com/k3d-io/k3d/v5/pkg/client" @@ -36,6 +37,10 @@ func (k *K3d) getKubeConfig(ctx context.Context, cluster *v1alpha1.RequestCluste if err != nil { return "", err } + // allow anyone to read the file since it will be consumed by the gitops agent later. + if err = os.Chmod(output, 0755); err != nil { + return "", err + } return output, nil } diff --git a/pkg/logging/logging.go b/pkg/logging/logging.go index a33a813..b619f2c 100644 --- a/pkg/logging/logging.go +++ b/pkg/logging/logging.go @@ -1,6 +1,8 @@ package logging import ( + "os" + "github.com/sirupsen/logrus" ) @@ -8,7 +10,9 @@ var Logger *logrus.Logger func init() { Logger = logrus.New() - Logger.SetLevel(logrus.DebugLevel) + if os.Getenv("DEBUG") != "" { + Logger.SetLevel(logrus.DebugLevel) + } } func Log() *logrus.Logger {