From cf3181a9724d737a366aa320ac502dfe8fdd0b5f Mon Sep 17 00:00:00 2001 From: Surya Kant Date: Sun, 10 Apr 2022 01:25:50 +0530 Subject: [PATCH 1/3] add cpu and memory constraints --- api/create.go | 13 +++++++++++-- config/config.go | 2 +- internal/postgres/postgres.go | 35 ++++++++++++++++++++++++++--------- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/api/create.go b/api/create.go index 639fed4..dc58afe 100644 --- a/api/create.go +++ b/api/create.go @@ -44,7 +44,7 @@ func CreateService(w http.ResponseWriter, req *http.Request) { } err = json.Unmarshal(byteArray, &s) - log.Printf("%d %d", s.Version.Maj, s.Version.Min) + log.Printf("%d %d %s %s", s.Version.Maj, s.Version.Min, s.Db.CPU, s.Db.Memory) if s.UserID == "" && apiKeyHeader != "" { s.UserID = "testuser" } @@ -82,7 +82,16 @@ func CreateService(w http.ResponseWriter, req *http.Request) { if err != nil { fmt.Printf("error creating client %v", err) } - postgresContainer, err := postgres.NewPostgresContainer(image, s.Db.Name, s.Db.Username, s.Db.Password, s.Db.Port) + postgresContainerProp := postgres.ContainerProps{ + Name: s.Db.Name, + Username: s.Db.Username, + Password: s.Db.Password, + Port: s.Db.Port, + Memory: s.Db.Memory, + CPUShares: s.Db.CPU, + Image: image, + } + postgresContainer, err := postgres.NewPostgresContainer(postgresContainerProp) if err != nil { log.Printf("ERROR: creating new docker service for %s %v", s.UserID, err) http.Error(w, "Error creating postgres docker service", 500) diff --git a/config/config.go b/config/config.go index 1f35656..0fae592 100644 --- a/config/config.go +++ b/config/config.go @@ -53,7 +53,7 @@ type dbCluster struct { Password string Memory string - Storage string + CPU string `json:"cpu"` Monitoring string } diff --git a/internal/postgres/postgres.go b/internal/postgres/postgres.go index b5d7b35..4d68a8d 100644 --- a/internal/postgres/postgres.go +++ b/internal/postgres/postgres.go @@ -20,7 +20,17 @@ const ( PGDATADIR = "/var/lib/postgresql/data/" ) -func NewPostgresContainer(image, name, postgresUsername, postgresPassword string, port int) (postgresContainer dockerservice.Container, err error) { +type ContainerProps struct { + Image string + Name string + Username string + Password string + Port int + Memory string + CPUShares string +} + +func NewPostgresContainer(props ContainerProps) (postgresContainer dockerservice.Container, err error) { dockerClient, err := dockerservice.NewDocker() if err != nil { fmt.Printf("error creating client %v", err) @@ -28,7 +38,7 @@ func NewPostgresContainer(image, name, postgresUsername, postgresPassword string newVolume, err := dockerservice.CreateVolume(context.Background(), dockerClient, volume.VolumeCreateBody{ Driver: "local", Labels: map[string]string{"purpose": "postgres data"}, - Name: name, + Name: props.Name, }) if err != nil { return dockerservice.Container{}, err @@ -41,7 +51,7 @@ func NewPostgresContainer(image, name, postgresUsername, postgresPassword string } } }() - networkResponse, err := dockerservice.CreateNetwork(context.Background(), dockerClient, name+"_default", types.NetworkCreate{CheckDuplicate: true}) + networkResponse, err := dockerservice.CreateNetwork(context.Background(), dockerClient, props.Name+"_default", types.NetworkCreate{CheckDuplicate: true}) if err != nil { return dockerservice.Container{}, err } @@ -53,9 +63,12 @@ func NewPostgresContainer(image, name, postgresUsername, postgresPassword string } } }() - containerName := PREFIXPGCONTAINER + name + containerName := PREFIXPGCONTAINER + props.Name - newHostPort, err := nat.NewPort("tcp", strconv.Itoa(port)) + CPUShares, _ := strconv.Atoi(props.CPUShares) + Memory, _ := strconv.Atoi(props.Memory) + + newHostPort, err := nat.NewPort("tcp", strconv.Itoa(props.Port)) if err != nil { return dockerservice.Container{}, err } @@ -82,21 +95,25 @@ func NewPostgresContainer(image, name, postgresUsername, postgresPassword string NetworkMode: "default", AutoRemove: false, Mounts: mounts, + Resources: container.Resources{ + CPUShares: int64(CPUShares), + Memory: int64(Memory * 1000000), + }, } endpointConfig := map[string]*network.EndpointSettings{} - networkName := name + "_default" + networkName := props.Name + "_default" // setting key and value for the map. networkName=$dbname_default (eg: viggy_default) endpointConfig[networkName] = &network.EndpointSettings{} nwConfig := network.NetworkingConfig{EndpointsConfig: endpointConfig} env := []string{} - env = append(env, misc.StringToDockerEnvVal("POSTGRES_USER", postgresUsername)) - env = append(env, misc.StringToDockerEnvVal("POSTGRES_PASSWORD", postgresPassword)) + env = append(env, misc.StringToDockerEnvVal("POSTGRES_USER", props.Username)) + env = append(env, misc.StringToDockerEnvVal("POSTGRES_PASSWORD", props.Password)) postgresContainer = dockerservice.NewContainer( containerName, container.Config{ - Image: image, + Image: props.Image, Env: env, }, hostConfig, From 310b427643a6dce900893f837fd9a88d667c0e2b Mon Sep 17 00:00:00 2001 From: Surya Kant Date: Sun, 10 Apr 2022 14:09:20 +0530 Subject: [PATCH 2/3] update docs --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e88862b..80f5641 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,9 @@ curl -X POST http://localhost:4434/createservice \ "type": "postgres", "name": "localtest", "username": "spinup", - "password": "spinup" + "password": "spinup", + "memory": "6000", //In Megabytes + "cpu": "2" }, "version": {"maj":9,"min":6} }' From b276190cc2c0da839c0bbe10a86b59336a98dba9 Mon Sep 17 00:00:00 2001 From: Surya Kant Date: Sun, 10 Apr 2022 21:58:00 +0530 Subject: [PATCH 3/3] resolve comments --- README.md | 4 ++-- api/create.go | 2 +- config/config.go | 4 ++-- internal/postgres/postgres.go | 12 ++++-------- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 80f5641..108a75e 100644 --- a/README.md +++ b/README.md @@ -80,8 +80,8 @@ curl -X POST http://localhost:4434/createservice \ "name": "localtest", "username": "spinup", "password": "spinup", - "memory": "6000", //In Megabytes - "cpu": "2" + "memory": 6000, //In Megabytes + "cpu": 2 }, "version": {"maj":9,"min":6} }' diff --git a/api/create.go b/api/create.go index dc58afe..b76b819 100644 --- a/api/create.go +++ b/api/create.go @@ -44,7 +44,7 @@ func CreateService(w http.ResponseWriter, req *http.Request) { } err = json.Unmarshal(byteArray, &s) - log.Printf("%d %d %s %s", s.Version.Maj, s.Version.Min, s.Db.CPU, s.Db.Memory) + log.Printf("%d %d %d %d", s.Version.Maj, s.Version.Min, s.Db.CPU, s.Db.Memory) if s.UserID == "" && apiKeyHeader != "" { s.UserID = "testuser" } diff --git a/config/config.go b/config/config.go index 0fae592..6bcaa0d 100644 --- a/config/config.go +++ b/config/config.go @@ -52,8 +52,8 @@ type dbCluster struct { Username string Password string - Memory string - CPU string `json:"cpu"` + Memory int64 + CPU int64 Monitoring string } diff --git a/internal/postgres/postgres.go b/internal/postgres/postgres.go index 4d68a8d..ac11e08 100644 --- a/internal/postgres/postgres.go +++ b/internal/postgres/postgres.go @@ -26,8 +26,8 @@ type ContainerProps struct { Username string Password string Port int - Memory string - CPUShares string + Memory int64 + CPUShares int64 } func NewPostgresContainer(props ContainerProps) (postgresContainer dockerservice.Container, err error) { @@ -64,10 +64,6 @@ func NewPostgresContainer(props ContainerProps) (postgresContainer dockerservice } }() containerName := PREFIXPGCONTAINER + props.Name - - CPUShares, _ := strconv.Atoi(props.CPUShares) - Memory, _ := strconv.Atoi(props.Memory) - newHostPort, err := nat.NewPort("tcp", strconv.Itoa(props.Port)) if err != nil { return dockerservice.Container{}, err @@ -96,8 +92,8 @@ func NewPostgresContainer(props ContainerProps) (postgresContainer dockerservice AutoRemove: false, Mounts: mounts, Resources: container.Resources{ - CPUShares: int64(CPUShares), - Memory: int64(Memory * 1000000), + CPUShares: props.CPUShares, + Memory: props.Memory * 1000000, }, }