Skip to content

Commit 7198d4a

Browse files
authored
feat: add cache invalidation support based on Cache-Control header (#10205)
* feat: add cache invalidation support based on Cache-Control header * fix lint issues and bump golangci-lint to v2 * fix makefile * fix unit tests
1 parent b0c1c0f commit 7198d4a

File tree

86 files changed

+1330
-913
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+1330
-913
lines changed

.golangci.yml

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,41 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
#
14+
15+
version: "2"
1516
run:
1617
modules-download-mode: readonly
17-
allow-parallel-runners: true
18-
timeout: 30m
1918
issues-exit-code: 1
2019
tests: true
20+
allow-parallel-runners: true
2121
linters:
22-
disable-all: true
22+
default: none
2323
enable:
24-
# default linters
2524
- errcheck
26-
- gosimple
25+
- errorlint
26+
- gocyclo
2727
- govet
2828
- ineffassign
29+
- misspell
2930
- staticcheck
30-
- typecheck
3131
- unused
32-
- gocyclo
33-
34-
# additional linters
35-
- errorlint
32+
exclusions:
33+
generated: lax
34+
presets:
35+
- comments
36+
- common-false-positives
37+
- legacy
38+
- std-error-handling
39+
paths:
40+
- third_party$
41+
- builtin$
42+
- examples$
43+
formatters:
44+
enable:
3645
- goimports
37-
- misspell
46+
exclusions:
47+
generated: lax
48+
paths:
49+
- third_party$
50+
- builtin$
51+
- examples$

modules/api/pkg/handler/apihandler.go

Lines changed: 158 additions & 163 deletions
Large diffs are not rendered by default.

modules/api/pkg/handler/parser/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"strings"
2020

2121
"github.com/emicklei/go-restful/v3"
22+
2223
metricapi "k8s.io/dashboard/api/pkg/integration/metric/api"
2324
"k8s.io/dashboard/api/pkg/resource/dataselect"
2425
)

modules/api/pkg/integration/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ type Handler struct {
3333
//
3434
// By default, endpoint for checking state of the integrations is installed. It allows user
3535
// to check state of integration by accessing `<DASHBOARD_URL>/api/v1/integration/{name}/state`.
36-
func (self Handler) Install(ws *restful.WebService) {
36+
func (in Handler) Install(ws *restful.WebService) {
3737
ws.Route(
3838
ws.GET("/integration/{name}/state").
39-
To(self.handleGetState).
39+
To(in.handleGetState).
4040
Writes(api.IntegrationState{}))
4141
}
4242

43-
func (self Handler) handleGetState(request *restful.Request, response *restful.Response) {
43+
func (in Handler) handleGetState(request *restful.Request, response *restful.Response) {
4444
integrationName := request.PathParameter("name")
45-
state, err := self.manager.GetState(api.IntegrationID(integrationName))
45+
state, err := in.manager.GetState(api.IntegrationID(integrationName))
4646
if err != nil {
4747
response.AddHeader("Content-Type", "text/plain")
4848
_ = response.WriteErrorString(http.StatusInternalServerError, err.Error()+"\n")

modules/api/pkg/integration/manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func areErrorsEqual(err1, err2 error) bool {
3131
// Removes all quote signs that might have been added to the message.
3232
// Might depend on dependencies version how they are constructed.
3333
func normalize(msg string) string {
34-
return strings.Replace(msg, "\"", "", -1)
34+
return strings.ReplaceAll(msg, "\"", "")
3535
}
3636

3737
func TestNewIntegrationManager(t *testing.T) {
@@ -82,6 +82,6 @@ func TestIntegrationManager_GetState(t *testing.T) {
8282
func TestIntegrationManager_Metric(t *testing.T) {
8383
metricManager := integration.NewIntegrationManager().Metric()
8484
if metricManager == nil {
85-
t.Error("Failed to get metric manager.")
85+
t.Error("failed to get metric manager.")
8686
}
8787
}

modules/api/pkg/integration/metric/api/types.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ type MetricPoint struct {
127127
// Label stores information about identity of resources (UIDs) described by metric.
128128
type Label map[types.ResourceKind][]apimachinery.UID
129129

130-
// AddMetricLabel returns a unique combined Label of self and other resource.
130+
// AddMetricLabel returns a unique combined Label of in and other resource.
131131
// New label describes both resources.
132-
func (self Label) AddMetricLabel(other Label) Label {
132+
func (in Label) AddMetricLabel(other Label) Label {
133133
if other == nil {
134-
return self
134+
return in
135135
}
136136

137137
uniqueMap := map[apimachinery.UID]bool{}
138-
for _, v := range self {
138+
for _, v := range in {
139139
for _, t := range v {
140140
uniqueMap[t] = true
141141
}
@@ -144,11 +144,11 @@ func (self Label) AddMetricLabel(other Label) Label {
144144
for k, v := range other {
145145
for _, t := range v {
146146
if _, exists := uniqueMap[t]; !exists {
147-
self[k] = append(self[k], t)
147+
in[k] = append(in[k], t)
148148
}
149149
}
150150
}
151-
return self
151+
return in
152152
}
153153

154154
// Metric is a format of data used in this module. This is also the format of data that is being sent by backend client.
@@ -190,18 +190,18 @@ func (metric *SidecarMetric) AddMetricPoint(item MetricPoint) []MetricPoint {
190190
return metric.MetricPoints
191191
}
192192

193-
func (metric *Metric) AddMetricPoint(item MetricPoint) []MetricPoint {
194-
metric.MetricPoints = append(metric.MetricPoints, item)
195-
return metric.MetricPoints
193+
func (in *Metric) AddMetricPoint(item MetricPoint) []MetricPoint {
194+
in.MetricPoints = append(in.MetricPoints, item)
195+
return in.MetricPoints
196196
}
197197

198198
// String implements stringer interface to allow easy printing
199-
func (self Metric) String() string {
200-
return "{\nDataPoints: " + fmt.Sprintf("%v", self.DataPoints) +
201-
"\nMetricPoints: " + fmt.Sprintf("%v", self.MetricPoints) +
202-
"\nMetricName: " + self.MetricName +
203-
"\nLabel: " + fmt.Sprintf("%v", self.Label) +
204-
"\nAggregate: " + fmt.Sprintf("%v", self.Aggregate)
199+
func (in Metric) String() string {
200+
return "{\nDataPoints: " + fmt.Sprintf("%v", in.DataPoints) +
201+
"\nMetricPoints: " + fmt.Sprintf("%v", in.MetricPoints) +
202+
"\nMetricName: " + in.MetricName +
203+
"\nLabel: " + fmt.Sprintf("%v", in.Label) +
204+
"\nAggregate: " + fmt.Sprintf("%v", in.Aggregate)
205205
}
206206

207207
// MetricPromise is used for parallel data extraction. Contains len 1 channels for Metric and Error.
@@ -211,12 +211,12 @@ type MetricPromise struct {
211211
}
212212

213213
// GetMetric returns pointer to received Metrics and forwarded error (if any)
214-
func (self MetricPromise) GetMetric() (*Metric, error) {
215-
err := <-self.Error
214+
func (in MetricPromise) GetMetric() (*Metric, error) {
215+
err := <-in.Error
216216
if err != nil {
217217
return nil, err
218218
}
219-
return <-self.Metric, nil
219+
return <-in.Metric, nil
220220
}
221221

222222
// NewMetricPromise creates a MetricPromise structure with both channels of length 1.
@@ -231,10 +231,10 @@ type MetricPromises []MetricPromise
231231

232232
// GetMetrics returns all metrics from MetricPromises.
233233
// In case of no metrics were downloaded it does not initialise []Metric and returns nil.
234-
func (self MetricPromises) GetMetrics() ([]Metric, error) {
234+
func (in MetricPromises) GetMetrics() ([]Metric, error) {
235235
result := make([]Metric, 0)
236236

237-
for _, metricPromise := range self {
237+
for _, metricPromise := range in {
238238
metric, err := metricPromise.GetMetric()
239239
if err != nil {
240240
// Do not fail when cannot resolve one of the metrics promises and return what can be resolved.
@@ -252,8 +252,8 @@ func (self MetricPromises) GetMetrics() ([]Metric, error) {
252252
}
253253

254254
// PutMetrics forwards provided list of metrics to all channels. If provided err is not nil, error will be forwarded.
255-
func (self MetricPromises) PutMetrics(metrics []Metric, err error) {
256-
for i, metricPromise := range self {
255+
func (in MetricPromises) PutMetrics(metrics []Metric, err error) {
256+
for i, metricPromise := range in {
257257
if err != nil {
258258
metricPromise.Metric <- nil
259259
} else {

modules/api/pkg/integration/metric/manager.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,79 +52,79 @@ type metricManager struct {
5252
}
5353

5454
// AddClient implements metric manager interface. See MetricManager for more information.
55-
func (self *metricManager) AddClient(client metricapi.MetricClient) MetricManager {
55+
func (in *metricManager) AddClient(client metricapi.MetricClient) MetricManager {
5656
if client != nil {
57-
self.clients[client.ID()] = client
57+
in.clients[client.ID()] = client
5858
}
5959

60-
return self
60+
return in
6161
}
6262

6363
// Client implements metric manager interface. See MetricManager for more information.
64-
func (self *metricManager) Client() metricapi.MetricClient {
65-
return self.active
64+
func (in *metricManager) Client() metricapi.MetricClient {
65+
return in.active
6666
}
6767

6868
// Enable implements metric manager interface. See MetricManager for more information.
69-
func (self *metricManager) Enable(id integrationapi.IntegrationID) error {
70-
metricClient, exists := self.clients[id]
69+
func (in *metricManager) Enable(id integrationapi.IntegrationID) error {
70+
metricClient, exists := in.clients[id]
7171
if !exists {
72-
return fmt.Errorf("No metric client found for integration id: %s", id)
72+
return fmt.Errorf("no metric client found for integration id: %s", id)
7373
}
7474

7575
err := metricClient.HealthCheck()
7676
if err != nil {
77-
return fmt.Errorf("Health check failed: %s", err.Error())
77+
return fmt.Errorf("health check failed: %s", err.Error())
7878
}
7979

80-
self.active = metricClient
80+
in.active = metricClient
8181
return nil
8282
}
8383

8484
// EnableWithRetry implements metric manager interface. See MetricManager for more information.
85-
func (self *metricManager) EnableWithRetry(id integrationapi.IntegrationID, period time.Duration) {
85+
func (in *metricManager) EnableWithRetry(id integrationapi.IntegrationID, period time.Duration) {
8686
go wait.Forever(func() {
87-
metricClient, exists := self.clients[id]
87+
metricClient, exists := in.clients[id]
8888
if !exists {
8989
klog.V(5).InfoS("Metric client does not exist", "clientID", id)
9090
return
9191
}
9292

9393
err := metricClient.HealthCheck()
9494
if err != nil {
95-
self.active = nil
95+
in.active = nil
9696
klog.Errorf("Metric client health check failed: %s. Retrying in %d seconds.", err, period)
9797
return
9898
}
9999

100-
if self.active == nil {
100+
if in.active == nil {
101101
klog.V(1).Infof("Successful request to %s", id)
102-
self.active = metricClient
102+
in.active = metricClient
103103
}
104104
}, period*time.Second)
105105
}
106106

107107
// List implements metric manager interface. See MetricManager for more information.
108-
func (self *metricManager) List() []integrationapi.Integration {
108+
func (in *metricManager) List() []integrationapi.Integration {
109109
result := make([]integrationapi.Integration, 0)
110-
for _, c := range self.clients {
110+
for _, c := range in.clients {
111111
result = append(result, c.(integrationapi.Integration))
112112
}
113113

114114
return result
115115
}
116116

117117
// ConfigureSidecar implements metric manager interface. See MetricManager for more information.
118-
func (self *metricManager) ConfigureSidecar(host string) MetricManager {
118+
func (in *metricManager) ConfigureSidecar(host string) MetricManager {
119119
inClusterClient := client.InClusterClient()
120120
metricClient, err := sidecar.CreateSidecarClient(host, inClusterClient)
121121
if err != nil {
122122
klog.Errorf("There was an error during sidecar client creation: %s", err.Error())
123-
return self
123+
return in
124124
}
125125

126-
self.clients[metricClient.ID()] = metricClient
127-
return self
126+
in.clients[metricClient.ID()] = metricClient
127+
return in
128128
}
129129

130130
// NewMetricManager creates metric manager.

modules/api/pkg/integration/metric/manager_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,25 @@ func (FakeMetricClient) ID() integrationapi.IntegrationID {
3333
return fakeMetricClientID
3434
}
3535

36-
func (self FakeMetricClient) HealthCheck() error {
37-
if self.healthOk {
36+
func (in FakeMetricClient) HealthCheck() error {
37+
if in.healthOk {
3838
return nil
3939
}
4040

4141
return errors.NewInvalid("test-error")
4242
}
4343

44-
func (self FakeMetricClient) DownloadMetric(selectors []api.ResourceSelector, metricName string,
44+
func (in FakeMetricClient) DownloadMetric(selectors []api.ResourceSelector, metricName string,
4545
cachedResources *api.CachedResources) api.MetricPromises {
4646
return nil
4747
}
4848

49-
func (self FakeMetricClient) DownloadMetrics(selectors []api.ResourceSelector, metricNames []string,
49+
func (in FakeMetricClient) DownloadMetrics(selectors []api.ResourceSelector, metricNames []string,
5050
cachedResources *api.CachedResources) api.MetricPromises {
5151
return nil
5252
}
5353

54-
func (self FakeMetricClient) AggregateMetrics(metrics api.MetricPromises, metricName string,
54+
func (in FakeMetricClient) AggregateMetrics(metrics api.MetricPromises, metricName string,
5555
aggregations api.AggregationModes) api.MetricPromises {
5656
return nil
5757
}
@@ -95,7 +95,7 @@ func TestMetricManager_Enable(t *testing.T) {
9595
client api.MetricClient
9696
expected error
9797
}{
98-
{&FakeMetricClient{healthOk: false}, errors.NewInvalid("Health check failed: test-error")},
98+
{&FakeMetricClient{healthOk: false}, errors.NewInvalid("health check failed: test-error")},
9999
{&FakeMetricClient{healthOk: true}, nil},
100100
}
101101

0 commit comments

Comments
 (0)