Skip to content

Commit

Permalink
Merge pull request #1181 from zebrunner/develop
Browse files Browse the repository at this point in the history
3.1.1
  • Loading branch information
vdelendik authored Sep 9, 2024
2 parents 1c8ce2b + 937dafa commit 7988cfb
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 60 deletions.
2 changes: 1 addition & 1 deletion build/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
E3S_VERSION=3.1
E3S_VERSION=3.1.1
1 change: 1 addition & 0 deletions build/Dockerfile.router
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ ENV AWS_LINUX_CAPACITY_PROVIDER=
ENV AWS_WIN_CAPACITY_PROVIDER=
ENV AWS_TARGET_GROUP=
ENV AWS_TASK_ROLE=
ENV E3S_URL=
ENV USE_PUBLIC_IP=

ENV ELASTIC_CACHE=
Expand Down
3 changes: 3 additions & 0 deletions build/entrypoint.router
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ fi
if [ ! -z ${AWS_TARGET_GROUP} ]; then
CMD_ARGS="${CMD_ARGS} -aws-target-group ${AWS_TARGET_GROUP}"
fi
if [ ! -z ${E3S_URL} ]; then
CMD_ARGS="${CMD_ARGS} -e3s-url ${E3S_URL}"
fi
if [ ! -z ${AWS_TASK_ROLE} ]; then
CMD_ARGS="${CMD_ARGS} -aws-task-role-arn ${AWS_TASK_ROLE}"
fi
Expand Down
136 changes: 83 additions & 53 deletions cmd/router/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,89 +148,100 @@ func CreateRouter() *gin.Engine {
return r
}

func InitClusterInfo() (*elbv2.TargetGroup, error) {
func InitClusterInfo() error {
aws, err := service.InitAws()
if err != nil {
log.WithError(err).Error("Failed to start aws session")
return nil, err
return err
}
service.AwsSess = aws

err = utils.RefreshIMDSV2Token()
if err != nil {
log.WithError(err).Error("Failed to generate IMDSV2 token")
return nil, err
} else {
go func() {
for {
time.Sleep(2*time.Hour + 30*time.Minute)
err := utils.RefreshIMDSV2Token()
if err != nil {
log.WithError(err).Error("Failed to generate IMDSV2 token")
} else {
log.Debug("Successfully generated IMDSV2 token")
}
}
}()
return err
}

go func() {
for {
time.Sleep(2*time.Hour + 30*time.Minute)
err := utils.RefreshIMDSV2Token()
if err != nil {
log.WithError(err).Error("Failed to generate IMDSV2 token")
} else {
log.Debug("Successfully generated IMDSV2 token")
}
}
}()

scalersMap, err := service.InitScalingData()
if err != nil {
log.WithError(err).Error("Failed to init scaling data")
return nil, err
return err
}

for capacityProvider, scaler := range scalersMap {
environment.AddSmallestInstanceResources(scaler.InstanceResources.CPU, scaler.InstanceResources.Memory, capacityProvider)
}

config.Conf.E3SUrl, err = getE3SUrl()
if err != nil {
return err
}

// wait until task definitions are updated
retryCount := 2
for {
if ok, err := definitions.IsTaskDefinitionRefreshDone(); err != nil {
retryCount--
if retryCount <= 0 {
log.WithError(err).Error("Failed to get expected response from e3s definitions service")
return err
}
} else if ok {
go definitionmap.ActualizeDefinitionsMap(time.Minute * 15)
break
}

time.Sleep(5 * time.Second)
}

return nil
}

func getE3SUrl() (string, error) {
if config.Conf.E3SUrl != "" {
return config.Conf.E3SUrl, nil
}

l := log.WithField("targetGroup", config.Conf.AwsTargetGroup)
targetGroup, err := service.DescribeTargetGroup(config.Conf.AwsTargetGroup)
if err != nil {
l.WithError(err).Error("Failed to describe target group")
return nil, err
return "", err
} else if len(targetGroup.LoadBalancerArns) < 1 || targetGroup.LoadBalancerArns[0] == nil {
err = fmt.Errorf("target group is not attached to load balancer")
l.WithError(err).Error("Failed to describe target group")
return nil, err
return "", err
}

loadBalancer, err := service.DescribeLoadBalancer(*targetGroup.LoadBalancerArns[0])
if err != nil {
l.WithError(err).Error("Failed to describe load balancer")
return nil, err
return "", err
} else if loadBalancer.DNSName == nil || *loadBalancer.DNSName == "" {
err = fmt.Errorf("load balancer without public dns")
l.WithError(err).Error("Failed to describe load balancer")
return nil, err
return "", err
}

listener, err := service.DescribeListener(*loadBalancer.LoadBalancerArn)
if err != nil {
l.WithError(err).Error("Failed to describe load balancer")
return nil, err
return "", err
}

config.Conf.AwsEsgUrl = fmt.Sprintf("%s://%s", *listener.Protocol, *loadBalancer.DNSName)

// wait until task definitions are updated
retryCount := 2
for {
if ok, err := definitions.IsTaskDefinitionRefreshDone(); err != nil {
retryCount--
if retryCount <= 0 {
log.WithError(err).Error("Failed to get expected response from e3s definitions service")
return nil, err
}
} else if ok {
go definitionmap.ActualizeDefinitionsMap(time.Minute * 15)
break
}

time.Sleep(5 * time.Second)
}

return targetGroup, nil
return fmt.Sprintf("%s://%s", *listener.Protocol, *loadBalancer.DNSName), nil
}

func main() {
Expand Down Expand Up @@ -262,18 +273,18 @@ func main() {
mapper.InitMapperWorkers()
resourcesToAllocate.InitResourceWorker()

targetGroup, err := InitClusterInfo()
err = InitClusterInfo()
if err != nil {
log.WithError(err).Error("Failed to init cluster info")
startMockRouter()
} else {
startRouter(targetGroup)
startRouter()
}

log.Info("Router exited")
}

func startRouter(targetGroup *elbv2.TargetGroup) {
func startRouter() {
// init all starter workers
starter.InitInstanceWorker()
starter.InitWaitWorker()
Expand All @@ -295,16 +306,31 @@ func startRouter(targetGroup *elbv2.TargetGroup) {
}
}()

l := log.WithField("targetGroup", config.Conf.AwsTargetGroup)
err := service.RegisterTarget(targetGroup, config.Conf.ExternalPort)
if err != nil {
utils.ExitWithError(err, "Failed to append target to the elb target group", l)
} else {
var tg *elbv2.TargetGroup = nil
if config.Conf.AwsTargetGroup != "" {
var err error
l := log.WithField("targetGroup", config.Conf.AwsTargetGroup)

tg, err = service.DescribeTargetGroup(config.Conf.AwsTargetGroup)
if err != nil {
l.WithError(err).Error("Failed to describe target group")
utils.ExitWithError(err, "Failed to append target to the elb target group", l)
} else if len(tg.LoadBalancerArns) < 1 || tg.LoadBalancerArns[0] == nil {
err = fmt.Errorf("target group is not attached to load balancer")
utils.ExitWithError(err, "Failed to append target to the elb target group", l)
}

err = service.RegisterTarget(tg, config.Conf.ExternalPort)
if err != nil {
utils.ExitWithError(err, "Failed to append target to the elb target group", l)
}

// wait until alb actually starts distributing requests to that specific target
// average time is between 5 to 15 seconds
time.Sleep(25 * time.Second)
l.Info("Registered target in target group")
}

log.Info("Service started")

<-quit
Expand All @@ -313,10 +339,14 @@ func startRouter(targetGroup *elbv2.TargetGroup) {
ctx, cancel := context.WithTimeout(context.Background(), config.Conf.ServiceStartupTimeout+5*time.Second)
defer cancel()

err = service.DeregisterTarget(targetGroup, config.Conf.ExternalPort)
if err != nil {
l.WithError(err).Fatal("Failed to detach target from the elb target group")
} else {
if tg != nil {
l := log.WithField("targetGroup", config.Conf.AwsTargetGroup)

err := service.DeregisterTarget(tg, config.Conf.ExternalPort)
if err != nil {
l.WithError(err).Fatal("Failed to detach target from the elb target group")
}

// wait until alb actually stops distributing requests to that specific target
// average time is between 5 to 15 seconds
time.Sleep(25 * time.Second)
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Config struct {
AwsSecretAccessKey string
AwsTaskRoleArn string
AwsTargetGroup string
AwsEsgUrl string
E3SUrl string

// Timeouts
MaxIdleTimeout time.Duration
Expand Down Expand Up @@ -87,6 +87,7 @@ func init() {
flag.StringVar(&Conf.AwsSecretAccessKey, "aws-secret-access-key", "", "Secret key for AWS services")
flag.StringVar(&Conf.AwsTaskRoleArn, "aws-task-role-arn", "", "Role that would be assigned to all task's definitions")
flag.StringVar(&Conf.AwsTargetGroup, "aws-target-group", "", "Application load balancer name")
flag.StringVar(&Conf.E3SUrl, "e3s-url", "", "e3s external url")

flag.DurationVar(&Conf.MaxIdleTimeout, "max-idle-timeout", 20*time.Minute, "Maximum session idle timeout time that could be set by user's capabilities")
flag.DurationVar(&Conf.IdleTimeout, "idle-timeout", 60*time.Second, "Session idle timeout in time.Duration format")
Expand Down
8 changes: 4 additions & 4 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: backend
services:
task-definitions:
container_name: task-definitions
image: public.ecr.aws/zebrunner/task-definitions:3.0
image: public.ecr.aws/zebrunner/task-definitions:3.1.1
env_file:
- ./properties/config.env
- ./properties/task-definitions.env
Expand All @@ -15,7 +15,7 @@ services:
# - "${HOME}/.aws:/root/.aws"
scaler:
container_name: scaler
image: public.ecr.aws/zebrunner/scaler:3.0
image: public.ecr.aws/zebrunner/scaler:3.1.1
env_file:
- ./properties/config.env
- ./properties/scaler.env
Expand All @@ -27,7 +27,7 @@ services:
# - "${HOME}/.aws:/root/.aws"
router_1:
container_name: router_1
image: public.ecr.aws/zebrunner/router:3.0
image: public.ecr.aws/zebrunner/router:3.1.1
environment:
- EXTERNAL_PORT=$ROUTER_PORT_1
env_file:
Expand All @@ -46,7 +46,7 @@ services:
# - "${HOME}/.aws:/root/.aws"
router_2:
container_name: router_2
image: public.ecr.aws/zebrunner/router:3.0
image: public.ecr.aws/zebrunner/router:3.1.1
environment:
- EXTERNAL_PORT=$ROUTER_PORT_2
env_file:
Expand Down
2 changes: 1 addition & 1 deletion environment/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func buildGeneric(workspace string, routerUUID string, image images.Image, caps
}

executorContainer.Env["UUID"] = routerUUID
executorContainer.Env["E3S_URL"] = config.Conf.AwsEsgUrl
executorContainer.Env["E3S_URL"] = config.Conf.E3SUrl

recorderContainer := Container{
Name: "recorder",
Expand Down
1 change: 1 addition & 0 deletions properties/router.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Router settings
API_ACCESS_KEY=secure_password
USE_PUBLIC_IP=false
E3S_URL=

# Timeoouts limitations in time.Duration format, for example: 1m0s
SESSION_DELETE_TIMEOUT=
Expand Down

0 comments on commit 7988cfb

Please sign in to comment.