Skip to content

Commit

Permalink
Merge pull request #110 from skant7/feature/resource-constraints
Browse files Browse the repository at this point in the history
add cpu and memory constraints
  • Loading branch information
viggy28 authored Apr 10, 2022
2 parents 49fd50e + b276190 commit 9667a42
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}'
Expand Down
13 changes: 11 additions & 2 deletions api/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 %d %d", s.Version.Maj, s.Version.Min, s.Db.CPU, s.Db.Memory)
if s.UserID == "" && apiKeyHeader != "" {
s.UserID = "testuser"
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type dbCluster struct {
Username string
Password string

Memory string
Storage string
Memory int64
CPU int64
Monitoring string
}

Expand Down
33 changes: 23 additions & 10 deletions internal/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ 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 int64
CPUShares int64
}

func NewPostgresContainer(props ContainerProps) (postgresContainer dockerservice.Container, err error) {
dockerClient, err := dockerservice.NewDocker()
if err != nil {
fmt.Printf("error creating client %v", err)
}
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
Expand All @@ -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
}
Expand All @@ -53,9 +63,8 @@ func NewPostgresContainer(image, name, postgresUsername, postgresPassword string
}
}
}()
containerName := PREFIXPGCONTAINER + name

newHostPort, err := nat.NewPort("tcp", strconv.Itoa(port))
containerName := PREFIXPGCONTAINER + props.Name
newHostPort, err := nat.NewPort("tcp", strconv.Itoa(props.Port))
if err != nil {
return dockerservice.Container{}, err
}
Expand All @@ -82,21 +91,25 @@ func NewPostgresContainer(image, name, postgresUsername, postgresPassword string
NetworkMode: "default",
AutoRemove: false,
Mounts: mounts,
Resources: container.Resources{
CPUShares: props.CPUShares,
Memory: props.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,
Expand Down

0 comments on commit 9667a42

Please sign in to comment.