-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Metrics for SyncSet and SelectorSyncSets
merging 8659 and 9545 Metrics for SyncSet and SelectorSyncSets
- Loading branch information
1 parent
3b6426c
commit 99c51f8
Showing
20 changed files
with
1,237 additions
and
16 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,72 @@ | ||
package frontend | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/ugorji/go/codec" | ||
|
||
"github.com/Azure/ARO-RP/pkg/api" | ||
"github.com/Azure/ARO-RP/pkg/database/cosmosdb" | ||
"github.com/Azure/ARO-RP/pkg/frontend/middleware" | ||
) | ||
|
||
func (f *frontend) getAdminHiveClusterSync(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry) | ||
resourceId := strings.TrimPrefix(filepath.Dir(r.URL.Path), "/admin") | ||
b, err := f._getAdminHiveClusterSync(ctx, resourceId) | ||
|
||
if cloudErr, ok := err.(*api.CloudError); ok { | ||
api.WriteCloudError(w, cloudErr) | ||
return | ||
} | ||
|
||
adminReply(log, w, nil, b, err) | ||
} | ||
|
||
func (f *frontend) _getAdminHiveClusterSync(ctx context.Context, resourceId string) ([]byte, error) { | ||
// We have to check if the frontend has a valid clustermanager since hive is not everywhere. | ||
if f.hiveClusterManager == nil { | ||
return nil, api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, "", "hive is not enabled") | ||
} | ||
|
||
dbOpenShiftClusters, err := f.dbGroup.OpenShiftClusters() | ||
if err != nil { | ||
return nil, api.NewCloudError(http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", err.Error()) | ||
} | ||
|
||
doc, err := dbOpenShiftClusters.Get(ctx, resourceId) | ||
switch { | ||
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound): | ||
return nil, api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeResourceNotFound, "", err.Error()) | ||
case err != nil: | ||
return nil, err | ||
} | ||
|
||
if doc.OpenShiftCluster.Properties.HiveProfile.Namespace == "" { | ||
return nil, api.NewCloudError(http.StatusNoContent, api.CloudErrorCodeResourceNotFound, "", "cluster is not managed by hive") | ||
} | ||
|
||
cd, err := f.hiveClusterManager.GetClusterSync(ctx, doc) | ||
switch { | ||
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound): | ||
return nil, api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeNotFound, "", err.Error()) | ||
case err != nil: | ||
return nil, err | ||
} | ||
|
||
var b []byte | ||
err = codec.NewEncoderBytes(&b, &codec.JsonHandle{}).Encode(cd) | ||
if err != nil { | ||
return nil, api.NewCloudError(http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", err.Error()) | ||
} | ||
|
||
return b, nil | ||
} |
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,116 @@ | ||
package frontend | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
hivev1alpha1 "github.com/openshift/hive/apis/hiveinternal/v1alpha1" | ||
"go.uber.org/mock/gomock" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/Azure/ARO-RP/pkg/api" | ||
"github.com/Azure/ARO-RP/pkg/metrics/noop" | ||
mock_env "github.com/Azure/ARO-RP/pkg/util/mocks/env" | ||
mock_hive "github.com/Azure/ARO-RP/pkg/util/mocks/hive" | ||
) | ||
|
||
func Test_getAdminHiveClusterSync(t *testing.T) { | ||
fakeUUID := "00000000-0000-0000-0000-000000000000" | ||
ctx := context.Background() | ||
clusterSync := hivev1alpha1.ClusterSync{Spec: hivev1alpha1.ClusterSyncSpec{}, Status: hivev1alpha1.ClusterSyncStatus{ | ||
SyncSets: []hivev1alpha1.SyncStatus{{Name: "syncSet1", ObservedGeneration: 0, Result: "success", LastTransitionTime: metav1.Time{Time: time.Date(2024, 7, 1, 0, 0, 0, 0, time.UTC)}}}, | ||
}} | ||
type test struct { | ||
name string | ||
resourceID string | ||
properties api.OpenShiftClusterProperties | ||
hiveEnabled bool | ||
expectedGetClusterSyncCallCount int | ||
wantStatusCode int | ||
wantResponse []byte | ||
wantError string | ||
} | ||
|
||
for _, tt := range []*test{ | ||
{ | ||
name: "cluster has hive profile with namespace", | ||
resourceID: fmt.Sprintf("/subscriptions/%s/resourcegroups/resourceGroup/providers/Microsoft.RedHatOpenShift/openShiftClusters/hive", fakeUUID), | ||
properties: api.OpenShiftClusterProperties{HiveProfile: api.HiveProfile{Namespace: fmt.Sprintf("aro-%s", fakeUUID)}}, | ||
hiveEnabled: true, | ||
expectedGetClusterSyncCallCount: 1, | ||
wantResponse: []byte(`{"status":{"syncSets":[{"name":"syncSet1","observedGeneration":0,"result":"success","lastTransitionTime":"2024-07-01T00:00:00Z"}]}}`), | ||
}, | ||
{ | ||
name: "cluster does not have hive profile with namespace", | ||
resourceID: fmt.Sprintf("/subscriptions/%s/resourcegroups/resourceGroup/providers/Microsoft.RedHatOpenShift/openShiftClusters/nonHive", fakeUUID), | ||
hiveEnabled: true, | ||
expectedGetClusterSyncCallCount: 0, | ||
wantStatusCode: http.StatusNoContent, | ||
wantError: "204: ResourceNotFound: : cluster is not managed by hive", | ||
}, | ||
{ | ||
name: "hive is not enabled", | ||
resourceID: fmt.Sprintf("/subscriptions/%s/resourcegroups/resourceGroup/providers/Microsoft.RedHatOpenShift/openShiftClusters/nonHive", fakeUUID), | ||
hiveEnabled: false, | ||
expectedGetClusterSyncCallCount: 0, | ||
wantStatusCode: http.StatusBadRequest, | ||
wantError: "400: InvalidParameter: : hive is not enabled", | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ti := newTestInfra(t).WithOpenShiftClusters().WithSubscriptions() | ||
controller := gomock.NewController(t) | ||
defer ti.done() | ||
defer controller.Finish() | ||
|
||
ti.fixture.AddOpenShiftClusterDocuments(&api.OpenShiftClusterDocument{ | ||
Key: strings.ToLower(tt.resourceID), | ||
OpenShiftCluster: &api.OpenShiftCluster{ | ||
ID: tt.resourceID, | ||
Name: "hive", | ||
Type: "Microsoft.RedHatOpenShift/openshiftClusters", | ||
Properties: tt.properties, | ||
}, | ||
}) | ||
|
||
err := ti.buildFixtures(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
_env := ti.env.(*mock_env.MockInterface) | ||
var f *frontend | ||
if tt.hiveEnabled { | ||
clusterManager := mock_hive.NewMockClusterManager(controller) | ||
clusterManager.EXPECT().GetClusterSync(gomock.Any(), gomock.Any()).Return(&clusterSync, nil).Times(tt.expectedGetClusterSyncCallCount) | ||
f, err = NewFrontend(ctx, ti.audit, ti.log, _env, ti.dbGroup, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, clusterManager, nil, nil, nil, nil) | ||
} else { | ||
f, err = NewFrontend(ctx, ti.audit, ti.log, _env, ti.dbGroup, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, nil, nil, nil, nil) | ||
} | ||
|
||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
hiveClusterSync, err := f._getAdminHiveClusterSync(ctx, strings.ToLower(tt.resourceID)) | ||
cloudErr, isCloudErr := err.(*api.CloudError) | ||
if tt.wantError != "" && isCloudErr && cloudErr != nil { | ||
if tt.wantError != cloudErr.Error() { | ||
t.Errorf("got %q but wanted %q", cloudErr.Error(), tt.wantError) | ||
} | ||
if tt.wantStatusCode != 0 && tt.wantStatusCode != cloudErr.StatusCode { | ||
t.Errorf("got %q but wanted %q", cloudErr.Error(), tt.wantError) | ||
} | ||
} | ||
|
||
if !strings.EqualFold(string(hiveClusterSync), string(tt.wantResponse)) { | ||
t.Errorf("got %q and expected %q", hiveClusterSync, tt.wantResponse) | ||
} | ||
}) | ||
} | ||
} |
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
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.