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 migrations #5

Merged
merged 7 commits into from
Apr 28, 2022
Merged
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
4 changes: 3 additions & 1 deletion cmd/tenant-provisioner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"k8s.io/klog/v2"
"path/filepath"
"totalsoft.ro/platform-controllers/internal/controllers"
"totalsoft.ro/platform-controllers/internal/migration"
"totalsoft.ro/platform-controllers/internal/provisioners/pulumi"
"totalsoft.ro/platform-controllers/pkg/generated/clientset/versioned"
"totalsoft.ro/platform-controllers/pkg/signals"
Expand Down Expand Up @@ -84,7 +85,8 @@ func main() {
eventBroadcaster.StartStructuredLogging(0)
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: kubeClient.CoreV1().Events("")})

controller := controllers.NewProvisioningController(clientset, pulumi.Create, eventBroadcaster)
controller := controllers.NewProvisioningController(clientset, pulumi.Create,
migration.KubeJobsMigrationForTenant(kubeClient, controllers.PlatformNamespaceFilter), eventBroadcaster)
if err = controller.Run(5, stopCh); err != nil {
klog.Fatalf("Error running controller: %s", err)
}
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ go 1.17

require (
github.com/pulumi/pulumi-azure-native/sdk v1.62.0
github.com/pulumi/pulumi-kubernetes/sdk/v3 v3.18.2
github.com/pulumi/pulumi-vault/sdk/v5 v5.4.0
github.com/pulumi/pulumi/sdk/v3 v3.28.0
github.com/pulumi/pulumi/sdk/v3 v3.30.0
k8s.io/api v0.23.3
k8s.io/apimachinery v0.23.3
k8s.io/client-go v0.23.3
Expand Down Expand Up @@ -37,7 +38,6 @@ require (
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.8 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
Expand All @@ -47,6 +47,7 @@ require (
github.com/opentracing/basictracer-go v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/term v1.1.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20180611051255-d3107576ba94 // indirect
Expand All @@ -64,7 +65,7 @@ require (
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
20 changes: 14 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34
github.com/go-openapi/jsonreference v0.19.3/go.mod h1:rjx6GuL8TTa9VaixXglHmQmIL98+wF9xc8zWvFonSJ8=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gofrs/flock v0.7.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gofrs/uuid v3.3.0+incompatible h1:8K4tyRfvU1CYPgJsveYFQMhpFd/wXNM7iK6rR7UHz84=
github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down Expand Up @@ -261,6 +262,7 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand All @@ -275,9 +277,8 @@ github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.8 h1:3tS41NlGYSmhhe/8fhGRzc+z3AYCw1Fe1WAyLuujKs0=
github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down Expand Up @@ -323,6 +324,8 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/term v1.1.0 h1:xIAAdCMh3QIAy+5FrE8Ad8XoDhEU4ufwbaSozViP9kk=
github.com/pkg/term v1.1.0/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand All @@ -337,16 +340,20 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/pulumi/pulumi-azure-native/sdk v1.62.0 h1:IAXv+iapWnCLWnznPtmmbgBq6P3WfFN/G7mcF0roNpc=
github.com/pulumi/pulumi-azure-native/sdk v1.62.0/go.mod h1:xCHFvUAySuT5I99EQ+bX3FK9IN4i+ejUXPNARRsjxUo=
github.com/pulumi/pulumi-kubernetes/sdk/v3 v3.18.2 h1:T/VBcEnkClh2E/Ovo3a87Jd5nXmFMEITAqpv5Tc4gfg=
github.com/pulumi/pulumi-kubernetes/sdk/v3 v3.18.2/go.mod h1:w+Y1d8uqc+gv7JYWLF4rfzvTsIIHR1SCL+GG6sX1xMM=
github.com/pulumi/pulumi-vault/sdk/v5 v5.4.0 h1:In/KXGimo7giys3dVmg53JjnvuOr3M7zqqrP+UExolc=
github.com/pulumi/pulumi-vault/sdk/v5 v5.4.0/go.mod h1:FtoQLeQQfJawT4BD2M4q9F1s1hH+LeioEqZYHUX4+Hg=
github.com/pulumi/pulumi/sdk/v3 v3.16.0/go.mod h1:252ou/zAU1g6E8iTwe2Y9ht7pb5BDl2fJlOuAgZCHiA=
github.com/pulumi/pulumi/sdk/v3 v3.23.2/go.mod h1:WHOQB00iuHZyXhwrymxpKXhpOahSguJIpRjVokmM11w=
github.com/pulumi/pulumi/sdk/v3 v3.27.0/go.mod h1:VsxW+TGv2VBLe/MeqsAr9r0zKzK/gbAhFT9QxYr24cY=
github.com/pulumi/pulumi/sdk/v3 v3.28.0 h1:/Q+b7C6zoZIAQmxjai28UxouXIn3xo417FF2y3BYpZM=
github.com/pulumi/pulumi/sdk/v3 v3.28.0/go.mod h1:Oe6AnW5MjJE5X2UJeqeXm1Al7qahdLVhIPeiKuoqxAw=
github.com/pulumi/pulumi/sdk/v3 v3.30.0 h1:0X5gSUS3x82XzenpCCW8EdJrmOzoQqHSdQd0O6QSMQQ=
github.com/pulumi/pulumi/sdk/v3 v3.30.0/go.mod h1:hGo/+AL1L4sPL9Ukd/i5bNFM3WHs3dHcA+GKEW7M3RA=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down Expand Up @@ -571,6 +578,7 @@ golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -584,13 +592,13 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210817190340-bfb29a6856f2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e h1:XMgFehsDnnLGtjvjOfqWSUzt0alpTR1RSEuznObga2c=
golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
44 changes: 44 additions & 0 deletions helm/crds/provisioning.totalsoft.ro_azuredatabases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,56 @@ spec:
properties:
dbName:
type: string
domain:
type: string
exports:
properties:
password:
properties:
toConfigMap:
properties:
keyTemplate:
type: string
required:
- keyTemplate
type: object
toVault:
properties:
keyTemplate:
type: string
required:
- keyTemplate
type: object
type: object
userName:
properties:
toConfigMap:
properties:
keyTemplate:
type: string
required:
- keyTemplate
type: object
toVault:
properties:
keyTemplate:
type: string
required:
- keyTemplate
type: object
type: object
type: object
platformRef:
type: string
sku:
type: string
required:
- dbName
- domain
- platformRef
type: object
required:
- spec
type: object
served: true
storage: true
Expand Down
3 changes: 3 additions & 0 deletions helm/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ rules:
- apiGroups: [""]
resources: ["events"]
verbs: ["*"]
- apiGroups: ["batch", "extensions"]
resources: [ "jobs" ]
verbs: [ "*" ]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
23 changes: 18 additions & 5 deletions internal/controllers/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const (
type ProvisioningController struct {
factory informers.SharedInformerFactory
clientset clientset.Interface
migrater func(platform string, tenant *provisioningv1.Tenant) error

// workqueue is a rate limited work queue. This is used to queue work to be
// processed instead of performing it as soon as a change happens. This
// means we can ensure we only process a fixed amount of resources at a
Expand All @@ -46,7 +48,7 @@ type ProvisioningController struct {
// Kubernetes API.
recorder record.EventRecorder

infraCreator provisioners.CreateInfrastructureFunc
provisioner provisioners.CreateInfrastructureFunc

platformInformer informersv1.PlatformInformer
tenantInformer informersv1.TenantInformer
Expand All @@ -55,7 +57,8 @@ type ProvisioningController struct {
}

func NewProvisioningController(clientSet clientset.Interface,
infraCreator provisioners.CreateInfrastructureFunc,
provisioner provisioners.CreateInfrastructureFunc,
migrater func(platform string, tenant *provisioningv1.Tenant) error,
eventBroadcaster record.EventBroadcaster) *ProvisioningController {

factory := informers.NewSharedInformerFactory(clientSet, 0)
Expand All @@ -76,8 +79,9 @@ func NewProvisioningController(clientSet clientset.Interface,
azureDbInformer: factory.Provisioning().V1alpha1().AzureDatabases(),
azureManagedDbInformer: factory.Provisioning().V1alpha1().AzureManagedDatabases(),

infraCreator: infraCreator,
clientset: clientSet,
provisioner: provisioner,
clientset: clientSet,
migrater: migrater,
}

if eventBroadcaster != nil {
Expand Down Expand Up @@ -227,10 +231,19 @@ func (c *ProvisioningController) syncHandler(key string) error {
}
azureManagedDbs = azureManagedDbs[:n]

err = c.infraCreator(platform, tenant, &provisioners.InfrastructureManifests{
err = c.provisioner(platform, tenant, &provisioners.InfrastructureManifests{
AzureDbs: azureDbs,
AzureManagedDbs: azureManagedDbs,
})

if err == nil {
if c.migrater != nil {
//TODO check if new resources were created
//schedule migrations after provisioning new resources
err = c.migrater(platform, tenant)
}
}

if err == nil {
c.recorder.Event(tenant, corev1.EventTypeNormal, SuccessSynced, SuccessSynced)
} else {
Expand Down
8 changes: 4 additions & 4 deletions internal/controllers/tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestProvisioningController_processNextWorkItem(t *testing.T) {
outputs = append(outputs, result{platform, tenant, infra})
return nil
}
c := NewProvisioningController(clientset, infraCreator, nil)
c := NewProvisioningController(clientset, infraCreator, nil, nil)
c.factory.Start(nil)
c.factory.WaitForCacheSync(nil)

Expand Down Expand Up @@ -77,7 +77,7 @@ func TestProvisioningController_processNextWorkItem(t *testing.T) {
wg.Wait()
return nil
}
c := NewProvisioningController(clientset, infraCreator, nil)
c := NewProvisioningController(clientset, infraCreator, nil, nil)
c.factory.Start(nil)
c.factory.WaitForCacheSync(nil)

Expand Down Expand Up @@ -118,7 +118,7 @@ func TestProvisioningController_processNextWorkItem(t *testing.T) {
outputs = append(outputs, result{platform, tenant, infra})
return nil
}
c := NewProvisioningController(clientset, infraCreator, nil)
c := NewProvisioningController(clientset, infraCreator, nil, nil)
c.factory.Start(nil)
c.factory.WaitForCacheSync(nil)

Expand Down Expand Up @@ -160,7 +160,7 @@ func TestProvisioningController_processNextWorkItem(t *testing.T) {
outputs = append(outputs, result{platform, tenant, infra})
return nil
}
c := NewProvisioningController(clientset, infraCreator, nil)
c := NewProvisioningController(clientset, infraCreator, nil, nil)
c.factory.Start(nil)
c.factory.WaitForCacheSync(nil)

Expand Down
7 changes: 7 additions & 0 deletions internal/controllers/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package controllers

import "strings"

func PlatformNamespaceFilter(namespace, platform string) bool {
return strings.HasPrefix(namespace, platform)
}
65 changes: 65 additions & 0 deletions internal/migration/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package migration

import (
"context"
"fmt"
v1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"time"
provisioningv1 "totalsoft.ro/platform-controllers/pkg/apis/provisioning/v1alpha1"
)

const (
JobLabelSelectorKey = "provisioning.totalsoft.ro/migration-job-template"
)

func KubeJobsMigrationForTenant(kubeClient kubernetes.Interface,
nsFilter func(string, string) bool) func(platform string, tenant *provisioningv1.Tenant) error {
namer := func(jName, tenant string) string {
return fmt.Sprintf("%s-%s-%d", jName, tenant, time.Now().Unix())
}

return func(platform string, tenant *provisioningv1.Tenant) error {
jobs, err := kubeClient.BatchV1().Jobs("").List(context.TODO(), metav1.ListOptions{
LabelSelector: JobLabelSelectorKey + "=true",
})
if err != nil {
return err
}
for _, job := range jobs.Items {
lghinet marked this conversation as resolved.
Show resolved Hide resolved
if !nsFilter(job.Namespace, platform) {
continue
}
j := &v1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: namer(job.Name, tenant.Spec.Code),
Namespace: job.Namespace,
},
Spec: v1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: job.Spec.Template.Spec,
},
},
}

for i, c := range j.Spec.Template.Spec.InitContainers {
c.Env = append(c.Env, corev1.EnvVar{Name: "TENANT_ID", Value: tenant.Spec.Id})
j.Spec.Template.Spec.InitContainers[i] = c
}
for i, c := range j.Spec.Template.Spec.Containers {
c.Env = append(c.Env, corev1.EnvVar{Name: "TENANT_ID", Value: tenant.Spec.Id})
j.Spec.Template.Spec.Containers[i] = c
}
_, err = kubeClient.BatchV1().Jobs(job.Namespace).Create(context.TODO(), j, metav1.CreateOptions{})
}

if err != nil {
klog.ErrorS(err, "error migration")
}

return err
}
}
Loading