Files
locust-operator/internal/controller/sync.go
Chris Richardson 60f4f65407
Some checks failed
Tests / Run on Ubuntu (push) Failing after 55s
E2E Tests / Run on Ubuntu (push) Failing after 1m15s
Lint / Run on Ubuntu (push) Has been cancelled
asD
2025-05-13 08:01:53 -04:00

52 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
}