Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add/nextcloud backup #205

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/go-logr/logr"
"github.com/spf13/cobra"
"github.com/spf13/viper"
_ "github.com/vshn/appcat/v4/pkg/comp-functions/functions/cloudscalebucket"
_ "github.com/vshn/appcat/v4/pkg/comp-functions/functions/miniobucket"
_ "github.com/vshn/appcat/v4/pkg/comp-functions/functions/vshnkeycloak"
_ "github.com/vshn/appcat/v4/pkg/comp-functions/functions/vshnmariadb"
Expand Down
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/blang/semver/v4 v4.0.0
github.com/cert-manager/cert-manager v1.13.2
github.com/crossplane/crossplane v1.15.3
github.com/crossplane/crossplane-runtime v1.15.1
github.com/crossplane/crossplane-runtime v1.16.0
github.com/crossplane/crossplane-tools v0.0.0-20240522174801-1ad3d4c87f21
github.com/crossplane/function-sdk-go v0.3.0-rc.0
github.com/deepmap/oapi-codegen v0.0.0-00010101000000-000000000000
Expand All @@ -36,6 +36,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/thediveo/enumflag/v2 v2.0.2
github.com/vektra/mockery/v2 v2.36.1
github.com/vshn/provider-cloudscale v0.5.2
github.com/vshn/provider-minio v0.3.0
go.uber.org/zap v1.27.0
golang.org/x/text v0.14.0
Expand All @@ -48,8 +49,8 @@ require (
k8s.io/code-generator v0.30.2
k8s.io/utils v0.0.0-20240310230437-4693a0247e57
sigs.k8s.io/apiserver-runtime v0.0.0-00010101000000-000000000000
sigs.k8s.io/controller-runtime v0.18.2
sigs.k8s.io/controller-tools v0.14.0
sigs.k8s.io/controller-runtime v0.18.4
sigs.k8s.io/controller-tools v0.15.0
sigs.k8s.io/kind v0.20.0
sigs.k8s.io/yaml v1.4.0
)
Expand Down Expand Up @@ -264,7 +265,7 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools v2.2.0+incompatible // indirect
helm.sh/helm/v3 v3.15.1
k8s.io/apiextensions-apiserver v0.30.0 // indirect
k8s.io/apiextensions-apiserver v0.30.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
Expand All @@ -276,3 +277,5 @@ replace github.com/openshift/machine-config-operator => github.com/openshift/mac
replace sigs.k8s.io/apiserver-runtime => github.com/Kidswiss/apiserver-runtime v0.0.0-20240704094752-0638be0fd3be

replace github.com/google/cel-go => github.com/google/cel-go v0.17.8

replace github.com/crossplane/crossplane-runtime => github.com/crossplane/crossplane-runtime v1.15.1
18 changes: 10 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8=
github.com/onsi/ginkgo/v2 v2.17.1/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
github.com/onsi/gomega v1.32.0 h1:JRYU78fJ1LPxlckP6Txi/EYqJvjtMrDC04/MM5XRHPk=
github.com/onsi/gomega v1.32.0/go.mod h1:a4x4gW6Pz2yK1MAmvluYme5lvYTn61afQ2ETw/8n4Lg=
github.com/onsi/gomega v1.33.0 h1:snPCflnZrpMsy94p4lXVEkHo12lmPnc3vY5XBbreexE=
github.com/onsi/gomega v1.33.0/go.mod h1:+925n5YtiFsLzzafLUHzVMBpvvRAzrydIBiSIxjX3wY=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
Expand Down Expand Up @@ -629,6 +629,8 @@ github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/vshn/provider-cloudscale v0.5.2 h1:4OPB8k84PulAAYuTItr4yUekwhDn0NHAdLG4lTf/uLg=
github.com/vshn/provider-cloudscale v0.5.2/go.mod h1:MpTl88VcId7C0YwXjjXlhUWxZLChzsq5R9cHHrvFoWg=
github.com/vshn/provider-minio v0.3.0 h1:2p3vN5fsWMjFyElxIX4W74R/i0BchWnHQOeIvvz2u8E=
github.com/vshn/provider-minio v0.3.0/go.mod h1:wkYcIc6FAVIDmZfBzlBCZQsXtcvIoRIuEFS6VyxYCY8=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
Expand Down Expand Up @@ -860,8 +862,8 @@ helm.sh/helm/v3 v3.15.1 h1:22ztacHz4gMqhXNqCQ9NAg6BFWoRUryNLvnkz6OVyw0=
helm.sh/helm/v3 v3.15.1/go.mod h1:fvfoRcB8UKRUV5jrIfOTaN/pG1TPhuqSb56fjYdTKXg=
k8s.io/api v0.30.2 h1:+ZhRj+28QT4UOH+BKznu4CBgPWgkXO7XAvMcMl0qKvI=
k8s.io/api v0.30.2/go.mod h1:ULg5g9JvOev2dG0u2hig4Z7tQ2hHIuS+m8MNZ+X6EmI=
k8s.io/apiextensions-apiserver v0.30.0 h1:jcZFKMqnICJfRxTgnC4E+Hpcq8UEhT8B2lhBcQ+6uAs=
k8s.io/apiextensions-apiserver v0.30.0/go.mod h1:N9ogQFGcrbWqAY9p2mUAL5mGxsLqwgtUce127VtRX5Y=
k8s.io/apiextensions-apiserver v0.30.1 h1:4fAJZ9985BmpJG6PkoxVRpXv9vmPUOVzl614xarePws=
k8s.io/apiextensions-apiserver v0.30.1/go.mod h1:R4GuSrlhgq43oRY9sF2IToFh7PVlF1JjfWdoG3pixk4=
k8s.io/apimachinery v0.30.2 h1:fEMcnBj6qkzzPGSVsAZtQThU62SmQ4ZymlXRC5yFSCg=
k8s.io/apimachinery v0.30.2/go.mod h1:iexa2somDaxdnj7bha06bhb43Zpa6eWH8N8dbqVjTUc=
k8s.io/apiserver v0.30.2 h1:ACouHiYl1yFI2VFI3YGM+lvxgy6ir4yK2oLOsLI1/tw=
Expand All @@ -888,10 +890,10 @@ k8s.io/utils v0.0.0-20240310230437-4693a0247e57 h1:gbqbevonBh57eILzModw6mrkbwM0g
k8s.io/utils v0.0.0-20240310230437-4693a0247e57/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.29.0 h1:/U5vjBbQn3RChhv7P11uhYvCSm5G2GaIi5AIGBS6r4c=
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.29.0/go.mod h1:z7+wmGM2dfIiLRfrC6jb5kV2Mq/sK1ZP303cxzkV5Y4=
sigs.k8s.io/controller-runtime v0.18.2 h1:RqVW6Kpeaji67CY5nPEfRz6ZfFMk0lWQlNrLqlNpx+Q=
sigs.k8s.io/controller-runtime v0.18.2/go.mod h1:tuAt1+wbVsXIT8lPtk5RURxqAnq7xkpv2Mhttslg7Hw=
sigs.k8s.io/controller-tools v0.14.0 h1:rnNoCC5wSXlrNoBKKzL70LNJKIQKEzT6lloG6/LF73A=
sigs.k8s.io/controller-tools v0.14.0/go.mod h1:TV7uOtNNnnR72SpzhStvPkoS/U5ir0nMudrkrC4M9Sc=
sigs.k8s.io/controller-runtime v0.18.4 h1:87+guW1zhvuPLh1PHybKdYFLU0YJp4FhJRmiHvm5BZw=
sigs.k8s.io/controller-runtime v0.18.4/go.mod h1:TVoGrfdpbA9VRFaRnKgk9P5/atA0pMwq+f+msb9M8Sg=
sigs.k8s.io/controller-tools v0.15.0 h1:4dxdABXGDhIa68Fiwaif0vcu32xfwmgQ+w8p+5CxoAI=
sigs.k8s.io/controller-tools v0.15.0/go.mod h1:8zUSS2T8Hx0APCNRhJWbS3CAQEbIxLa07khzh7pZmXM=
sigs.k8s.io/gateway-api v0.8.0 h1:isQQ3Jx2qFP7vaA3ls0846F0Amp9Eq14P08xbSwVbQg=
sigs.k8s.io/gateway-api v0.8.0/go.mod h1:okOnjPNBFbIS/Rw9kAhuIUaIkLhTKEu+ARIuXk2dgaM=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
Expand Down
153 changes: 153 additions & 0 deletions pkg/comp-functions/functions/cloudscalebucket/cloudscalebucket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package cloudscalebucket

import (
"context"
"fmt"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/resource"
xfnproto "github.com/crossplane/function-sdk-go/proto/v1beta1"
appcatv1 "github.com/vshn/appcat/v4/apis/v1"
"github.com/vshn/appcat/v4/pkg/comp-functions/runtime"
cloudscalev1 "github.com/vshn/provider-cloudscale/apis/cloudscale/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// ProvisionCloudscalebucket will create a bucket in cloudscale.
// This function will leverage provider-cloudscale to deploy proper users
// alongside the bucket.
func ProvisionCloudscalebucket(_ context.Context, svc *runtime.ServiceRuntime) *xfnproto.Result {

bucket := &appcatv1.ObjectBucket{}

err := svc.GetObservedComposite(bucket)
if err != nil {
return runtime.NewFatalResult(err)
}

config, ok := svc.Config.Data["providerConfig"]
if !ok {
return runtime.NewFatalResult(fmt.Errorf("no providerConfig specified"))
}

err = addBucket(svc, bucket, config)
if err != nil {
return runtime.NewFatalResult(err)
}

err = addUser(svc, bucket, config)
if err != nil {
return runtime.NewFatalResult(err)
}

svc.SetConnectionDetail("BUCKET_NAME", []byte(bucket.Spec.Parameters.BucketName))
svc.SetConnectionDetail("AWS_REGION", []byte(bucket.Spec.Parameters.Region))

err = populateEndpointConnectionDetails(svc)
if err != nil {
return runtime.NewFatalResult(err)
}

return nil
}

func addBucket(svc *runtime.ServiceRuntime, bucket *appcatv1.ObjectBucket, config string) error {

mb := &cloudscalev1.Bucket{
ObjectMeta: metav1.ObjectMeta{},
Spec: cloudscalev1.BucketSpec{
ForProvider: cloudscalev1.BucketParameters{
BucketDeletionPolicy: cloudscalev1.BucketDeletionPolicy(bucket.Spec.Parameters.BucketDeletionPolicy),
Region: bucket.Spec.Parameters.Region,
BucketName: bucket.Spec.Parameters.BucketName,
CredentialsSecretRef: v1.SecretReference{
Namespace: svc.Config.Data["providerSecretNamespace"],
},
},
ResourceSpec: xpv1.ResourceSpec{
ProviderConfigReference: &xpv1.Reference{
Name: config,
},
},
},
}

objName := getBucketObjectName(svc, bucket, "cloudscale-bucket", mb.DeepCopy())

mb.ObjectMeta.Name = objName
mb.Spec.ForProvider.CredentialsSecretRef.Name = objName

return svc.SetDesiredComposedResourceWithName(mb, "cloudscale-bucket")
}

func addUser(svc *runtime.ServiceRuntime, bucket *appcatv1.ObjectBucket, config string) error {

user := &cloudscalev1.ObjectsUser{
ObjectMeta: metav1.ObjectMeta{},
Spec: cloudscalev1.ObjectsUserSpec{
ResourceSpec: xpv1.ResourceSpec{
ProviderConfigReference: &xpv1.Reference{
Name: config,
},
WriteConnectionSecretToReference: &xpv1.SecretReference{
Namespace: svc.Config.Data["providerSecretNamespace"],
},
},
ForProvider: cloudscalev1.ObjectsUserParameters{
DisplayName: fmt.Sprintf("%s.%s", bucket.Labels["crossplane.io/claim-namespace"], bucket.Labels["crossplane.io/claim-name"]),
},
},
}

objName := getBucketObjectName(svc, bucket, "cloudscale-user", user.DeepCopy())

user.ObjectMeta.Name = objName
user.Spec.WriteConnectionSecretToReference.Name = objName

cd, err := svc.GetObservedComposedResourceConnectionDetails("cloudscale-user")
if err != nil && err != runtime.ErrNotFound {
return err
}

for v, k := range cd {
svc.SetConnectionDetail(v, k)
}

return svc.SetDesiredComposedResourceWithName(user, "cloudscale-user")
}

func populateEndpointConnectionDetails(svc *runtime.ServiceRuntime) error {

bucket := &cloudscalev1.Bucket{}

err := svc.GetObservedComposedResource(bucket, "cloudscale-bucket")
if err != nil && err == runtime.ErrNotFound {
return nil
} else if err != nil {
return err
}

svc.SetConnectionDetail("ENDPOINT", []byte(bucket.Status.Endpoint))
svc.SetConnectionDetail("ENDPOINT_URL", []byte(bucket.Status.EndpointURL))

return nil

}

// Legacy buckets where created with wrong object names that break nested
// services.
// This logic returns the new naming scheme, if there's no existing bucket CR.
// If there's already a CR it will simply return it's name.
// The `obj` parameter should always be a deepCopy of the original, otherwise
// the pointer in the calling function will have all the fields populated. Which
// can lead to unexpected side effects.
func getBucketObjectName(svc *runtime.ServiceRuntime, bucket *appcatv1.ObjectBucket, resName string, obj resource.Managed) string {

err := svc.GetObservedComposedResource(obj, resName)
if err != nil {
return bucket.Spec.Parameters.BucketName
}

return obj.GetName()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package cloudscalebucket
14 changes: 14 additions & 0 deletions pkg/comp-functions/functions/cloudscalebucket/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cloudscalebucket

import "github.com/vshn/appcat/v4/pkg/comp-functions/runtime"

func init() {
runtime.RegisterService("cloudscalebucket", runtime.Service{
Steps: []runtime.Step{
{
Name: "provision-bucket",
Execute: ProvisionCloudscalebucket,
},
},
})
}
7 changes: 1 addition & 6 deletions pkg/comp-functions/functions/miniobucket/miniobucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
accessKeyName = "AWS_ACCESS_KEY_ID"
secretKeyName = "AWS_SECRET_ACCESS_KEY"
)

// ProvisionMiniobucket will create a bucket in a pre-deployed minio instance.
// This function will leverage provider-minio to deploy proper policies and users
// alongside the bucket.
func ProvisionMiniobucket(ctx context.Context, svc *runtime.ServiceRuntime) *xfnproto.Result {
func ProvisionMiniobucket(_ context.Context, svc *runtime.ServiceRuntime) *xfnproto.Result {

bucket := &appcatv1.ObjectBucket{}

Expand Down
4 changes: 2 additions & 2 deletions pkg/comp-functions/functions/vshnnextcloud/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var nextcloudBackupScript string

func AddBackup(ctx context.Context, svc *runtime.ServiceRuntime) *xfnproto.Result {
comp := &vshnv1.VSHNNextcloud{}
err := svc.GetObservedComposite(comp)
err := svc.GetDesiredComposite(comp)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("can't get composite: %w", err))
}
Expand All @@ -37,7 +37,7 @@ func AddBackup(ctx context.Context, svc *runtime.ServiceRuntime) *xfnproto.Resul

err = updateRelease(svc, comp)
if err != nil {
return runtime.NewFatalResult(fmt.Errorf("cannot update release with backup configuration: %w", err))
return runtime.NewWarningResult(fmt.Sprintf("cannot update release with backup configuration: %s", err))
}

return nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
netv1 "k8s.io/api/networking/v1"
rbacv1 "k8s.io/api/rbac/v1"

cloudscalev1 "github.com/vshn/provider-cloudscale/apis/cloudscale/v1"
"k8s.io/apimachinery/pkg/runtime"
)

Expand Down Expand Up @@ -55,4 +56,5 @@ func AddToScheme(s *runtime.Scheme) {
_ = managedupgradev1beta1.AddToScheme(s)
_ = pgv1alpha1.SchemeBuilder.AddToScheme(s)
_ = apix.AddToScheme(s)
_ = cloudscalev1.SchemeBuilder.AddToScheme(s)
}