Skip to content

Commit 352b0a5

Browse files
committed
merging 8659 and 9545
1 parent 8d1a6e9 commit 352b0a5

File tree

7 files changed

+193
-18
lines changed

7 files changed

+193
-18
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package frontend
2+
3+
// Copyright (c) Microsoft Corporation.
4+
// Licensed under the Apache License 2.0.
5+
6+
import (
7+
"context"
8+
"net/http"
9+
"path/filepath"
10+
"strings"
11+
12+
"github.com/sirupsen/logrus"
13+
"github.com/ugorji/go/codec"
14+
15+
"github.com/Azure/ARO-RP/pkg/api"
16+
"github.com/Azure/ARO-RP/pkg/frontend/middleware"
17+
)
18+
19+
func (f *frontend) getAdminHiveSyncsetResources(w http.ResponseWriter, r *http.Request) {
20+
ctx := r.Context()
21+
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)
22+
clusterdeployment := strings.TrimPrefix(filepath.Dir(r.URL.Path), "/admin")
23+
b, err := f._getAdminHiveSyncsetResources(ctx, clusterdeployment)
24+
25+
if cloudErr, ok := err.(*api.CloudError); ok {
26+
api.WriteCloudError(w, cloudErr)
27+
return
28+
}
29+
30+
adminReply(log, w, nil, b, err)
31+
}
32+
33+
func (f *frontend) _getAdminHiveSyncsetResources(ctx context.Context, namespace string) ([]byte, error) {
34+
// we have to check if the frontend has a valid clustermanager since hive is not everywhere.
35+
if f.hiveClusterManager == nil {
36+
return nil, api.NewCloudError(http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", "hive is not enabled")
37+
}
38+
39+
dbOpenShiftClusters, err := f.dbGroup.OpenShiftClusters()
40+
if err != nil {
41+
return nil, api.NewCloudError(http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", err.Error())
42+
}
43+
44+
doc, err := dbOpenShiftClusters.Get(ctx, namespace)
45+
if err != nil {
46+
return nil, api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeNotFound, "", "cluster not found")
47+
}
48+
49+
if doc.OpenShiftCluster.Properties.HiveProfile.Namespace == "" {
50+
return nil, api.NewCloudError(http.StatusNoContent, api.CloudErrorCodeResourceNotFound, "", "cluster is not managed by hive")
51+
}
52+
53+
cd, err := f.hiveClusterManager.GetSyncSetResources(ctx, doc)
54+
if err != nil {
55+
return nil, api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeNotFound, "", "cluster deployment not found")
56+
}
57+
58+
var b []byte
59+
err = codec.NewEncoderBytes(&b, &codec.JsonHandle{}).Encode(cd)
60+
if err != nil {
61+
return nil, api.NewCloudError(http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", "unable to marshal response")
62+
}
63+
64+
return b, nil
65+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package frontend
2+
3+
// Copyright (c) Microsoft Corporation.
4+
// Licensed under the Apache License 2.0.
5+
6+
import (
7+
"context"
8+
"net/http"
9+
"strings"
10+
"testing"
11+
12+
"github.com/golang/mock/gomock"
13+
"github.com/openshift/hive/apis/hiveinternal/v1alpha1"
14+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15+
16+
"github.com/Azure/ARO-RP/pkg/api"
17+
"github.com/Azure/ARO-RP/pkg/metrics/noop"
18+
mock_env "github.com/Azure/ARO-RP/pkg/util/mocks/env"
19+
mock_hive "github.com/Azure/ARO-RP/pkg/util/mocks/hive"
20+
)
21+
22+
func TestGetAdminHiveSyncsetResources(t *testing.T) {
23+
fakeNamespace := "aro-00000000-0000-0000-0000-000000000000"
24+
ctx := context.Background()
25+
clusterSyncsetTest := &v1alpha1.ClusterSync{
26+
ObjectMeta: metav1.ObjectMeta{
27+
Name: "clustersync1",
28+
Namespace: fakeNamespace,
29+
},
30+
}
31+
32+
type test struct {
33+
name string
34+
namespace string
35+
hiveEnabled bool
36+
mocks func(*test, *mock_hive.MockClusterManager)
37+
wantStatusCode int
38+
wantResponse []byte
39+
wantError string
40+
}
41+
42+
for _, tt := range []*test{
43+
{
44+
name: "Cluster SyncSets must be namespaced",
45+
namespace: "",
46+
hiveEnabled: true,
47+
mocks: func(tt *test, s *mock_hive.MockClusterManager) {},
48+
wantStatusCode: http.StatusNotFound,
49+
wantError: "404: NotFound: : cluster not found",
50+
},
51+
{
52+
name: "List ClusterSync resources successfully",
53+
namespace: "hive",
54+
wantError: "",
55+
mocks: func(tt *test, s *mock_hive.MockClusterManager) {
56+
s.EXPECT().
57+
GetSyncSetResources(gomock.Any(), gomock.Any()).
58+
Return(&clusterSyncsetTest, nil).Times(1)
59+
},
60+
wantStatusCode: http.StatusOK,
61+
},
62+
{
63+
name: "Hive is not enabled",
64+
namespace: fakeNamespace,
65+
mocks: nil,
66+
hiveEnabled: false,
67+
wantStatusCode: http.StatusInternalServerError,
68+
wantError: "500: InternalServerError: : hive is not enabled",
69+
},
70+
} {
71+
t.Run(tt.name, func(t *testing.T) {
72+
ti := newTestInfra(t).WithOpenShiftClusters().WithSubscriptions()
73+
defer ti.done()
74+
75+
_env := ti.env.(*mock_env.MockInterface)
76+
var f *frontend
77+
var err error
78+
if tt.hiveEnabled {
79+
s := mock_hive.NewMockClusterManager(ti.controller) //NewMockSyncSetResourceManager(ti.controller)
80+
tt.mocks(tt, s)
81+
f, err = NewFrontend(ctx, ti.audit, ti.log, _env, ti.dbGroup, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, nil, nil, nil, nil)
82+
} else {
83+
f, err = NewFrontend(ctx, ti.audit, ti.log, _env, ti.dbGroup, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, nil, nil, nil, nil)
84+
}
85+
if err != nil {
86+
t.Fatal(err)
87+
}
88+
89+
clusterSyncSet, err := f._getAdminHiveSyncsetResources(ctx, tt.namespace)
90+
cloudErr, isCloudErr := err.(*api.CloudError)
91+
if tt.wantError != "" && isCloudErr && cloudErr != nil {
92+
if tt.wantError != cloudErr.Error() {
93+
t.Fatalf("got %q but wanted %q", cloudErr.Error(), tt.wantError)
94+
}
95+
if tt.wantStatusCode != 0 && tt.wantStatusCode != cloudErr.StatusCode {
96+
t.Fatalf("got %q but wanted %q", cloudErr.Error(), tt.wantError)
97+
}
98+
}
99+
100+
if !strings.EqualFold(string(clusterSyncSet), string(tt.wantResponse)) {
101+
t.Fatalf("got %q and expected %q", clusterSyncSet, tt.wantResponse)
102+
}
103+
})
104+
}
105+
}

pkg/hive/manager.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"errors"
99
"fmt"
10+
"log"
1011
"sort"
1112

1213
hivev1 "github.com/openshift/hive/apis/hive/v1"
@@ -43,7 +44,7 @@ type ClusterManager interface {
4344
IsClusterInstallationComplete(ctx context.Context, doc *api.OpenShiftClusterDocument) (bool, error)
4445
GetClusterDeployment(ctx context.Context, doc *api.OpenShiftClusterDocument) (*hivev1.ClusterDeployment, error)
4546
ResetCorrelationData(ctx context.Context, doc *api.OpenShiftClusterDocument) error
46-
GetClusterSyncforClusterDeployment(ctx context.Context, doc *api.OpenShiftClusterDocument) (*hivev1alpha1.ClusterSync, error)
47+
GetSyncSetResources(ctx context.Context, doc *api.OpenShiftClusterDocument) (*hivev1alpha1.ClusterSync, error)
4748
}
4849

4950
type clusterManager struct {
@@ -265,15 +266,18 @@ func (hr *clusterManager) installLogsForLatestDeployment(ctx context.Context, cd
265266
return latestProvision.Spec.InstallLog, nil
266267
}
267268

268-
func (hr *clusterManager) GetClusterSyncforClusterDeployment(ctx context.Context, doc *api.OpenShiftClusterDocument) (*hivev1alpha1.ClusterSync, error) {
269-
cs := &hivev1alpha1.ClusterSync{}
270-
err := hr.hiveClientset.Get(ctx, client.ObjectKey{
269+
func (hr *clusterManager) GetSyncSetResources(ctx context.Context, doc *api.OpenShiftClusterDocument) (*hivev1alpha1.ClusterSync, error) {
270+
clusterSync := &hivev1alpha1.ClusterSync{}
271+
272+
key := client.ObjectKey{
273+
Name: ClusterDeploymentName, // "cluster",
271274
Namespace: doc.OpenShiftCluster.Properties.HiveProfile.Namespace,
272-
Name: ClusterDeploymentName,
273-
}, cs)
275+
}
274276

277+
err := hr.hiveClientset.Get(ctx, key, clusterSync)
275278
if err != nil {
276-
return nil, err
279+
log.Fatalf("Error getting ClusterSync resources: %s", err.Error())
277280
}
278-
return cs, nil
281+
282+
return clusterSync, nil
279283
}

pkg/hive/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ func TestGetClusterSyncforClusterDeployment(t *testing.T) {
586586
log: logrus.NewEntry(logrus.StandardLogger()),
587587
}
588588

589-
result, err := c.GetClusterSyncforClusterDeployment(context.Background(), doc)
589+
result, err := c.GetSyncSetResources(context.Background(), doc)
590590
if err != nil && err.Error() != tt.wantErr ||
591591
err == nil && tt.wantErr != "" {
592592
t.Fatal(err)

pkg/monitor/cluster/syncsetstatus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func (mon *Monitor) emitSyncSetStatus(ctx context.Context) error {
11-
cs, error := mon.hiveClusterManager.GetClusterSyncforClusterDeployment(ctx, mon.doc)
11+
cs, error := mon.hiveClusterManager.GetSyncSetResources(ctx, mon.doc)
1212
if error != nil {
1313
return nil
1414
}

pkg/util/mocks/env/core.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/util/mocks/hive/hive.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)