|
| 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 | +} |
0 commit comments