Skip to content

Commit 8aadee0

Browse files
committed
feedback
1 parent 0b55f28 commit 8aadee0

File tree

7 files changed

+42
-111
lines changed

7 files changed

+42
-111
lines changed

cmd/installer/cli/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func preRunInstallCommon(cmd *cobra.Command, flags *InstallCmdFlags, rc runtimec
396396
}
397397
flags.licenseBytes = b
398398

399-
// validate the the license is indeed a license file
399+
// validate the license is indeed a license file
400400
l, err := helpers.ParseLicenseFromBytes(b)
401401
if err != nil {
402402
var notALicenseFileErr helpers.ErrNotALicenseFile

cmd/installer/cli/replicatedapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func newReplicatedAPIClient(license *kotsv1beta1.License, clusterID string) (rep
3333
func syncLicense(ctx context.Context, client replicatedapi.Client, license *kotsv1beta1.License) (*kotsv1beta1.License, []byte, error) {
3434
logrus.Debug("Syncing license")
3535

36-
updatedLicense, licenseBytes, err := client.SyncLicense(ctx, nil)
36+
updatedLicense, licenseBytes, err := client.SyncLicense(ctx)
3737
if err != nil {
3838
return nil, nil, fmt.Errorf("get latest license: %w", err)
3939
}

cmd/installer/cli/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func preRunUpgrade(ctx context.Context, flags UpgradeCmdFlags, upgradeConfig *up
242242
}
243243
upgradeConfig.licenseBytes = data
244244

245-
// validate the the license is indeed a license file
245+
// validate the license is indeed a license file
246246
l, err := helpers.ParseLicenseFromBytes(data)
247247
if err != nil {
248248
var notALicenseFileErr helpers.ErrNotALicenseFile

pkg-new/replicatedapi/client.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
var defaultHTTPClient = newRetryableHTTPClient()
1919

2020
type Client interface {
21-
SyncLicense(ctx context.Context, reportingInfo *ReportingInfo) (*kotsv1beta1.License, []byte, error)
21+
SyncLicense(ctx context.Context) (*kotsv1beta1.License, []byte, error)
2222
}
2323

2424
type client struct {
@@ -60,7 +60,7 @@ func NewClient(replicatedAppURL string, license *kotsv1beta1.License, releaseDat
6060
}
6161

6262
// SyncLicense fetches the latest license from the Replicated API
63-
func (c *client) SyncLicense(ctx context.Context, reportingInfo *ReportingInfo) (*kotsv1beta1.License, []byte, error) {
63+
func (c *client) SyncLicense(ctx context.Context) (*kotsv1beta1.License, []byte, error) {
6464
u := fmt.Sprintf("%s/license/%s", c.replicatedAppURL, c.license.Spec.AppSlug)
6565

6666
params := url.Values{}
@@ -70,7 +70,7 @@ func (c *client) SyncLicense(ctx context.Context, reportingInfo *ReportingInfo)
7070
}
7171
u = fmt.Sprintf("%s?%s", u, params.Encode())
7272

73-
req, err := c.newRetryableRequest(ctx, http.MethodGet, u, nil, reportingInfo)
73+
req, err := c.newRetryableRequest(ctx, http.MethodGet, u, nil)
7474
if err != nil {
7575
return nil, nil, fmt.Errorf("create request: %w", err)
7676
}
@@ -112,23 +112,23 @@ func (c *client) SyncLicense(ctx context.Context, reportingInfo *ReportingInfo)
112112
}
113113

114114
// newRetryableRequest returns a retryablehttp.Request object with kots defaults set, including a User-Agent header.
115-
func (c *client) newRetryableRequest(ctx context.Context, method string, url string, body io.Reader, reportingInfo *ReportingInfo) (*retryablehttp.Request, error) {
115+
func (c *client) newRetryableRequest(ctx context.Context, method string, url string, body io.Reader) (*retryablehttp.Request, error) {
116116
req, err := retryablehttp.NewRequestWithContext(ctx, method, url, body)
117117
if err != nil {
118118
return nil, err
119119
}
120120

121-
c.injectHeaders(req.Header, reportingInfo)
121+
c.injectHeaders(req.Header)
122122

123123
return req, nil
124124
}
125125

126126
// injectHeaders injects the basic auth header, user agent header, and reporting info headers into the http.Header.
127-
func (c *client) injectHeaders(header http.Header, reportingInfo *ReportingInfo) {
127+
func (c *client) injectHeaders(header http.Header) {
128128
header.Set("Authorization", "Basic "+basicAuth(c.license.Spec.LicenseID, c.license.Spec.LicenseID))
129129
header.Set("User-Agent", fmt.Sprintf("Embedded-Cluster/%s", versions.Version))
130130

131-
c.injectReportingInfoHeaders(header, reportingInfo)
131+
c.injectReportingInfoHeaders(header)
132132
}
133133

134134
func (c *client) getChannelFromLicense() (*kotsv1beta1.Channel, error) {

pkg-new/replicatedapi/client_test.go

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func TestSyncLicense(t *testing.T) {
236236
req.NoError(err)
237237

238238
// Execute test
239-
license, rawLicense, err := c.SyncLicense(context.Background(), nil)
239+
license, rawLicense, err := c.SyncLicense(context.Background())
240240

241241
// Validate results
242242
if tt.wantErr != "" {
@@ -301,7 +301,7 @@ func TestSyncLicense_ContextCancellation(t *testing.T) {
301301
cancel()
302302

303303
// Execute test
304-
result, rawLicense, err := c.SyncLicense(ctx, nil)
304+
result, rawLicense, err := c.SyncLicense(ctx)
305305

306306
// Should return error due to cancelled context
307307
req.Error(err)
@@ -313,14 +313,12 @@ func TestGetReportingInfoHeaders(t *testing.T) {
313313
tests := []struct {
314314
name string
315315
clusterID string
316-
reportingInfo *ReportingInfo
317316
expectedCount int
318317
checkHeaders map[string]string
319318
}{
320319
{
321-
name: "with cluster ID and nil reporting info",
320+
name: "with cluster ID",
322321
clusterID: "cluster-123",
323-
reportingInfo: nil,
324322
expectedCount: 7, // EmbeddedClusterID, ChannelID, ChannelName, K8sVersion, K8sDistribution, EmbeddedClusterVersion, IsKurl
325323
checkHeaders: map[string]string{
326324
"X-Replicated-EmbeddedClusterID": "cluster-123",
@@ -332,24 +330,9 @@ func TestGetReportingInfoHeaders(t *testing.T) {
332330
"X-Replicated-IsKurl": "false",
333331
},
334332
},
335-
{
336-
name: "with reporting info fields",
337-
clusterID: "cluster-abc",
338-
reportingInfo: &ReportingInfo{
339-
EmbeddedClusterNodes: stringPtr("5"),
340-
AppStatus: stringPtr("ready"),
341-
},
342-
expectedCount: 9, // 7 from above + 2 from reportingInfo
343-
checkHeaders: map[string]string{
344-
"X-Replicated-EmbeddedClusterNodes": "5",
345-
"X-Replicated-AppStatus": "ready",
346-
"X-Replicated-IsKurl": "false",
347-
},
348-
},
349333
{
350334
name: "zero values should be skipped",
351335
clusterID: "",
352-
reportingInfo: &ReportingInfo{},
353336
expectedCount: 6, // ChannelID, ChannelName, K8sVersion, K8sDistribution, EmbeddedClusterVersion, IsKurl
354337
checkHeaders: map[string]string{
355338
"X-Replicated-IsKurl": "false",
@@ -389,7 +372,7 @@ func TestGetReportingInfoHeaders(t *testing.T) {
389372
clusterID: tt.clusterID,
390373
}
391374

392-
headers := c.getReportingInfoHeaders(tt.reportingInfo)
375+
headers := c.getReportingInfoHeaders()
393376

394377
req.Len(headers, tt.expectedCount)
395378

@@ -434,14 +417,9 @@ func TestInjectHeaders(t *testing.T) {
434417
clusterID: "test-cluster-id",
435418
}
436419

437-
reportingInfo := &ReportingInfo{
438-
EmbeddedClusterNodes: stringPtr("3"),
439-
AppStatus: stringPtr("ready"),
440-
}
441-
442420
// Test the internal method directly
443421
header := http.Header{}
444-
c.injectHeaders(header, reportingInfo)
422+
c.injectHeaders(header)
445423

446424
// Validate User-Agent header was injected
447425
userAgent := header.Get("User-Agent")
@@ -461,7 +439,5 @@ func TestInjectHeaders(t *testing.T) {
461439
req.Equal(versions.K0sVersion, header.Get("X-Replicated-K8sVersion"))
462440
req.Equal(DistributionEmbeddedCluster, header.Get("X-Replicated-K8sDistribution"))
463441
req.Equal(versions.Version, header.Get("X-Replicated-EmbeddedClusterVersion"))
464-
req.Equal("3", header.Get("X-Replicated-EmbeddedClusterNodes"))
465-
req.Equal("ready", header.Get("X-Replicated-AppStatus"))
466442
req.Equal("false", header.Get("X-Replicated-IsKurl"))
467443
}

pkg-new/replicatedapi/reporting.go

Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package replicatedapi
22

33
import (
4-
"fmt"
54
"net/http"
6-
"reflect"
75

86
"github.com/replicatedhq/embedded-cluster/pkg/versions"
97
)
@@ -12,40 +10,13 @@ const (
1210
DistributionEmbeddedCluster string = "embedded-cluster"
1311
)
1412

15-
type ReportingInfo struct {
16-
EmbeddedClusterNodes *string `header:"X-Replicated-EmbeddedClusterNodes"`
17-
ReplHelmInstalls *string `header:"X-Replicated-ReplHelmInstalls"`
18-
NativeHelmInstalls *string `header:"X-Replicated-NativeHelmInstalls"`
19-
AppStatus *string `header:"X-Replicated-AppStatus"`
20-
InstallStatus *string `header:"X-Replicated-InstallStatus"`
21-
PreflightStatus *string `header:"X-Replicated-PreflightStatus"`
22-
DownstreamChannelSequence *string `header:"X-Replicated-DownstreamChannelSequence"`
23-
DownstreamSequence *string `header:"X-Replicated-DownstreamSequence"`
24-
DownstreamSource *string `header:"X-Replicated-DownstreamSource"`
25-
SkipPreflights *string `header:"X-Replicated-SkipPreflights"`
26-
27-
// unsupported headers
28-
// X-Replicated-KotsInstallID
29-
// X-Replicated-KurlInstallID
30-
// X-Replicated-KurlNodeCountTotal
31-
// X-Replicated-KurlNodeCountReady
32-
// X-Replicated-IsGitOpsEnabled
33-
// X-Replicated-GitOpsProvider
34-
// X-Replicated-SnapshotProvider
35-
// X-Replicated-SnapshotFullSchedule
36-
// X-Replicated-SnapshotFullTTL
37-
// X-Replicated-SnapshotPartialSchedule
38-
// X-Replicated-SnapshotPartialTTL
39-
40-
}
41-
42-
func (c *client) injectReportingInfoHeaders(header http.Header, reportingInfo *ReportingInfo) {
43-
for key, value := range c.getReportingInfoHeaders(reportingInfo) {
13+
func (c *client) injectReportingInfoHeaders(header http.Header) {
14+
for key, value := range c.getReportingInfoHeaders() {
4415
header.Set(key, value)
4516
}
4617
}
4718

48-
func (c *client) getReportingInfoHeaders(reportingInfo *ReportingInfo) map[string]string {
19+
func (c *client) getReportingInfoHeaders() map[string]string {
4920
headers := make(map[string]string)
5021

5122
// add headers from client
@@ -67,45 +38,6 @@ func (c *client) getReportingInfoHeaders(reportingInfo *ReportingInfo) map[strin
6738
// Add static headers
6839
headers["X-Replicated-IsKurl"] = "false"
6940

70-
if reportingInfo != nil {
71-
// Use reflection to read struct tags and map fields to headers
72-
v := reflect.ValueOf(reportingInfo).Elem()
73-
t := v.Type()
74-
75-
for i := 0; i < t.NumField(); i++ {
76-
field := t.Field(i)
77-
headerName := field.Tag.Get("header")
78-
if headerName == "" {
79-
continue
80-
}
81-
82-
fieldValue := v.Field(i)
83-
84-
// Check if the pointer field is nil (not set)
85-
if fieldValue.IsNil() {
86-
continue
87-
}
88-
89-
// Dereference the pointer to get the actual value
90-
actualValue := fieldValue.Elem()
91-
var strValue string
92-
93-
switch actualValue.Kind() {
94-
case reflect.String:
95-
strValue = actualValue.String()
96-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
97-
strValue = fmt.Sprintf("%d", actualValue.Int())
98-
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
99-
strValue = fmt.Sprintf("%d", actualValue.Uint())
100-
case reflect.Bool:
101-
strValue = fmt.Sprintf("%t", actualValue.Bool())
102-
default:
103-
panic(fmt.Sprintf("reporting info field %s has unsupported type: %s", field.Name, actualValue.Kind()))
104-
}
105-
headers[headerName] = strValue
106-
}
107-
}
108-
10941
// remove empty headers
11042
for key, value := range headers {
11143
if value == "" {
@@ -115,3 +47,26 @@ func (c *client) getReportingInfoHeaders(reportingInfo *ReportingInfo) map[strin
11547

11648
return headers
11749
}
50+
51+
// TODO: the following headers are injected by KOTS and are not yet supported by Embedded Cluster
52+
// X-Replicated-EmbeddedClusterNodes
53+
// X-Replicated-ReplHelmInstalls
54+
// X-Replicated-NativeHelmInstalls
55+
// X-Replicated-AppStatus
56+
// X-Replicated-InstallStatus
57+
// X-Replicated-PreflightStatus
58+
// X-Replicated-DownstreamChannelSequence
59+
// X-Replicated-DownstreamSequence
60+
// X-Replicated-DownstreamSource
61+
// X-Replicated-SkipPreflights
62+
// X-Replicated-KotsInstallID
63+
// X-Replicated-KurlInstallID
64+
// X-Replicated-KurlNodeCountTotal
65+
// X-Replicated-KurlNodeCountReady
66+
// X-Replicated-IsGitOpsEnabled
67+
// X-Replicated-GitOpsProvider
68+
// X-Replicated-SnapshotProvider
69+
// X-Replicated-SnapshotFullSchedule
70+
// X-Replicated-SnapshotFullTTL
71+
// X-Replicated-SnapshotPartialSchedule
72+
// X-Replicated-SnapshotPartialTTL

pkg/helpers/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func ParseLicenseFromBytes(data []byte) (*kotsv1beta1.License, error) {
4949
return nil, ErrNotALicenseFile{Err: err}
5050
}
5151
if license.Spec.LicenseID == "" {
52-
return nil, ErrNotALicenseFile{Err: fmt.Errorf("license id empty")}
52+
return nil, ErrNotALicenseFile{Err: fmt.Errorf("license id is empty")}
5353
}
5454
return &license, nil
5555
}

0 commit comments

Comments
 (0)