|
| 1 | +package action |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "helm.sh/helm/v3/pkg/storage/driver" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/fields" |
| 11 | + k8slabels "k8s.io/apimachinery/pkg/labels" |
| 12 | + "k8s.io/apimachinery/pkg/watch" |
| 13 | + clientcorev1 "k8s.io/client-go/kubernetes/typed/core/v1" |
| 14 | + "k8s.io/client-go/rest" |
| 15 | + crcache "sigs.k8s.io/controller-runtime/pkg/cache" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 19 | + |
| 20 | + helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client" |
| 21 | + "github.com/operator-framework/helm-operator-plugins/pkg/storage" |
| 22 | +) |
| 23 | + |
| 24 | +func ChunkedStorageDriverMapper(mgr manager.Manager, releaseStorageNamespace string) (helmclient.ObjectToStorageDriverMapper, error) { |
| 25 | + corev1Client, err := clientcorev1.NewForConfig(mgr.GetConfig()) |
| 26 | + if err != nil { |
| 27 | + return nil, fmt.Errorf("unable to create corev1 client: %w", err) |
| 28 | + } |
| 29 | + secretsClient := newSecretsDelegatingClient(corev1Client.Secrets(releaseStorageNamespace), releaseStorageNamespace, mgr.GetCache()) |
| 30 | + |
| 31 | + return func(ctx context.Context, object client.Object, config *rest.Config) (driver.Driver, error) { |
| 32 | + log := logf.FromContext(ctx).V(2) |
| 33 | + return storage.NewChunkedSecrets(secretsClient, "operator-controller", storage.ChunkedSecretsConfig{ |
| 34 | + ChunkSize: 1024 * 1024, |
| 35 | + MaxReadChunks: 10, |
| 36 | + MaxWriteChunks: 10, |
| 37 | + Log: func(format string, args ...interface{}) { log.Info(fmt.Sprintf(format, args...)) }, |
| 38 | + }), nil |
| 39 | + }, nil |
| 40 | +} |
| 41 | + |
| 42 | +var _ clientcorev1.SecretInterface = &secretsDelegatingClient{} |
| 43 | + |
| 44 | +type secretsDelegatingClient struct { |
| 45 | + clientcorev1.SecretInterface |
| 46 | + namespace string |
| 47 | + cache crcache.Cache |
| 48 | +} |
| 49 | + |
| 50 | +func newSecretsDelegatingClient(client clientcorev1.SecretInterface, namespace string, cache crcache.Cache) clientcorev1.SecretInterface { |
| 51 | + return &secretsDelegatingClient{ |
| 52 | + SecretInterface: client, |
| 53 | + namespace: namespace, |
| 54 | + cache: cache, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func (s secretsDelegatingClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*corev1.Secret, error) { |
| 59 | + var secret corev1.Secret |
| 60 | + if err := s.cache.Get(ctx, client.ObjectKey{Namespace: s.namespace, Name: name}, &secret, &client.GetOptions{Raw: &opts}); err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + return &secret, nil |
| 64 | +} |
| 65 | + |
| 66 | +func (s secretsDelegatingClient) List(ctx context.Context, opts metav1.ListOptions) (*corev1.SecretList, error) { |
| 67 | + listOpts, err := metaOptionsToClientOptions(s.namespace, opts) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + |
| 72 | + var secrets corev1.SecretList |
| 73 | + if err := s.cache.List(ctx, &secrets, listOpts); err != nil { |
| 74 | + return nil, fmt.Errorf("error listing from cache: %w", err) |
| 75 | + } |
| 76 | + return &secrets, nil |
| 77 | +} |
| 78 | + |
| 79 | +func (s secretsDelegatingClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) { |
| 80 | + panic("intentionally not implemented: watch is not intended to be called") |
| 81 | +} |
| 82 | + |
| 83 | +func metaOptionsToClientOptions(namespace string, opts metav1.ListOptions) (*client.ListOptions, error) { |
| 84 | + clientListOptions := &client.ListOptions{ |
| 85 | + Namespace: namespace, |
| 86 | + Limit: opts.Limit, |
| 87 | + Continue: opts.Continue, |
| 88 | + } |
| 89 | + |
| 90 | + if opts.LabelSelector != "" { |
| 91 | + labelSelector, err := k8slabels.Parse(opts.LabelSelector) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + clientListOptions.LabelSelector = labelSelector |
| 96 | + } |
| 97 | + |
| 98 | + if opts.FieldSelector != "" { |
| 99 | + fieldSelector, err := fields.ParseSelector(opts.FieldSelector) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + clientListOptions.FieldSelector = fieldSelector |
| 104 | + } |
| 105 | + |
| 106 | + opts.LabelSelector = "" |
| 107 | + opts.FieldSelector = "" |
| 108 | + opts.Limit = 0 |
| 109 | + opts.Continue = "" |
| 110 | + clientListOptions.Raw = &opts |
| 111 | + |
| 112 | + return clientListOptions, nil |
| 113 | +} |
0 commit comments