Skip to content

Commit

Permalink
refactor interface to report level
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig O'Donnell committed Oct 25, 2023
1 parent b766b23 commit dc017d6
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 284 deletions.
64 changes: 34 additions & 30 deletions pkg/reporting/app_airgap.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,51 @@ func (r *AirgapReporter) SubmitAppInfo(appID string) error {
}
reportingInfo := GetReportingInfo(appID)

event := GetInstanceReportEvent(license.Spec.LicenseID, reportingInfo)
report := GetInstanceReport(license.Spec.LicenseID, reportingInfo)

if err := CreateReportEvent(r.clientset, util.PodNamespace, a.Slug, event); err != nil {
return errors.Wrap(err, "failed to create instance report event")
if err := AppendReport(r.clientset, util.PodNamespace, a.Slug, report); err != nil {
return errors.Wrap(err, "failed to append instance report")
}

return nil
}

func GetInstanceReportEvent(licenseID string, reportingInfo *types.ReportingInfo) *InstanceReportEvent {
func GetInstanceReport(licenseID string, reportingInfo *types.ReportingInfo) *InstanceReport {
// not using the "cursor" packages because it doesn't provide access to the underlying int64
downstreamSequence, err := strconv.ParseUint(reportingInfo.Downstream.Cursor, 10, 64)
if err != nil {
logger.Debugf("failed to parse downstream cursor %q: %v", reportingInfo.Downstream.Cursor, err)
}

return &InstanceReportEvent{
ReportedAt: time.Now().UTC().UnixMilli(),
LicenseID: licenseID,
InstanceID: reportingInfo.InstanceID,
ClusterID: reportingInfo.ClusterID,
AppStatus: reportingInfo.AppStatus,
IsKurl: reportingInfo.IsKurl,
KurlNodeCountTotal: reportingInfo.KurlNodeCountTotal,
KurlNodeCountReady: reportingInfo.KurlNodeCountReady,
K8sVersion: reportingInfo.K8sVersion,
K8sDistribution: reportingInfo.K8sDistribution,
KotsVersion: reportingInfo.KOTSVersion,
KotsInstallID: reportingInfo.KOTSInstallID,
KurlInstallID: reportingInfo.KURLInstallID,
IsGitOpsEnabled: reportingInfo.IsGitOpsEnabled,
GitOpsProvider: reportingInfo.GitOpsProvider,
DownstreamChannelID: reportingInfo.Downstream.ChannelID,
DownstreamChannelSequence: downstreamSequence,
DownstreamChannelName: reportingInfo.Downstream.ChannelName,
DownstreamSequence: reportingInfo.Downstream.Sequence,
DownstreamSource: reportingInfo.Downstream.Source,
InstallStatus: reportingInfo.Downstream.Status,
PreflightState: reportingInfo.Downstream.PreflightState,
SkipPreflights: reportingInfo.Downstream.SkipPreflights,
ReplHelmInstalls: reportingInfo.Downstream.ReplHelmInstalls,
NativeHelmInstalls: reportingInfo.Downstream.NativeHelmInstalls,
return &InstanceReport{
Events: []InstanceReportEvent{
{
ReportedAt: time.Now().UTC().UnixMilli(),
LicenseID: licenseID,
InstanceID: reportingInfo.InstanceID,
ClusterID: reportingInfo.ClusterID,
AppStatus: reportingInfo.AppStatus,
IsKurl: reportingInfo.IsKurl,
KurlNodeCountTotal: reportingInfo.KurlNodeCountTotal,
KurlNodeCountReady: reportingInfo.KurlNodeCountReady,
K8sVersion: reportingInfo.K8sVersion,
K8sDistribution: reportingInfo.K8sDistribution,
KotsVersion: reportingInfo.KOTSVersion,
KotsInstallID: reportingInfo.KOTSInstallID,
KurlInstallID: reportingInfo.KURLInstallID,
IsGitOpsEnabled: reportingInfo.IsGitOpsEnabled,
GitOpsProvider: reportingInfo.GitOpsProvider,
DownstreamChannelID: reportingInfo.Downstream.ChannelID,
DownstreamChannelSequence: downstreamSequence,
DownstreamChannelName: reportingInfo.Downstream.ChannelName,
DownstreamSequence: reportingInfo.Downstream.Sequence,
DownstreamSource: reportingInfo.Downstream.Source,
InstallStatus: reportingInfo.Downstream.Status,
PreflightState: reportingInfo.Downstream.PreflightState,
SkipPreflights: reportingInfo.Downstream.SkipPreflights,
ReplHelmInstalls: reportingInfo.Downstream.ReplHelmInstalls,
NativeHelmInstalls: reportingInfo.Downstream.NativeHelmInstalls,
},
},
}
}
76 changes: 76 additions & 0 deletions pkg/reporting/instance_report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package reporting

import (
"fmt"
"sync"

"github.com/pkg/errors"
)

var instanceReportMtx = sync.Mutex{}

type InstanceReport struct {
Events []InstanceReportEvent `json:"events"`
}

type InstanceReportEvent struct {
ReportedAt int64 `json:"reported_at"`
LicenseID string `json:"license_id"`
InstanceID string `json:"instance_id"`
ClusterID string `json:"cluster_id"`
AppStatus string `json:"app_status"`
IsKurl bool `json:"is_kurl"`
KurlNodeCountTotal int `json:"kurl_node_count_total"`
KurlNodeCountReady int `json:"kurl_node_count_ready"`
K8sVersion string `json:"k8s_version"`
K8sDistribution string `json:"k8s_distribution,omitempty"`
KotsVersion string `json:"kots_version"`
KotsInstallID string `json:"kots_install_id,omitempty"`
KurlInstallID string `json:"kurl_install_id,omitempty"`
IsGitOpsEnabled bool `json:"is_gitops_enabled"`
GitOpsProvider string `json:"gitops_provider"`
DownstreamChannelID string `json:"downstream_channel_id,omitempty"`
DownstreamChannelSequence uint64 `json:"downstream_channel_sequence,omitempty"`
DownstreamChannelName string `json:"downstream_channel_name,omitempty"`
DownstreamSequence *int64 `json:"downstream_sequence,omitempty"`
DownstreamSource string `json:"downstream_source,omitempty"`
InstallStatus string `json:"install_status,omitempty"`
PreflightState string `json:"preflight_state,omitempty"`
SkipPreflights bool `json:"skip_preflights"`
ReplHelmInstalls int `json:"repl_helm_installs"`
NativeHelmInstalls int `json:"native_helm_installs"`
}

func (r *InstanceReport) GetType() ReportType {
return ReportTypeInstance
}

func (r *InstanceReport) GetSecretName(appSlug string) string {
return fmt.Sprintf(ReportSecretNameFormat, fmt.Sprintf("%s-%s", appSlug, r.GetType()))
}

func (r *InstanceReport) GetSecretKey() string {
return ReportSecretKey
}

func (r *InstanceReport) AppendEvents(report Report) error {
reportToAppend, ok := report.(*InstanceReport)
if !ok {
return errors.Errorf("report is not an instance report")
}

r.Events = append(r.Events, reportToAppend.Events...)
if len(r.Events) > r.GetEventLimit() {
r.Events = r.Events[len(r.Events)-r.GetEventLimit():]
}

return nil
}

func (r *InstanceReport) GetEventLimit() int {
return ReportEventLimit
}

func (r *InstanceReport) GetMtx() *sync.Mutex {
return &instanceReportMtx
}
32 changes: 18 additions & 14 deletions pkg/reporting/preflight_airgap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,26 @@ func (r *AirgapReporter) SubmitPreflightData(license *kotsv1beta1.License, appID
return errors.Wrap(err, "failed to get airgapped app")
}

event := &PreflightReportEvent{
ReportedAt: time.Now().UTC().UnixMilli(),
LicenseID: license.Spec.LicenseID,
InstanceID: appID,
ClusterID: clusterID,
Sequence: sequence,
SkipPreflights: skipPreflights,
InstallStatus: string(installStatus),
IsCLI: isCLI,
PreflightStatus: preflightStatus,
AppStatus: appStatus,
KotsVersion: buildversion.Version(),
report := &PreflightReport{
Events: []PreflightReportEvent{
{
ReportedAt: time.Now().UTC().UnixMilli(),
LicenseID: license.Spec.LicenseID,
InstanceID: appID,
ClusterID: clusterID,
Sequence: sequence,
SkipPreflights: skipPreflights,
InstallStatus: string(installStatus),
IsCLI: isCLI,
PreflightStatus: preflightStatus,
AppStatus: appStatus,
KotsVersion: buildversion.Version(),
},
},
}

if err := CreateReportEvent(r.clientset, util.PodNamespace, app.Slug, event); err != nil {
return errors.Wrap(err, "failed to create preflight report event")
if err := AppendReport(r.clientset, util.PodNamespace, app.Slug, report); err != nil {
return errors.Wrap(err, "failed to append preflight report")
}

return nil
Expand Down
62 changes: 62 additions & 0 deletions pkg/reporting/preflight_report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package reporting

import (
"fmt"
"sync"

"github.com/pkg/errors"
)

var preflightReportMtx = sync.Mutex{}

type PreflightReport struct {
Events []PreflightReportEvent `json:"events"`
}

type PreflightReportEvent struct {
ReportedAt int64 `json:"reported_at"`
LicenseID string `json:"license_id"`
InstanceID string `json:"instance_id"`
ClusterID string `json:"cluster_id"`
Sequence int64 `json:"sequence"`
SkipPreflights bool `json:"skip_preflights"`
InstallStatus string `json:"install_status"`
IsCLI bool `json:"is_cli"`
PreflightStatus string `json:"preflight_status"`
AppStatus string `json:"app_status"`
KotsVersion string `json:"kots_version"`
}

func (r *PreflightReport) GetType() ReportType {
return ReportTypePreflight
}

func (r *PreflightReport) GetSecretName(appSlug string) string {
return fmt.Sprintf(ReportSecretNameFormat, fmt.Sprintf("%s-%s", appSlug, r.GetType()))
}

func (r *PreflightReport) GetSecretKey() string {
return ReportSecretKey
}

func (r *PreflightReport) AppendEvents(report Report) error {
reportToAppend, ok := report.(*PreflightReport)
if !ok {
return errors.Errorf("report is not a preflight report")
}

r.Events = append(r.Events, reportToAppend.Events...)
if len(r.Events) > r.GetEventLimit() {
r.Events = r.Events[len(r.Events)-r.GetEventLimit():]
}

return nil
}

func (r *PreflightReport) GetEventLimit() int {
return ReportEventLimit
}

func (r *PreflightReport) GetMtx() *sync.Mutex {
return &preflightReportMtx
}
Loading

0 comments on commit dc017d6

Please sign in to comment.