Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 114 additions & 90 deletions frontend/pkg/frontend/cluster.go

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions frontend/pkg/frontend/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import (
"fmt"
"net/http"

ocmerrors "github.com/openshift-online/ocm-sdk-go/errors"

"github.com/Azure/ARO-HCP/internal/api/arm"
"github.com/Azure/ARO-HCP/internal/database"
"github.com/Azure/ARO-HCP/internal/ocm"
)

// erroringHTTPHandler is an http handler that leaves error reporting to a higher layer
Expand Down Expand Up @@ -53,6 +57,13 @@ func writeError(ctx context.Context, w http.ResponseWriter, err error, args ...i

logger.Error(fmt.Sprintf("%v", err), args...) // fmt used to handle nil

var ocmError *ocmerrors.Error
if errors.As(err, &ocmError) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convenient for error handling when we're able to do this.

resourceID, _ := ResourceIDFromContext(ctx) // used for error reporting
arm.WriteCloudError(w, ocm.CSErrorToCloudError(err, resourceID, w.Header()))
return nil
}

var cloudErr *arm.CloudError
if err != nil && errors.As(err, &cloudErr) {
if cloudErr != nil { // difference between interface is nil and the content is nil
Expand All @@ -61,6 +72,16 @@ func writeError(ctx context.Context, w http.ResponseWriter, err error, args ...i
}
}

if database.IsResponseError(err, http.StatusNotFound) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convenient for error handling simplification.

resourceID, err := ResourceIDFromContext(ctx) // used for error reporting
if err != nil {
arm.WriteInternalServerError(w)
return nil
}
Comment on lines +77 to +80
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as above except arm.NewResourceNotFoundError does require a resource ID. I can't think of any cases where this will be a problem, so maybe it's fine until we discover it isn't.

arm.WriteCloudError(w, arm.NewResourceNotFoundError(resourceID))
return nil
}

arm.WriteInternalServerError(w)
return nil
}
36 changes: 3 additions & 33 deletions frontend/pkg/frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"log/slog"
"maps"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -774,31 +773,6 @@ func (f *Frontend) OperationStatus(writer http.ResponseWriter, request *http.Req
return nil
}

// mergeToInternalCluster renders a CS Cluster object in JSON format, applying
// the necessary conversions for the API version of the request.
// TODO this overwrite will transformed into a "set" function as we transition fields to ownership in cosmos
func mergeToInternalCluster(csCluster *arohcpv1alpha1.Cluster, internalCluster *api.HCPOpenShiftCluster) (*api.HCPOpenShiftCluster, error) {
clusterServiceBasedInternalCluster, err := ocm.ConvertCStoHCPOpenShiftCluster(internalCluster.ID, csCluster)
if err != nil {
return nil, err
}

clusterServiceBasedInternalCluster.SystemData = internalCluster.SystemData
clusterServiceBasedInternalCluster.Tags = maps.Clone(internalCluster.Tags)
clusterServiceBasedInternalCluster.ServiceProviderProperties.ProvisioningState = internalCluster.ServiceProviderProperties.ProvisioningState
if clusterServiceBasedInternalCluster.Identity == nil {
clusterServiceBasedInternalCluster.Identity = &arm.ManagedServiceIdentity{}
}

if internalCluster.Identity != nil {
clusterServiceBasedInternalCluster.Identity.PrincipalID = internalCluster.Identity.PrincipalID
clusterServiceBasedInternalCluster.Identity.TenantID = internalCluster.Identity.TenantID
clusterServiceBasedInternalCluster.Identity.Type = internalCluster.Identity.Type
}

return clusterServiceBasedInternalCluster, nil
}

func getSubscriptionDifferences(oldSub, newSub *arm.Subscription) []string {
var messages []string

Expand Down Expand Up @@ -944,7 +918,7 @@ func (f *Frontend) OperationResult(writer http.ResponseWriter, request *http.Req
}

case cosmosOperation.InternalID.Kind() == arohcpv1alpha1.ClusterKind:
resultingInternalCluster, err := f.GetInternalClusterFromStorage(ctx, cosmosOperation.ExternalID)
resultingInternalCluster, err := f.getInternalClusterFromStorage(ctx, cosmosOperation.ExternalID)
if err != nil {
return err
}
Expand All @@ -954,15 +928,11 @@ func (f *Frontend) OperationResult(writer http.ResponseWriter, request *http.Req
}

case cosmosOperation.ExternalID.ResourceType.String() == api.NodePoolResourceType.String():
internalObj, err := f.dbClient.HCPClusters(cosmosOperation.ExternalID.SubscriptionID, cosmosOperation.ExternalID.ResourceGroupName).NodePools(cosmosOperation.ExternalID.Parent.Name).Get(ctx, cosmosOperation.ExternalID.Name)
resultingInternalNodePool, err := f.getInternalNodePoolFromStorage(ctx, cosmosOperation.ExternalID)
if err != nil {
return err
}
clusterServiceObj, err := f.clusterServiceClient.GetNodePool(ctx, internalObj.ServiceProviderProperties.ClusterServiceID)
if err != nil {
return ocm.CSErrorToCloudError(err, resourceID, nil)
}
responseBody, err = mergeToExternalNodePool(clusterServiceObj, internalObj, versionedInterface)
responseBody, err = arm.MarshalJSON(versionedInterface.NewHCPOpenShiftClusterNodePool(resultingInternalNodePool))
if err != nil {
return err
}
Expand Down
23 changes: 0 additions & 23 deletions frontend/pkg/frontend/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,26 +358,3 @@ func (f *Frontend) DeleteResource(ctx context.Context, transaction database.DBTr

return operationID, nil
}

func (f *Frontend) GetInternalClusterFromStorage(ctx context.Context, resourceID *azcorearm.ResourceID) (*api.HCPOpenShiftCluster, error) {
internalCluster, err := f.dbClient.HCPClusters(resourceID.SubscriptionID, resourceID.ResourceGroupName).Get(ctx, resourceID.Name)
if database.IsResponseError(err, http.StatusNotFound) {
return nil, arm.NewResourceNotFoundError(resourceID)
}
if err != nil {
return nil, err
}

csCluster, err := f.clusterServiceClient.GetCluster(ctx, internalCluster.ServiceProviderProperties.ClusterServiceID)
if err != nil {
return nil, ocm.CSErrorToCloudError(err, resourceID, nil)
}

// TODO this overwrite will transformed into a "set" function as we transition fields to ownership in cosmos
internalCluster, err = mergeToInternalCluster(csCluster, internalCluster)
if err != nil {
return nil, err
}

return internalCluster, nil
}
Loading
Loading