Skip to content

Commit 585626e

Browse files
committed
Add support for .spec.proxySecretRef for generic provider of Bucket API
1 parent 81b4dd0 commit 585626e

File tree

6 files changed

+102
-6
lines changed

6 files changed

+102
-6
lines changed

api/v1beta2/bucket_types.go

+5
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ type BucketSpec struct {
100100
// +optional
101101
CertSecretRef *meta.LocalObjectReference `json:"certSecretRef,omitempty"`
102102

103+
// ProxySecretRef specifies the Secret containing the proxy configuration
104+
// to use while communicating with the Bucket server.
105+
// +optional
106+
ProxySecretRef *meta.LocalObjectReference `json:"proxySecretRef,omitempty"`
107+
103108
// Interval at which the Bucket Endpoint is checked for updates.
104109
// This interval is approximate and may be subject to jitter to ensure
105110
// efficient use of resources.

api/v1beta2/zz_generated.deepcopy.go

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/source.toolkit.fluxcd.io_buckets.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,17 @@ spec:
391391
- gcp
392392
- azure
393393
type: string
394+
proxySecretRef:
395+
description: |-
396+
ProxySecretRef specifies the Secret containing the proxy configuration
397+
to use while communicating with the Bucket server.
398+
properties:
399+
name:
400+
description: Name of the referent.
401+
type: string
402+
required:
403+
- name
404+
type: object
394405
region:
395406
description: Region of the Endpoint where the BucketName is located
396407
in.

docs/api/v1beta2/source.md

+30
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,21 @@ be of type <code>Opaque</code> or <code>kubernetes.io/tls</code>.</p>
191191
</tr>
192192
<tr>
193193
<td>
194+
<code>proxySecretRef</code><br>
195+
<em>
196+
<a href="https://pkg.go.dev/github.com/fluxcd/pkg/apis/meta#LocalObjectReference">
197+
github.com/fluxcd/pkg/apis/meta.LocalObjectReference
198+
</a>
199+
</em>
200+
</td>
201+
<td>
202+
<em>(Optional)</em>
203+
<p>ProxySecretRef specifies the Secret containing the proxy configuration
204+
to use while communicating with the Bucket server.</p>
205+
</td>
206+
</tr>
207+
<tr>
208+
<td>
194209
<code>interval</code><br>
195210
<em>
196211
<a href="https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Duration">
@@ -1541,6 +1556,21 @@ be of type <code>Opaque</code> or <code>kubernetes.io/tls</code>.</p>
15411556
</tr>
15421557
<tr>
15431558
<td>
1559+
<code>proxySecretRef</code><br>
1560+
<em>
1561+
<a href="https://pkg.go.dev/github.com/fluxcd/pkg/apis/meta#LocalObjectReference">
1562+
github.com/fluxcd/pkg/apis/meta.LocalObjectReference
1563+
</a>
1564+
</em>
1565+
</td>
1566+
<td>
1567+
<em>(Optional)</em>
1568+
<p>ProxySecretRef specifies the Secret containing the proxy configuration
1569+
to use while communicating with the Bucket server.</p>
1570+
</td>
1571+
</tr>
1572+
<tr>
1573+
<td>
15441574
<code>interval</code><br>
15451575
<em>
15461576
<a href="https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Duration">

internal/controller/bucket_controller.go

+32-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
stdtls "crypto/tls"
2222
"errors"
2323
"fmt"
24+
"net/url"
2425
"os"
2526
"path/filepath"
2627
"strings"
@@ -468,7 +469,13 @@ func (r *BucketReconciler) reconcileSource(ctx context.Context, sp *patch.Serial
468469
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Error())
469470
return sreconcile.ResultEmpty, e
470471
}
471-
if provider, err = minio.NewClient(obj, secret, tlsConfig); err != nil {
472+
proxyURL, err := r.getProxyURL(ctx, obj)
473+
if err != nil {
474+
e := serror.NewGeneric(err, sourcev1.AuthenticationFailedReason)
475+
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Error())
476+
return sreconcile.ResultEmpty, e
477+
}
478+
if provider, err = minio.NewClient(obj, secret, tlsConfig, proxyURL); err != nil {
472479
e := serror.NewGeneric(err, "ClientError")
473480
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Error())
474481
return sreconcile.ResultEmpty, e
@@ -703,6 +710,30 @@ func (r *BucketReconciler) getTLSConfig(ctx context.Context, obj *bucketv1.Bucke
703710
return tlsConfig, nil
704711
}
705712

713+
func (r *BucketReconciler) getProxyURL(ctx context.Context, obj *bucketv1.Bucket) (*url.URL, error) {
714+
namespace := obj.GetNamespace()
715+
proxySecret, err := r.getSecret(ctx, obj.Spec.ProxySecretRef, namespace)
716+
if err != nil || proxySecret == nil {
717+
return nil, err
718+
}
719+
proxyData := proxySecret.Data
720+
address, ok := proxyData["address"]
721+
if !ok {
722+
return nil, fmt.Errorf("invalid proxy secret '%s/%s': key 'address' is missing",
723+
obj.Spec.ProxySecretRef.Name, namespace)
724+
}
725+
proxyURL, err := url.Parse(string(address))
726+
if err != nil {
727+
return nil, fmt.Errorf("failed to parse proxy address '%s': %w", address, err)
728+
}
729+
user, hasUser := proxyData["username"]
730+
password, hasPassword := proxyData["password"]
731+
if hasUser || hasPassword {
732+
proxyURL.User = url.UserPassword(string(user), string(password))
733+
}
734+
return proxyURL, nil
735+
}
736+
706737
// eventLogf records events, and logs at the same time.
707738
//
708739
// This log is different from the debug log in the EventRecorder, in the sense

pkg/minio/minio.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"crypto/tls"
2222
"errors"
2323
"fmt"
24+
"net/http"
25+
"net/url"
2426

2527
"github.com/minio/minio-go/v7"
2628
"github.com/minio/minio-go/v7/pkg/credentials"
@@ -37,7 +39,9 @@ type MinioClient struct {
3739
}
3840

3941
// NewClient creates a new Minio storage client.
40-
func NewClient(bucket *sourcev1.Bucket, secret *corev1.Secret, tlsConfig *tls.Config) (*MinioClient, error) {
42+
func NewClient(bucket *sourcev1.Bucket, secret *corev1.Secret,
43+
tlsConfig *tls.Config, proxyURL *url.URL) (*MinioClient, error) {
44+
4145
opt := minio.Options{
4246
Region: bucket.Spec.Region,
4347
Secure: !bucket.Spec.Insecure,
@@ -61,15 +65,25 @@ func NewClient(bucket *sourcev1.Bucket, secret *corev1.Secret, tlsConfig *tls.Co
6165
opt.Creds = credentials.NewIAM("")
6266
}
6367

64-
if opt.Secure && tlsConfig != nil {
68+
secure := opt.Secure && tlsConfig != nil
69+
proxy := proxyURL != nil
70+
if secure || proxy {
6571
// Use the default minio transport, but override the TLS config.
66-
secure := false // true causes the TLS config to be defined internally, but here we have our own so we just pass false.
67-
transport, err := minio.DefaultTransport(secure)
72+
minioSecure := false // true causes the TLS config to be defined internally, but here we have our own so we just pass false.
73+
transport, err := minio.DefaultTransport(minioSecure)
6874
if err != nil {
6975
// The error returned here is always nil, but we keep the check for future compatibility.
7076
return nil, fmt.Errorf("failed to create default minio transport: %w", err)
7177
}
72-
transport.TLSClientConfig = tlsConfig.Clone()
78+
79+
if secure {
80+
transport.TLSClientConfig = tlsConfig.Clone()
81+
}
82+
83+
if proxy {
84+
transport.Proxy = http.ProxyURL(proxyURL)
85+
}
86+
7387
opt.Transport = transport
7488
}
7589

0 commit comments

Comments
 (0)