-
Notifications
You must be signed in to change notification settings - Fork 6
EVEREST-1853 PMM3 support #902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ce5968d
EVEREST-1853 PMM3 support
oksana-grishchenko 2fe0e56
use PMM_SERVER_TOKEN key
oksana-grishchenko 89feaaf
Handle secrets
oksana-grishchenko eab92da
move exposed methods between packages
oksana-grishchenko 9d97e78
use string version
oksana-grishchenko ef2c468
use client depending on the PMM version
oksana-grishchenko 217ef41
Merge branch 'main' into EVEREST-1853-PMM3-support
oksana-grishchenko cc94877
Merge branch 'main' into EVEREST-1853-PMM3-support
oksana-grishchenko 6abfea9
fix tests
oksana-grishchenko 39b9227
fix tests
oksana-grishchenko fd9d42e
fix tests
oksana-grishchenko 4bf8460
consistent var names
oksana-grishchenko 75c676d
address comments
oksana-grishchenko 9b41242
Merge branch 'main' into EVEREST-1853-PMM3-support
oksana-grishchenko f934fb4
Merge branch 'refs/heads/main' into EVEREST-1853-PMM3-support
oksana-grishchenko b944442
fix test
oksana-grishchenko 94fb076
add sleep 5
oksana-grishchenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| // everest-operator | ||
| // Copyright (C) 2022 Percona LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package v1alpha1 | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "crypto/tls" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| ) | ||
|
|
||
| type pmmErrorMessage struct { | ||
| Message string `json:"message"` | ||
| } | ||
|
|
||
| func createKey(ctx context.Context, hostname, apiKeyName string, auth iAuth, skipTLSVerify bool) (string, error) { | ||
| body := nameAndRoleMap(apiKeyName) | ||
| resp, err := doJSONRequest[struct { | ||
| Key string `json:"key"` | ||
| }](ctx, http.MethodPost, fmt.Sprintf("%s/graph/api/auth/keys", hostname), auth, body, skipTLSVerify) | ||
oksana-grishchenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return resp.Key, nil | ||
| } | ||
|
|
||
| func createServiceAccountAndToken(ctx context.Context, hostname, apiKeyName string, auth iAuth, skipTLSVerify bool) (string, error) { | ||
| // for transparency, use the same name for the generated service account and token | ||
| nameAndRole := nameAndRoleMap(apiKeyName) | ||
| account, err := doJSONRequest[struct { | ||
| Uid string `json:"uid"` | ||
| }](ctx, http.MethodPost, fmt.Sprintf("%s/graph/api/serviceaccounts", hostname), auth, nameAndRole, skipTLSVerify) | ||
oksana-grishchenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return "", err | ||
| } | ||
| token, err := doJSONRequest[struct { | ||
| Key string `json:"key"` | ||
| }](ctx, http.MethodPost, fmt.Sprintf("%s/graph/api/serviceaccounts/%s/tokens", hostname, account.Uid), auth, nameAndRole, skipTLSVerify) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return token.Key, nil | ||
| } | ||
|
|
||
| // makes an HTTP request using JSON content type | ||
oksana-grishchenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| func doJSONRequest[T any](ctx context.Context, method, url string, auth iAuth, body any, skipTLSVerify bool) (T, error) { | ||
| var zero T | ||
| b, err := json.Marshal(body) | ||
| if err != nil { | ||
| return zero, fmt.Errorf("marshal request: %w", err) | ||
| } | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(b)) | ||
| if err != nil { | ||
| return zero, fmt.Errorf("build request: %w", err) | ||
| } | ||
|
|
||
| req.Header.Set("Content-Type", "application/json; charset=utf-8") | ||
| if auth != nil { | ||
| auth.apply(req) | ||
| } | ||
| req.Close = true | ||
|
|
||
| httpClient := newHTTPClient(skipTLSVerify) | ||
| resp, err := httpClient.Do(req) | ||
| if err != nil { | ||
| return zero, fmt.Errorf("do request: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| data, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return zero, fmt.Errorf("read response: %w", err) | ||
| } | ||
|
|
||
| if resp.StatusCode >= http.StatusBadRequest { | ||
| var pmmErr *pmmErrorMessage | ||
| if err := json.Unmarshal(data, &pmmErr); err != nil { | ||
| return zero, errors.Join(err, fmt.Errorf("PMM returned an unknown error. HTTP %d", resp.StatusCode)) | ||
| } | ||
| return zero, fmt.Errorf("PMM returned an error: %s", pmmErr.Message) | ||
| } | ||
|
|
||
| var result T | ||
| if err := json.Unmarshal(data, &result); err != nil { | ||
| return zero, fmt.Errorf("unmarshal response: %w", err) | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| func nameAndRoleMap(name string) map[string]string { | ||
| return map[string]string{ | ||
| "name": name, | ||
| "role": "Admin", | ||
| } | ||
| } | ||
|
|
||
| func newHTTPClient(insecure bool) *http.Client { | ||
| client := http.DefaultClient | ||
| client.Transport = &http.Transport{ | ||
| TLSClientConfig: &tls.Config{ | ||
| InsecureSkipVerify: insecure, //nolint:gosec | ||
| }, | ||
| } | ||
| return client | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.