|
| 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 | +} |
0 commit comments