Skip to content

Commit

Permalink
fix dockerfile for churro-operator to work with embedded templates in…
Browse files Browse the repository at this point in the history
… multi-stage build (#9)

* change go.mod to use golang 1.16 and relocate templates so they can be embedded into churro-operator, remove template volume from churro-operator and instead look for it dynamically and if not found use the embedded templates

* fix Dockerfile for multi-stage build of churro-operator to be able to access embedded templates dir
  • Loading branch information
churromechanic authored Sep 28, 2021
1 parent 4fe2574 commit 869af23
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ build-sftp-image:

compile-operator:
go build -o build/churro-operator cmd/churro-operator/churro-operator.go
build-operator-image-local:
docker build -f ./images/Dockerfile.churro-operator -t docker.io/churrodata/churro-operator:latest .
build-operator-image-local: compile-operator
docker build -f ./images/Dockerfile.churro-operator.local -t docker.io/churrodata/churro-operator:latest .
build-operator-image:
docker buildx build --load --platform $(PLATFORMS) -f ./images/Dockerfile.churro-operator -t docker.io/churrodata/churro-operator .

Expand Down
35 changes: 23 additions & 12 deletions cmd/churro-operator/churro-operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,36 +321,47 @@ func getTemplate(templateKey string) (templateBytes []byte) {

ns := os.Getenv("CHURRO_NAMESPACE")


// get the template from the embedded dir as a default
data, err := embeddedTemplates.ReadFile(deployTemplatesDir + templateKey)
if err != nil {
fmt.Printf("error here %s\n", err.Error())
os.Exit(2)
}
if len(data) == 0 {
fmt.Printf("error here 2 %s\n", err.Error())
os.Exit(2)
}

// connect to the Kube API
clientset, _, err := pkg.GetKubeClient()
if err != nil {
fmt.Printf("error here 3 %s\n", err.Error())

os.Exit(2)
}

fmt.Printf("looking for template key [%s]\n", templateKey)
var thisMap *v1.ConfigMap
thisMap, err = getConfigMap(clientset, ns, configMapName)
if kerrors.IsNotFound(err) {
fmt.Printf("configMap not found\n")
data, err := embeddedTemplates.ReadFile(deployTemplatesDir + templateKey)
if err != nil {
os.Exit(2)
}
if len(data) == 0 {
os.Exit(2)
}
fmt.Printf("found embedded template %s\n", templateKey)

fmt.Printf("configMap not found, will use embedded %s\n", templateKey)
return data
} else if err != nil {
fmt.Print(err.Error())
fmt.Printf("error here 4 %s\n", err.Error())

os.Exit(2)
}
fmt.Printf("configmap found\n")
str := thisMap.Data[templateKey]
templateBytes = []byte(str)
if len(templateBytes) == 0 {
fmt.Printf("invalid template %s from ConfigMap length is 0", templateKey)
os.Exit(2)

fmt.Printf("invalid template %s from ConfigMap length is 0, will use embedded version instead", templateKey)
return data


}
fmt.Printf("found templateKey in ConfigMap! %s\n", templateBytes)
return templateBytes
Expand Down
2 changes: 2 additions & 0 deletions images/Dockerfile.churro-operator
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
FROM golang:1.16 as builder
ADD . /build
ADD ./cmd/churro-operator/churro-operator.go /build
ADD ./cmd/churro-operator/deploy/templates/* /build/deploy/templates/
RUN ls /build/deploy/templates
WORKDIR /build
RUN CGO_ENABLED=0 GOOS=linux go build -o churro-operator

Expand Down
7 changes: 7 additions & 0 deletions images/Dockerfile.churro-operator.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM fedora:latest

# Copy our static executable
COPY build/churro-operator /usr/local/bin

# Run the churro-operator binary.
ENTRYPOINT ["/usr/local/bin/churro-operator"]

0 comments on commit 869af23

Please sign in to comment.