Skip to content

Commit

Permalink
Feat: support bucket lookup type
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiyu Wang <[email protected]>
  • Loading branch information
zhiyu0729 committed Aug 25, 2023
1 parent e5a429a commit 1b92340
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 1 deletion.
17 changes: 17 additions & 0 deletions api/v1beta2/bucket_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ const (
AzureBucketProvider string = "azure"
)

const (
// BucketLookupAuto is automatic determine URL access
BucketLookupAuto = "auto"
// BucketLookupDNS is virtual hosted style URL access
BucketLookupDNS = "dns"
// BucketLookupPath is path style URL access
BucketLookupPath = "path"
)

// BucketSpec specifies the required configuration to produce an Artifact for
// an object storage bucket.
type BucketSpec struct {
Expand Down Expand Up @@ -109,6 +118,14 @@ type BucketSpec struct {
// NOTE: Not implemented, provisional as of https://github.com/fluxcd/flux2/pull/2092
// +optional
AccessFrom *acl.AccessFrom `json:"accessFrom,omitempty"`

// LookupType is type of url lookup supported by bucket server,
// only takes effect when the Provider is 'generic'.
// Defaults to 'path'.
// +kubebuilder:validation:Enum=auto;dns;path
// +kubebuilder:default:=path
// +optional
LookupType *string `json:"lookupType,omitempty"`
}

// BucketStatus records the observed state of a Bucket.
Expand Down
10 changes: 10 additions & 0 deletions config/crd/bases/source.toolkit.fluxcd.io_buckets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,16 @@ spec:
to ensure efficient use of resources.
pattern: ^([0-9]+(\.[0-9]+)?(ms|s|m|h))+$
type: string
lookupType:
default: path
description: LookupType is type of url lookup supported by bucket
server, only takes effect when the Provider is 'generic'. Defaults
to 'path'.
enum:
- auto
- dns
- path
type: string
provider:
default: generic
description: Provider of the object storage bucket. Defaults to 'generic',
Expand Down
28 changes: 28 additions & 0 deletions docs/api/v1beta2/source.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ references to this object.
NOTE: Not implemented, provisional as of <a href="https://github.com/fluxcd/flux2/pull/2092">https://github.com/fluxcd/flux2/pull/2092</a></p>
</td>
</tr>
<tr>
<td>
<code>lookupType</code><br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>LookupType is type of url lookup supported by bucket server,
only takes effect when the Provider is &lsquo;generic&rsquo;.
Defaults to &lsquo;path&rsquo;.</p>
</td>
</tr>
</table>
</td>
</tr>
Expand Down Expand Up @@ -1507,6 +1521,20 @@ references to this object.
NOTE: Not implemented, provisional as of <a href="https://github.com/fluxcd/flux2/pull/2092">https://github.com/fluxcd/flux2/pull/2092</a></p>
</td>
</tr>
<tr>
<td>
<code>lookupType</code><br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>LookupType is type of url lookup supported by bucket server,
only takes effect when the Provider is &lsquo;generic&rsquo;.
Defaults to &lsquo;path&rsquo;.</p>
</td>
</tr>
</tbody>
</table>
</div>
Expand Down
11 changes: 11 additions & 0 deletions docs/spec/v1beta2/buckets.md
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,17 @@ Artifact. When the field is set to `false` or removed, it will resume.
For practical information, see
[suspending and resuming](#suspending-and-resuming).

### Lookup type

`.spec.lookupType` is an optional field to select which url lookup type to access bucket. For example, `dns` will use virtual-hosted–style URL access , `path` will use path-style URL access, or you can also use `auto` to automatic.

Supported options are:
- auto
- dns
- path

It only takes effect when the [Provider](#provider) is [generic](#generic). If you do not specify `.spec.lookupType`, it defaults to `path`.

## Working with Buckets

### Excluding files
Expand Down
18 changes: 17 additions & 1 deletion pkg/minio/minio.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewClient(bucket *sourcev1.Bucket, secret *corev1.Secret) (*MinioClient, er
opt := minio.Options{
Region: bucket.Spec.Region,
Secure: !bucket.Spec.Insecure,
BucketLookup: minio.BucketLookupPath,
BucketLookup: getLookupType(bucket),
}

if secret != nil {
Expand Down Expand Up @@ -133,3 +133,19 @@ func (c *MinioClient) ObjectIsNotFound(err error) bool {
func (c *MinioClient) Close(_ context.Context) {
// Minio client does not provide a close method
}

var lookupNameToEnumType = map[string]minio.BucketLookupType{
sourcev1.BucketLookupAuto: minio.BucketLookupAuto,
sourcev1.BucketLookupDNS: minio.BucketLookupDNS,
sourcev1.BucketLookupPath: minio.BucketLookupPath,
}

func getLookupType(bucket *sourcev1.Bucket) minio.BucketLookupType {
if bucket.Spec.LookupType != nil {
if t, ok := lookupNameToEnumType[*bucket.Spec.LookupType]; ok {
return t
}
}
// default value
return minio.BucketLookupPath
}
56 changes: 56 additions & 0 deletions pkg/minio/minio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/google/uuid"
miniov7 "github.com/minio/minio-go/v7"
"github.com/onsi/gomega"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"gotest.tools/assert"
Expand Down Expand Up @@ -349,3 +350,58 @@ func getObjectFile() string {
timeout: 30s
`
}

func TestGetLookupType(t *testing.T) {
typeUnexpect := "unexpect"
typeAuto := "auto"
typeDNS := "dns"
typePath := "path"

tests := []struct {
name string
bucket sourcev1.Bucket
want miniov7.BucketLookupType
}{
{
name: "lookup type defalut",
bucket: sourcev1.Bucket{Spec: sourcev1.BucketSpec{}},
want: miniov7.BucketLookupPath,
},
{
name: "lookup type unexpect string",
bucket: sourcev1.Bucket{
Spec: sourcev1.BucketSpec{LookupType: &typeUnexpect},
},
want: miniov7.BucketLookupPath,
},
{
name: "lookup type auto",
bucket: sourcev1.Bucket{
Spec: sourcev1.BucketSpec{LookupType: &typeAuto},
},
want: miniov7.BucketLookupAuto,
},
{
name: "lookup type dns",
bucket: sourcev1.Bucket{
Spec: sourcev1.BucketSpec{LookupType: &typeDNS},
},
want: miniov7.BucketLookupDNS,
},
{
name: "lookup type path",
bucket: sourcev1.Bucket{
Spec: sourcev1.BucketSpec{LookupType: &typePath},
},
want: miniov7.BucketLookupPath,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := gomega.NewWithT(t)

got := getLookupType(&tt.bucket)
g.Expect(got).To(gomega.Equal(tt.want))
})
}
}

0 comments on commit 1b92340

Please sign in to comment.