Skip to content

Commit 6f546d9

Browse files
committed
Add support for .spec.proxySecretRef for generic provider of Bucket API
Signed-off-by: Matheus Pimenta <matheuscscp@gmail.com>
1 parent 59ad5a7 commit 6f546d9

File tree

11 files changed

+478
-17
lines changed

11 files changed

+478
-17
lines changed

api/v1beta2/bucket_types.go

+7
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ 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+
//
106+
// Only supported for the generic provider.
107+
// +optional
108+
ProxySecretRef *meta.LocalObjectReference `json:"proxySecretRef,omitempty"`
109+
103110
// Interval at which the Bucket Endpoint is checked for updates.
104111
// This interval is approximate and may be subject to jitter to ensure
105112
// 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

+14
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,20 @@ 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+
399+
400+
Only supported for the generic provider.
401+
properties:
402+
name:
403+
description: Name of the referent.
404+
type: string
405+
required:
406+
- name
407+
type: object
394408
region:
395409
description: Region of the Endpoint where the BucketName is located
396410
in.

docs/api/v1beta2/source.md

+32
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,22 @@ 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+
<p>Only supported for the generic provider.</p>
206+
</td>
207+
</tr>
208+
<tr>
209+
<td>
194210
<code>interval</code><br>
195211
<em>
196212
<a href="https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Duration">
@@ -1541,6 +1557,22 @@ be of type <code>Opaque</code> or <code>kubernetes.io/tls</code>.</p>
15411557
</tr>
15421558
<tr>
15431559
<td>
1560+
<code>proxySecretRef</code><br>
1561+
<em>
1562+
<a href="https://pkg.go.dev/github.com/fluxcd/pkg/apis/meta#LocalObjectReference">
1563+
github.com/fluxcd/pkg/apis/meta.LocalObjectReference
1564+
</a>
1565+
</em>
1566+
</td>
1567+
<td>
1568+
<em>(Optional)</em>
1569+
<p>ProxySecretRef specifies the Secret containing the proxy configuration
1570+
to use while communicating with the Bucket server.</p>
1571+
<p>Only supported for the generic provider.</p>
1572+
</td>
1573+
</tr>
1574+
<tr>
1575+
<td>
15441576
<code>interval</code><br>
15451577
<em>
15461578
<a href="https://pkg.go.dev/k8s.io/apimachinery/pkg/apis/meta/v1#Duration">

docs/spec/v1beta2/buckets.md

+37
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,43 @@ stringData:
824824
ca.crt: <PEM-encoded cert>
825825
```
826826

827+
### Proxy secret reference
828+
829+
`.spec.proxySecretRef.name` is an optional field used to specify the name of a
830+
Secret that contains the proxy settings for the object. These settings are used
831+
for all the remote operations related to the Bucket.
832+
The Secret can contain three keys:
833+
834+
- `address`, to specify the address of the proxy server. This is a required key.
835+
- `username`, to specify the username to use if the proxy server is protected by
836+
basic authentication. This is an optional key.
837+
- `password`, to specify the password to use if the proxy server is protected by
838+
basic authentication. This is an optional key.
839+
840+
The proxy server must be HTTP/S.
841+
842+
This API is only supported for the `generic` [provider](#provider).
843+
844+
Example:
845+
846+
```yaml
847+
---
848+
apiVersion: v1
849+
kind: Secret
850+
metadata:
851+
name: http-proxy
852+
type: Opaque
853+
stringData:
854+
address: http://proxy.com
855+
username: mandalorian
856+
password: grogu
857+
```
858+
859+
Proxying can also be configured in the source-controller Deployment directly by
860+
using the standard environment variables such as `HTTPS_PROXY`, `ALL_PROXY`, etc.
861+
862+
`.spec.proxySecretRef.name` takes precedence over all environment variables.
863+
827864
### Insecure
828865

829866
`.spec.insecure` is an optional field to allow connecting to an insecure (HTTP)

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/distribution/distribution/v3 v3.0.0-alpha.1
2020
github.com/docker/cli v24.0.9+incompatible
2121
github.com/docker/go-units v0.5.0
22+
github.com/elazarl/goproxy v0.0.0-20231117061959-7cc037d33fb5
2223
github.com/fluxcd/cli-utils v0.36.0-flux.7
2324
github.com/fluxcd/pkg/apis/event v0.9.0
2425
github.com/fluxcd/pkg/apis/meta v1.5.0

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkp
311311
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
312312
github.com/elazarl/goproxy v0.0.0-20231117061959-7cc037d33fb5 h1:m62nsMU279qRD9PQSWD1l66kmkXzuYcnVJqL4XLeV2M=
313313
github.com/elazarl/goproxy v0.0.0-20231117061959-7cc037d33fb5/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
314+
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM=
315+
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
314316
github.com/emicklei/go-restful/v3 v3.12.0 h1:y2DdzBAURM29NFF94q6RaY4vjIH1rtwDapwQtU84iWk=
315317
github.com/emicklei/go-restful/v3 v3.12.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
316318
github.com/emicklei/proto v1.12.1 h1:6n/Z2pZAnBwuhU66Gs8160B8rrrYKo7h2F2sCOnNceE=
@@ -831,6 +833,7 @@ github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0
831833
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
832834
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
833835
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
836+
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
834837
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
835838
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
836839
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=

internal/controller/bucket_controller.go

+42-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,23 @@ 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+
var opts []minio.Option
479+
if secret != nil {
480+
opts = append(opts, minio.WithSecret(secret))
481+
}
482+
if tlsConfig != nil {
483+
opts = append(opts, minio.WithTLSConfig(tlsConfig))
484+
}
485+
if proxyURL != nil {
486+
opts = append(opts, minio.WithProxyURL(proxyURL))
487+
}
488+
if provider, err = minio.NewClient(obj, opts...); err != nil {
472489
e := serror.NewGeneric(err, "ClientError")
473490
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, e.Reason, e.Error())
474491
return sreconcile.ResultEmpty, e
@@ -703,6 +720,30 @@ func (r *BucketReconciler) getTLSConfig(ctx context.Context, obj *bucketv1.Bucke
703720
return tlsConfig, nil
704721
}
705722

723+
func (r *BucketReconciler) getProxyURL(ctx context.Context, obj *bucketv1.Bucket) (*url.URL, error) {
724+
namespace := obj.GetNamespace()
725+
proxySecret, err := r.getSecret(ctx, obj.Spec.ProxySecretRef, namespace)
726+
if err != nil || proxySecret == nil {
727+
return nil, err
728+
}
729+
proxyData := proxySecret.Data
730+
address, ok := proxyData["address"]
731+
if !ok {
732+
return nil, fmt.Errorf("invalid proxy secret '%s/%s': key 'address' is missing",
733+
obj.Spec.ProxySecretRef.Name, namespace)
734+
}
735+
proxyURL, err := url.Parse(string(address))
736+
if err != nil {
737+
return nil, fmt.Errorf("failed to parse proxy address '%s': %w", address, err)
738+
}
739+
user, hasUser := proxyData["username"]
740+
password, hasPassword := proxyData["password"]
741+
if hasUser || hasPassword {
742+
proxyURL.User = url.UserPassword(string(user), string(password))
743+
}
744+
return proxyURL, nil
745+
}
746+
706747
// eventLogf records events, and logs at the same time.
707748
//
708749
// This log is different from the debug log in the EventRecorder, in the sense

0 commit comments

Comments
 (0)