2019-11-02 14:32:58 +00:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-01-17 19:46:51 +00:00
|
|
|
"os"
|
2019-11-02 14:32:58 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/utils"
|
2019-11-02 14:32:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var kubernetesSuiteName = "Kubernetes"
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
kind := Kind{}
|
|
|
|
kubectl := Kubectl{}
|
|
|
|
|
|
|
|
setup := func(suitePath string) error {
|
2020-02-09 17:04:28 +00:00
|
|
|
cmd := utils.Shell("docker-compose -p authelia -f internal/suites/docker-compose.yml -f internal/suites/example/compose/kind/docker-compose.yml build")
|
2019-11-30 16:49:52 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-09 17:04:28 +00:00
|
|
|
cmd = utils.Shell("docker build -t nginx-backend internal/suites/example/compose/nginx/backend")
|
2019-11-30 16:49:52 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
exists, err := kind.ClusterExists()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
log.Debug("Kubernetes cluster already exists")
|
|
|
|
} else {
|
|
|
|
err = kind.CreateCluster()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 19:46:51 +00:00
|
|
|
log.Debug("Building authelia:dist image or use cache if already built...")
|
|
|
|
if os.Getenv("CI") != "true" {
|
|
|
|
if err := utils.Shell("authelia-scripts docker build").Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-02 14:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Loading images into Kubernetes container...")
|
2020-01-17 19:46:51 +00:00
|
|
|
if err := loadDockerImages(); err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-16 15:22:10 +00:00
|
|
|
log.Debug("Starting Kubernetes dashboard...")
|
|
|
|
if err := kubectl.StartDashboard(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-02 14:32:58 +00:00
|
|
|
log.Debug("Deploying thirdparties...")
|
2020-01-17 19:46:51 +00:00
|
|
|
if err := kubectl.DeployThirdparties(); err != nil {
|
2019-11-02 14:32:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Waiting for services to be ready...")
|
|
|
|
if err := waitAllPodsAreReady(5 * time.Minute); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Deploying Authelia...")
|
|
|
|
if err = kubectl.DeployAuthelia(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Waiting for services to be ready...")
|
|
|
|
if err := waitAllPodsAreReady(2 * time.Minute); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("Starting proxy...")
|
|
|
|
if err := kubectl.StartProxy(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
teardown := func(suitePath string) error {
|
2020-04-22 03:33:14 +00:00
|
|
|
kubectl.StopDashboard() //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
|
|
|
kubectl.StopProxy() //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2019-11-02 14:32:58 +00:00
|
|
|
return kind.DeleteCluster()
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalRegistry.Register(kubernetesSuiteName, Suite{
|
|
|
|
SetUp: setup,
|
2019-12-27 11:07:53 +00:00
|
|
|
SetUpTimeout: 12 * time.Minute,
|
2019-11-30 16:49:52 +00:00
|
|
|
TestTimeout: 2 * time.Minute,
|
2019-11-02 14:32:58 +00:00
|
|
|
TearDown: teardown,
|
2019-11-30 16:49:52 +00:00
|
|
|
TearDownTimeout: 2 * time.Minute,
|
2019-11-02 14:32:58 +00:00
|
|
|
Description: "This suite has been created to test Authelia in a Kubernetes context and using nginx as the ingress controller.",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadDockerImages() error {
|
|
|
|
kind := Kind{}
|
2019-11-30 16:49:52 +00:00
|
|
|
images := []string{"authelia:dist", "nginx-backend"}
|
2019-11-02 14:32:58 +00:00
|
|
|
|
|
|
|
for _, image := range images {
|
|
|
|
err := kind.LoadImage(image)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitAllPodsAreReady(timeout time.Duration) error {
|
|
|
|
kubectl := Kubectl{}
|
|
|
|
// Wait in case the deployment has just been done and some services do not appear in kubectl logs.
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
fmt.Println("Check services are running")
|
|
|
|
if err := kubectl.WaitPodsReady(timeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("All pods are ready")
|
|
|
|
return nil
|
|
|
|
}
|