зеркало из https://github.com/Azure/ARO-RP.git
Merge pull request #2194 from AldoFusterTurpin/refactor/simplify-openshift_cluster_databse
Refactor/simplify openshift cluster database
This commit is contained in:
Коммит
f62cf96546
|
@ -133,7 +133,8 @@ func (m *manager) Install(ctx context.Context) error {
|
|||
steps.AuthorizationRefreshingAction(m.fpAuthorizer, steps.Action(m.validateResources)),
|
||||
steps.Action(m.ensureACRToken),
|
||||
steps.Action(m.ensureInfraID),
|
||||
steps.Action(m.generateSSHKey),
|
||||
steps.Action(m.ensureSSHKey),
|
||||
steps.Action(m.ensureStorageSuffix),
|
||||
steps.Action(m.populateMTUSize),
|
||||
|
||||
steps.Action(m.createDNS),
|
||||
|
|
|
@ -13,31 +13,25 @@ import (
|
|||
"github.com/Azure/ARO-RP/pkg/api"
|
||||
)
|
||||
|
||||
func (m *manager) generateSSHKey(ctx context.Context) error {
|
||||
var err error
|
||||
m.doc, err = m.db.PatchWithLease(ctx, m.doc.Key, func(doc *api.OpenShiftClusterDocument) error {
|
||||
if doc.OpenShiftCluster.Properties.SSHKey == nil {
|
||||
sshKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
doc.OpenShiftCluster.Properties.SSHKey = x509.MarshalPKCS1PrivateKey(sshKey)
|
||||
}
|
||||
|
||||
if doc.OpenShiftCluster.Properties.StorageSuffix == "" {
|
||||
doc.OpenShiftCluster.Properties.StorageSuffix, err = randomLowerCaseAlphanumericStringWithNoVowels(5)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
doc.OpenShiftCluster.Properties.ImageRegistryStorageAccountName = "imageregistry" + doc.OpenShiftCluster.Properties.StorageSuffix
|
||||
}
|
||||
|
||||
doc.OpenShiftCluster.Properties.ImageRegistryStorageAccountName = "imageregistry" + doc.OpenShiftCluster.Properties.StorageSuffix
|
||||
|
||||
func mutateSSHKey(doc *api.OpenShiftClusterDocument) error {
|
||||
if doc.OpenShiftCluster.Properties.SSHKey != nil {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
sshKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
doc.OpenShiftCluster.Properties.SSHKey = x509.MarshalPKCS1PrivateKey(sshKey)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *manager) ensureSSHKey(ctx context.Context) error {
|
||||
updatedDoc, err := m.db.PatchWithLease(ctx, m.doc.Key, mutateSSHKey)
|
||||
m.doc = updatedDoc
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package cluster
|
||||
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the Apache License 2.0.
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Azure/ARO-RP/pkg/api"
|
||||
)
|
||||
|
||||
func setDocStorageSuffix(doc *api.OpenShiftClusterDocument) error {
|
||||
isDocStorageSuffixValid := doc.OpenShiftCluster.Properties.StorageSuffix != ""
|
||||
if isDocStorageSuffixValid {
|
||||
return nil
|
||||
}
|
||||
|
||||
storageSuffix, err := randomLowerCaseAlphanumericStringWithNoVowels(5)
|
||||
doc.OpenShiftCluster.Properties.StorageSuffix = storageSuffix
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func mutateStorageSuffix(doc *api.OpenShiftClusterDocument) error {
|
||||
err := setDocStorageSuffix(doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
doc.OpenShiftCluster.Properties.ImageRegistryStorageAccountName = "imageregistry" + doc.OpenShiftCluster.Properties.StorageSuffix
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *manager) ensureStorageSuffix(ctx context.Context) error {
|
||||
updatedDoc, err := m.db.PatchWithLease(ctx, m.doc.Key, mutateStorageSuffix)
|
||||
m.doc = updatedDoc
|
||||
|
||||
return err
|
||||
}
|
|
@ -25,6 +25,8 @@ const (
|
|||
OpenshiftClustersResourceGroupQuery = `SELECT * FROM OpenShiftClusters doc WHERE doc.clusterResourceGroupIdKey = @resourceGroupID`
|
||||
)
|
||||
|
||||
type OpenShiftClusterDocumentMutator func(*api.OpenShiftClusterDocument) error
|
||||
|
||||
type openShiftClusters struct {
|
||||
c cosmosdb.OpenShiftClusterDocumentClient
|
||||
collc cosmosdb.CollectionClient
|
||||
|
@ -36,8 +38,8 @@ type OpenShiftClusters interface {
|
|||
Create(context.Context, *api.OpenShiftClusterDocument) (*api.OpenShiftClusterDocument, error)
|
||||
Get(context.Context, string) (*api.OpenShiftClusterDocument, error)
|
||||
QueueLength(context.Context, string) (int, error)
|
||||
Patch(context.Context, string, func(*api.OpenShiftClusterDocument) error) (*api.OpenShiftClusterDocument, error)
|
||||
PatchWithLease(context.Context, string, func(*api.OpenShiftClusterDocument) error) (*api.OpenShiftClusterDocument, error)
|
||||
Patch(context.Context, string, OpenShiftClusterDocumentMutator) (*api.OpenShiftClusterDocument, error)
|
||||
PatchWithLease(context.Context, string, OpenShiftClusterDocumentMutator) (*api.OpenShiftClusterDocument, error)
|
||||
Update(context.Context, *api.OpenShiftClusterDocument) (*api.OpenShiftClusterDocument, error)
|
||||
Delete(context.Context, *api.OpenShiftClusterDocument) error
|
||||
ChangeFeed() cosmosdb.OpenShiftClusterDocumentIterator
|
||||
|
@ -179,11 +181,11 @@ func (c *openShiftClusters) QueueLength(ctx context.Context, collid string) (int
|
|||
return countTotal, nil
|
||||
}
|
||||
|
||||
func (c *openShiftClusters) Patch(ctx context.Context, key string, f func(*api.OpenShiftClusterDocument) error) (*api.OpenShiftClusterDocument, error) {
|
||||
func (c *openShiftClusters) Patch(ctx context.Context, key string, f OpenShiftClusterDocumentMutator) (*api.OpenShiftClusterDocument, error) {
|
||||
return c.patch(ctx, key, f, nil)
|
||||
}
|
||||
|
||||
func (c *openShiftClusters) patch(ctx context.Context, key string, f func(*api.OpenShiftClusterDocument) error, options *cosmosdb.Options) (*api.OpenShiftClusterDocument, error) {
|
||||
func (c *openShiftClusters) patch(ctx context.Context, key string, f OpenShiftClusterDocumentMutator, options *cosmosdb.Options) (*api.OpenShiftClusterDocument, error) {
|
||||
var doc *api.OpenShiftClusterDocument
|
||||
|
||||
err := cosmosdb.RetryOnPreconditionFailed(func() (err error) {
|
||||
|
@ -204,11 +206,11 @@ func (c *openShiftClusters) patch(ctx context.Context, key string, f func(*api.O
|
|||
return doc, err
|
||||
}
|
||||
|
||||
func (c *openShiftClusters) PatchWithLease(ctx context.Context, key string, f func(*api.OpenShiftClusterDocument) error) (*api.OpenShiftClusterDocument, error) {
|
||||
func (c *openShiftClusters) PatchWithLease(ctx context.Context, key string, f OpenShiftClusterDocumentMutator) (*api.OpenShiftClusterDocument, error) {
|
||||
return c.patchWithLease(ctx, key, f, nil)
|
||||
}
|
||||
|
||||
func (c *openShiftClusters) patchWithLease(ctx context.Context, key string, f func(*api.OpenShiftClusterDocument) error, options *cosmosdb.Options) (*api.OpenShiftClusterDocument, error) {
|
||||
func (c *openShiftClusters) patchWithLease(ctx context.Context, key string, f OpenShiftClusterDocumentMutator, options *cosmosdb.Options) (*api.OpenShiftClusterDocument, error) {
|
||||
return c.patch(ctx, key, func(doc *api.OpenShiftClusterDocument) error {
|
||||
if doc.LeaseOwner != c.uuid {
|
||||
return fmt.Errorf("lost lease")
|
||||
|
|
Загрузка…
Ссылка в новой задаче