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
danimo committed Jul 5, 2024
1 parent 94ffb73 commit 692d238
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 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 @@ -159,20 +160,27 @@ func (o *B3ScaleOperator) innerReconcile(ctx context.Context, op *skop.Operator,
}

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(),
})
existingFrontend, err := getFrontendByName(ctx, *o.apiClient, bbbFrontend.Spec.Credentials.Frontend)
var id string
if err != nil {
return err
// 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
} else {
id = existingFrontend.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 +226,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 692d238

Please sign in to comment.