Skip to content

Commit

Permalink
plz: add uid as annotation in CR
Browse files Browse the repository at this point in the history
  • Loading branch information
yorugac committed Feb 27, 2024
1 parent df39311 commit 5f49055
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
14 changes: 11 additions & 3 deletions api/v1alpha1/privateloadzone_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"fmt"

guuid "github.com/google/uuid"

"github.com/go-logr/logr"
"github.com/grafana/k6-operator/pkg/cloud"

Expand Down Expand Up @@ -69,25 +71,27 @@ func init() {
// Register attempts to register PLZ with the k6 Cloud.
// Regardless of the result, condition PLZRegistered will be set to False.
// Callee is expected to check the returned error and set condition when it's appropriate.
func (plz *PrivateLoadZone) Register(ctx context.Context, logger logr.Logger, client *cloudapi.Client) error {
func (plz *PrivateLoadZone) Register(ctx context.Context, logger logr.Logger, client *cloudapi.Client) (string, error) {
plz.UpdateCondition(PLZRegistered, metav1.ConditionFalse)

uid := uuid()
data := cloud.PLZRegistrationData{
LoadZoneID: plz.Name,
Resources: cloud.PLZResources{
CPU: plz.Spec.Resources.Limits.Cpu().String(),
Memory: plz.Spec.Resources.Limits.Memory().String(),
},
UID: uid,
}

if err := cloud.RegisterPLZ(client, data); err != nil {
logger.Error(err, fmt.Sprintf("Failed to register PLZ %s.", plz.Name))
return err
return "", err
}

logger.Info(fmt.Sprintf("Registered PLZ %s.", plz.Name))

return nil
return uid, nil
}

// Deregister attempts to deregister PLZ with the k6 Cloud.
Expand All @@ -102,3 +106,7 @@ func (plz *PrivateLoadZone) Deregister(ctx context.Context, logger logr.Logger,

return nil
}

func uuid() string {
return guuid.New().String()
}
11 changes: 9 additions & 2 deletions controllers/plz_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ import (
"github.com/grafana/k6-operator/pkg/cloud"
)

const plzFinalizer = "privateloadzones.k6.io/finalizer"
const (
plzFinalizer = "privateloadzones.k6.io/finalizer"
plzUIDAnnotation = "privateloadzones.k6.io/plz-uid"
)

// PrivateLoadZoneReconciler reconciles a PrivateLoadZone object
type PrivateLoadZoneReconciler struct {
Expand Down Expand Up @@ -99,13 +102,17 @@ func (r *PrivateLoadZoneReconciler) Reconcile(ctx context.Context, req ctrl.Requ
}
} else {
// This is the first reconcile: PLZ should be registered
if err := plz.Register(ctx, logger, r.poller.Client); err != nil {
uid, err := plz.Register(ctx, logger, r.poller.Client)
if err != nil {
return ctrl.Result{}, err
}

logger.Info(fmt.Sprintf("PLZ %s is registered with k6 Cloud.", plz.Name))

controllerutil.AddFinalizer(plz, plzFinalizer)
plz.SetAnnotations(map[string]string{
plzUIDAnnotation: uid,
})

if err := r.Update(ctx, plz); err != nil {
return ctrl.Result{}, err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/google/pprof v0.0.0-20230728192033-2ba5b33183c6 h1:ZgoomqkdjGbQ3+qQXCkvYMCDvGDNg2k5JJDjjdTB6jY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grafana/xk6-browser v1.2.1 h1:O2fuHHvmmhXvWTPXzD+jsnt1XkVgVjx0+Lj1hsGIWMM=
Expand Down
5 changes: 5 additions & 0 deletions pkg/cloud/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ func (trs TestRunStatus) Aborted() bool {

// PLZRegistrationData holds info that needs to be sent to /v1/load-zones
type PLZRegistrationData struct {
// defined by user as `name`
LoadZoneID string `json:"k6_load_zone_id"`
Resources PLZResources `json:"pod_tiers"`
// Unique identifier of PLZ, generated by k6-operator
// during PLZ registration. It's purpose is to distinguish
// between PLZs with accidentally duplicate names.
UID string `json:"provider_id"`
}

type PLZResources struct {
Expand Down

0 comments on commit 5f49055

Please sign in to comment.