Skip to content

Commit

Permalink
[WIP] implement terraform runner
Browse files Browse the repository at this point in the history
  • Loading branch information
chanwit committed Jan 30, 2022
1 parent 69f77d5 commit b605ee2
Show file tree
Hide file tree
Showing 23 changed files with 5,441 additions and 359 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ COPY go.sum go.sum
RUN go mod download

# Copy the go source
COPY main.go main.go
COPY cmd/manager/main.go cmd/manager/main.go
COPY api/ api/
COPY controllers/ controllers/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o tf-controller main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o tf-controller cmd/controller/main.go

ADD https://releases.hashicorp.com/terraform/1.1.4/terraform_1.1.4_linux_amd64.zip /terraform_1.1.4_linux_amd64.zip
RUN unzip -q /terraform_1.1.4_linux_amd64.zip
Expand Down
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,18 @@ download-crd-deps:
test: manifests generate download-crd-deps fmt vet envtest api-docs ## Run tests.
DISABLE_K8S_LOGS=1 DISABLE_TF_LOGS=1 DISABLE_TF_K8S_BACKEND=1 KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./controllers -coverprofile cover.out -v

gen-grpc:
protoc --go_out=. --go_opt=Mrunner/runner.proto=runner/ --go-grpc_out=. --go-grpc_opt=Mrunner/runner.proto=runner/ runner/runner.proto
##@ Build

.PHONY: build
build: generate fmt vet ## Build manager binary.
go build -o bin/manager main.go
build: gen-grpc generate fmt vet ## Build manager binary.
go build -o bin/runner cmd/runner/main.go
go build -o bin/manager cmd/manager/main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go
go run cmd/manager/main.go

.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
Expand Down
23 changes: 23 additions & 0 deletions main.go → cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package main

import (
"github.com/chanwit/tf-controller/runner"
"google.golang.org/grpc"
"net"
"os"
"time"

Expand Down Expand Up @@ -147,6 +150,26 @@ func main() {
os.Exit(1)
}

if os.Getenv("INSECURE_LOCAL_RUNNER") == "1" {
listener, err := net.Listen("tcp", "localhost:30000")
if err != nil {
panic(err.Error())
}

server := grpc.NewServer()
// local runner, use the same client as the manager
runner.RegisterRunnerServer(server, &runner.TerraformRunnerServer{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
})

go func() {
if err := server.Serve(listener); err != nil {
panic(err.Error())
}
}()
}

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
Expand Down
26 changes: 26 additions & 0 deletions cmd/runner/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2021.
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
http://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.
*/

package main

import (
"fmt"
_ "github.com/chanwit/tf-controller/runner"
)

func main() {
fmt.Println("hello world")
}
40 changes: 40 additions & 0 deletions controllers/object_encode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package controllers

import (
infrav1 "github.com/chanwit/tf-controller/api/v1alpha1"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)

func TestObjectEncode(t *testing.T) {
g := NewGomegaWithT(t)

helloWorldTF := infrav1.Terraform{
TypeMeta: metav1.TypeMeta{
APIVersion: infrav1.GroupVersion.Group + "/" + infrav1.GroupVersion.Version,
Kind: infrav1.TerraformKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: "tf-1",
Namespace: "flux-system",
},
Spec: infrav1.TerraformSpec{
ApprovePlan: "auto",
Path: "./terraform-hello-world-example",
SourceRef: infrav1.CrossNamespaceSourceReference{
Kind: "GitRepository",
Name: "flux-system",
Namespace: "flux-system",
},
},
}

b, err := reconciler.ToBytes(helloWorldTF)
g.Expect(err).To(BeNil())
tf, err := runnerServer.ToTerraform(b)
g.Expect(err).To(BeNil())
g.Expect(tf).To(Equal(helloWorldTF))
g.Expect(tf.Spec.ApprovePlan).To(Equal("auto"))
g.Expect(tf.Spec.Path).To(Equal("./terraform-hello-world-example"))
}
52 changes: 40 additions & 12 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ package controllers
import (
"context"
"fmt"
"github.com/chanwit/tf-controller/runner"
"google.golang.org/grpc"
"io"
"io/ioutil"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"math/rand"
"net"
"net/http"
"os"
"path/filepath"
Expand All @@ -36,7 +41,6 @@ import (

infrav1 "github.com/chanwit/tf-controller/api/v1alpha1"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -58,11 +62,12 @@ const (
)

var (
cfg *rest.Config
k8sClient client.Client
testEnv *envtest.Environment
server *ghttp.Server
reconciler *TerraformReconciler
cfg *rest.Config
k8sClient client.Client
testEnv *envtest.Environment
server *ghttp.Server
reconciler *TerraformReconciler
runnerServer *runner.TerraformRunnerServer
)

var (
Expand Down Expand Up @@ -97,18 +102,25 @@ func TestMain(m *testing.M) {
panic("cfg cannot be nil")
}

err = sourcev1.AddToScheme(scheme.Scheme)
scheme := runtime.NewScheme()

err = clientgoscheme.AddToScheme(scheme)
if err != nil {
panic(err.Error())
}

err = sourcev1.AddToScheme(scheme)
if err != nil {
panic(err.Error())
}

err = infrav1.AddToScheme(scheme.Scheme)
err = infrav1.AddToScheme(scheme)
if err != nil {
panic(err.Error())
}

//+kubebuilder:scaffold:scheme
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
if err != nil {
panic(err.Error())
}
Expand Down Expand Up @@ -162,7 +174,7 @@ func TestMain(m *testing.M) {
}

k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
Scheme: scheme,
})
if err != nil {
panic(err.Error())
Expand All @@ -181,8 +193,24 @@ func TestMain(m *testing.M) {
}

go func() {
err = k8sManager.Start(ctx)
if err != nil {
if err := k8sManager.Start(ctx); err != nil {
panic(err.Error())
}
}()

listener, err := net.Listen("tcp", "localhost:30000")
if err != nil {
panic(err.Error())
}
s := grpc.NewServer()
runnerServer = &runner.TerraformRunnerServer{
Client: k8sManager.GetClient(),
Scheme: scheme,
}
runner.RegisterRunnerServer(s, runnerServer)

go func() {
if err := s.Serve(listener); err != nil {
panic(err.Error())
}
}()
Expand Down
2 changes: 1 addition & 1 deletion controllers/tc000011_bad_tar_gz_no_outputs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func Test_000011_bad_tar_gz_no_outputs_test(t *testing.T) {
}, timeout, interval).Should(Equal(map[string]interface{}{
"Type": "Ready",
"Reason": "ArtifactFailed",
"Message": "failed to untar artifact, error: requires gzip-compressed body: gzip: invalid header",
"Message": "rpc error: code = Unknown desc = failed to untar artifact, error: requires gzip-compressed body: gzip: invalid header",
"Status": metav1.ConditionFalse,
}))
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func Test_000072_varsfrom_optional_secret_and_controlled_outputs_test(t *testing
return helloWorldTF.Status
}, timeout, interval).Should(Equal(map[string]interface{}{
"Reason": "VarsGenerationFailed",
"Message": "Secret \"my-vars-helloworld-vars-from-optional-secret-controlled-output\" not found",
"Message": "rpc error: code = Unknown desc = Secret \"my-vars-helloworld-vars-from-optional-secret-controlled-output\" not found",
"Status": metav1.ConditionFalse,
"Type": "Ready",
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func Test_000074_varsfrom_accepts_many_secrets_with_last_supplied_key_precedence
},
}

_, err = reconciler.generateVarsForTF(ctx, terraform, tfExec, "main")
_, err = reconciler.generateVarsForTF(ctx, terraform, tfExec.WorkingDir(), "main")
g.Expect(err).Should(BeNil())

By("verifying the generated vars file matches the expected result")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func Test_000082_varsfrom_accepts_many_configmaps_with_last_supplied_precedence(
},
}

_, err = reconciler.generateVarsForTF(ctx, terraform, tfExec, "main")
_, err = reconciler.generateVarsForTF(ctx, terraform, tfExec.WorkingDir(), "main")
g.Expect(err).Should(BeNil())

By("verifying the generated vars file matches the expected result")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func Test_000140_auto_applied_resource_should_transit_to_plan_then_apply_when_dr
return map[string]interface{}{
"SavedPlan": tfplanSecret.Labels["savedPlan"],
"Is TFPlan empty ?": string(tfplanSecret.Data["tfplan"]) == "",
"HasEncodingAnnotation": tfplanSecret.Annotations["encoding"] != "" && tfplanSecret.Annotations["encoding"] == "gzip",
"HasEncodingAnnotation": tfplanSecret.Annotations["encoding"] == "gzip",
}
}, timeout, interval).Should(Equal(map[string]interface{}{
"SavedPlan": "plan-master-b8e362c206e3d0cbb7ed22ced771a0056455a2fb",
Expand Down
5 changes: 3 additions & 2 deletions controllers/tc000210_plan_encode_decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"bytes"
"compress/gzip"
"github.com/chanwit/tf-controller/utils"
"io/ioutil"
"testing"

Expand All @@ -26,7 +27,7 @@ func Test_000210_plan_encode_decode_test(t *testing.T) {

for _, tt := range encodeTests {
It("should encode the terraform plan")
r, err := reconciler.gzipEncode(tt.tfplan)
r, err := utils.GzipEncode(tt.tfplan)
g.Expect(err).ShouldNot(HaveOccurred())

var buf bytes.Buffer
Expand All @@ -46,7 +47,7 @@ func Test_000210_plan_encode_decode_test(t *testing.T) {

for _, tt := range decodeTests {
It("should decode the encoded terraform plan")
r, err := reconciler.gzipDecode(tt.encodedPlan)
r, err := utils.GzipDecode(tt.encodedPlan)
g.Expect(err).ShouldNot(HaveOccurred())

re := bytes.NewReader(tt.encodedPlan)
Expand Down
Loading

0 comments on commit b605ee2

Please sign in to comment.