Experience Report: Automate deployments with Flux CD, Google Cloud Source Repositories, Google Cloud Build and Google Artifact Registry #3361
-
I recently set up Flux for GitOps-style Kubernetes configuration deployments with Google Cloud Source Repositories, Google Cloud Build and Google Artifact Registry. Notably I did not use the ssh access method for Cloud Source Repositories. I am posting here because maybe my notes will help someone in the future, and I'm also going to note a few issues which might inform future Flux work. The short overview of the final system is as follows: Code is stored in a subdirectory of a Google Cloud Source Repository. A Cloud Build trigger invokes a Cloud Source Repository issue and solution.The documentation (https://fluxcd.io/flux/use-cases/gcp-source-repository/) suggests the following configuration:
I ran into two issues with this.
Because I wasn't sure, I tried hacking to see if I could get flux to use native Google Cloud authentication for its git operations. I went through https://fluxcd.io/flux/components/source/gitrepositories/, tried forcing libgit2, looked through the code and more. This was a big time sink partly because the documentation didn't clearly state that it is not supported, and partly because flux does support Google Cloud authentication in other areas, just not for git. Now I'm more convinced that Flux doesn't support it. Maybe one day CSR will support more authentication types (doubt it), maybe go-git can be extended to support gitcredentials, or maybe Flux can be directly extended to allow "real" git porcelain with git credential helper support as an optional git implementation, or something else. One way or the other, without flux able to run a git clone, that ruled out Manual Flux bootstrapOutside of Flux directly, I set up a Cloud Source Repository, a dedicated Service Account with Workload Identity enabled, a Cloud Build trigger, and a Google Artifact Registry, all with a messy tangle of required permissions. I happened to use Terraform for configuring these. I also performed my own Flux bootstrap: I put a apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- gotk-components.yaml
- gotk-sync.yaml
patches:
- patch: |-
- op: add
path: /metadata/annotations
value:
iam.gke.io/gcp-service-account: {my-service-account}
target:
kind: ServiceAccount
name: source-controller I committed these files to git and then ran Note this Kustomization for Workload Identity is slightly different from what was in the Flux docs. I wasn't able to get the suggested Kustomization from the docs to work. Maybe there has been some drift. flux pushNext I set up a Cloud Build trigger. Here is the minimal build script: timeout: 300s
options:
logging: CLOUD_LOGGING_ONLY
steps:
- name: "${_GAR}/flux"
args:
- push
- artifact
- oci://${_GAR}/${_KUBERNETES_NAME}:latest
- --path
- ${_FLUX_PATH}
- --source
- "Cloud Build ${TRIGGER_NAME}"
- --revision
- ${REVISION_ID}
- --provider
- gcp A few notes here:
DeploymentAfter a couple round trips where my noob errors resulted in Flux deleting its own controllers, I got it working. I tested it out by using a hello world deployment found at https://cloud.google.com/kubernetes-engine/docs/samples/container-helloapp-deployment and was surprised to see an error: “Deployment/helloweb namespace not specified, error: the server could not find the requested resource”. Similar to #2433. I found it surprising that Flux has a behavior difference from native kubernetes/kubectl where a missing namespace implicitly uses the "default" namespace. Thankfully the workaround is minor but it was an issue worth mentioning. Deployment BlissI mentioned earlier I was using Terraform for provisioning Google Cloud infrastructure. Looking back at build logs, a typical Terraform deploy with Google-provided scripts on a bare-bones GKE set up was taking about 1m20s to 2m30s, with occasional spikes way beyond. By comparison, with Flux the Cloud Builds are taking 10s–20s. Nice. More importantly Flux offers a much more Kubernetes-native model at very low complexity which I'm hoping will pay dividends over time. As I mentioned in the beginning I have high hopes this experience report is useful to anyone considering Flux on GCP and to the Flux team. Thanks again. Please reach out if you have any questions or comments. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Thank you very much @aran for the detailed guide and great feedback. We'll create dedicated issues for the things you reported. I do think we need to promote more OCI vs Git, I really like how you've setup OCI with Google Artifact Registry instead of Git/SSH with Google Cloud Source. Thanks again ❤️ |
Beta Was this translation helpful? Give feedback.
-
I had an issue pushing the oci manifests from Google Cloud Run (using the
and updated the |
Beta Was this translation helpful? Give feedback.
Thank you very much @aran for the detailed guide and great feedback. We'll create dedicated issues for the things you reported. I do think we need to promote more OCI vs Git, I really like how you've setup OCI with Google Artifact Registry instead of Git/SSH with Google Cloud Source. Thanks again ❤️