Compare commits
2 Commits
7214e26168
...
ef37f17378
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef37f17378 | ||
|
|
778bab644f |
@@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types/registry"
|
"github.com/docker/docker/api/types/registry"
|
||||||
);
|
);
|
||||||
@@ -24,6 +25,7 @@ type Config struct {
|
|||||||
HelperServiceName string
|
HelperServiceName string
|
||||||
StorageContainerName string
|
StorageContainerName string
|
||||||
PrepullImageServiceName string
|
PrepullImageServiceName string
|
||||||
|
ServiceScaleTimeout time.Duration
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,6 +47,7 @@ func GetConfig()(Config, error){
|
|||||||
cfg.Constants.HelperServiceName = "blazenaHelper";
|
cfg.Constants.HelperServiceName = "blazenaHelper";
|
||||||
cfg.Constants.StorageContainerName = "blazenaStorage";
|
cfg.Constants.StorageContainerName = "blazenaStorage";
|
||||||
cfg.Constants.PrepullImageServiceName = "blazenaPrepull";
|
cfg.Constants.PrepullImageServiceName = "blazenaPrepull";
|
||||||
|
cfg.Constants.ServiceScaleTimeout = time.Second * 15;
|
||||||
|
|
||||||
err = json.Unmarshal(rawConfig, &cfg);
|
err = json.Unmarshal(rawConfig, &cfg);
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -23,7 +22,6 @@ import (
|
|||||||
|
|
||||||
// Add mutex.
|
// Add mutex.
|
||||||
var ApiClient *client.Client;
|
var ApiClient *client.Client;
|
||||||
var scale sync.Map;
|
|
||||||
var token string = "12345";
|
var token string = "12345";
|
||||||
var theConfig cfg.Config;
|
var theConfig cfg.Config;
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,10 @@ package docker
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -53,16 +56,16 @@ func scaleDown(w http.ResponseWriter, r *http.Request){
|
|||||||
newScale := uint64(0);
|
newScale := uint64(0);
|
||||||
updatedSpec.Mode.Replicated.Replicas = &newScale;
|
updatedSpec.Mode.Replicated.Replicas = &newScale;
|
||||||
updatedSpec.Labels["blazena.scaledDown"] = "true";
|
updatedSpec.Labels["blazena.scaledDown"] = "true";
|
||||||
|
updatedSpec.Labels["blazena.originalScale"] = strconv.FormatUint(*originalScale, 10);
|
||||||
scale.Store(serviceId, *originalScale);
|
|
||||||
|
|
||||||
_, err = ApiClient.ServiceUpdate(context.Background(), serviceId, inspectresoult.Version, updatedSpec, swarm.ServiceUpdateOptions{});
|
_, err = ApiClient.ServiceUpdate(context.Background(), serviceId, inspectresoult.Version, updatedSpec, swarm.ServiceUpdateOptions{});
|
||||||
if(err != nil){
|
if(err != nil){
|
||||||
panic("Failed to update service."+ err.Error());
|
panic("Failed to update service."+ err.Error());
|
||||||
}
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), theConfig.Constants.ServiceScaleTimeout);
|
||||||
|
defer cancel();
|
||||||
|
|
||||||
//TODO: Add proper wait system
|
waitForScale(serviceId, ctx, 0);
|
||||||
time.Sleep(15 * time.Second);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func scaleUp(w http.ResponseWriter, r *http.Request){
|
func scaleUp(w http.ResponseWriter, r *http.Request){
|
||||||
@@ -99,22 +102,62 @@ func scaleUp(w http.ResponseWriter, r *http.Request){
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
originalScale, ok := scale.Load(serviceId);
|
originalScale := inspectresoult.Spec.Labels["blazena.originalScale"];
|
||||||
if(!ok){
|
|
||||||
|
if(originalScale == ""){
|
||||||
panic("Its not okay!");
|
panic("Its not okay!");
|
||||||
}
|
}
|
||||||
|
|
||||||
originalScaleChecked, ok := originalScale.(uint64);
|
originalScaleChecked, err := strconv.ParseUint(originalScale, 10, 64);
|
||||||
if(!ok){
|
if(err != nil){
|
||||||
panic("Its very not okay!")
|
panic("Its very not okay!"+ err.Error())
|
||||||
}
|
}
|
||||||
updatedSpec := inspectresoult.Spec;
|
updatedSpec := inspectresoult.Spec;
|
||||||
|
|
||||||
updatedSpec.Mode.Replicated.Replicas = &originalScaleChecked;
|
updatedSpec.Mode.Replicated.Replicas = &originalScaleChecked;
|
||||||
delete(updatedSpec.Labels, "blazena.scaledDown");
|
delete(updatedSpec.Labels, "blazena.scaledDown");
|
||||||
|
delete(updatedSpec.Labels, "blazena.originalScale");
|
||||||
|
|
||||||
ApiClient.ServiceUpdate(context.Background(), serviceId, inspectresoult.Version, updatedSpec, swarm.ServiceUpdateOptions{});
|
ApiClient.ServiceUpdate(context.Background(), serviceId, inspectresoult.Version, updatedSpec, swarm.ServiceUpdateOptions{});
|
||||||
|
|
||||||
//TODO: Add proper wait system
|
ctx, cancel := context.WithTimeout(context.Background(), theConfig.Constants.ServiceScaleTimeout);
|
||||||
time.Sleep(15 * time.Second);
|
defer cancel();
|
||||||
|
|
||||||
|
waitForScale(serviceId, ctx, int(originalScaleChecked));
|
||||||
|
}
|
||||||
|
|
||||||
|
func waitForScale(serviceId string, ctx context.Context, desiredCount int){
|
||||||
|
startTime := time.Now();
|
||||||
|
for ctx.Err() == nil {
|
||||||
|
tasks, err := ApiClient.TaskList(context.Background(), swarm.TaskListOptions{});
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Failed to list tasks.", slog.Any("propagatedError", err));
|
||||||
|
os.Exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
var running int;
|
||||||
|
for _, task := range tasks {
|
||||||
|
if task.ServiceID != serviceId {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if task.Status.State == swarm.TaskStateRunning{
|
||||||
|
running ++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if running == desiredCount {
|
||||||
|
slog.Debug("Rescaled Service",
|
||||||
|
slog.String("serviceId", serviceId),
|
||||||
|
slog.Any("took", time.Since(startTime)),
|
||||||
|
slog.Any("targetScale", desiredCount),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
time.Sleep(1*time.Second);
|
||||||
|
}
|
||||||
|
if ctx.Err() == context.DeadlineExceeded{
|
||||||
|
slog.Error("Failed to rescale service in given time.", slog.Any("serviceId", serviceId));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
main.go
12
main.go
@@ -1,12 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
cfg "github.com/rony5394/blazena/config"
|
||||||
"github.com/rony5394/blazena/docker"
|
"github.com/rony5394/blazena/docker"
|
||||||
"github.com/rony5394/blazena/host"
|
"github.com/rony5394/blazena/host"
|
||||||
cfg "github.com/rony5394/blazena/config"
|
)
|
||||||
"log/slog"
|
|
||||||
);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
If the exit code is X then it means Y:
|
If the exit code is X then it means Y:
|
||||||
@@ -39,6 +41,8 @@ func main() {
|
|||||||
|
|
||||||
slog.Debug("Config", slog.Any("Value", config));
|
slog.Debug("Config", slog.Any("Value", config));
|
||||||
|
|
||||||
|
startTime := time.Now();
|
||||||
|
|
||||||
mode := os.Args[1];
|
mode := os.Args[1];
|
||||||
switch mode {
|
switch mode {
|
||||||
case "docker":
|
case "docker":
|
||||||
@@ -53,4 +57,6 @@ func main() {
|
|||||||
default:
|
default:
|
||||||
panic("Invalid runtime mode!");
|
panic("Invalid runtime mode!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Debug("Whole run took", slog.String("time", time.Since(startTime).String()));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user