Skip to content

Commit d3718a0

Browse files
committed
Fix
Signed-off-by: v.oleynikov <[email protected]>
1 parent a79d2be commit d3718a0

File tree

21 files changed

+650
-67
lines changed

21 files changed

+650
-67
lines changed

.werf/consts.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# base images
22
{{- $_ := set $ "BASE_ALT" "registry.deckhouse.io/base_images/alt:p10@sha256:f105773c682498700680d7cd61a702a4315c4235aee3622757591fd510fb8b4a" }}
33
{{- $_ := set $ "BASE_ALT_P11" "registry.deckhouse.io/base_images/alt:p11@sha256:e47d84424485d3674240cb2f67d3a1801b37d327e6d1eb8cc8d01be8ed3b34f3" }}
4-
{{- $_ := set $ "BASE_GOLANG_1_23" "registry.deckhouse.io/base_images/golang:1.23.5-alpine3.20@sha256:623ef3f63012bbd648021a2f097de3f411889332ba83bd98f0ac8d1288bdaa06" }}
4+
{{- $_ := set $ "BASE_GOLANG_1_23" "registry.deckhouse.io/base_images/golang:1.23.6-alpine3.20@sha256:3058c63e0e2532881949c4186414baa24a0f9a8f9349b1853daa49be816f42e9" }}
55
{{- $_ := set $ "BASE_SCRATCH" "registry.deckhouse.io/base_images/scratch@sha256:653ae76965c98c8cd1c8c9ff7725316d2983986f896655b30e0f44d2f8b2dd7e" }}
66
{{- $_ := set $ "BASE_ALPINE" "registry.deckhouse.io/base_images/alpine:3.20.3@sha256:41628df7c9b935d248f64542634e7a843f9bc7f2252d7f878e77f7b79a947466" }}
77

api/v1alpha1/ceph_storage_class.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type CephStorageClassList struct {
4343

4444
type CephStorageClassSpec struct {
4545
ClusterConnectionName string `json:"clusterConnectionName"`
46-
ClusterAuthenticationName string `json:"clusterAuthenticationName"`
46+
ClusterAuthenticationName string `json:"clusterAuthenticationName,omitempty"`
4747
ReclaimPolicy string `json:"reclaimPolicy"`
4848
Type string `json:"type"`
4949
RBD *CephStorageClassRBD `json:"rbd,omitempty"`
@@ -57,6 +57,7 @@ type CephStorageClassRBD struct {
5757

5858
type CephStorageClassCephFS struct {
5959
FSName string `json:"fsName,omitempty"`
60+
Pool string `json:"pool,omitempty"`
6061
}
6162

6263
type CephStorageClassStatus struct {

crds/cephstorageclass.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ spec:
9292
- rule: self == oldSelf
9393
message: Value is immutable.
9494
minLength: 1
95+
pool:
96+
description: |
97+
Name of the CephFS pool.
98+
type: string
99+
x-kubernetes-validations:
100+
- rule: self == oldSelf
101+
message: Value is immutable.
102+
minLength: 1
95103
rbd:
96104
type: object
97105
description: |

hack/boilerplate.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Copyright YEAR Flant JSC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/

hack/generate_code.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# run from repository root
4+
cd hooks/go/api
5+
6+
go get k8s.io/code-generator/cmd/deepcopy-gen
7+
8+
go run k8s.io/code-generator/cmd/deepcopy-gen -v 2 \
9+
--output-file zz_generated.deepcopy.go \
10+
--go-header-file ../../../hack/boilerplate.txt \
11+
./v1alpha1
12+
13+
cd ..

hooks/go/050-migrate-auth-to-connection/migrate-auth-to-connection.go

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,15 @@ import (
88
"github.com/deckhouse/module-sdk/pkg"
99
"github.com/deckhouse/module-sdk/pkg/registry"
1010

11-
v1 "k8s.io/api/core/v1"
12-
sv1 "k8s.io/api/storage/v1"
13-
extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
14-
apiruntime "k8s.io/apimachinery/pkg/runtime"
11+
funcs "csi-ceph/funcs"
12+
1513
"k8s.io/apimachinery/pkg/types"
16-
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
17-
"k8s.io/client-go/rest"
18-
"k8s.io/client-go/tools/clientcmd"
1914
"sigs.k8s.io/controller-runtime/pkg/client"
2015
)
2116

2217
const (
23-
MigratedLabel = "storage.deckhouse.io/migratedFromCephClusterAuthentication"
24-
MigratedLabelValue = "true"
18+
CephClusterAuthenticationMigratedLabel = "storage.deckhouse.io/migratedFromCephClusterAuthentication"
19+
CephClusterAuthenticationMigratedLabelValue = "true"
2520
)
2621

2722
var _ = registry.RegisterFunc(configMigrateAuthToConnection, handlerMigrateAuthToConnection)
@@ -35,50 +30,15 @@ func cephStorageClassLabelUpdate(cephStorageClass *v1alpha1.CephStorageClass, cl
3530
cephStorageClass.Labels = make(map[string]string)
3631
}
3732

38-
cephStorageClass.Labels[MigratedLabel] = MigratedLabelValue
33+
cephStorageClass.Labels[CephClusterAuthenticationMigratedLabel] = CephClusterAuthenticationMigratedLabelValue
3934
err := cl.Update(ctx, cephStorageClass)
4035
return err
4136
}
4237

43-
func NewKubeClient() (client.Client, error) {
44-
var config *rest.Config
45-
var err error
46-
47-
config, err = clientcmd.BuildConfigFromFlags("", "")
48-
49-
if err != nil {
50-
return nil, err
51-
}
52-
53-
var (
54-
resourcesSchemeFuncs = []func(*apiruntime.Scheme) error{
55-
v1alpha1.AddToScheme,
56-
clientgoscheme.AddToScheme,
57-
extv1.AddToScheme,
58-
v1.AddToScheme,
59-
sv1.AddToScheme,
60-
}
61-
)
62-
63-
scheme := apiruntime.NewScheme()
64-
for _, f := range resourcesSchemeFuncs {
65-
err = f(scheme)
66-
if err != nil {
67-
return nil, err
68-
}
69-
}
70-
71-
clientOpts := client.Options{
72-
Scheme: scheme,
73-
}
74-
75-
return client.New(config, clientOpts)
76-
}
77-
7838
func handlerMigrateAuthToConnection(ctx context.Context, input *pkg.HookInput) error {
7939
fmt.Printf("[csi-ceph-migration-from-ceph-cluster-authentication]: Started migration from CephClusterAuthentication\n")
8040

81-
cl, err := NewKubeClient()
41+
cl, err := funcs.NewKubeClient()
8242
if err != nil {
8343
fmt.Printf("%s", err.Error())
8444
return err
@@ -95,7 +55,7 @@ func handlerMigrateAuthToConnection(ctx context.Context, input *pkg.HookInput) e
9555
for _, cephStorageClass := range cephStorageClassList.Items {
9656
fmt.Printf("[csi-ceph-migration-from-ceph-cluster-authentication]: Migrating %s\n", cephStorageClass.Name)
9757

98-
if cephStorageClass.Labels[MigratedLabel] == MigratedLabelValue {
58+
if cephStorageClass.Labels[CephClusterAuthenticationMigratedLabel] == CephClusterAuthenticationMigratedLabelValue {
9959
fmt.Printf("[csi-ceph-migration-from-ceph-cluster-authentication]: %s already migrated\n", cephStorageClass.Name)
10060
continue
10161
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package hooks_common
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
oldApi "csi-ceph/api/v1alpha1"
8+
9+
"github.com/deckhouse/csi-ceph/api/v1alpha1"
10+
"github.com/deckhouse/module-sdk/pkg"
11+
"github.com/deckhouse/module-sdk/pkg/registry"
12+
13+
funcs "csi-ceph/funcs"
14+
15+
"sigs.k8s.io/controller-runtime/pkg/client"
16+
)
17+
18+
const (
19+
CephClusterAuthenticationMigratedLabel = "storage.deckhouse.io/migratedFromCephClusterAuthentication"
20+
CephClusterAuthenticationMigratedLabelValue = "true"
21+
CephCSIMigratedLabel = "storage.deckhouse.io/migratedFromCephCSI"
22+
CephCSIMigratedLabelValue = "true"
23+
)
24+
25+
var _ = registry.RegisterFunc(configMigrateAuthToConnection, handlerMigrateFromCephCsiModule)
26+
27+
var configMigrateAuthToConnection = &pkg.HookConfig{
28+
OnBeforeHelm: &pkg.OrderedConfig{Order: 5},
29+
}
30+
31+
func handlerMigrateFromCephCsiModule(_ context.Context, input *pkg.HookInput) error {
32+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: Started migration from Ceph CSI module\n")
33+
34+
ctx := context.Background()
35+
cl, err := funcs.NewKubeClient()
36+
if err != nil {
37+
fmt.Printf("%s", err.Error())
38+
return err
39+
}
40+
41+
cephCSIDriverList := &oldApi.CephCSIDriverList{}
42+
43+
err = cl.List(ctx, cephCSIDriverList)
44+
if err != nil {
45+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: CephCSIDriverList get error %s\n", err)
46+
return err
47+
}
48+
49+
for _, cephCSIDriver := range cephCSIDriverList.Items {
50+
if cephCSIDriver.Labels[CephCSIMigratedLabel] == CephCSIMigratedLabelValue {
51+
continue
52+
}
53+
54+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: Migrating %s\n", cephCSIDriver.Name)
55+
56+
cephClusterConnectionName := cephCSIDriver.Name
57+
58+
cephClusterConnection := &v1alpha1.CephClusterConnection{}
59+
60+
err = cl.Get(ctx, client.ObjectKey{Name: cephClusterConnectionName}, cephClusterConnection)
61+
if err != nil {
62+
if client.IgnoreNotFound(err) == nil {
63+
cephClusterConnection.Name = cephClusterConnectionName
64+
cephClusterConnection.ObjectMeta.Labels = map[string]string{CephClusterAuthenticationMigratedLabel: CephClusterAuthenticationMigratedLabelValue}
65+
cephClusterConnection.Spec = v1alpha1.CephClusterConnectionSpec{
66+
Monitors: cephCSIDriver.Spec.Monitors,
67+
ClusterID: cephCSIDriver.Spec.ClusterID,
68+
UserID: cephCSIDriver.Spec.UserID,
69+
UserKey: cephCSIDriver.Spec.UserKey}
70+
err := cl.Create(ctx, cephClusterConnection)
71+
if err != nil {
72+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: CephStorageClass create error %s\n", err)
73+
return err
74+
}
75+
} else {
76+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: CephClusterConnection get error %s\n", err)
77+
return err
78+
}
79+
} else {
80+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: CephClusterConnection already exists\n")
81+
}
82+
83+
if cephCSIDriver.Spec.CephFS != nil {
84+
for _, cephFsStorageClass := range cephCSIDriver.Spec.CephFS.StorageClasses {
85+
cephStorageClassName := fmt.Sprintf("%s-%s", cephClusterConnectionName, cephFsStorageClass.NamePostfix)
86+
cephStorageClass := &v1alpha1.CephStorageClass{}
87+
88+
err = cl.Get(ctx, client.ObjectKey{Name: cephStorageClassName}, cephStorageClass)
89+
if err != nil {
90+
if client.IgnoreNotFound(err) == nil {
91+
cephStorageClass.Name = cephStorageClassName
92+
cephStorageClass.Spec = v1alpha1.CephStorageClassSpec{
93+
ClusterConnectionName: cephClusterConnectionName,
94+
Type: v1alpha1.CephStorageClassTypeCephFS,
95+
CephFS: &v1alpha1.CephStorageClassCephFS{FSName: cephFsStorageClass.FsName, Pool: cephFsStorageClass.Pool},
96+
ReclaimPolicy: cephFsStorageClass.ReclaimPolicy}
97+
err := cl.Create(ctx, cephStorageClass)
98+
if err != nil {
99+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: CephStorageClass create error %s\n", err)
100+
return err
101+
}
102+
} else {
103+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: CephStorageClass get error %s\n", err)
104+
return err
105+
}
106+
} else {
107+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: CephStorageClass already exists\n")
108+
}
109+
}
110+
}
111+
112+
if cephCSIDriver.Spec.RBD != nil {
113+
for _, cephRbdStorageClass := range cephCSIDriver.Spec.RBD.StorageClasses {
114+
cephStorageClassName := fmt.Sprintf("%s-%s", cephClusterConnectionName, cephRbdStorageClass.NamePostfix)
115+
cephStorageClass := &v1alpha1.CephStorageClass{}
116+
117+
err = cl.Get(ctx, client.ObjectKey{Name: cephStorageClassName}, cephStorageClass)
118+
if err != nil {
119+
if client.IgnoreNotFound(err) == nil {
120+
cephStorageClass.Name = cephStorageClassName
121+
cephStorageClass.Spec = v1alpha1.CephStorageClassSpec{
122+
ClusterConnectionName: cephClusterConnectionName,
123+
Type: v1alpha1.CephStorageClassTypeRBD,
124+
RBD: &v1alpha1.CephStorageClassRBD{Pool: cephRbdStorageClass.Pool, DefaultFSType: cephRbdStorageClass.DefaultFSType},
125+
ReclaimPolicy: cephRbdStorageClass.ReclaimPolicy}
126+
err := cl.Create(ctx, cephStorageClass)
127+
if err != nil {
128+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: CephStorageClass create error %s\n", err)
129+
return err
130+
}
131+
} else {
132+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: CephStorageClass get error %s\n", err)
133+
return err
134+
}
135+
} else {
136+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: CephStorageClass already exists %s\n", err)
137+
}
138+
}
139+
}
140+
141+
if cephCSIDriver.Labels == nil {
142+
cephCSIDriver.Labels = make(map[string]string)
143+
}
144+
cephCSIDriver.Labels[CephCSIMigratedLabel] = CephCSIMigratedLabelValue
145+
err = cl.Update(ctx, &cephCSIDriver)
146+
if err != nil {
147+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: CephCSIDriver update error %s\n", err)
148+
return err
149+
}
150+
}
151+
152+
fmt.Printf("[csi-ceph-migration-from-ceph-csi-module]: Finished migration from Ceph CSI module\n")
153+
154+
return nil
155+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package hooks_common
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/deckhouse/module-sdk/pkg"
8+
"github.com/deckhouse/module-sdk/pkg/registry"
9+
10+
funcs "csi-ceph/funcs"
11+
12+
v1 "k8s.io/api/core/v1"
13+
"sigs.k8s.io/controller-runtime/pkg/client"
14+
)
15+
16+
const (
17+
configMapName = "ceph-csi-config"
18+
namespace = "d8-csi-ceph"
19+
)
20+
21+
var _ = registry.RegisterFunc(configMigrateAuthToConnection, handlerMigrateFromCephCsiModule)
22+
23+
var configMigrateAuthToConnection = &pkg.HookConfig{
24+
OnAfterDeleteHelm: &pkg.OrderedConfig{Order: 5},
25+
}
26+
27+
func handlerMigrateFromCephCsiModule(_ context.Context, input *pkg.HookInput) error {
28+
fmt.Printf("[csi-ceph-remove-finalizers-on-module-delete]: removing finalizers\n")
29+
30+
cephConfigMap := &v1.ConfigMap{}
31+
32+
ctx := context.Background()
33+
cl, err := funcs.NewKubeClient()
34+
if err != nil {
35+
fmt.Printf("%s", err.Error())
36+
return err
37+
}
38+
39+
err = cl.Get(ctx, client.ObjectKey{Name: configMapName, Namespace: namespace}, cephConfigMap)
40+
if err != nil {
41+
if client.IgnoreNotFound(err) == nil {
42+
fmt.Printf("[csi-ceph-remove-finalizers-on-module-delete]: configmap %s is absent, all ok\n", configMapName)
43+
} else {
44+
fmt.Printf("[csi-ceph-remove-finalizers-on-module-delete]: configmap %s get error %s\n", configMapName, err)
45+
return err
46+
}
47+
} else {
48+
cephConfigMap.Finalizers = nil
49+
err = cl.Update(ctx, cephConfigMap)
50+
if err != nil {
51+
fmt.Printf("[csi-ceph-remove-finalizers-on-module-delete]: configmap %s update error %s\n", configMapName, err)
52+
return err
53+
}
54+
}
55+
56+
fmt.Printf("[csi-ceph-remove-finalizers-on-module-delete]: finalizers removed\n")
57+
58+
return nil
59+
}

0 commit comments

Comments
 (0)