Skip to content

Commit

Permalink
fix: update jenkins configs, upstream modules (#51)
Browse files Browse the repository at this point in the history
* fix: update jenkins configs, upstream modules

* fix dependency for k8s provider

* add test

* add contrib guide
  • Loading branch information
bharathkkb authored Dec 7, 2021
1 parent 914fda3 commit 96dc183
Show file tree
Hide file tree
Showing 16 changed files with 1,323 additions and 72 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ credentials.json

# Local .terraform directories
**/.terraform/*
.terraform.lock.hcl

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

#mac
.DS_Store
.DS_Store
92 changes: 75 additions & 17 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,84 @@
# How to Contribute
# Contributing

We'd love to accept your patches and contributions to this project. There are
just a few small guidelines you need to follow.
This document provides guidelines for contributing to the module.

## Contributor License Agreement
## Dependencies

Contributions to this project must be accompanied by a Contributor License
Agreement. You (or your employer) retain the copyright to your contribution;
this simply gives us permission to use and redistribute your contributions as
part of the project. Head over to <https://cla.developers.google.com/> to see
your current agreements on file or to sign a new one.
The following dependencies must be installed on the development system:

You generally only need to submit a CLA once, so if you've already submitted one
(even if it was for a different project), you probably don't need to do it
again.
- [Docker Engine][docker-engine]
- [Google Cloud SDK][google-cloud-sdk]
- [make]

## Code reviews
## Generating Documentation for Inputs and Outputs

All submissions, including submissions by project members, require review. We
use GitHub pull requests for this purpose. Consult
[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
information on using pull requests.
The Inputs and Outputs tables in the READMEs of the root module,
submodules, and example modules are automatically generated based on
the `variables` and `outputs` of the respective modules. These tables
must be refreshed if the module interfaces are changed.

## Integration Testing

Integration tests are used to verify the behaviour of the root module,
submodules, and example modules. Additions, changes, and fixes should
be accompanied with tests.

The integration tests are run using Golang and
are packaged within a Docker image for convenience.

The general strategy for these tests is to verify the behaviour of the
[example modules](./examples/), thus ensuring that the root module,
submodules, and example modules are all functionally correct.

### Test Environment

The easiest way to test the module is in an isolated test project. The setup for such a project is defined in [test/setup](./test/setup/) directory.

To use this setup, you need a service account with Project Creator access on a folder. Export the Service Account credentials to your environment like so:

```
export SERVICE_ACCOUNT_JSON=$(< credentials.json)
```

You will also need to set a few environment variables:
```
export TF_VAR_org_id="your_org_id"
export TF_VAR_folder_id="your_folder_id"
export TF_VAR_billing_account="your_billing_account_id"
```

With these settings in place, you can prepare a test project using Docker:
```
make docker_test_prepare
```

### Noninteractive Execution

Run `make docker_test_integration` to test all of the example modules
noninteractively, using the prepared test project.

### Interactive Execution

Review the [framework documentation](https://pkg.go.dev/github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test#readme-4-test-execution) to learn how to execute tests.

## Linting and Formatting

Many of the files in the repository can be linted or formatted to
maintain a standard of quality.

### Execution

Run `make docker_test_lint`.

[docker-engine]: https://www.docker.com/products/docker-engine
[flake8]: http://flake8.pycqa.org/en/latest/
[gofmt]: https://golang.org/cmd/gofmt/
[google-cloud-sdk]: https://cloud.google.com/sdk/install
[hadolint]: https://github.com/hadolint/hadolint
[make]: https://en.wikipedia.org/wiki/Make_(software)
[shellcheck]: https://www.shellcheck.net/
[terraform-docs]: https://github.com/segmentio/terraform-docs
[terraform]: https://terraform.io/

## Community Guidelines

Expand Down
70 changes: 70 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Make will use bash instead of sh
SHELL := /usr/bin/env bash

DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 1.0
DOCKER_IMAGE_DEVELOPER_TOOLS := cft/developer-tools
REGISTRY_URL := gcr.io/cloud-foundation-cicd

# Enter docker container for local development
.PHONY: docker_run
docker_run:
docker run --rm -it \
-e SERVICE_ACCOUNT_JSON \
-v $(CURDIR):/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/bin/bash

# Execute prepare tests within the docker container
.PHONY: docker_test_prepare
docker_test_prepare:
docker run --rm -it \
-e SERVICE_ACCOUNT_JSON \
-e TF_VAR_org_id \
-e TF_VAR_folder_id \
-e TF_VAR_billing_account \
-v $(CURDIR):/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/usr/local/bin/execute_with_credentials.sh prepare_environment

# Clean up test environment within the docker container
.PHONY: docker_test_cleanup
docker_test_cleanup:
docker run --rm -it \
-e SERVICE_ACCOUNT_JSON \
-e TF_VAR_org_id \
-e TF_VAR_folder_id \
-e TF_VAR_billing_account \
-v $(CURDIR):/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/usr/local/bin/execute_with_credentials.sh cleanup_environment

# Execute integration tests within the docker container
.PHONY: docker_test_integration
docker_test_integration:
docker run --rm -it \
-e SERVICE_ACCOUNT_JSON \
-v $(CURDIR):/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/bin/bash -c 'source /usr/local/bin/task_helper_functions.sh && init_credentials && cd test/integration && go test -v -timeout 0 -p 1'

# Execute lint tests within the docker container
.PHONY: docker_test_lint
docker_test_lint:
docker run --rm -it \
-v $(CURDIR):/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/usr/local/bin/test_lint.sh
13 changes: 5 additions & 8 deletions jenkins-gke/tf-gke/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*****************************************/
module "enables-google-apis" {
source = "terraform-google-modules/project-factory/google//modules/project_services"
version = "6.0.0"
version = "11.0.0"

project_id = var.project_id

Expand All @@ -41,7 +41,7 @@ module "enables-google-apis" {
*****************************************/
module "jenkins-vpc" {
source = "terraform-google-modules/network/google"
version = "~> 2.0"
version = "~> 3.0"

project_id = module.enables-google-apis.project_id
network_name = var.network_name
Expand Down Expand Up @@ -73,7 +73,7 @@ module "jenkins-vpc" {
*****************************************/
module "jenkins-gke" {
source = "terraform-google-modules/kubernetes-engine/google//modules/beta-public-cluster/"
version = "~> 7.0"
version = "~> 15.0"
project_id = module.enables-google-apis.project_id
name = "jenkins"
regional = false
Expand Down Expand Up @@ -115,7 +115,7 @@ resource "google_project_iam_member" "gke" {
*****************************************/
module "workload_identity" {
source = "terraform-google-modules/kubernetes-engine/google//modules/workload-identity"
version = "~> 7.0"
version = "~> 15.0"
project_id = module.enables-google-apis.project_id
name = "jenkins-wi-${module.jenkins-gke.name}"
namespace = "default"
Expand All @@ -129,9 +129,6 @@ resource "google_project_iam_member" "cluster-dev" {
member = module.workload_identity.gcp_service_account_fqn
}

data "google_client_config" "default" {
}

/*****************************************
K8S secrets for configuring K8S executers
*****************************************/
Expand Down Expand Up @@ -188,7 +185,7 @@ data "local_file" "helm_chart_values" {

resource "helm_release" "jenkins" {
name = "jenkins"
repository = "https://kubernetes-charts.storage.googleapis.com"
repository = "https://charts.helm.sh/stable"
chart = "jenkins"
version = "1.9.18"
timeout = 1200
Expand Down
7 changes: 1 addition & 6 deletions jenkins-gke/tf-gke/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ output "kubernetes_endpoint" {
value = module.jenkins-gke.endpoint
}

output "client_token" {
description = "The bearer token for auth"
sensitive = true
value = base64encode(data.google_client_config.default.access_token)
}

output "ca_certificate" {
description = "The cluster ca certificate (base64 encoded)"
sensitive = true
value = module.jenkins-gke.ca_certificate
}

Expand Down
49 changes: 23 additions & 26 deletions jenkins-gke/tf-gke/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,41 @@
*/

/*****************************************
Google Provider Configuration
Kubernetes provider configuration
*****************************************/
provider "google" {
version = "~> 3.1"
}

provider "google-beta" {
version = "~> 3.1"
data "google_client_config" "default" {
}

/*****************************************
Kubernetes provider configuration
*****************************************/
provider "kubernetes" {
version = "~> 1.10"
load_config_file = false
host = module.jenkins-gke.endpoint
host = "https://${module.jenkins-gke.endpoint}"
token = data.google_client_config.default.access_token
cluster_ca_certificate = base64decode(module.jenkins-gke.ca_certificate)
}

/*****************************************
Helm provider configuration
*****************************************/
module "gke_auth" {
source = "terraform-google-modules/kubernetes-engine/google//modules/auth"
version = "~> 9.1"

project_id = module.enables-google-apis.project_id
cluster_name = module.jenkins-gke.name
location = module.jenkins-gke.location
}

provider "helm" {
kubernetes {
load_config_file = false
cluster_ca_certificate = module.gke_auth.cluster_ca_certificate
host = module.gke_auth.host
token = module.gke_auth.token
cluster_ca_certificate = base64decode(module.jenkins-gke.ca_certificate)
host = "https://${module.jenkins-gke.endpoint}"
token = data.google_client_config.default.access_token
}
}

terraform {
required_providers {
google = {
source = "hashicorp/google"
version = ">= 3.39.0, <4.0.0"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "~> 2.0"
}
helm = {
source = "hashicorp/helm"
version = "~> 2.0"
}
}
}
Loading

0 comments on commit 96dc183

Please sign in to comment.