53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
|
|
locustCluster "git.lilpenguins.com/crichardson/locust-operator/api/v1alpha1"
|
|
"github.com/go-logr/logr"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
)
|
|
|
|
func GetLocustCluster(client client.Client, ctx context.Context, namespace string, name string) (
|
|
*locustCluster.LocustCluster, error) {
|
|
cluster := &locustCluster.LocustCluster{}
|
|
err := client.Get(ctx, BuildObjectKey(namespace, name), cluster)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return cluster, nil
|
|
}
|
|
|
|
func (r *LocustClusterReconciler) IsLeaderUp(log logr.Logger, locustCluster *locustCluster.LocustCluster, ctx context.Context) (*unstructured.UnstructuredList, error) {
|
|
podkind := &unstructured.UnstructuredList{}
|
|
podkind.SetKind("pod")
|
|
podkind.SetAPIVersion("v1")
|
|
|
|
searchLabels := map[string]string{
|
|
"job-name": BuildName(locustCluster.GetName(), KeyLeader),
|
|
}
|
|
err := r.List(ctx, podkind, client.InNamespace(locustCluster.Namespace), client.MatchingLabels(searchLabels))
|
|
if err != nil {
|
|
log.Info("Leader not found")
|
|
return nil, err
|
|
}
|
|
return podkind, nil
|
|
}
|
|
|
|
func (r *LocustClusterReconciler) CreateUpdateLeader(log logr.Logger, locustCluster *locustCluster.LocustCluster, ctx context.Context,
|
|
podList *unstructured.UnstructuredList) error {
|
|
var err error
|
|
if len(podList.Items) == 0 {
|
|
if err = r.CreateLeaderJob(log, locustCluster, ctx); err != nil {
|
|
log.Error(err, "Failed to create leader")
|
|
return err
|
|
}
|
|
if err = r.CreateLeaderService(log, locustCluster, ctx); err != nil {
|
|
log.Error(err, "Failed to create leader service")
|
|
return err
|
|
}
|
|
}
|
|
return err
|
|
}
|