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

NO-ISSUE: POC: new usage framework #5

Open
wants to merge 1 commit 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
100 changes: 55 additions & 45 deletions internal/bminventory/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ func NewBareMetalInventory(
gcConfig garbagecollector.Config,
providerRegistry registry.ProviderRegistry,
) *bareMetalInventory {
//Register Usages POC
//here or outside in main?
clusterApi.RegisterUsages(usageApi)
//
return &bareMetalInventory{
db: db,
log: log,
Expand Down Expand Up @@ -1617,7 +1621,7 @@ func (b *bareMetalInventory) InstallClusterInternal(ctx context.Context, params
//3 hosts (which would automatically be assigned as masters if the hw is sufficient)
if usages, uerr := usage.Unmarshal(cluster.Cluster.FeatureUsage); uerr == nil {
report := cluster.Cluster.EnabledHostCount > common.MinMasterHostsNeededForInstallation && selected
b.setUsage(report, usage.AutoAssignRoleUsage, nil, usages)
b.usageApi.Set(report, usage.AutoAssignRoleUsage, nil, usages)
b.usageApi.Save(tx, *cluster.ID, usages)
}
return nil
Expand Down Expand Up @@ -2393,6 +2397,10 @@ func (b *bareMetalInventory) updateClusterInternal(ctx context.Context, v1Params
return nil, err
}

//SARAH POC - put all the usages here
b.usageApi.NewSet(usage.ProxyUsage, &cluster.Cluster, &cluster.Cluster, b.db)
//

cluster.HostNetworks = b.calculateHostNetworks(log, cluster)
for _, host := range cluster.Hosts {
if err = b.customizeHost(host); err != nil {
Expand Down Expand Up @@ -2672,7 +2680,7 @@ func (b *bareMetalInventory) setDiskEncryptionUsage(c *models.Cluster, diskEncry
props["mode"] = swag.StringValue(diskEncryption.Mode)
props["tang_servers"] = diskEncryption.TangServers
}
b.setUsage(swag.StringValue(c.DiskEncryption.EnableOn) != models.DiskEncryptionEnableOnNone, usage.DiskEncryption, &props, usages)
b.usageApi.Set(swag.StringValue(c.DiskEncryption.EnableOn) != models.DiskEncryptionEnableOnNone, usage.DiskEncryption, &props, usages)
}

func (b *bareMetalInventory) updateClusterData(_ context.Context, cluster *common.Cluster, params installer.V2UpdateClusterParams, usages map[string]models.Usage, db *gorm.DB, log logrus.FieldLogger, interactivity Interactivity) error {
Expand All @@ -2686,7 +2694,7 @@ func (b *bareMetalInventory) updateClusterData(_ context.Context, cluster *commo
optionalParam(params.ClusterUpdateParams.SSHPublicKey, "ssh_public_key", updates)
optionalParam(params.ClusterUpdateParams.Hyperthreading, "hyperthreading", updates)

b.setProxyUsage(params.ClusterUpdateParams.HTTPProxy, params.ClusterUpdateParams.HTTPSProxy, params.ClusterUpdateParams.NoProxy, usages)
//b.setProxyUsage(params.ClusterUpdateParams.HTTPProxy, params.ClusterUpdateParams.HTTPSProxy, params.ClusterUpdateParams.NoProxy, usages)

if err = b.updateProviderParams(params, updates, usages); err != nil {
return err
Expand Down Expand Up @@ -2727,7 +2735,7 @@ func (b *bareMetalInventory) updateClusterData(_ context.Context, cluster *commo
if params.ClusterUpdateParams.SchedulableMasters != nil {
value := swag.BoolValue(params.ClusterUpdateParams.SchedulableMasters)
updates["schedulable_masters"] = value
b.setUsage(value, usage.SchedulableMasters, nil, usages)
b.usageApi.Set(value, usage.SchedulableMasters, nil, usages)
}

if params.ClusterUpdateParams.DiskEncryption != nil {
Expand Down Expand Up @@ -3064,7 +3072,7 @@ func (b *bareMetalInventory) updateNetworkParams(params installer.V2UpdateCluste
return err
}

b.setUsage(vipDhcpAllocation, usage.VipDhcpAllocationUsage, nil, usages)
b.usageApi.Set(vipDhcpAllocation, usage.VipDhcpAllocationUsage, nil, usages)
return nil
}

Expand Down Expand Up @@ -3093,7 +3101,7 @@ func (b *bareMetalInventory) updateNtpSources(params installer.V2UpdateClusterPa
updates["additional_ntp_source"] = ntpSource

//if additional ntp sources are defined by the user, report usage of this feature
b.setUsage(additionalNtpSourcesDefined, usage.AdditionalNtpSourceUsage, &map[string]interface{}{
b.usageApi.Set(additionalNtpSourcesDefined, usage.AdditionalNtpSourceUsage, &map[string]interface{}{
"source_count": len(strings.Split(ntpSource, ","))}, usages)
}
return nil
Expand Down Expand Up @@ -3129,22 +3137,16 @@ func optionalParam(data *string, field string, updates map[string]interface{}) {
}
}

func (b *bareMetalInventory) setUsage(enabled bool, name string, props *map[string]interface{}, usages map[string]models.Usage) {
if enabled {
b.usageApi.Add(usages, name, props)
} else {
b.usageApi.Remove(usages, name)
}
}

func (b *bareMetalInventory) setDefaultUsage(cluster *models.Cluster) error {
//OLD CODE
usages := make(map[string]models.Usage)
b.setUsage(swag.BoolValue(cluster.VipDhcpAllocation), usage.VipDhcpAllocationUsage, nil, usages)
b.setUsage(cluster.AdditionalNtpSource != "", usage.AdditionalNtpSourceUsage, &map[string]interface{}{
b.usageApi.Set(swag.BoolValue(cluster.VipDhcpAllocation), usage.VipDhcpAllocationUsage, nil, usages)
b.usageApi.Set(cluster.AdditionalNtpSource != "", usage.AdditionalNtpSourceUsage, &map[string]interface{}{
"source_count": len(strings.Split(cluster.AdditionalNtpSource, ","))}, usages)
b.setUsage(swag.StringValue(cluster.HighAvailabilityMode) == models.ClusterHighAvailabilityModeNone,
b.usageApi.Set(swag.StringValue(cluster.HighAvailabilityMode) == models.ClusterHighAvailabilityModeNone,
usage.HighAvailabilityModeUsage, nil, usages)
b.setProxyUsage(&cluster.HTTPProxy, &cluster.HTTPProxy, &cluster.NoProxy, usages)
//b.setProxyUsage(&cluster.HTTPProxy, &cluster.HTTPProxy, &cluster.NoProxy, usages) SARAH POC
//b.usageApi.NewSet(usage.ProxyUsage, cluster, cluster, b.db)
olmOperators := funk.Filter(cluster.MonitoredOperators, func(op *models.MonitoredOperator) bool {
return op != nil && op.OperatorType == models.OperatorTypeOlm
}).([]*models.MonitoredOperator)
Expand All @@ -3158,39 +3160,47 @@ func (b *bareMetalInventory) setDefaultUsage(cluster *models.Cluster) error {
}
featusage, _ := json.Marshal(usages)
cluster.FeatureUsage = string(featusage)
//

//POC for usage by categories
for _, name := range b.usageApi.GetByCategory(usage.ClusterUsageCategory) {
b.usageApi.NewSet(name, cluster, cluster, b.db)
}
// Here will come also the operators category, platforms etc
return nil
}

func (b *bareMetalInventory) setNetworkTypeUsage(networkType *string, usages map[string]models.Usage) {
switch swag.StringValue(networkType) {
case models.ClusterNetworkTypeOVNKubernetes:
b.setUsage(true, usage.OVNNetworkTypeUsage, nil, usages)
b.usageApi.Set(true, usage.OVNNetworkTypeUsage, nil, usages)
case models.ClusterNetworkTypeOpenShiftSDN:
b.setUsage(true, usage.SDNNetworkTypeUsage, nil, usages)
}
}

func (b *bareMetalInventory) setProxyUsage(httpProxy *string, httpsProxy *string, noProxy *string, usages map[string]models.Usage) {
props := map[string]interface{}{}
enabled := false
if swag.StringValue(httpProxy) != "" {
props["http_proxy"] = 1
enabled = true
}
if swag.StringValue(httpsProxy) != "" {
props["https_proxy"] = 1
enabled = true
}
if swag.StringValue(noProxy) != "" {
props["no_proxy"] = 1
enabled = true
}
if enabled {
b.usageApi.Add(usages, usage.ProxyUsage, &props)
} else {
b.usageApi.Remove(usages, usage.ProxyUsage)
}
}
b.usageApi.Set(true, usage.SDNNetworkTypeUsage, nil, usages)
}
}

//SARAH
// func (b *bareMetalInventory) setProxyUsage(httpProxy *string, httpsProxy *string, noProxy *string, usages map[string]models.Usage) {
// props := map[string]interface{}{}
// enabled := false
// if swag.StringValue(httpProxy) != "" {
// props["http_proxy"] = 1
// enabled = true
// }
// if swag.StringValue(httpsProxy) != "" {
// props["https_proxy"] = 1
// enabled = true
// }
// if swag.StringValue(noProxy) != "" {
// props["no_proxy"] = 1
// enabled = true
// }
// if enabled {
// b.usageApi.Add(usages, usage.ProxyUsage, &props)
// } else {
// b.usageApi.Remove(usages, usage.ProxyUsage)
// }
// }

func (b *bareMetalInventory) setOperatorsUsage(updateOLMOperators []*models.MonitoredOperator, removedOLMOperators []*models.MonitoredOperator, usages map[string]models.Usage) {
for _, operator := range updateOLMOperators {
Expand Down Expand Up @@ -3247,7 +3257,7 @@ func (b *bareMetalInventory) updateHostNames(ctx context.Context, params install
return common.NewApiError(http.StatusConflict, err)
}
host_count = host_count + 1
b.setUsage(true, usage.RequestedHostnameUsage,
b.usageApi.Set(true, usage.RequestedHostnameUsage,
&map[string]interface{}{"host_count": host_count}, usages)
}
return nil
Expand Down
8 changes: 4 additions & 4 deletions internal/bminventory/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func mockClusterRegisterSteps() {
mockVersions.EXPECT().GetReleaseImage(gomock.Any(), gomock.Any()).Return(common.TestDefaultConfig.ReleaseImage, nil).Times(1)
mockOperatorManager.EXPECT().GetSupportedOperatorsByType(models.OperatorTypeBuiltin).Return([]*models.MonitoredOperator{&common.TestDefaultConfig.MonitoredOperator}).Times(1)
mockProviderRegistry.EXPECT().SetPlatformUsages(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
mockUsageReports()
}

func mockClusterRegisterSuccess(bm *bareMetalInventory, withEvents bool) {
Expand Down Expand Up @@ -173,6 +174,8 @@ func mockAMSSubscription(ctx context.Context) {
func mockUsageReports() {
mockUsage.EXPECT().Add(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
mockUsage.EXPECT().Remove(gomock.Any(), gomock.Any()).AnyTimes()
mockUsage.EXPECT().Set(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
mockUsage.EXPECT().NewSet(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
mockUsage.EXPECT().Save(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
}

Expand Down Expand Up @@ -10770,7 +10773,6 @@ var _ = Describe("TestRegisterCluster", func() {
By("Register cluster", func() {

mockClusterRegisterSuccess(diskEncryptionBm, true)
mockUsageReports()
mockAMSSubscription(ctx)

reply := diskEncryptionBm.RegisterCluster(ctx, installer.RegisterClusterParams{
Expand Down Expand Up @@ -11087,7 +11089,6 @@ var _ = Describe("AMS subscriptions", func() {
db, dbName = common.PrepareTestDB()
bm = createInventory(db, cfg)
bm.clusterApi = cluster.NewManager(cluster.Config{}, common.GetTestLog(), db, mockEvents, nil, nil, nil, nil, nil, nil, nil, nil)
mockUsageReports()
})

AfterEach(func() {
Expand Down Expand Up @@ -11303,6 +11304,7 @@ var _ = Describe("AMS subscriptions", func() {
clusterID := strfmt.UUID(uuid.New().String())

By("register cluster", func() {
mockUsageReports()
err := db.Create(&common.Cluster{Cluster: models.Cluster{
ID: &clusterID,
OpenshiftVersion: common.TestDefaultConfig.OpenShiftVersion,
Expand Down Expand Up @@ -11382,7 +11384,6 @@ var _ = Describe("[V2UpdateCluster] AMS subscriptions", func() {
db, dbName = common.PrepareTestDB()
bm = createInventory(db, cfg)
bm.clusterApi = cluster.NewManager(cluster.Config{}, common.GetTestLog(), db, mockEvents, nil, nil, nil, nil, nil, nil, nil, nil)
mockUsageReports()
})

AfterEach(func() {
Expand Down Expand Up @@ -13003,7 +13004,6 @@ var _ = Describe("Platform tests", func() {
}

mockClusterRegisterSuccess(bm, true)
mockUsageReports()
mockOperators.EXPECT().ValidateCluster(ctx, gomock.Any()).AnyTimes()
})

Expand Down
2 changes: 2 additions & 0 deletions internal/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/openshift/assisted-service/internal/metrics"
"github.com/openshift/assisted-service/internal/network"
"github.com/openshift/assisted-service/internal/operators"
"github.com/openshift/assisted-service/internal/usage"
"github.com/openshift/assisted-service/models"
"github.com/openshift/assisted-service/pkg/commonutils"
"github.com/openshift/assisted-service/pkg/leader"
Expand Down Expand Up @@ -90,6 +91,7 @@ type API interface {
RegistrationAPI
InstallationAPI
ProgressAPI
usage.UsageHandler
// Refresh state in case of hosts update
RefreshStatus(ctx context.Context, c *common.Cluster, db *gorm.DB) (*common.Cluster, error)
ClusterMonitoring()
Expand Down
13 changes: 13 additions & 0 deletions internal/cluster/mock_cluster_api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions internal/cluster/usages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cluster

import (
"errors"

"github.com/openshift/assisted-service/internal/usage"
"github.com/openshift/assisted-service/models"
)

func (m *Manager) RegisterUsages(r usage.Registry) {
//Example: Proxy field usage. Add here all usages relating to cluster
r.Register(usage.ProxyUsage, usage.UsageRegistryEntry{Enabled: IsProxyEnabled,
OnAdd: OnProxyAdd,
OnRemove: nil})
}

func IsProxyEnabled(in interface{}) (bool, error) {
c, ok := in.(*models.Cluster)
if !ok {
return false, errors.New("IsProxyEnabled incompatible type of Cluster")
}
return (c.HTTPProxy != "" || c.HTTPSProxy != "" || c.NoProxy != ""), nil
}

func OnProxyAdd(in interface{}, props map[string]interface{}) error {
c, ok := in.(*models.Cluster)
if !ok {
return errors.New("OnProxyAdd incompatible type of Cluster")
}
if c.HTTPProxy != "" {
props["http_proxy"] = 1

}
if c.HTTPSProxy != "" {
props["https_proxy"] = 1

}
if c.NoProxy != "" {
props["no_proxy"] = 1
}
return nil
}
Loading