Skip to content

Commit 7c8aaeb

Browse files
committed
Initial commit
1 parent cb37516 commit 7c8aaeb

File tree

503 files changed

+316052
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

503 files changed

+316052
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cloudsql-proxy-inject-linux-amd64
2+
cloudsql-proxy-inject-darwin-amd64

Gopkg.lock

Lines changed: 206 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
#
22+
# [prune]
23+
# non-go = false
24+
# go-tests = true
25+
# unused-packages = true
26+
27+
28+
[[constraint]]
29+
name = "github.com/go-yaml/yaml"
30+
version = "2.2.2"
31+
32+
[[constraint]]
33+
name = "gopkg.in/alecthomas/kingpin.v2"
34+
version = "2.2.6"
35+
36+
[[constraint]]
37+
branch = "master"
38+
name = "k8s.io/api"
39+
40+
[prune]
41+
go-tests = true
42+
unused-packages = true

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
BINARY=cloudsql-proxy-inject
2+
3+
.PHONY: build-linux build-darwin
4+
5+
build-linux:
6+
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ${BINARY}-linux-amd64 -a -ldflags '-s' -installsuffix cgo main.go
7+
8+
build-darwin:
9+
@CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o ${BINARY}-darwin-amd64 -a -ldflags '-s' -installsuffix cgo main.go

main.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
7+
"github.com/go-yaml/yaml"
8+
"gopkg.in/alecthomas/kingpin.v2"
9+
"k8s.io/api/apps/v1beta1"
10+
v1 "k8s.io/api/core/v1"
11+
"k8s.io/apimachinery/pkg/api/resource"
12+
k8sjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
13+
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
14+
)
15+
16+
var (
17+
path = kingpin.Arg("path", "Deployment file path").Required().String()
18+
)
19+
20+
func main() {
21+
kingpin.Parse()
22+
23+
var cloudSQLProxyContainer v1.Container
24+
{
25+
limitCPUQt := resource.NewMilliQuantity(100, resource.BinarySI)
26+
limitMemoryQt := resource.NewQuantity(128*1024e3, resource.BinarySI)
27+
limits := v1.ResourceList{
28+
v1.ResourceCPU: *limitCPUQt,
29+
v1.ResourceMemory: *limitMemoryQt,
30+
}
31+
requestCPUQt := resource.NewMilliQuantity(5, resource.BinarySI)
32+
requestMemoryQt := resource.NewMilliQuantity(8, resource.BinarySI)
33+
requests := v1.ResourceList{
34+
v1.ResourceCPU: *requestCPUQt,
35+
v1.ResourceMemory: *requestMemoryQt,
36+
}
37+
38+
var runAsUser int64 = 2
39+
var allowPrivilegeEscalation = false
40+
41+
securityContext := v1.SecurityContext{
42+
RunAsUser: &runAsUser,
43+
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
44+
}
45+
46+
volumeMount := v1.VolumeMount{
47+
Name: "cloudsql-proxy-credentials",
48+
MountPath: "/secrets/cloudsql",
49+
ReadOnly: true,
50+
}
51+
52+
cloudSQLProxyContainer = v1.Container{}
53+
cloudSQLProxyContainer.Name = "cloudsql-proxy"
54+
cloudSQLProxyContainer.Image = "gcr.io/cloudsql-docker/gce-proxy:1.11"
55+
cloudSQLProxyContainer.Command = []string{"/cloud_sql_proxy", "-instances=ricardo-dev-ch:europe-west1:ricardo-dev-postgres=tcp:5432", "-credential_file=/secrets/cloudsql/credentials.json"}
56+
cloudSQLProxyContainer.Resources = v1.ResourceRequirements{Limits: limits, Requests: requests}
57+
cloudSQLProxyContainer.SecurityContext = &securityContext
58+
cloudSQLProxyContainer.VolumeMounts = []v1.VolumeMount{volumeMount}
59+
}
60+
61+
b, err := yaml.Marshal(&cloudSQLProxyContainer)
62+
if err != nil {
63+
panic(err)
64+
}
65+
f, err := os.Open(*path)
66+
if err != nil {
67+
panic(err)
68+
}
69+
defer f.Close()
70+
71+
b, _ = json.Marshal(&cloudSQLProxyContainer)
72+
73+
deploy := &v1beta1.Deployment{}
74+
err = json.Unmarshal(b, &deploy)
75+
if err != nil {
76+
panic(err)
77+
}
78+
k8syaml.NewYAMLOrJSONDecoder(f, 4096).Decode(&deploy)
79+
80+
deploy.Spec.Template.Spec.Volumes = []v1.Volume{v1.Volume{
81+
Name: "cloudsql-proxy-credentials",
82+
VolumeSource: v1.VolumeSource{
83+
Secret: &v1.SecretVolumeSource{
84+
SecretName: "cloudsql-proxy-credentials",
85+
},
86+
},
87+
}}
88+
deploy.Spec.Template.Spec.Containers = append(deploy.Spec.Template.Spec.Containers, cloudSQLProxyContainer)
89+
90+
serializer := k8sjson.NewYAMLSerializer(k8sjson.DefaultMetaFactory, nil, nil)
91+
serializer.Encode(deploy, os.Stdout)
92+
}

vendor/github.com/alecthomas/template/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)