Skip to content

Commit

Permalink
save terraform outputs to secret
Browse files Browse the repository at this point in the history
  • Loading branch information
danisla committed Sep 15, 2018
1 parent 46ace1d commit 0c7773b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
10 changes: 5 additions & 5 deletions Gopkg.lock

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

35 changes: 35 additions & 0 deletions cmd/terraform-operator/statePodRunning.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

tftype "github.com/danisla/terraform-operator/pkg/types"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func statePodRunning(parentType ParentType, parent *tftype.Terraform, status *tftype.TerraformOperatorStatus, children *TerraformOperatorRequestChildren, desiredChildren *[]interface{}) (tftype.TerraformOperatorState, error) {
Expand Down Expand Up @@ -92,6 +93,16 @@ func statePodRunning(parentType ParentType, parent *tftype.Terraform, status *tf

myLog(parent, "INFO", fmt.Sprintf("Extracted %d output variables.", len(outputVars)))

// Create Secret with output var map
secretName := fmt.Sprintf("%s-tfapply-outputs", parent.GetName())
secret := makeOutputVarsSecret(secretName, parent.GetNamespace(), outputVars)

myLog(parent, "INFO", fmt.Sprintf("Created output var secret: %s", secret.GetName()))

status.TFOutputSecret = secret.GetName()

*desiredChildren = append(*desiredChildren, secret)

} else {
myLog(parent, "ERROR", fmt.Sprintf("terraform-plan annotation not found on successful pod completion: %s", pod.Name))
}
Expand Down Expand Up @@ -174,6 +185,30 @@ func makeOutputVars(data string) (map[string]tftype.TerraformOutputVar, error) {
return outputVars, err
}

func makeOutputVarsSecret(name string, namespace string, vars map[string]tftype.TerraformOutputVar) corev1.Secret {
var secret corev1.Secret

data := make(map[string]string, 0)

for k, v := range vars {
data[k] = v.Value
}

secret = corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
StringData: data,
}

return secret
}

func setFinalPodStatus(parent *tftype.Terraform, status *tftype.TerraformOperatorStatus, cStatus corev1.ContainerStatus, pod corev1.Pod) {
status.FinishedAt = cStatus.State.Terminated.FinishedAt.Format(time.RFC3339)

Expand Down
4 changes: 4 additions & 0 deletions cmd/terraform-operator/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func makeStatus(parent *tftype.Terraform, children *TerraformOperatorRequestChil
status.TFOutput = parent.Status.TFOutput
}

if parent.Status.TFOutputSecret != "" && changed == false {
status.TFOutputSecret = parent.Status.TFOutputSecret
}

if parent.Status.TFPlanDiff != nil && changed == false {
status.TFPlanDiff = parent.Status.TFPlanDiff
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/terraform-operator/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func sync(parentType ParentType, parent *tftype.Terraform, children *TerraformOp
desiredChildren = append(desiredChildren, o)
}

// Claim the Secrets.
for _, o := range children.Secrets {
desiredChildren = append(desiredChildren, o)
}

// Advance the state
if status.StateCurrent != nextState {
myLog(parent, "INFO", fmt.Sprintf("State %s -> %s", status.StateCurrent, nextState))
Expand Down
1 change: 1 addition & 0 deletions cmd/terraform-operator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type SyncResponse struct {
type TerraformOperatorRequestChildren struct {
Pods map[string]corev1.Pod `json:"Pod.v1"`
ConfigMaps map[string]corev1.ConfigMap `json:"ConfigMap.v1"`
Secrets map[string]corev1.Secret `json:"Secret.v1"`
}

// TerraformInputVars is a map of output var names from TerraformApply Objects.
Expand Down
1 change: 1 addition & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type TerraformOperatorStatus struct {
TFPlan string `json:"planFile"`
TFPlanDiff *TerraformPlanFileSummary `json"planDiff"`
TFOutput map[string]TerraformOutputVar `json:"outputs"`
TFOutputSecret string `json:"outputsSecret"`
RetryCount int `json:"retryCount"`
RetryNextAt string `json:"retryNextAt"`
Workspace string `json:"workspace"`
Expand Down

0 comments on commit 0c7773b

Please sign in to comment.