Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support OCM geneva actions to get cluster info and to cancel cluster upgrade policy #3607

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion cmd/aro/rp.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func rp(ctx context.Context, log, audit *logrus.Entry) error {
if err != nil {
return err
}
f, err := frontend.NewFrontend(ctx, audit, log.WithField("component", "frontend"), _env, dbAsyncOperations, dbClusterManagerConfiguration, dbOpenShiftClusters, dbSubscriptions, dbOpenShiftVersions, api.APIs, metrics, clusterm, feAead, hiveClusterManager, adminactions.NewKubeActions, adminactions.NewAzureActions, clusterdata.NewParallelEnricher(metrics, _env))
f, err := frontend.NewFrontend(ctx, audit, log.WithField("component", "frontend"), _env, dbAsyncOperations, dbClusterManagerConfiguration, dbOpenShiftClusters, dbSubscriptions, dbOpenShiftVersions, api.APIs, metrics, clusterm, feAead, hiveClusterManager, adminactions.NewKubeActions, adminactions.NewAzureActions, adminactions.NewOCMActions, clusterdata.NewParallelEnricher(metrics, _env))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/frontend/admin_hive_clusterdeployment_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ func Test_getAdminHiveClusterDeployment(t *testing.T) {
clusterManager := mock_hive.NewMockClusterManager(controller)
clusterManager.EXPECT().GetClusterDeployment(gomock.Any(), gomock.Any()).Return(&clusterDeployment, nil).Times(tt.expectedGetClusterDeploymentCallCount)
f, err = NewFrontend(ctx, ti.audit, ti.log, _env, ti.asyncOperationsDatabase, ti.clusterManagerDatabase, ti.openShiftClustersDatabase,
ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, clusterManager, nil, nil, nil)
ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, clusterManager, nil, nil, nil, nil)
} else {
f, err = NewFrontend(ctx, ti.audit, ti.log, _env, ti.asyncOperationsDatabase, ti.clusterManagerDatabase, ti.openShiftClustersDatabase,
ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, nil, nil, nil)
ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, nil, nil, nil, nil)
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/frontend/admin_openshiftcluster_approvecsr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestAdminApproveCSR(t *testing.T) {

f, err := NewFrontend(ctx, ti.audit, ti.log, ti.env, ti.asyncOperationsDatabase, ti.clusterManagerDatabase, ti.openShiftClustersDatabase, ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, func(*logrus.Entry, env.Interface, *api.OpenShiftCluster) (adminactions.KubeActions, error) {
return k, nil
}, nil, nil)
}, nil, nil, nil)

if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/frontend/admin_openshiftcluster_cordonnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestAdminCordonUncordonNode(t *testing.T) {

f, err := NewFrontend(ctx, ti.audit, ti.log, ti.env, ti.asyncOperationsDatabase, ti.clusterManagerDatabase, ti.openShiftClustersDatabase, ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, func(*logrus.Entry, env.Interface, *api.OpenShiftCluster) (adminactions.KubeActions, error) {
return k, nil
}, nil, nil)
}, nil, nil, nil)

if err != nil {
t.Fatal(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestAdminDeleteManagedResource(t *testing.T) {

f, err := NewFrontend(ctx, ti.audit, ti.log, ti.env, ti.asyncOperationsDatabase, ti.clusterManagerDatabase, ti.openShiftClustersDatabase, ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, nil, func(*logrus.Entry, env.Interface, *api.OpenShiftCluster, *api.SubscriptionDocument) (adminactions.AzureActions, error) {
return a, nil
}, nil)
}, nil, nil)

if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/frontend/admin_openshiftcluster_drainnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestAdminDrainNode(t *testing.T) {

f, err := NewFrontend(ctx, ti.audit, ti.log, ti.env, ti.asyncOperationsDatabase, ti.clusterManagerDatabase, ti.openShiftClustersDatabase, ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, func(*logrus.Entry, env.Interface, *api.OpenShiftCluster) (adminactions.KubeActions, error) {
return k, nil
}, nil, nil)
}, nil, nil, nil)

if err != nil {
t.Fatal(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ func TestAdminEtcdCertificateRenew(t *testing.T) {
return k, nil
},
nil,
nil,
nil)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -762,6 +763,7 @@ func TestAdminEtcdCertificateRecovery(t *testing.T) {
return k, nil
},
nil,
nil,
nil)
if err != nil {
t.Fatal(err)
Expand Down
1 change: 1 addition & 0 deletions pkg/frontend/admin_openshiftcluster_etcdrecovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func TestAdminEtcdRecovery(t *testing.T) {
nil,
kubeActionsFactory,
nil,
nil,
ti.enricher)
if err != nil {
t.Fatal(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func TestAdminKubernetesObjectsGetAndDelete(t *testing.T) {

f, err := NewFrontend(ctx, ti.audit, ti.log, ti.env, ti.asyncOperationsDatabase, ti.clusterManagerDatabase, ti.openShiftClustersDatabase, ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, func(*logrus.Entry, env.Interface, *api.OpenShiftCluster) (adminactions.KubeActions, error) {
return k, nil
}, nil, nil)
}, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -413,7 +413,7 @@ func TestAdminPostKubernetesObjects(t *testing.T) {

f, err := NewFrontend(ctx, ti.audit, ti.log, ti.env, ti.asyncOperationsDatabase, ti.clusterManagerDatabase, ti.openShiftClustersDatabase, ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, func(*logrus.Entry, env.Interface, *api.OpenShiftCluster) (adminactions.KubeActions, error) {
return k, nil
}, nil, nil)
}, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestAdminKubernetesGetPodLogs(t *testing.T) {

f, err := NewFrontend(ctx, ti.audit, ti.log, ti.env, ti.asyncOperationsDatabase, ti.clusterManagerDatabase, ti.openShiftClustersDatabase, ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, nil, nil, func(*logrus.Entry, env.Interface, *api.OpenShiftCluster) (adminactions.KubeActions, error) {
return k, nil
}, nil, nil)
}, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/frontend/admin_openshiftcluster_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestAdminListOpenShiftCluster(t *testing.T) {
ti.openShiftClustersClient.SetError(tt.throwsError)
}

f, err := NewFrontend(ctx, ti.audit, ti.log, ti.env, ti.asyncOperationsDatabase, ti.clusterManagerDatabase, ti.openShiftClustersDatabase, ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, aead, nil, nil, nil, ti.enricher)
f, err := NewFrontend(ctx, ti.audit, ti.log, ti.env, ti.asyncOperationsDatabase, ti.clusterManagerDatabase, ti.openShiftClustersDatabase, ti.subscriptionsDatabase, nil, api.APIs, &noop.Noop{}, &noop.Noop{}, aead, nil, nil, nil, nil, ti.enricher)
if err != nil {
t.Fatal(err)
}
Expand Down
154 changes: 154 additions & 0 deletions pkg/frontend/admin_openshiftcluster_ocm_operations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package frontend

// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.

import (
"context"
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"strings"

"github.com/ghodss/yaml"
"github.com/go-chi/chi/v5"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kruntime "k8s.io/apimachinery/pkg/runtime"

"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/database/cosmosdb"
"github.com/Azure/ARO-RP/pkg/frontend/adminactions"
"github.com/Azure/ARO-RP/pkg/frontend/middleware"
ocmapi "github.com/Azure/ARO-RP/pkg/util/ocm/api"
"github.com/Azure/ARO-RP/pkg/util/pullsecret"
)

func (f *frontend) getOCMClusterInfo(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)

ocmActions, err := f.getOCMActions(ctx, r, log)
if err != nil {
api.WriteError(w, http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", "%v", err)
return
}

clusterInfo, err := ocmActions.GetClusterInfoWithUpgradePolicies(ctx)
if err != nil {
api.WriteError(w, http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", "%v", err)
return
}

clusterInfoBytes, err := json.Marshal(clusterInfo)
if err != nil {
api.WriteError(w, http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", "%v", err)
return
}

adminReply(log, w, nil, clusterInfoBytes, nil)
}

func (f *frontend) postAdminOCMCancelUpgradePolicy(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)
policyID := r.URL.Query().Get("policyID")

ocmActions, err := f.getOCMActions(ctx, r, log)
if err != nil {
api.WriteError(w, http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", "%v", err)
return
}

response, err := ocmActions.CancelClusterUpgradePolicy(ctx, policyID)
if err != nil {
api.WriteError(w, http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", "%v", err)
return
}

responseBytes, err := json.Marshal(response)
if err != nil {
api.WriteError(w, http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", "%v", err)
return
}

adminReply(log, w, nil, responseBytes, nil)
}

func (f *frontend) getOCMActions(ctx context.Context, r *http.Request, log *logrus.Entry) (adminactions.OCMActions, error) {
resType, resName, resGroupName := chi.URLParam(r, "resourceType"), chi.URLParam(r, "resourceName"), chi.URLParam(r, "resourceGroupName")

r.URL.Path = filepath.Dir(r.URL.Path)
resourceID := strings.TrimPrefix(r.URL.Path, "/admin")
doc, err := f.dbOpenShiftClusters.Get(ctx, resourceID)
switch {
case cosmosdb.IsErrorStatusCode(err, http.StatusNotFound):
return nil, api.NewCloudError(http.StatusNotFound, api.CloudErrorCodeResourceNotFound, "", "The Resource '%s/%s' under resource group '%s' was not found.", resType, resName, resGroupName)
case err != nil:
return nil, err
}

clusterID := doc.ID
k, err := f.kubeActionsFactory(log, f.env, doc.OpenShiftCluster)
if err != nil {
return nil, err
}

ps, err := k.KubeGet(ctx, "Secret", "openshift-config", "pull-secret")
if err != nil {
return nil, err
}

var u unstructured.Unstructured
if err := json.Unmarshal(ps, &u); err != nil {
return nil, err
}
var secret corev1.Secret
if err := kruntime.DefaultUnstructuredConverter.FromUnstructured(u.Object, &secret); err != nil {
return nil, err
}

psKeys, err := pullsecret.UnmarshalSecretData(&secret)
if err != nil {
return nil, err
}

token, ok := psKeys["cloud.openshift.com"]
if !ok {
return nil, fmt.Errorf("token not found in pull secret")
}

cm, err := k.KubeGet(ctx, "ConfigMap", "openshift-managed-upgrade-operator", "managed-upgrade-operator-config")
if err != nil {
return nil, err
}

if err := json.Unmarshal(cm, &u); err != nil {
return nil, err
}
var configMap corev1.ConfigMap
if err := kruntime.DefaultUnstructuredConverter.FromUnstructured(u.Object, &configMap); err != nil {
return nil, err
}

configYaml, ok := configMap.Data["config.yaml"]
if !ok {
return nil, fmt.Errorf("config.yaml not found")
}

var config ocmapi.Config
err = yaml.Unmarshal([]byte(configYaml), &config)
if err != nil {
return nil, err
}

// default OCM base URL jic pending upgrade exists
ocmBaseUrl := "https://api.openshift.com"
if config.ConfigManager.Source == "OCM" {
ocmBaseUrl = config.ConfigManager.OcmBaseURL
}

return f.ocmActionsFactory(clusterID, ocmBaseUrl, token), nil
}
Loading
Loading