Skip to content

Commit

Permalink
refactor code for Go lint & enhance docs (#12)
Browse files Browse the repository at this point in the history
* refactor code with the introduction of go linting

* lint step in PR workflow

* docs update

Co-authored-by: ibraheem Al Saady <ibraheemalsaady@ibraheems-MacBook-Pro-2.local>
  • Loading branch information
IbraheemAlSaady and ibraheem Al Saady authored May 30, 2022
1 parent c9fb3f2 commit 28da89f
Show file tree
Hide file tree
Showing 27 changed files with 221 additions and 191 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
go tool cover -func coverage.txt
rm coverage.tmp.txt
- name: Docker Build
- name: Docker Build Test
run: docker build -t controller:latest .

- name: Codecov Push
Expand Down
12 changes: 8 additions & 4 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ on:
branches:
- main
- master
- 'releases/**'
- 'feature/**'
- 'fix/**'

jobs:
build:
Expand All @@ -24,5 +21,12 @@ jobs:
- name: Build
run: make build

## Ref: https://github.com/golangci/golangci-lint-action
- name: golangci-lint
uses: golangci/golangci-lint-action@v3

- name: Test
run: make docker-build
run: make docker-build

- name: Docker Build Test
run: docker build -t controller:latest .
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ Chart can be found [here](https://github.com/kube-champ/helm-charts/tree/master/
## Docuemntation
Check the Terraform Operator [docs](https://kube-champ.github.io/terraform-operator/) for more details and examples

## Features
- [x] Point to any Terraform module (including Git)
- [x] Private Git repos authentication
- [x] Define Terraform variables and variable files
- [x] Target specific Terraform workspace
- [x] Custom backend & providers configuration
- [x] Terraform module outputs written to a Kubernetes Secret
- [x] Dependency on other workflows
- [x] Terraform variables from the output of a dependency workflow
- [x] Specify retry limits

## Usage
For more examples on how to use this CRD, check the [samples](https://kube-champ.github.io/terraform-operator/examples/)

Expand Down Expand Up @@ -84,6 +95,12 @@ spec:
variables:
- key: length
value: "16"

- key: something
## only works if the dependency is in the same namespace
dependencyRef:
name: my-dependency-name
key: the output secret key

- key: AWS_ACCESS_KEY
valueFrom:
Expand All @@ -101,6 +118,8 @@ spec:
## can also be 'secret'
configMap:
name: "terraform-env-config"
# secret:
# secretName: mysecret

dependsOn:
- name: run-base
Expand All @@ -110,7 +129,10 @@ spec:
## ssh key from a secret to allow pull modules from private git repos
gitSSHKey:
valueFrom:
....
secret:
## secret key must be id_rsa
secretName: git-ssh-key
defaultMode: 0600

## outputs defined will be stored in a Kubernetes secret
outputs:
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

// Kubernetes Controller information
var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "run.terraform-operator.io", Version: "v1alpha1"}
Expand Down
17 changes: 10 additions & 7 deletions api/v1alpha1/helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package v1alpha1

import (
"crypto/rand"
"fmt"
"math/rand"
"math/big"
"strings"
)

Expand All @@ -28,22 +29,24 @@ func removeString(slice []string, s string) (result []string) {
}

// generates a random alphanumeric based on the length provided
func random(n int) string {
func random(n int64) string {
var letters = []rune("123456790abcdefghijklmnopqrstuvwxyz")

b := make([]rune, n)

for i := range b {
b[i] = letters[rand.Intn(len(letters))]
generated, _ := rand.Int(rand.Reader, big.NewInt(n))

b[i] = letters[generated.Int64()]
}
return string(b)
}

// returns common labels to be attached to children resources
func getCommonLabels(name string, runId string) map[string]string {
func getCommonLabels(name string, runID string) map[string]string {
return map[string]string{
"terraformRunName": name,
"terraformRunId": runId,
"terraformRunId": runID,
"component": "Terraform-run",
"owner": "run.terraform-operator.io",
}
Expand All @@ -61,8 +64,8 @@ func truncateResourceName(s string, i int) string {
}

// creates a name for the terraform Run job
func getUniqueResourceName(name string, runId string) string {
func getUniqueResourceName(name string, runID string) string {
// return fmt.Sprintf("tf-apply-%s-%s", name, runId)

return fmt.Sprintf("%s-%s", truncateResourceName(name, 220), runId)
return fmt.Sprintf("%s-%s", truncateResourceName(name, 220), runID)
}
14 changes: 7 additions & 7 deletions api/v1alpha1/k8s_configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
"k8s.io/apimachinery/pkg/types"
)

// creates a k8s ConifgMap for the terraform module string
// This configmap will be mounted in the pod so it can run the Terraform

func getConfigMapSpecForModule(name string, namespace string, module string, runId string, owner metav1.OwnerReference) *corev1.ConfigMap {
// getConfigMapSpecForModule returns a Kubernetes ConifgMap spec for the terraform module
// This configmap will be mounted in the Terraform Runner pod
func getConfigMapSpecForModule(name string, namespace string, module string, runID string, owner metav1.OwnerReference) *corev1.ConfigMap {
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: getUniqueResourceName(name, runId),
Name: getUniqueResourceName(name, runID),
Namespace: namespace,
Labels: getCommonLabels(name, runId),
Labels: getCommonLabels(name, runID),
OwnerReferences: []metav1.OwnerReference{
owner,
},
Expand All @@ -30,6 +29,7 @@ func getConfigMapSpecForModule(name string, namespace string, module string, run
return cm
}

// createConfigMapForModule creates the ConfigMap for the Terraform workflow/run
func createConfigMapForModule(namespacedName types.NamespacedName, run *Terraform) (*corev1.ConfigMap, error) {
configMaps := kube.ClientSet.CoreV1().ConfigMaps(namespacedName.Namespace)

Expand All @@ -42,7 +42,7 @@ func createConfigMapForModule(namespacedName types.NamespacedName, run *Terrafor
configMap := getConfigMapSpecForModule(
namespacedName.Name,
namespacedName.Namespace,
string(tpl), run.Status.RunId,
string(tpl), run.Status.RunID,
run.GetOwnerReference())

if _, err := configMaps.Create(context.TODO(), configMap, metav1.CreateOptions{}); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/k8s_configmaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var _ = Describe("Kubernetes ConfigMaps", func() {
DeleteCompletedJobs: false,
},
Status: TerraformStatus{
RunId: "1234",
RunID: "1234",
},
}

Expand Down
12 changes: 7 additions & 5 deletions api/v1alpha1/k8s_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
corev1 "k8s.io/api/core/v1"
)

// returns a volume spec
// getVolumeSpec returns a volume spec
func getVolumeSpec(name string, source corev1.VolumeSource) corev1.Volume {
return corev1.Volume{
Name: name,
VolumeSource: source,
}
}

// returns a volume spec from configMap
// getVolumeSpecFromConfigMap returns a volume spec from configMap
func getVolumeSpecFromConfigMap(volumeName string, configMapName string) corev1.Volume {
return corev1.Volume{
Name: volumeName,
Expand All @@ -26,7 +26,7 @@ func getVolumeSpecFromConfigMap(volumeName string, configMapName string) corev1.
}
}

// returns and emptyDir volume spec
// getEmptyDirVolume returns and emptyDir volume spec
func getEmptyDirVolume(name string) corev1.Volume {
return corev1.Volume{
Name: name,
Expand All @@ -36,7 +36,7 @@ func getEmptyDirVolume(name string) corev1.Volume {
}
}

// returns a volume mount spec
// getVolumeMountSpec returns a volume mount spec
func getVolumeMountSpec(volumeName string, mountPath string, readOnly bool) corev1.VolumeMount {
return corev1.VolumeMount{
Name: volumeName,
Expand All @@ -45,7 +45,7 @@ func getVolumeMountSpec(volumeName string, mountPath string, readOnly bool) core
}
}

// returns a volume mount spec with subpath option
// getVolumeMountSpecWithSubPath returns a volume mount spec with subpath option
func getVolumeMountSpecWithSubPath(volumeName string, mountPath string, subPath string, readOnly bool) corev1.VolumeMount {
return corev1.VolumeMount{
Name: volumeName,
Expand All @@ -55,13 +55,15 @@ func getVolumeMountSpecWithSubPath(volumeName string, mountPath string, subPath
}
}

// getEnvVariable returns a Kubernetes environment variable spec
func getEnvVariable(name string, value string) corev1.EnvVar {
return corev1.EnvVar{
Name: name,
Value: value,
}
}

// getEnvVariableFromFieldSelector returns a Kubernetes environment variable from a field selector
func getEnvVariableFromFieldSelector(name string, path string) corev1.EnvVar {
return corev1.EnvVar{
Name: name,
Expand Down
Loading

0 comments on commit 28da89f

Please sign in to comment.