-
Notifications
You must be signed in to change notification settings - Fork 81
[SREP-618] feat: Add /config endpoint integration with lazy loading #899
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
Open
smarthall
wants to merge
1
commit into
openshift:main
Choose a base branch
from
smarthall:feat/config_endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,236
−10
Open
Changes from all commits
Commits
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,129 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
|
|
||
| BackplaneApi "github.com/openshift/backplane-api/pkg/client" | ||
| "github.com/openshift/backplane-cli/pkg/cli/config" | ||
| "github.com/openshift/backplane-cli/pkg/info" | ||
| "github.com/openshift/backplane-cli/pkg/ocm" | ||
| logger "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| func newRefreshCmd() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "refresh", | ||
| Short: "Refresh configuration from backplane-api server", | ||
| Long: `Fetch the latest configuration values from the backplane-api server and update the local config file. | ||
|
|
||
| This command will overwrite the following server-managed configuration values: | ||
| - jira-base-url | ||
| - assume-initial-arn | ||
| - prod-env-name | ||
| - jira-config-for-access-requests | ||
|
|
||
| User-defined values (proxy-url, session-dir, etc.) will not be modified.`, | ||
| SilenceUsage: true, | ||
| Args: cobra.NoArgs, | ||
| RunE: runRefresh, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runRefresh(cmd *cobra.Command, args []string) error { | ||
| logger.Info("Refreshing configuration from backplane-api...") | ||
|
|
||
| // Get current config to determine backplane URL | ||
| bpConfig, err := config.GetBackplaneConfiguration() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to load current configuration: %w", err) | ||
| } | ||
|
|
||
| // Get OCM access token | ||
| token, err := ocm.DefaultOCMInterface.GetOCMAccessToken() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get OCM access token: %w", err) | ||
| } | ||
|
|
||
| // Create backplane API client | ||
| client, err := BackplaneApi.NewClient(bpConfig.URL, func(c *BackplaneApi.Client) error { | ||
| c.RequestEditors = append(c.RequestEditors, func(ctx context.Context, req *http.Request) error { | ||
| req.Header.Add("Authorization", "Bearer "+*token) | ||
| req.Header.Set("User-Agent", "backplane-cli"+info.Version) | ||
| req.Header.Set("Backplane-Version", info.DefaultInfoService.GetVersion()) | ||
| return nil | ||
| }) | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create backplane API client: %w", err) | ||
| } | ||
|
|
||
| // Fetch fresh config from API | ||
| remoteConfig, err := config.DefaultAPIClient.FetchRemoteConfig(client) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to fetch configuration from server: %w", err) | ||
| } | ||
|
|
||
| // Get config file path | ||
| configPath, err := config.GetConfigFilePath() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get config file path: %w", err) | ||
| } | ||
|
|
||
| // Reset viper to clear any defaults that were set by GetBackplaneConfiguration | ||
| // This prevents defaults like 'govcloud' from leaking into the user's config file | ||
| viper.Reset() | ||
|
|
||
| // Load existing config file to preserve user settings | ||
| viper.SetConfigFile(configPath) | ||
| viper.SetConfigType("json") | ||
| if err := viper.ReadInConfig(); err != nil { | ||
| // If config doesn't exist, that's okay - we'll create it | ||
| logger.Debugf("No existing config file found, will create new one") | ||
| } | ||
|
|
||
| // Update only server-managed values | ||
| updated := false | ||
| if remoteConfig.JiraBaseURL != nil { | ||
| viper.Set(config.JiraBaseURLKey, *remoteConfig.JiraBaseURL) | ||
| logger.Infof("Updated jira-base-url: %s", *remoteConfig.JiraBaseURL) | ||
| updated = true | ||
| } | ||
|
|
||
| if remoteConfig.AssumeInitialArn != nil { | ||
| viper.Set(config.AssumeInitialArnKey, *remoteConfig.AssumeInitialArn) | ||
| logger.Infof("Updated assume-initial-arn: %s", *remoteConfig.AssumeInitialArn) | ||
| updated = true | ||
| } | ||
|
|
||
| if remoteConfig.ProdEnvName != nil { | ||
| viper.Set(config.ProdEnvNameKey, *remoteConfig.ProdEnvName) | ||
| logger.Infof("Updated prod-env-name: %s", *remoteConfig.ProdEnvName) | ||
| updated = true | ||
| } | ||
|
|
||
| if remoteConfig.JiraConfigForAccessRequests != nil { | ||
| viper.Set(config.JiraConfigForAccessRequestsKey, remoteConfig.JiraConfigForAccessRequests) | ||
| logger.Info("Updated jira-config-for-access-requests") | ||
| updated = true | ||
| } | ||
|
|
||
| if !updated { | ||
| logger.Warn("No configuration values were returned from the server") | ||
| } | ||
|
|
||
| // Write updated config to file | ||
| if err := viper.WriteConfigAs(configPath); err != nil { | ||
| return fmt.Errorf("failed to write config file: %w", err) | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| fmt.Printf("Configuration refreshed successfully and saved to %s\n", configPath) | ||
| return nil | ||
| } | ||
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.