-
Notifications
You must be signed in to change notification settings - Fork 667
HELM-611: add OCI registry client for chart operations #15925
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
sowmya-sl
wants to merge
2
commits into
openshift:main
Choose a base branch
from
sowmya-sl:add-oci-support
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.
+178
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,42 @@ | ||
| package actions | ||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "helm.sh/helm/v3/pkg/action" | ||
| "helm.sh/helm/v3/pkg/registry" | ||
| ) | ||
|
|
||
| // newRegistryClient is a package-level variable to allow mocking in tests | ||
| var newRegistryClient = registry.NewClient | ||
|
|
||
| func GetDefaultOCIRegistry(conf *action.Configuration) error { | ||
| return GetOCIRegistry(conf, false, false) | ||
| } | ||
|
|
||
| func GetOCIRegistry(conf *action.Configuration, skipTLSVerify bool, plainHTTP bool) error { | ||
| if conf == nil { | ||
| return fmt.Errorf("action configuration cannot be nil") | ||
| } | ||
| opts := []registry.ClientOption{ | ||
| registry.ClientOptDebug(false), | ||
| } | ||
| if plainHTTP { | ||
| opts = append(opts, registry.ClientOptPlainHTTP()) | ||
| } | ||
| if skipTLSVerify { | ||
| transport := http.DefaultTransport.(*http.Transport).Clone() | ||
| transport.TLSClientConfig = &tls.Config{ | ||
| InsecureSkipVerify: true, | ||
| } | ||
| opts = append(opts, registry.ClientOptHTTPClient(&http.Client{Transport: transport})) | ||
| } | ||
| registryClient, err := newRegistryClient(opts...) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create registry client: %w", err) | ||
| } | ||
| conf.RegistryClient = registryClient | ||
| return nil | ||
| } |
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,132 @@ | ||
| package actions | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "helm.sh/helm/v3/pkg/action" | ||
| "helm.sh/helm/v3/pkg/chartutil" | ||
| kubefake "helm.sh/helm/v3/pkg/kube/fake" | ||
| "helm.sh/helm/v3/pkg/registry" | ||
| "helm.sh/helm/v3/pkg/storage" | ||
| "helm.sh/helm/v3/pkg/storage/driver" | ||
| ) | ||
|
|
||
| func TestGetDefaultOCIRegistry_Success(t *testing.T) { | ||
| store := storage.Init(driver.NewMemory()) | ||
| conf := &action.Configuration{ | ||
| RESTClientGetter: FakeConfig{}, | ||
| Releases: store, | ||
| KubeClient: &kubefake.PrintingKubeClient{Out: io.Discard}, | ||
| Capabilities: chartutil.DefaultCapabilities, | ||
| } | ||
| require.Nil(t, conf.RegistryClient, "Registry Client should be nil") | ||
|
|
||
| // Store original values | ||
| originalReleases := conf.Releases | ||
| originalKubeClient := conf.KubeClient | ||
| originalCapabilities := conf.Capabilities | ||
|
|
||
| err := GetDefaultOCIRegistry(conf) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, conf.RegistryClient, "Registry Client should not be nil") | ||
|
|
||
| // Verify other configuration fields are not modified. | ||
| require.Equal(t, originalReleases, conf.Releases, "Releases should not be modified") | ||
| require.Equal(t, originalKubeClient, conf.KubeClient, "KubeClient should not be modified") | ||
| require.Equal(t, originalCapabilities, conf.Capabilities, "Capabilities should not be modified") | ||
|
|
||
| } | ||
|
|
||
| func TestGetOCIRegistry_NilConfig(t *testing.T) { | ||
| err := GetOCIRegistry(nil, false, false) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "action configuration cannot be nil") | ||
| } | ||
|
|
||
| func TestGetOCIRegistry_Success(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| skipTLSVerify bool | ||
| plainHTTP bool | ||
| }{ | ||
| { | ||
| name: "default options", | ||
| skipTLSVerify: false, | ||
| plainHTTP: false, | ||
| }, | ||
| { | ||
| name: "with skipTLSVerify", | ||
| skipTLSVerify: true, | ||
| plainHTTP: false, | ||
| }, | ||
| { | ||
| name: "with plainHTTP", | ||
| skipTLSVerify: false, | ||
| plainHTTP: true, | ||
| }, | ||
| { | ||
| name: "with both skipTLSVerify and plainHTTP", | ||
| skipTLSVerify: true, | ||
| plainHTTP: true, | ||
| }, | ||
| } | ||
| originalNewRegistryClient := newRegistryClient | ||
| defer func() { | ||
| newRegistryClient = originalNewRegistryClient | ||
| }() | ||
|
|
||
| for _, tt := range tests { | ||
| newRegistryClient = func(options ...registry.ClientOption) (*registry.Client, error) { | ||
| count := 0 | ||
| if tt.plainHTTP { | ||
| count += 1 | ||
| } | ||
| if tt.skipTLSVerify { | ||
| count += 1 | ||
| } | ||
| require.Equal(t, count, len(options)-1, "Expected %d options, got %d", count, len(options)) | ||
| return ®istry.Client{}, nil | ||
| } | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| store := storage.Init(driver.NewMemory()) | ||
| conf := &action.Configuration{ | ||
| RESTClientGetter: FakeConfig{}, | ||
| Releases: store, | ||
| KubeClient: &kubefake.PrintingKubeClient{Out: io.Discard}, | ||
| Capabilities: chartutil.DefaultCapabilities, | ||
| } | ||
| require.Nil(t, conf.RegistryClient, "Registry Client should be nil initially") | ||
|
|
||
| err := GetOCIRegistry(conf, tt.skipTLSVerify, tt.plainHTTP) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, conf.RegistryClient, "Registry Client should not be nil after GetOCIRegistry") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestGetOCIRegistry_NewClientError(t *testing.T) { | ||
|
Comment on lines
+48
to
+110
This comment was marked as resolved.
Sorry, something went wrong.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added this in the test. |
||
| // Save original function and restore after test | ||
| originalNewRegistryClient := newRegistryClient | ||
| defer func() { newRegistryClient = originalNewRegistryClient }() | ||
|
|
||
| // Mock newRegistryClient to return an error | ||
| newRegistryClient = func(options ...registry.ClientOption) (*registry.Client, error) { | ||
| return nil, errors.New("mock registry client error") | ||
| } | ||
|
|
||
| store := storage.Init(driver.NewMemory()) | ||
| conf := &action.Configuration{ | ||
| RESTClientGetter: FakeConfig{}, | ||
| Releases: store, | ||
| KubeClient: &kubefake.PrintingKubeClient{Out: io.Discard}, | ||
| Capabilities: chartutil.DefaultCapabilities, | ||
| } | ||
|
|
||
| err := GetOCIRegistry(conf, false, false) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "failed to create registry client") | ||
| require.Contains(t, err.Error(), "mock registry client error") | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My apologies, @sowmya-sl, but I didn't fully appreciate what you had accomplished here!
So, I now have to ask: why implement
GetDefaultOCIRegistry()as a separate function, or, more to the point, why call it from here? Why not in-line or call it insideconf.Init()?Also, as an aside (since it's arguably outside the scope of this PR), my IDE notes that the code is ignoring the error returned by
conf.Init(). 😞 We should probably not be doing that -- instead, the code should be returningnil(and maybe anerror) and letting the caller address the situation. But, I don't know if you're up for trying to fix that, now (it affects a dozen callers, as you presumably know well).