-
Notifications
You must be signed in to change notification settings - Fork 171
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
84816de
commit 8648fda
Showing
27 changed files
with
2,829 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,65 @@ | ||
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/frontend/middleware" | ||
) | ||
|
||
func (f *frontend) getAdminHiveSyncsetResources(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry) | ||
clusterdeployment := strings.TrimPrefix(filepath.Dir(r.URL.Path), "/admin") | ||
b, err := f._getAdminHiveSyncsetResources(ctx, clusterdeployment) | ||
|
||
if cloudErr, ok := err.(*api.CloudError); ok { | ||
api.WriteCloudError(w, cloudErr) | ||
return | ||
} | ||
|
||
adminReply(log, w, nil, b, err) | ||
} | ||
|
||
func (f *frontend) _getAdminHiveSyncsetResources(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) | ||
if err != nil { | ||
return nil, api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeNotFound, "", "cluster not found") | ||
} | ||
|
||
if doc.OpenShiftCluster.Properties.HiveProfile.Namespace == "" { | ||
return nil, api.NewCloudError(http.StatusNoContent, api.CloudErrorCodeResourceNotFound, "", "cluster is not managed by hive") | ||
} | ||
|
||
cd, err := f.hiveClusterManager.GetSyncSetResources(ctx, doc) | ||
if err != nil { | ||
return nil, api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeNotFound, "", "cluster deployment not found") | ||
} | ||
|
||
var b []byte | ||
err = codec.NewEncoderBytes(&b, &codec.JsonHandle{}).Encode(cd) | ||
if err != nil { | ||
return nil, api.NewCloudError(http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", "unable to marshal response") | ||
} | ||
|
||
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,103 @@ | ||
package frontend | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/openshift/hive/apis/hiveinternal/v1alpha1" | ||
"github.com/stretchr/testify/assert" | ||
"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 TestGetAdminHiveSyncsetResources(t *testing.T) { | ||
fakeNamespace := "aro-00000000-0000-0000-0000-000000000000" | ||
ctx := context.Background() | ||
clusterSyncsetTest := &v1alpha1.ClusterSync{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "clustersync1", | ||
Namespace: fakeNamespace, | ||
}, | ||
} | ||
|
||
type test struct { | ||
name string | ||
namespace string | ||
hiveEnabled bool | ||
mocks func(*test, *mock_hive.MockClusterManager) | ||
wantStatusCode int | ||
wantResponse []byte | ||
wantError string | ||
} | ||
|
||
for _, tt := range []*test{ | ||
{ | ||
name: "Cluster SyncSets must be namespaced", | ||
namespace: "", | ||
hiveEnabled: true, | ||
mocks: func(tt *test, s *mock_hive.MockClusterManager) {}, | ||
wantStatusCode: http.StatusBadRequest, | ||
wantError: "400: InvalidParameter: : hive is not enabled", | ||
}, | ||
{ | ||
name: "List ClusterSync resources successfully", | ||
namespace: "hive", | ||
wantError: "", | ||
mocks: func(tt *test, s *mock_hive.MockClusterManager) { | ||
s.EXPECT(). | ||
GetSyncSetResources(gomock.Any(), gomock.Any()). | ||
Return(&clusterSyncsetTest, nil).Times(1) | ||
}, | ||
wantStatusCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "Hive is not enabled", | ||
namespace: fakeNamespace, | ||
mocks: nil, | ||
hiveEnabled: false, | ||
wantStatusCode: http.StatusBadRequest, | ||
wantError: "400: StatusBadRequest: : hive is not enabled", | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ti := newTestInfra(t).WithOpenShiftClusters().WithSubscriptions() | ||
defer ti.done() | ||
|
||
_env := ti.env.(*mock_env.MockInterface) | ||
var f *frontend | ||
var err error | ||
if tt.hiveEnabled { | ||
s := mock_hive.NewMockClusterManager(ti.controller) | ||
tt.mocks(tt, s) | ||
} | ||
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) | ||
} | ||
|
||
clusterSyncSet, err := f._getAdminHiveSyncsetResources(ctx, tt.namespace) | ||
cloudErr, isCloudErr := err.(*api.CloudError) | ||
if tt.wantError != "" && isCloudErr && cloudErr != nil { | ||
assert.Equal(t, tt.wantStatusCode, cloudErr.StatusCode, "got %q but wanted %q", cloudErr.Error(), tt.wantError) | ||
if tt.wantStatusCode != 0 && tt.wantStatusCode != cloudErr.StatusCode { | ||
t.Fatalf("got %q but wanted %q", cloudErr.Error(), tt.wantError) | ||
} | ||
} | ||
|
||
if !strings.EqualFold(string(clusterSyncSet), string(tt.wantResponse)) { | ||
t.Fatalf("got %q and expected %q", clusterSyncSet, 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.