Skip to content

Instantly share code, notes, and snippets.

@timatooth timatooth/main.go
Created Feb 9, 2018

Embed
What would you like to do?
watch, build, helm deploy
package main
import (
"archive/tar"
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"path/filepath"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/radovskyb/watcher"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/helm"
"k8s.io/helm/pkg/proto/hapi/chart"
)
func createDeployment(chartPath string, imageID string) {
// for testing: kubectl -n kube-system port-forward tiller-deploy-7777bff5d-7j5x4 44134
hc := helm.NewClient(helm.Host("127.0.0.1:44134"))
fmt.Println("Loading chart...")
achart, err := chartutil.Load(chartPath)
if err != nil {
log.Fatalln("Could not load chart", err)
} else {
fmt.Println("installing chart")
// chart.Load() reads in the raw values YAML but does not pass them to the chart it seems.
//is this the right way. hmm...
// thevalues, err := chartutil.ReadValuesFile("testchart/values.yaml")
// if err != nil {
// fmt.Println("Could not read values yaml")
// }
//should this be uninitialized?git@github.com:kubernetes/helm.git
// I have no idea what im doing.jpg
achart.Values.Values = make(map[string]*chart.Value)
achart.Values.Values["imageID"] = &chart.Value{Value: imageID}
response, err := hc.InstallReleaseFromChart(achart, "default")
if err != nil {
log.Fatalln("Failed to Install chart", err)
} else {
fmt.Println(response)
}
}
}
func buildSampleImage(contextPath string) {
cli, err := client.NewEnvClient()
if err != nil {
panic(err)
}
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
panic(err)
}
for _, container := range containers {
fmt.Printf("OMG CONTAINER: %s %s\n", container.ID[:10], container.Image)
}
//create a go ctx to watch for build progress
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
defer tw.Close()
dockerFile := "Dockerfile"
rootDirectory := contextPath
walkDirFn := func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
newPath := path[len(rootDirectory)+1:]
if len(newPath) == 0 {
return nil
}
fmt.Println(newPath)
//read all files, add em' to the tar
aFile, err := os.Open(path)
if err != nil {
log.Fatal(err, " :unable to open "+path)
}
defer aFile.Close()
h, err := tar.FileInfoHeader(info, newPath)
if err != nil {
fmt.Println("Couldnt create tar header ")
} else {
h.Name = newPath
err = tw.WriteHeader(h)
if err != nil {
fmt.Println("Error writing tar header")
}
}
length, err := io.Copy(tw, aFile)
if err != nil {
fmt.Println("Error coping file contents to tar")
} else {
fmt.Printf("Wrote tar contents of %s %d bytes\n", newPath, length)
}
return nil
}
filepath.Walk("samples", walkDirFn)
dockerFileTarReader := bytes.NewReader(buf.Bytes())
ctx := context.Background()
imageBuildResponse, err := cli.ImageBuild(
ctx,
dockerFileTarReader,
types.ImageBuildOptions{
Tags: []string{"albi/yolo"},
Context: dockerFileTarReader,
Dockerfile: dockerFile,
Remove: true})
if err != nil {
log.Fatal(err, " :unable to build docker image")
}
defer imageBuildResponse.Body.Close()
_, err = io.Copy(os.Stdout, imageBuildResponse.Body)
if err != nil {
log.Fatal(err, " :unable to read image build response")
}
//cbf parsing the stream to get the id. Instead do image list
// then assume the newest is the one just built
listOpts := types.ImageListOptions{All: true}
imageList, _ := cli.ImageList(ctx, listOpts)
// hope that image build was successful and is the first result
imageID := imageList[0].ID
createDeployment("testchart", imageID)
}
func main() {
createDeployment("testchart", "hello-world")
contextPath := "samples"
w := watcher.New()
go func() {
for {
select {
case event := <-w.Event:
fmt.Println(event)
buildSampleImage(contextPath)
case err := <-w.Error:
log.Fatalln(err)
case <-w.Closed:
return
}
}
}()
if err := w.AddRecursive(contextPath); err != nil {
log.Fatalln(err)
}
fmt.Println("Watching files for changes:")
for path, f := range w.WatchedFiles() {
fmt.Printf("%s: %s\n", path, f.Name())
}
if err := w.Start(time.Millisecond * 100); err != nil {
log.Fatalln(err)
}
}
@shilpakarthik

This comment has been minimized.

Copy link

shilpakarthik commented Sep 26, 2018

Hi,
I have similar usecase of deploying helm charts using golang anf tried using the above sample.
Could you please provide in executing the program .. after build and running above its giving below error
./inst
Loading chart...
installing chart
2018/09/26 07:30:10 Failed to Install chart context deadline exceeded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.