-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helm: use chunking release driver in systemNamespace
Signed-off-by: Joe Lanford <joe.lanford@gmail.com>
- Loading branch information
1 parent
0796ecf
commit 011246e
Showing
4 changed files
with
132 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,113 @@ | ||
package action | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"helm.sh/helm/v3/pkg/storage/driver" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/fields" | ||
k8slabels "k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/watch" | ||
clientcorev1 "k8s.io/client-go/kubernetes/typed/core/v1" | ||
"k8s.io/client-go/rest" | ||
crcache "sigs.k8s.io/controller-runtime/pkg/cache" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
|
||
helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client" | ||
"github.com/operator-framework/helm-operator-plugins/pkg/storage" | ||
) | ||
|
||
func ChunkedStorageDriverMapper(mgr manager.Manager, releaseStorageNamespace string) (helmclient.ObjectToStorageDriverMapper, error) { | ||
corev1Client, err := clientcorev1.NewForConfig(mgr.GetConfig()) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create corev1 client: %w", err) | ||
} | ||
secretsClient := newSecretsDelegatingClient(corev1Client.Secrets(releaseStorageNamespace), releaseStorageNamespace, mgr.GetCache()) | ||
|
||
return func(ctx context.Context, object client.Object, config *rest.Config) (driver.Driver, error) { | ||
log := logf.FromContext(ctx).V(2) | ||
return storage.NewChunkedSecrets(secretsClient, "operator-controller", storage.ChunkedSecretsConfig{ | ||
ChunkSize: 1024 * 1024, | ||
MaxReadChunks: 10, | ||
MaxWriteChunks: 10, | ||
Log: func(format string, args ...interface{}) { log.Info(fmt.Sprintf(format, args...)) }, | ||
}), nil | ||
}, nil | ||
} | ||
|
||
var _ clientcorev1.SecretInterface = &secretsDelegatingClient{} | ||
|
||
type secretsDelegatingClient struct { | ||
clientcorev1.SecretInterface | ||
namespace string | ||
cache crcache.Cache | ||
} | ||
|
||
func newSecretsDelegatingClient(client clientcorev1.SecretInterface, namespace string, cache crcache.Cache) clientcorev1.SecretInterface { | ||
return &secretsDelegatingClient{ | ||
SecretInterface: client, | ||
namespace: namespace, | ||
cache: cache, | ||
} | ||
} | ||
|
||
func (s secretsDelegatingClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*corev1.Secret, error) { | ||
var secret corev1.Secret | ||
if err := s.cache.Get(ctx, client.ObjectKey{Namespace: s.namespace, Name: name}, &secret, &client.GetOptions{Raw: &opts}); err != nil { | ||
return nil, err | ||
} | ||
return &secret, nil | ||
} | ||
|
||
func (s secretsDelegatingClient) List(ctx context.Context, opts metav1.ListOptions) (*corev1.SecretList, error) { | ||
listOpts, err := metaOptionsToClientOptions(s.namespace, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var secrets corev1.SecretList | ||
if err := s.cache.List(ctx, &secrets, listOpts); err != nil { | ||
return nil, fmt.Errorf("error listing from cache: %w", err) | ||
} | ||
return &secrets, nil | ||
} | ||
|
||
func (s secretsDelegatingClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) { | ||
panic("intentionally not implemented: watch is not intended to be called") | ||
} | ||
|
||
func metaOptionsToClientOptions(namespace string, opts metav1.ListOptions) (*client.ListOptions, error) { | ||
clientListOptions := &client.ListOptions{ | ||
Namespace: namespace, | ||
Limit: opts.Limit, | ||
Continue: opts.Continue, | ||
} | ||
|
||
if opts.LabelSelector != "" { | ||
labelSelector, err := k8slabels.Parse(opts.LabelSelector) | ||
if err != nil { | ||
return nil, err | ||
} | ||
clientListOptions.LabelSelector = labelSelector | ||
} | ||
|
||
if opts.FieldSelector != "" { | ||
fieldSelector, err := fields.ParseSelector(opts.FieldSelector) | ||
if err != nil { | ||
return nil, err | ||
} | ||
clientListOptions.FieldSelector = fieldSelector | ||
} | ||
|
||
opts.LabelSelector = "" | ||
opts.FieldSelector = "" | ||
opts.Limit = 0 | ||
opts.Continue = "" | ||
clientListOptions.Raw = &opts | ||
|
||
return clientListOptions, nil | ||
} |