forked from Azure/ARO-RP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement generic handling for Hive ProvisionFailed=True conditions w…
…/ Azure policy errors
- Loading branch information
Showing
7 changed files
with
339 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package main | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
//go:generate go run ../genhiveconfig ./hive-additional-install-log-regexes.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package failure | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"regexp" | ||
|
||
mgmtfeatures "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-07-01/features" | ||
hivev1 "github.com/openshift/hive/apis/hive/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/Azure/ARO-RP/pkg/api" | ||
) | ||
|
||
var genericErr = &api.CloudError{ | ||
StatusCode: http.StatusInternalServerError, | ||
CloudErrorBody: &api.CloudErrorBody{ | ||
Code: api.CloudErrorCodeInternalServerError, | ||
Message: "Deployment failed.", | ||
}, | ||
} | ||
|
||
func HandleProvisionFailed(ctx context.Context, cd *hivev1.ClusterDeployment, cond hivev1.ClusterDeploymentCondition, installLog *string) error { | ||
if cond.Status != corev1.ConditionTrue { | ||
return nil | ||
} | ||
|
||
switch cond.Reason { | ||
case AzureRequestDisallowedByPolicy.Reason: | ||
armError, err := parseDeploymentFailedJson(*installLog) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return wrapArmError( | ||
AzureRequestDisallowedByPolicy.Message, | ||
*armError, | ||
) | ||
case AzureInvalidTemplateDeployment.Reason: | ||
armError, err := parseDeploymentFailedJson(*installLog) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return wrapArmError( | ||
AzureInvalidTemplateDeployment.Message, | ||
*armError, | ||
) | ||
default: | ||
return genericErr | ||
} | ||
} | ||
|
||
func parseDeploymentFailedJson(installLog string) (*mgmtfeatures.ErrorResponse, error) { | ||
regex := regexp.MustCompile(`level=error msg=400: DeploymentFailed: : Deployment failed. Details: : : (\{.*\})`) | ||
rawJson := regex.FindStringSubmatch(installLog)[1] | ||
|
||
armResponse := &mgmtfeatures.ErrorResponse{} | ||
if err := json.Unmarshal([]byte(rawJson), armResponse); err != nil { | ||
return nil, err | ||
} | ||
return armResponse, nil | ||
} | ||
|
||
func wrapArmError(errorMessage string, armError mgmtfeatures.ErrorResponse) *api.CloudError { | ||
details := make([]api.CloudErrorBody, len(*armError.Details)) | ||
for i, detail := range *armError.Details { | ||
details[i] = errorResponseToCloudErrorBody(detail) | ||
} | ||
|
||
return &api.CloudError{ | ||
StatusCode: http.StatusBadRequest, | ||
CloudErrorBody: &api.CloudErrorBody{ | ||
Code: api.CloudErrorCodeDeploymentFailed, | ||
Message: errorMessage, | ||
Details: details, | ||
}, | ||
} | ||
} | ||
|
||
func errorResponseToCloudErrorBody(errorResponse mgmtfeatures.ErrorResponse) api.CloudErrorBody { | ||
body := api.CloudErrorBody{ | ||
Code: *errorResponse.Code, | ||
Message: *errorResponse.Message, | ||
} | ||
|
||
if errorResponse.Target != nil { | ||
body.Target = *errorResponse.Target | ||
} | ||
|
||
return body | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.