Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
Signed-off-by: Cloud-Native Toolkit <[email protected]>
  • Loading branch information
cloudnativetoolkit committed Feb 8, 2022
0 parents commit 29c8b0c
Show file tree
Hide file tree
Showing 31 changed files with 1,431 additions and 0 deletions.
65 changes: 65 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.DS_Store
terraform-provider-hashicups
bin

# Created by https://www.toptal.com/developers/gitignore/api/go,terraform
# Edit at https://www.toptal.com/developers/gitignore?templates=go,terraform

### Go ###
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

### Go Patch ###
/vendor/
/Godeps/

### Terraform ###
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log

# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
# .tfvars files are managed as part of configuration and so should be included in
# version control.
#
# example.tfvars

# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include override files you do wish to add to version control using negated pattern
# !example_override.tf

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*

# End of https://www.toptal.com/developers/gitignore/api/go,terraform

# Ignore CLI configuration files
.terraformrc
terraform.rc
terraform
.idea/
37 changes: 37 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
TEST?=$$(go list ./... | grep -v 'vendor')
HOSTNAME=hashicorp.com
NAMESPACE=cntk
NAME=gitops
BINARY=terraform-provider-${NAME}
VERSION=0.1
OS_ARCH=darwin_amd64

default: install

build:
go build -o ${BINARY}

release:
GOOS=darwin GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_darwin_amd64
GOOS=freebsd GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_freebsd_386
GOOS=freebsd GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_freebsd_amd64
GOOS=freebsd GOARCH=arm go build -o ./bin/${BINARY}_${VERSION}_freebsd_arm
GOOS=linux GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_linux_386
GOOS=linux GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_linux_amd64
GOOS=linux GOARCH=arm go build -o ./bin/${BINARY}_${VERSION}_linux_arm
GOOS=openbsd GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_openbsd_386
GOOS=openbsd GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_openbsd_amd64
GOOS=solaris GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_solaris_amd64
GOOS=windows GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_windows_386
GOOS=windows GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_windows_amd64

install: build
mkdir -p ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}
mv ${BINARY} ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${NAME}/${VERSION}/${OS_ARCH}

test:
go test -i $(TEST) || exit 1
echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4

testacc:
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Terraform Provider Gitops

Run the following command to build the provider

```shell
go build -o terraform-provider-gitops
```

## Test sample configuration

First, build and install the provider.

```shell
make install
```

Then, run the following command to initialize the workspace and apply the sample configuration.

```shell
terraform init && terraform apply
```
5 changes: 5 additions & 0 deletions docker_compose/conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"db_connection": "host=db port=5432 user=postgres password=password dbname=products sslmode=disable",
"bind_address": "0.0.0.0:9090",
"metrics_address": "localhost:9102"
}
20 changes: 20 additions & 0 deletions docker_compose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: '3.7'
services:
api:
image: "hashicorpdemoapp/product-api:v4280cf7"
ports:
- "19090:9090"
volumes:
- ./conf.json:/config/config.json
environment:
CONFIG_FILE: '/config/config.json'
depends_on:
- db
db:
image: "hashicorpdemoapp/product-api-db:v4280cf7"
ports:
- "15432:5432"
environment:
POSTGRES_DB: 'products'
POSTGRES_USER: 'postgres'
POSTGRES_PASSWORD: 'password'
4 changes: 4 additions & 0 deletions examples/apply.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

terraform apply -auto-approve

3 changes: 3 additions & 0 deletions examples/destroy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

terraform destroy -auto-approve
19 changes: 19 additions & 0 deletions examples/gitops_module/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
terraform {
required_providers {
gitops = {
version = "0.1"
source = "hashicorp.com/cntk/gitops"
}
}
}

resource gitops_module ns {
name = var.name
namespace = var.namespace
content_dir = "${path.module}/yaml"
server_name = var.server_name
layer = var.layer
type = var.type
config = var.config
credentials = var.credentials
}
23 changes: 23 additions & 0 deletions examples/gitops_module/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

variable "config" {
}

variable "credentials" {
}

variable "server_name" {
}

variable "layer" {
default = "infrastructure"
}

variable "type" {
default = "base"
}

variable "namespace" {
}

variable "name" {
}
6 changes: 6 additions & 0 deletions examples/gitops_module/yaml/mymodule.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: mymodule
data:
test: value
16 changes: 16 additions & 0 deletions examples/gitops_namespace/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
terraform {
required_providers {
gitops = {
version = "0.1"
source = "hashicorp.com/cntk/gitops"
}
}
}

resource gitops_namespace ns {
name = "cntk"
content_dir = "${path.module}/yaml"
server_name = var.server_name
config = var.config
credentials = var.credentials
}
3 changes: 3 additions & 0 deletions examples/gitops_namespace/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "name" {
value = gitops_namespace.ns.name
}
9 changes: 9 additions & 0 deletions examples/gitops_namespace/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

variable "config" {
}

variable "credentials" {
}

variable "server_name" {
}
4 changes: 4 additions & 0 deletions examples/gitops_namespace/yaml/ns.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: cntk
4 changes: 4 additions & 0 deletions examples/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

module setup_clis {
source = "github.com/cloud-native-toolkit/terraform-util-clis.git"
}
6 changes: 6 additions & 0 deletions examples/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

provider "gitops" {
username = var.git_username
token = var.git_token
bin_dir = module.setup_clis.bin_dir
}
4 changes: 4 additions & 0 deletions examples/reset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

rm .terraform.lock.hcl
terraform init
4 changes: 4 additions & 0 deletions examples/stage1-cert.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module "cert" {
source = "github.com/cloud-native-toolkit/terraform-util-sealed-secret-cert.git"

}
23 changes: 23 additions & 0 deletions examples/stage1-gitops.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module "gitops" {
source = "github.com/cloud-native-toolkit/terraform-tools-gitops"

host = var.git_host
type = var.git_type
org = var.git_org
repo = var.git_repo
token = var.git_token
public = true
username = var.git_username
gitops_namespace = var.gitops_namespace
sealed_secrets_cert = module.cert.cert
}

resource null_resource gitops_output {
provisioner "local-exec" {
command = "echo -n '${module.gitops.config_repo}' > git_repo"
}

provisioner "local-exec" {
command = "echo -n '${module.gitops.config_token}' > git_token"
}
}
8 changes: 8 additions & 0 deletions examples/stage2-namespace.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

module cntk_namespace {
source = "./gitops_namespace"

server_name = module.gitops.server_name
config = jsonencode(module.gitops.gitops_config)
credentials = jsonencode(module.gitops.git_credentials)
}
20 changes: 20 additions & 0 deletions examples/stage3-mymodule.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

module cntk_module {
source = "./gitops_module"

name = "my-module"
namespace = module.cntk_namespace.name
server_name = module.gitops.server_name
config = jsonencode(module.gitops.gitops_config)
credentials = jsonencode(module.gitops.git_credentials)
}

module another_module {
source = "./gitops_module"

name = "another-module"
namespace = module.cntk_namespace.name
server_name = module.gitops.server_name
config = jsonencode(module.gitops.gitops_config)
credentials = jsonencode(module.gitops.git_credentials)
}
2 changes: 2 additions & 0 deletions examples/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
git_token="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"
git_username="cloudntivetoolkit"
26 changes: 26 additions & 0 deletions examples/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

variable "git_host" {
default = "github.com"
}

variable "git_type" {
default = "github"
}

variable "git_org" {
default = "cloud-native-toolkit-test"
}

variable "git_repo" {
default = "provider-test"
}

variable "git_token" {
}

variable "git_username" {
}

variable "gitops_namespace" {
default = "openshift-gitops"
}
7 changes: 7 additions & 0 deletions examples/version.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
required_providers {
gitops = {
source = "hashicorp.com/cntk/gitops"
}
}
}
Loading

0 comments on commit 29c8b0c

Please sign in to comment.