Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion misc/roll_apps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
gem 'aws-sdk-ecr'
end

FILES = %W[k8s/**/*.{j,lib}sonnet tf/**/*.tf]
REGISTRIES = %w[005216166247]
TAG_RE = %r{(?<registry>\d+)\.dkr\.ecr\.(?<region>[\w-]+)\.amazonaws\.com/(?<repository>[\w/-]+):\K(?<tag>[0-9a-f]{40})}

@ecr = Hash.new {|h, region| h[region] = Aws::ECR::Client.new(region:) }

Pathname(__dir__).glob('../k8s/**/*.{j,lib}sonnet') do |path|
Pathname(__dir__).parent.glob(FILES) do |path|
content = path.read

updated = content.gsub!(TAG_RE) do
Expand Down
2 changes: 0 additions & 2 deletions tf/acme-responder/ec2.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ resource "aws_instance" "acme-responder-apne1c" {

user_data = local.user_data

associate_public_ip_address = false
ipv6_addresses = [
cidrhost(data.aws_subnet.main-public-c.ipv6_cidr_block, 4294945854) # 0xFFFF_AC3E
]
Expand Down Expand Up @@ -86,7 +85,6 @@ resource "aws_instance" "acme-responder-apne1d" {

user_data = local.user_data

associate_public_ip_address = false
ipv6_addresses = [
cidrhost(data.aws_subnet.main-public-d.ipv6_cidr_block, 4294945854) # 0xFFFF_AC3E
]
Expand Down
63 changes: 63 additions & 0 deletions tf/acmesmith/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

241 changes: 241 additions & 0 deletions tf/acmesmith/acmesmith.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
locals {
image = "005216166247.dkr.ecr.ap-northeast-1.amazonaws.com/acmesmith:aec356de54ac789e482af0043cdd002b4e5ba9ab"

hosted_zones = [data.aws_route53_zone.rubykaigi_net]
hosted_zone_map = {
for z in local.hosted_zones : "${z.name}." => "/hostedzone/${z.zone_id}"
}
}

data "aws_route53_zone" "rubykaigi_net" {
name = "rubykaigi.net."
private_zone = false
}

data "aws_s3_bucket" "rubykaigi-public" {
bucket = "rubykaigi-public"
}

data "external" "letsencrypt-staging" {
program = ["${path.module}/../jsonnet.rb"]
query = {
path = "${path.module}/letsencrypt.jsonnet"
args = jsonencode({
staging = true
hosted_zone_map = local.hosted_zone_map
bucket = data.aws_s3_bucket.rubykaigi-public.bucket
})
}
}

data "external" "letsencrypt" {
program = ["${path.module}/../jsonnet.rb"]
query = {
path = "${path.module}/letsencrypt.jsonnet"
args = jsonencode({
staging = false
hosted_zone_map = local.hosted_zone_map
bucket = data.aws_s3_bucket.rubykaigi-public.bucket
})
}
}

resource "kubernetes_config_map_v1" "letsencrypt-staging" {
metadata {
name = "acmesmith-letsencrypt-staging"
namespace = "default"
}
data = {
"acmesmith.yml" = data.external.letsencrypt-staging.result.json
}
}

resource "kubernetes_config_map_v1" "letsencrypt" {
metadata {
name = "acmesmith-letsencrypt"
namespace = "default"
}
data = {
"acmesmith.yml" = data.external.letsencrypt.result.json
}
}

resource "kubernetes_service_account_v1" "acmesmith" {
metadata {
name = "acmesmith"
namespace = "default"
annotations = {
"eks.amazonaws.com/role-arn" = aws_iam_role.acmesmith.arn
"eks.amazonaws.com/sts-regional-endpoints" = true
}
}
}

resource "kubernetes_role_v1" "acmesmith" {
metadata {
name = "acmesmith"
namespace = "default"
}
rule {
api_groups = [""]
resources = ["secrets"]
verbs = ["get", "list", "watch", "create", "update", "patch", "delete"]
}
rule {
api_groups = ["apps"]
resources = ["deployments"]
verbs = ["get", "list", "patch", "update"]
}
}

resource "kubernetes_role_binding_v1" "acmesmith" {
metadata {
name = "acmesmith"
namespace = "default"
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "Role"
name = kubernetes_role_v1.acmesmith.metadata[0].name
}
subject {
kind = "ServiceAccount"
name = kubernetes_service_account_v1.acmesmith.metadata[0].name
namespace = "default"
}
}

resource "kubernetes_job_v1" "new-account" {
for_each = tomap({
letsencrypt-staging = kubernetes_config_map_v1.letsencrypt-staging
letsencrypt = kubernetes_config_map_v1.letsencrypt
})

metadata {
name = "acmesmith-new-account-${each.key}"
namespace = "default"
}
spec {
template {
metadata {}
spec {
container {
name = "acmesmith"
image = local.image
args = [
"new-account",
"-c", "/config/acmesmith/acmesmith.yml",
"mailto:info@rubykaigi.org",
]
volume_mount {
name = "config"
mount_path = "/config/acmesmith"
read_only = true
}
}
volume {
name = "config"
config_map {
name = each.value.metadata[0].name
}
}
service_account_name = kubernetes_service_account_v1.acmesmith.metadata[0].name
restart_policy = "Never"
}
}
}

wait_for_completion = false
}

resource "kubernetes_cron_job_v1" "autorenew" {
for_each = tomap({
letsencrypt-staging = kubernetes_config_map_v1.letsencrypt-staging
letsencrypt = kubernetes_config_map_v1.letsencrypt
})

metadata {
name = "acmesmith-autorenew-${each.key}"
namespace = "default"
}
Comment thread
hanazuki marked this conversation as resolved.
spec {
schedule = "0 * * * *"
job_template {
metadata {}
spec {
template {
metadata {}
spec {
container {
name = "acmesmith"
image = local.image
args = [
"autorenew",
"-c", "/config/acmesmith/acmesmith.yml",
"-r", "1/2",
]
volume_mount {
name = "config"
mount_path = "/config/acmesmith"
read_only = true
}
}
volume {
name = "config"
config_map {
name = each.value.metadata[0].name
}
}
service_account_name = kubernetes_service_account_v1.acmesmith.metadata[0].name
restart_policy = "Never"
}
}
}
}
}
}

resource "kubernetes_job_v1" "order-resolver-rubykaigi-net" {
for_each = tomap({
letsencrypt = kubernetes_config_map_v1.letsencrypt
})

metadata {
name = "acmesmith-order-resolver-rubykaigi-net-${each.key}"
namespace = "default"
}
spec {
template {
metadata {}
spec {
container {
name = "acmesmith"
image = local.image
args = [
"order",
"-c", "/config/acmesmith/acmesmith.yml",
"--key-type", "ec", "--elliptic-curve", "prime256v1",
"resolver.rubykaigi.net",
"192.50.220.164", "192.50.220.165",
"2001:df0:8500:ca6d:53::0:c", "2001:df0:8500:ca6d:53:0::d",
]
volume_mount {
name = "config"
mount_path = "/config/acmesmith"
read_only = true
}
}
volume {
name = "config"
config_map {
name = each.value.metadata[0].name
}
}
service_account_name = kubernetes_service_account_v1.acmesmith.metadata[0].name
restart_policy = "Never"
}
}
}

wait_for_completion = false
}
12 changes: 12 additions & 0 deletions tf/acmesmith/aws.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
provider "aws" {
region = "ap-northeast-1"
allowed_account_ids = ["005216166247"]
default_tags {
tags = {
Project = "rk26net"
Component = "acmesmith"
}
}
}

data "aws_caller_identity" "current" {}
17 changes: 17 additions & 0 deletions tf/acmesmith/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
terraform {
backend "s3" {
bucket = "rk-infra"
region = "ap-northeast-1"
key = "terraform/nw-acmesmith.tfstate"
dynamodb_table = "rk-terraform"
}
}

data "terraform_remote_state" "k8s" {
backend = "s3"
config = {
bucket = "rk-infra"
region = "ap-northeast-1"
key = "terraform/nw-k8s.tfstate"
}
}
23 changes: 23 additions & 0 deletions tf/acmesmith/ecr.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
resource "aws_ecr_repository" "acmesmith" {
name = "acmesmith"
}

resource "aws_ecr_lifecycle_policy" "acmesmith" {
repository = aws_ecr_repository.acmesmith.name
policy = jsonencode({
rules = [
{
rulePriority = 10
description = "expire old images"
selection = {
tagStatus = "any"
countType = "imageCountMoreThan"
countNumber = 10
}
action = {
type = "expire"
}
}
]
})
}
Loading