@@ -6,6 +6,7 @@ package cluster
66import (
77 "context"
88 "fmt"
9+ "net/http"
910 "time"
1011
1112 "k8s.io/apimachinery/pkg/util/wait"
@@ -15,6 +16,11 @@ import (
1516 msgraph_errors "github.com/Azure/ARO-RP/pkg/util/graph/graphsdk/models/odataerrors"
1617)
1718
19+ const (
20+ GraphApiMaxRetries int = 5
21+ GraphApiRetryInterval time.Duration = 5 * time .Second
22+ )
23+
1824func (c * Cluster ) createApplication (ctx context.Context , displayName string ) (string , string , error ) {
1925 appBody := msgraph_models .NewApplication ()
2026 appBody .SetDisplayName (& displayName )
@@ -35,7 +41,31 @@ func (c *Cluster) createApplication(ctx context.Context, displayName string) (st
3541 // ByApplicationId is confusingly named, but it refers to
3642 // the application's Object ID, not to the Application ID.
3743 // https://learn.microsoft.com/en-us/graph/api/application-addpassword?view=graph-rest-1.0&tabs=http#http-request
38- pwResult , err := c .spGraphClient .Applications ().ByApplicationId (id ).AddPassword ().Post (ctx , pwCredentialRequestBody , nil )
44+
45+ // retry loop, due to eventual consistency, the application we just created might not be found when queried immediately, only after a retry
46+ // check if returned error is 404 not found, if so, retry the operation
47+ numRetries := 0
48+ var pwResult msgraph_models.PasswordCredentialable
49+ err = wait .PollUntilWithContext (ctx , GraphApiRetryInterval , func (ctx context.Context ) (done bool , err error ) {
50+ pwResult , err = c .spGraphClient .Applications ().ByApplicationId (id ).AddPassword ().Post (ctx , pwCredentialRequestBody , nil )
51+ if err == nil {
52+ return true , nil
53+ }
54+
55+ mainErr , ok := err .(* msgraph_errors.ODataError )
56+ // some unknown error, bubble it up to caller
57+ if ! ok || mainErr .GetStatusCode () != http .StatusNotFound {
58+ return false , err
59+ }
60+
61+ // Check if we already tried too often
62+ numRetries ++
63+ if numRetries > GraphApiMaxRetries {
64+ return false , mainErr
65+ }
66+
67+ return false , nil
68+ })
3969 if err != nil {
4070 return "" , "" , err
4171 }
0 commit comments