1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
//
// Copyright (c) 2015-2024 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package madmin
import (
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"path"
"strconv"
"time"
)
// tierAPI is API path prefix for tier related admin APIs
const tierAPI = "tier"
// AddTierIgnoreInUse adds a new remote tier, ignoring if it's being used by another MinIO deployment.
func (adm *AdminClient) AddTierIgnoreInUse(ctx context.Context, cfg *TierConfig) error {
return adm.addTier(ctx, cfg, true)
}
// AddTier adds a new remote tier.
func (adm *AdminClient) addTier(ctx context.Context, cfg *TierConfig, ignoreInUse bool) error {
data, err := json.Marshal(cfg)
if err != nil {
return err
}
encData, err := EncryptData(adm.getSecretKey(), data)
if err != nil {
return err
}
queryVals := url.Values{}
queryVals.Set("force", strconv.FormatBool(ignoreInUse))
reqData := requestData{
relPath: path.Join(adminAPIPrefix, tierAPI),
content: encData,
queryValues: queryVals,
}
// Execute PUT on /minio/admin/v3/tier to add a remote tier
resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
defer closeResponse(resp)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return httpRespToErrorResponse(resp)
}
return nil
}
// AddTier adds a new remote tier.
func (adm *AdminClient) AddTier(ctx context.Context, cfg *TierConfig) error {
return adm.addTier(ctx, cfg, false)
}
// ListTiers returns a list of remote tiers configured.
func (adm *AdminClient) ListTiers(ctx context.Context) ([]*TierConfig, error) {
reqData := requestData{
relPath: path.Join(adminAPIPrefix, tierAPI),
}
// Execute GET on /minio/admin/v3/tier to list remote tiers configured.
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
defer closeResponse(resp)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, httpRespToErrorResponse(resp)
}
var tiers []*TierConfig
b, err := io.ReadAll(resp.Body)
if err != nil {
return tiers, err
}
err = json.Unmarshal(b, &tiers)
if err != nil {
return tiers, err
}
return tiers, nil
}
// TierCreds is used to pass remote tier credentials in a tier-edit operation.
type TierCreds struct {
AccessKey string `json:"access,omitempty"`
SecretKey string `json:"secret,omitempty"`
AWSRole bool `json:"awsrole"`
AWSRoleWebIdentityTokenFile string `json:"awsroleWebIdentity,omitempty"`
AWSRoleARN string `json:"awsroleARN,omitempty"`
AzSP ServicePrincipalAuth `json:"azSP,omitempty"`
CredsJSON []byte `json:"creds,omitempty"`
}
// EditTier supports updating credentials for the remote tier identified by tierName.
func (adm *AdminClient) EditTier(ctx context.Context, tierName string, creds TierCreds) error {
data, err := json.Marshal(creds)
if err != nil {
return err
}
var encData []byte
encData, err = EncryptData(adm.getSecretKey(), data)
if err != nil {
return err
}
reqData := requestData{
relPath: path.Join(adminAPIPrefix, tierAPI, tierName),
content: encData,
}
// Execute POST on /minio/admin/v3/tier/tierName to edit a tier
// configured.
resp, err := adm.executeMethod(ctx, http.MethodPost, reqData)
defer closeResponse(resp)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return httpRespToErrorResponse(resp)
}
return nil
}
// RemoveTier removes an empty tier identified by tierName
func (adm *AdminClient) RemoveTier(ctx context.Context, tierName string) error {
if tierName == "" {
return ErrTierNameEmpty
}
reqData := requestData{
relPath: path.Join(adminAPIPrefix, tierAPI, tierName),
}
// Execute DELETE on /minio/admin/v3/tier/tierName to remove an empty tier.
resp, err := adm.executeMethod(ctx, http.MethodDelete, reqData)
defer closeResponse(resp)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return httpRespToErrorResponse(resp)
}
return nil
}
// RemoveTierOpts - options for a remote tiering removal
type RemoveTierOpts struct {
Force bool
}
// RemoveTierV2 removes an empty tier identified by tierName, the tier is not
// required to be reachable or empty if force flag is set to true.
func (adm *AdminClient) RemoveTierV2(ctx context.Context, tierName string, opts RemoveTierOpts) error {
if tierName == "" {
return ErrTierNameEmpty
}
queryVals := url.Values{}
queryVals.Set("force", strconv.FormatBool(opts.Force))
reqData := requestData{
relPath: path.Join(adminAPIPrefix, tierAPI, tierName),
queryValues: queryVals,
}
// Execute DELETE on /minio/admin/v3/tier/tierName to remove an empty tier.
resp, err := adm.executeMethod(ctx, http.MethodDelete, reqData)
defer closeResponse(resp)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return httpRespToErrorResponse(resp)
}
return nil
}
// VerifyTier verifies tierName's remote tier config
func (adm *AdminClient) VerifyTier(ctx context.Context, tierName string) error {
if tierName == "" {
return ErrTierNameEmpty
}
reqData := requestData{
relPath: path.Join(adminAPIPrefix, tierAPI, tierName),
}
// Execute GET on /minio/admin/v3/tier/tierName to verify tierName's config.
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
defer closeResponse(resp)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return httpRespToErrorResponse(resp)
}
return nil
}
// TierInfo contains tier name, type and statistics
type TierInfo struct {
Name string
Type string
Stats TierStats
DailyStats DailyTierStats
}
type DailyTierStats struct {
Bins [24]TierStats
UpdatedAt time.Time
}
// TierStats returns per-tier stats of all configured tiers (incl. internal
// hot-tier)
func (adm *AdminClient) TierStats(ctx context.Context) ([]TierInfo, error) {
reqData := requestData{
relPath: path.Join(adminAPIPrefix, "tier-stats"),
}
// Execute GET on /minio/admin/v3/tier-stats to list tier-stats.
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
defer closeResponse(resp)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, httpRespToErrorResponse(resp)
}
var tierInfos []TierInfo
b, err := io.ReadAll(resp.Body)
if err != nil {
return tierInfos, err
}
err = json.Unmarshal(b, &tierInfos)
if err != nil {
return tierInfos, err
}
return tierInfos, nil
}
|