Skip to content

Commit

Permalink
Add existing PVC support (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
halamix2 authored Jul 25, 2024
1 parent b617efb commit 110d55d
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 3 deletions.
5 changes: 5 additions & 0 deletions components/operator/api/v1alpha1/dockerregistry_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Storage struct {
S3 *StorageS3 `json:"s3,omitempty"`
GCS *StorageGCS `json:"gcs,omitempty"`
BTPObjectStore *StorageBTPObjectStore `json:"btpObjectStore,omitempty"`
PVC *StoragePVC `json:"pvc,omitempty"`
}

type StorageAzure struct {
Expand Down Expand Up @@ -86,6 +87,10 @@ type StorageBTPObjectStore struct {
SecretName string `json:"secretName,omitempty"`
}

type StoragePVC struct {
Name string `json:"name"`
}

type State string

type Served string
Expand Down
20 changes: 20 additions & 0 deletions components/operator/api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions components/operator/internal/chart/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type FlagsBuilder interface {
WithAzure(secret *v1alpha1.StorageAzureSecrets) *flagsBuilder
WithS3(config *v1alpha1.StorageS3, secret *v1alpha1.StorageS3Secrets) *flagsBuilder
WithFilesystem() *flagsBuilder
WithPVC(config *v1alpha1.StoragePVC) *flagsBuilder
WithGCS(config *v1alpha1.StorageGCS, secret *v1alpha1.StorageGCSSecrets) *flagsBuilder
}

Expand Down Expand Up @@ -141,6 +142,12 @@ func (fb *flagsBuilder) WithFilesystem() *flagsBuilder {
return fb
}

func (fb *flagsBuilder) WithPVC(config *v1alpha1.StoragePVC) *flagsBuilder {
fb.flags["persistence.enabled"] = true
fb.flags["persistence.existingClaim"] = config.Name
return fb
}

func (fb *flagsBuilder) WithGCS(config *v1alpha1.StorageGCS, secret *v1alpha1.StorageGCSSecrets) *flagsBuilder {
fb.flags["storage"] = "gcs"
fb.flags["gcs.bucket"] = config.Bucket
Expand Down
4 changes: 3 additions & 1 deletion components/operator/internal/state/installed_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
GCSStorageName = "gcs"
S3StorageName = "s3"
FilesystemStorageName = "filesystem"
PVCStorageName = "pvc"
)

func sFnUpdateFinalStatus(ctx context.Context, r *reconciler, s *systemState) (stateFn, *controllerruntime.Result, error) {
Expand Down Expand Up @@ -122,8 +123,9 @@ func getStorageField(ctx context.Context, storage *v1alpha1.Storage, instance *v
}
storageType := getBTPStorageHyperscaler(btpSecret.Data)
storageName = fmt.Sprintf("%s-%s", BTPStorageName, storageType)
} else if storage.PVC != nil {
storageName = PVCStorageName
}

}
return fieldToUpdate{storageName, &instance.Status.Storage, "Storage type", ""}, nil
}
Expand Down
11 changes: 11 additions & 0 deletions components/operator/internal/state/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func prepareStorage(ctx context.Context, r *reconciler, s *systemState) error {
return prepareGCSStorage(ctx, r, s)
} else if s.instance.Spec.Storage.BTPObjectStore != nil {
return prepareBTPStorage(ctx, r, s)
} else if s.instance.Spec.Storage.PVC != nil {
return preparePVCStorage(s)
}
}
s.flagsBuilder.WithFilesystem()
Expand All @@ -61,6 +63,9 @@ func prepareStorageUnique(s *systemState) error {
if s.instance.Spec.Storage.BTPObjectStore != nil {
storages++
}
if s.instance.Spec.Storage.PVC != nil {
storages++
}
if storages > 1 {
return errors.New("only one storage option can be used")
}
Expand Down Expand Up @@ -146,3 +151,9 @@ func prepareBTPStorage(ctx context.Context, r *reconciler, s *systemState) error
}
return nil
}

func preparePVCStorage(s *systemState) error {
s.flagsBuilder.WithFilesystem()
s.flagsBuilder.WithPVC(s.instance.Spec.Storage.PVC)
return nil
}
83 changes: 83 additions & 0 deletions components/operator/internal/state/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,87 @@ func Test_sFnStorageConfiguration(t *testing.T) {
require.EqualValues(t, expectedFlags, s.flagsBuilder.Build())
})

t.Run("internal registry using btp pvc storage", func(t *testing.T) {
gcsSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "btpSecret",
Namespace: "kyma-system",
},
Data: map[string][]byte{
"base64EncodedPrivateKeyData": []byte("YWNjb3VudGtleQ=="),
"bucket": []byte("gcsBucket"),
},
}

s := &systemState{
instance: v1alpha1.DockerRegistry{
ObjectMeta: metav1.ObjectMeta{
Namespace: "kyma-system",
},
Spec: v1alpha1.DockerRegistrySpec{
Storage: &v1alpha1.Storage{
PVC: &v1alpha1.StoragePVC{
Name: "pvc",
},
},
},
},
statusSnapshot: v1alpha1.DockerRegistryStatus{},
flagsBuilder: chart.NewFlagsBuilder(),
}
r := &reconciler{
k8s: k8s{client: fake.NewClientBuilder().WithObjects(gcsSecret).Build()},
log: zap.NewNop().Sugar(),
}

expectedFlags := map[string]interface{}{
"configData": map[string]interface{}{
"storage": map[string]interface{}{
"filesystem": map[string]interface{}{
"rootdirectory": "/var/lib/registry",
},
},
},
"storage": "filesystem",
"persistence": map[string]interface{}{
"enabled": true,
"existingClaim": "pvc",
},
}

next, result, err := sFnStorageConfiguration(context.Background(), r, s)
require.NoError(t, err)
require.Nil(t, result)
requireEqualFunc(t, sFnUpdateConfigurationStatus, next)

require.EqualValues(t, expectedFlags, s.flagsBuilder.Build())
})

t.Run("internal registry using multiple storages", func(t *testing.T) {
s := &systemState{
instance: v1alpha1.DockerRegistry{
ObjectMeta: metav1.ObjectMeta{
Namespace: "kyma-system",
},
Spec: v1alpha1.DockerRegistrySpec{
Storage: &v1alpha1.Storage{
BTPObjectStore: &v1alpha1.StorageBTPObjectStore{},
Azure: &v1alpha1.StorageAzure{},
},
},
},
statusSnapshot: v1alpha1.DockerRegistryStatus{},
flagsBuilder: chart.NewFlagsBuilder(),
}
r := &reconciler{
k8s: k8s{client: fake.NewClientBuilder().Build()},
log: zap.NewNop().Sugar(),
}

next, result, err := sFnStorageConfiguration(context.Background(), r, s)
require.Error(t, err)
require.Nil(t, result)
require.Nil(t, next)
})

}
3 changes: 1 addition & 2 deletions config/docker-registry/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ persistence:
enabled: true
size: 20Gi
# storageClass: '-'
# existingClaim: ""

# set the type of filesystem to use: filesystem, s3.
# If filesystem is used, you should also add it to configData, below
Expand Down Expand Up @@ -120,13 +121,11 @@ storage: filesystem
# authurl: http://swift.example.com/
# container: my-container


# gcs:
# bucket: ""
# rootdirectory: ""
# chunkSize: 5242880


# https://docs.docker.com/registry/configuration/
configData: # example: https://github.com/docker/distribution/blob/master/cmd/registry/config-dev.yml
version: 0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ spec:
required:
- bucket
type: object
pvc:
properties:
name:
type: string
required:
- name
type: object
s3:
properties:
bucket:
Expand Down

0 comments on commit 110d55d

Please sign in to comment.