-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #647 from swade1987/github-self-managed-ssh-keypair
feat(docs): Adding example of managing the flux ssh keypair in terraform.
- Loading branch information
Showing
8 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# GitHub via SSH | ||
|
||
The example demonstrates how to bootstrap a KinD cluster with Flux using a GitHub repository via SSH. | ||
|
||
The SSH keypair is managed by Terraform and can be rotated as needed. | ||
|
||
Note: The GitHub repository is created and auto initialised ready for Flux to use. | ||
|
||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
## Requirements | ||
|
||
| Name | Version | | ||
|------|---------| | ||
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.7.0 | | ||
| <a name="requirement_flux"></a> [flux](#requirement\_flux) | >= 1.2 | | ||
| <a name="requirement_github"></a> [github](#requirement\_github) | >= 6.1 | | ||
| <a name="requirement_kind"></a> [kind](#requirement\_kind) | >= 0.4 | | ||
| <a name="requirement_kubernetes"></a> [kubernetes](#requirement\_kubernetes) | >= 2.27 | | ||
| <a name="requirement_tls"></a> [tls](#requirement\_tls) | >= 4.0 | | ||
|
||
## Providers | ||
|
||
| Name | Version | | ||
|------|---------| | ||
| <a name="provider_flux"></a> [flux](#provider\_flux) | >= 1.2 | | ||
| <a name="provider_github"></a> [github](#provider\_github) | >= 6.1 | | ||
| <a name="provider_kind"></a> [kind](#provider\_kind) | >= 0.4 | | ||
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | >= 2.27 | | ||
| <a name="provider_tls"></a> [tls](#provider\_tls) | >= 4.0 | | ||
|
||
## Modules | ||
|
||
No modules. | ||
|
||
## Resources | ||
|
||
| Name | Type | | ||
|------|------| | ||
| [flux_bootstrap_git.this](https://registry.terraform.io/providers/fluxcd/flux/latest/docs/resources/bootstrap_git) | resource | | ||
| [github_repository.this](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository) | resource | | ||
| [github_repository_deploy_key.this](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_deploy_key) | resource | | ||
| [kind_cluster.this](https://registry.terraform.io/providers/tehcyx/kind/latest/docs/resources/cluster) | resource | | ||
| [kubernetes_namespace.flux_system](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/namespace) | resource | | ||
| [kubernetes_secret.ssh_keypair](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret) | resource | | ||
| [tls_private_key.flux](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key) | resource | | ||
|
||
## Inputs | ||
|
||
| Name | Description | Type | Default | Required | | ||
|------|-------------|------|---------|:--------:| | ||
| <a name="input_github_org"></a> [github\_org](#input\_github\_org) | GitHub organization | `string` | `""` | no | | ||
| <a name="input_github_repository"></a> [github\_repository](#input\_github\_repository) | GitHub repository | `string` | `""` | no | | ||
| <a name="input_github_token"></a> [github\_token](#input\_github\_token) | GitHub token | `string` | `""` | no | | ||
|
||
## Outputs | ||
|
||
No outputs. | ||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
terraform { | ||
required_version = ">= 1.7.0" | ||
|
||
required_providers { | ||
flux = { | ||
source = "fluxcd/flux" | ||
version = ">= 1.2" | ||
} | ||
github = { | ||
source = "integrations/github" | ||
version = ">= 6.1" | ||
} | ||
kind = { | ||
source = "tehcyx/kind" | ||
version = ">= 0.4" | ||
} | ||
kubernetes = { | ||
source = "hashicorp/kubernetes" | ||
version = ">= 2.27" | ||
} | ||
tls = { | ||
source = "hashicorp/tls" | ||
version = ">= 4.0" | ||
} | ||
} | ||
} | ||
|
||
# ======================================================================== | ||
# Construct KinD cluster | ||
# ======================================================================== | ||
|
||
resource "kind_cluster" "this" { | ||
name = "flux-e2e" | ||
} | ||
|
||
# ======================================================================== | ||
# Initialise a Github project | ||
# ======================================================================== | ||
|
||
resource "github_repository" "this" { | ||
name = var.github_repository | ||
description = var.github_repository | ||
visibility = "public" | ||
auto_init = true # This is extremely important as flux_bootstrap_git will not work without a repository that has been initialised | ||
} | ||
|
||
# ======================================================================== | ||
# Add deploy key to GitHub repository | ||
# ======================================================================== | ||
|
||
resource "tls_private_key" "flux" { | ||
algorithm = "ECDSA" | ||
ecdsa_curve = "P256" | ||
} | ||
|
||
resource "github_repository_deploy_key" "this" { | ||
title = "Flux" | ||
repository = github_repository.this.name | ||
key = tls_private_key.flux.public_key_openssh | ||
read_only = "false" | ||
} | ||
|
||
# ======================================================================== | ||
# Manage the SSH keypair flux uses to authenticate with GitHub | ||
# ======================================================================== | ||
|
||
resource "kubernetes_namespace" "flux_system" { | ||
metadata { | ||
name = "flux-system" | ||
} | ||
|
||
lifecycle { | ||
ignore_changes = [metadata] | ||
} | ||
} | ||
|
||
resource "kubernetes_secret" "ssh_keypair" { | ||
metadata { | ||
name = "flux-system" | ||
namespace = "flux-system" | ||
} | ||
|
||
type = "Opaque" | ||
|
||
data = { | ||
"identity.pub" = tls_private_key.flux.public_key_openssh | ||
"identity" = tls_private_key.flux.private_key_pem | ||
"known_hosts" = "github.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=" | ||
} | ||
|
||
depends_on = [kubernetes_namespace.flux_system] | ||
} | ||
|
||
# ======================================================================== | ||
# Bootstrap KinD cluster | ||
# ======================================================================== | ||
|
||
resource "flux_bootstrap_git" "this" { | ||
depends_on = [github_repository_deploy_key.this, kubernetes_secret.ssh_keypair] | ||
|
||
path = "clusters/my-cluster" | ||
disable_secret_creation = true | ||
} |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
provider "flux" { | ||
kubernetes = { | ||
host = kind_cluster.this.endpoint | ||
client_certificate = kind_cluster.this.client_certificate | ||
client_key = kind_cluster.this.client_key | ||
cluster_ca_certificate = kind_cluster.this.cluster_ca_certificate | ||
} | ||
git = { | ||
url = "ssh://[email protected]/${var.github_org}/${var.github_repository}.git" | ||
ssh = { | ||
username = "git" | ||
private_key = tls_private_key.flux.private_key_pem | ||
} | ||
} | ||
} | ||
|
||
provider "github" { | ||
owner = var.github_org | ||
token = var.github_token | ||
} | ||
|
||
provider "kind" {} | ||
|
||
provider "kubernetes" { | ||
host = kind_cluster.this.endpoint | ||
client_certificate = kind_cluster.this.client_certificate | ||
client_key = kind_cluster.this.client_key | ||
cluster_ca_certificate = kind_cluster.this.cluster_ca_certificate | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
variable "github_token" { | ||
description = "GitHub token" | ||
sensitive = true | ||
type = string | ||
default = "" | ||
} | ||
|
||
variable "github_org" { | ||
description = "GitHub organization" | ||
type = string | ||
default = "" | ||
} | ||
|
||
variable "github_repository" { | ||
description = "GitHub repository" | ||
type = string | ||
default = "" | ||
} |
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