Skip to content

Commit

Permalink
KOTS reporting to replicated-app (#4202)
Browse files Browse the repository at this point in the history
* update reporting

* add function to get reporting info by app slug

* slug is not consistent use app id

* set store in kots for testing

* Empty-Commit

* remove reporting from getApplicationMetadataFromHost function

* store not initialized error
  • Loading branch information
FourSigma authored Dec 20, 2023
1 parent 4997f6f commit 950a658
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
12 changes: 12 additions & 0 deletions integration/replicated/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ import (
"path"
"testing"

"github.com/golang/mock/gomock"
"github.com/mholt/archiver/v3"
"github.com/replicatedhq/kots/integration/util"
"github.com/replicatedhq/kots/pkg/pull"
"github.com/replicatedhq/kots/pkg/store"
mock_store "github.com/replicatedhq/kots/pkg/store/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_PullReplicated(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockStore := mock_store.NewMockStore(ctrl)
store.SetStore(mockStore)
defer store.SetStore(nil)

mockStore.EXPECT().ListInstalledApps().MaxTimes(1)

namespace := "test_ns"

testDirs, err := ioutil.ReadDir("tests")
Expand Down
4 changes: 4 additions & 0 deletions pkg/persistence/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ var (
uri string
)

func IsInitialized() bool {
return db != nil
}

func SetDB(database *gorqlite.Connection) {
db = database
}
Expand Down
34 changes: 34 additions & 0 deletions pkg/replicatedapp/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/pkg/errors"
apptypes "github.com/replicatedhq/kots/pkg/app/types"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/persistence"
"github.com/replicatedhq/kots/pkg/reporting"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/util"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
"github.com/replicatedhq/kotskinds/client/kotsclientset/scheme"
Expand Down Expand Up @@ -59,6 +61,26 @@ func GetLatestLicenseForHelm(licenseID string) (*LicenseData, error) {
return licenseData, nil
}

func getAppIdFromLicenseId(s store.Store, licenseID string) (string, error) {
apps, err := s.ListInstalledApps()
if err != nil {
return "", errors.Wrap(err, "failed to get all app licenses")
}

for _, a := range apps {
l, err := s.GetLatestLicenseForApp(a.ID)
if err != nil {
return "", errors.Wrap(err, "failed to get latest license for app")
}

if l.Spec.LicenseID == licenseID {
return a.ID, nil
}
}

return "", nil
}

func getLicenseFromAPI(url string, licenseID string) (*LicenseData, error) {
req, err := util.NewRequest("GET", url, nil)
if err != nil {
Expand All @@ -67,6 +89,18 @@ func getLicenseFromAPI(url string, licenseID string) (*LicenseData, error) {

req.SetBasicAuth(licenseID, licenseID)

if persistence.IsInitialized() {
appId, err := getAppIdFromLicenseId(store.GetStore(), licenseID)
if err != nil {
return nil, errors.Wrap(err, "failed to get license by id")
}

if appId != "" {
reportingInfo := reporting.GetReportingInfo(appId)
reporting.InjectReportingInfoHeaders(req, reportingInfo)
}
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to execute get request")
Expand Down
9 changes: 9 additions & 0 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ func GetStore() Store {
func storeFromEnv() Store {
return kotsstore.StoreFromEnv()
}

func SetStore(s Store) {
if s == nil {
hasStore = false
globalStore = nil
}
hasStore = true
globalStore = s
}

0 comments on commit 950a658

Please sign in to comment.