Skip to content

Commit

Permalink
fix: add id of frontend in b3scale if it already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Neuser committed Jul 5, 2024
1 parent 94ffb73 commit 90d9c7f
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"k8s.io/apimachinery/pkg/util/json"

"fmt"
"net/url"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -158,21 +159,30 @@ func (o *B3ScaleOperator) innerReconcile(ctx context.Context, op *skop.Operator,
return err
}

if bbbFrontend.Spec.FrontendID == nil {
// Create frontend in B3Scale backend
createdFrontend, err := o.apiClient.FrontendCreate(ctx, &store.FrontendState{
Active: true,
Frontend: &bbb.Frontend{
Key: bbbFrontend.Spec.Credentials.Frontend,
Secret: frontendSecret,
},
Settings: bbbFrontend.Spec.Settings.ToAPIFrontendSettings(),
})
if err != nil {
return err
if bbbFrontend.Spec.FrontendID == nil || bbbFrontend.Spec.FrontendID == "" {
var id string

// Try reading ID from existing frontend
existingFrontend, err := getFrontendByName(ctx, *o.apiClient, bbbFrontend.Spec.Credentials.Frontend)
if err == nil {
id = existingFrontend.ID
} else {
// Create frontend in B3Scale backend
createdFrontend, err := o.apiClient.FrontendCreate(ctx, &store.FrontendState{
Active: true,
Frontend: &bbb.Frontend{
Key: bbbFrontend.Spec.Credentials.Frontend,
Secret: frontendSecret,
},
Settings: bbbFrontend.Spec.Settings.ToAPIFrontendSettings(),
})
if err != nil {
return err
}
id = createdFrontend.ID
}

err = operatorKubernetesClient.CompleteBBBFrontend(ctx, bbbFrontend, FINALIZER_URL, createdFrontend.ID)
err = operatorKubernetesClient.CompleteBBBFrontend(ctx, bbbFrontend, FINALIZER_URL, id)
if err != nil {
return err
}
Expand Down Expand Up @@ -218,3 +228,19 @@ func makeLogger() log.Logger {
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
return logger
}

// Frontend retrieval helper
func getFrontendByName(
ctx context.Context, c b3scaleclient.Client, key string,
) (*store.FrontendState, error) {
frontends, err := c.FrontendsList(ctx, url.Values{
"key": []string{key},
})
if err != nil {
return nil, err
}
if len(frontends) > 0 {
return frontends[0], nil
}
return nil, nil
}

0 comments on commit 90d9c7f

Please sign in to comment.