Skip to content

Commit 56087c6

Browse files
committed
Address review comments
1 parent 55c2bce commit 56087c6

File tree

4 files changed

+180
-128
lines changed

4 files changed

+180
-128
lines changed

functionaltests/internal/kbclient/client.go

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"fmt"
2828
"io"
2929
"net/http"
30-
"slices"
3130
"time"
3231
)
3332

@@ -236,108 +235,3 @@ func (c *Client) EnableIntegrationsServer(ctx context.Context) error {
236235

237236
return nil
238237
}
239-
240-
func (c *Client) ResolveMigrationDeprecations(ctx context.Context) error {
241-
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
242-
defer cancel()
243-
244-
deprecations, err := c.QueryCriticalESDeprecations(ctx)
245-
if err != nil {
246-
return fmt.Errorf("failed to query critical deprecations: %w", err)
247-
}
248-
249-
for _, deprecation := range deprecations {
250-
switch deprecation.Type {
251-
case "index_settings":
252-
if err = c.markIndexAsReadOnly(ctx, deprecation.Name); err != nil {
253-
return fmt.Errorf("failed to mark index as read-only: %w", err)
254-
}
255-
case "data_streams":
256-
if err = c.markDataStreamAsReadOnly(
257-
ctx,
258-
deprecation.Name,
259-
deprecation.CorrectiveAction.Metadata.IndicesRequiringUpgrade,
260-
); err != nil {
261-
return fmt.Errorf("failed to mark data stream as read-only: %w", err)
262-
}
263-
default:
264-
return fmt.Errorf("unknown deprecation type: %s", deprecation.Type)
265-
}
266-
}
267-
268-
return nil
269-
}
270-
271-
type MigrationDeprecation struct {
272-
Name string `json:"index"`
273-
Type string `json:"type"`
274-
IsCritical bool `json:"isCritical"`
275-
CorrectiveAction struct {
276-
Type string `json:"type"`
277-
Metadata struct {
278-
IndicesRequiringUpgrade []string `json:"indicesRequiringUpgrade,omitempty"`
279-
}
280-
} `json:"correctiveAction"`
281-
}
282-
283-
type esDeprecationsResponse struct {
284-
MigrationDeprecations []MigrationDeprecation `json:"migrationsDeprecations"`
285-
}
286-
287-
// QueryCriticalESDeprecations retrieves the critical deprecation warnings for Elasticsearch.
288-
// It is essentially equivalent to `GET _migration/deprecations`, but through Kibana Upgrade
289-
// Assistant API.
290-
func (c *Client) QueryCriticalESDeprecations(ctx context.Context) ([]MigrationDeprecation, error) {
291-
path := "/api/upgrade_assistant/es_deprecations"
292-
b, err := c.sendRequest(ctx, http.MethodGet, path, nil, nil)
293-
if err != nil {
294-
return nil, err
295-
}
296-
297-
var esDeprecationsResp esDeprecationsResponse
298-
if err = json.Unmarshal(b, &esDeprecationsResp); err != nil {
299-
return nil, fmt.Errorf("cannot unmarshal response body: %w", err)
300-
}
301-
302-
// Remove all non-critical deprecation info.
303-
return slices.DeleteFunc(
304-
esDeprecationsResp.MigrationDeprecations,
305-
func(dep MigrationDeprecation) bool {
306-
return !dep.IsCritical
307-
},
308-
), nil
309-
}
310-
311-
type upgradeAssistUpdateIndexRequest struct {
312-
Operations []string `json:"operations"`
313-
}
314-
315-
// markIndexAsReadOnly updates the index to read-only through the Upgrade Assistant API:
316-
// https://www.elastic.co/guide/en/kibana/current/upgrade-assistant.html.
317-
func (c *Client) markIndexAsReadOnly(ctx context.Context, index string) error {
318-
path := fmt.Sprintf("/api/upgrade_assistant/update_index/%s", index)
319-
req := upgradeAssistUpdateIndexRequest{
320-
Operations: []string{"blockWrite", "unfreeze"},
321-
}
322-
323-
_, err := c.sendRequest(ctx, http.MethodPost, path, req, nil)
324-
return err
325-
}
326-
327-
type upgradeAssistMigrateDSRequest struct {
328-
Indices []string `json:"indices"`
329-
}
330-
331-
// markDataStreamAsReadOnly marks the backing indices of the data stream as read-only
332-
// through the Upgrade Assistant API:
333-
// https://www.elastic.co/guide/en/kibana/current/upgrade-assistant.html.
334-
func (c *Client) markDataStreamAsReadOnly(ctx context.Context, dataStream string, indices []string) error {
335-
// Data stream
336-
path := fmt.Sprintf("/api/upgrade_assistant/migrate_data_stream/%s/readonly", dataStream)
337-
req := upgradeAssistMigrateDSRequest{
338-
Indices: indices,
339-
}
340-
341-
_, err := c.sendRequest(ctx, http.MethodPost, path, req, nil)
342-
return err
343-
}

functionaltests/internal/kbclient/client_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -149,25 +149,3 @@ func TestClient_UpdatePackagePolicyByID(t *testing.T) {
149149
require.NoError(t, err)
150150
assert.Equal(t, "Hello World", policy.Description)
151151
}
152-
153-
func TestClient_ResolveMigrationDeprecations(t *testing.T) {
154-
kbc := newRecordedTestClient(t)
155-
156-
ctx := context.Background()
157-
// Check that there are some critical deprecation warnings.
158-
deprecations, err := kbc.QueryCriticalESDeprecations(ctx)
159-
require.NoError(t, err)
160-
require.Greater(t, len(deprecations), 0)
161-
for _, deprecation := range deprecations {
162-
require.True(t, deprecation.IsCritical)
163-
}
164-
165-
// Resolve them.
166-
err = kbc.ResolveMigrationDeprecations(ctx)
167-
require.NoError(t, err)
168-
169-
// Check that there are no more.
170-
deprecations, err = kbc.QueryCriticalESDeprecations(ctx)
171-
require.NoError(t, err)
172-
assert.Len(t, deprecations, 0)
173-
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package kbclient
19+
20+
import (
21+
"context"
22+
"encoding/json"
23+
"errors"
24+
"fmt"
25+
"net/http"
26+
"slices"
27+
"time"
28+
)
29+
30+
func (c *Client) ResolveMigrationDeprecations(ctx context.Context) error {
31+
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
32+
defer cancel()
33+
34+
deprecations, err := c.QueryCriticalESDeprecations(ctx)
35+
if err != nil {
36+
return fmt.Errorf("failed to query critical deprecations: %w", err)
37+
}
38+
39+
var errs []error
40+
for _, deprecation := range deprecations {
41+
switch deprecation.Type {
42+
case "index_settings":
43+
errs = append(errs, c.markIndexAsReadOnly(ctx, deprecation.Name))
44+
case "data_streams":
45+
errs = append(errs, c.markDataStreamAsReadOnly(
46+
ctx,
47+
deprecation.Name,
48+
deprecation.CorrectiveAction.Metadata.IndicesRequiringUpgrade,
49+
))
50+
default:
51+
errs = append(errs, fmt.Errorf("unknown deprecation type: %s", deprecation.Type))
52+
}
53+
}
54+
55+
return errors.Join(errs...)
56+
}
57+
58+
type MigrationDeprecation struct {
59+
Name string `json:"index"`
60+
Type string `json:"type"`
61+
IsCritical bool `json:"isCritical"`
62+
CorrectiveAction struct {
63+
Type string `json:"type"`
64+
Metadata struct {
65+
IndicesRequiringUpgrade []string `json:"indicesRequiringUpgrade,omitempty"`
66+
}
67+
} `json:"correctiveAction"`
68+
}
69+
70+
type esDeprecationsResponse struct {
71+
MigrationDeprecations []MigrationDeprecation `json:"migrationsDeprecations"`
72+
}
73+
74+
// QueryCriticalESDeprecations retrieves the critical deprecation warnings for Elasticsearch.
75+
// It is essentially equivalent to `GET _migration/deprecations`, but through Kibana Upgrade
76+
// Assistant API.
77+
func (c *Client) QueryCriticalESDeprecations(ctx context.Context) ([]MigrationDeprecation, error) {
78+
path := "/api/upgrade_assistant/es_deprecations"
79+
b, err := c.sendRequest(ctx, http.MethodGet, path, nil, nil)
80+
if err != nil {
81+
return nil, err
82+
}
83+
84+
var esDeprecationsResp esDeprecationsResponse
85+
if err = json.Unmarshal(b, &esDeprecationsResp); err != nil {
86+
return nil, fmt.Errorf("cannot unmarshal response body: %w", err)
87+
}
88+
89+
// Remove all non-critical deprecation info.
90+
return slices.DeleteFunc(
91+
esDeprecationsResp.MigrationDeprecations,
92+
func(dep MigrationDeprecation) bool {
93+
return !dep.IsCritical
94+
},
95+
), nil
96+
}
97+
98+
type upgradeAssistUpdateIndexRequest struct {
99+
Operations []string `json:"operations"`
100+
}
101+
102+
// markIndexAsReadOnly updates the index to read-only through the Upgrade Assistant API:
103+
// https://www.elastic.co/guide/en/kibana/current/upgrade-assistant.html.
104+
func (c *Client) markIndexAsReadOnly(ctx context.Context, index string) error {
105+
path := fmt.Sprintf("/api/upgrade_assistant/update_index/%s", index)
106+
req := upgradeAssistUpdateIndexRequest{
107+
Operations: []string{"blockWrite", "unfreeze"},
108+
}
109+
110+
_, err := c.sendRequest(ctx, http.MethodPost, path, req, nil)
111+
return err
112+
}
113+
114+
type upgradeAssistMigrateDSRequest struct {
115+
Indices []string `json:"indices"`
116+
}
117+
118+
// markDataStreamAsReadOnly marks the backing indices of the data stream as read-only
119+
// through the Upgrade Assistant API:
120+
// https://www.elastic.co/guide/en/kibana/current/upgrade-assistant.html.
121+
func (c *Client) markDataStreamAsReadOnly(ctx context.Context, dataStream string, indices []string) error {
122+
// Data stream
123+
path := fmt.Sprintf("/api/upgrade_assistant/migrate_data_stream/%s/readonly", dataStream)
124+
req := upgradeAssistMigrateDSRequest{
125+
Indices: indices,
126+
}
127+
128+
_, err := c.sendRequest(ctx, http.MethodPost, path, req, nil)
129+
return err
130+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package kbclient_test
19+
20+
import (
21+
"context"
22+
"testing"
23+
24+
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
func TestClient_ResolveMigrationDeprecations(t *testing.T) {
29+
kbc := newRecordedTestClient(t)
30+
31+
ctx := context.Background()
32+
// Check that there are some critical deprecation warnings.
33+
deprecations, err := kbc.QueryCriticalESDeprecations(ctx)
34+
require.NoError(t, err)
35+
require.Greater(t, len(deprecations), 0)
36+
for _, deprecation := range deprecations {
37+
require.True(t, deprecation.IsCritical)
38+
}
39+
40+
// Resolve them.
41+
// NOTE: This test will not be effective against different deprecation
42+
// types than what was initially set.
43+
err = kbc.ResolveMigrationDeprecations(ctx)
44+
require.NoError(t, err)
45+
46+
// Check that there are no more.
47+
deprecations, err = kbc.QueryCriticalESDeprecations(ctx)
48+
require.NoError(t, err)
49+
assert.Len(t, deprecations, 0)
50+
}

0 commit comments

Comments
 (0)