Skip to content

Commit

Permalink
Fix/support podman (#18)
Browse files Browse the repository at this point in the history
* fix: support podman

Signed-off-by: rumstead <[email protected]>
  • Loading branch information
rumstead authored Sep 14, 2023
1 parent 9f8926a commit e3750e5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# 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).

Expand All @@ -16,10 +19,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?
Expand Down
10 changes: 10 additions & 0 deletions TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
@@ -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`
13 changes: 11 additions & 2 deletions pkg/gitops/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/gitops/argocd/embed/addClusters.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions pkg/kubernetes/k3d/k3d.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/logging/logging.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package logging

import (
"os"

"github.com/sirupsen/logrus"
)

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 {
Expand Down

0 comments on commit e3750e5

Please sign in to comment.