Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Niladri Halder <niladri.halder26@gmail.com>
  • Loading branch information
niladrih committed Jul 26, 2023
1 parent 6f99521 commit b328054
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 42 deletions.
34 changes: 0 additions & 34 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package driver

import (
"fmt"
"github.com/openebs/zfs-localpv/pkg/patcher"
"k8s.io/apimachinery/pkg/runtime/schema"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -87,38 +85,6 @@ func NewController(d *CSIDriver) csi.ControllerServer {
}

func (cs *controller) init() error {
// CRD upgrade
// Ref: https://github.com/openebs/zfs-localpv/pull/439
// Ref: https://github.com/openebs/zfs-localpv/pull/457
crdGvr := schema.GroupVersionResource{
Group: "apiextensions.k8s.io",
Version: "v1",
Resource: "CustomResourceDefinition",
}

oldCrd, _ := patcher.OldZfsVolumesCrd()
newCrd, _ := patcher.NewZfsVolumesCrd()
patcher.PatchCrdOrIgnore(crdGvr, "").WithMergePatchFrom(
"zfsvolumes.zfs.openebs.io",
oldCrd,
newCrd,
)

oldCrd, _ = patcher.OldZfsSnapshotsCrd()
newCrd, _ = patcher.NewZfsSnapshotsCrd()
patcher.PatchCrdOrIgnore(crdGvr, "").WithMergePatchFrom(
"zfssnapshots.zfs.openebs.io",
oldCrd,
newCrd,
)

oldCrd, _ = patcher.OldZfsRestoresCrd()
newCrd, _ = patcher.NewZfsRestoresCrd()
patcher.PatchCrdOrIgnore(crdGvr, "").WithMergePatchFrom(
"zfsrestores.zfs.openebs.io",
oldCrd,
newCrd,
)

cfg, err := k8sapi.Config().Get()
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package driver
import (
"github.com/container-storage-interface/spec/lib/go/csi"
config "github.com/openebs/zfs-localpv/pkg/config"
"github.com/openebs/zfs-localpv/pkg/patcher"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/klog/v2"
)

Expand Down Expand Up @@ -64,6 +66,39 @@ func New(config *config.Config) *CSIDriver {

switch config.PluginType {
case "controller":
// CRD upgrade
// Ref: https://github.com/openebs/zfs-localpv/pull/439
// Ref: https://github.com/openebs/zfs-localpv/pull/457
crdGvr := schema.GroupVersionResource{
Group: "apiextensions.k8s.io",
Version: "v1",
Resource: "CustomResourceDefinition",
}

oldCrd, _ := patcher.OldZfsVolumesCrd()
newCrd, _ := patcher.NewZfsVolumesCrd()
patcher.PatchCrdOrIgnore(crdGvr, "").WithMergePatchFrom(
"zfsvolumes.zfs.openebs.io",
oldCrd,
newCrd,
)

oldCrd, _ = patcher.OldZfsSnapshotsCrd()
newCrd, _ = patcher.NewZfsSnapshotsCrd()
patcher.PatchCrdOrIgnore(crdGvr, "").WithMergePatchFrom(
"zfssnapshots.zfs.openebs.io",
oldCrd,
newCrd,
)

oldCrd, _ = patcher.OldZfsRestoresCrd()
newCrd, _ = patcher.NewZfsRestoresCrd()
patcher.PatchCrdOrIgnore(crdGvr, "").WithMergePatchFrom(
"zfsrestores.zfs.openebs.io",
oldCrd,
newCrd,
)

driver.cs = NewController(driver)

case "agent":
Expand Down
10 changes: 5 additions & 5 deletions pkg/patcher/patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package patcher
import (
"context"
"fmt"
"github.com/openebs/lib-csi/pkg/common/errors"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
k8stypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"os"
)

type Patchable struct {
Expand All @@ -20,13 +20,13 @@ type Patchable struct {
func PatchCrdOrIgnore(gvr schema.GroupVersionResource, namespace string) *Patchable {
cfg, err := rest.InClusterConfig()
if err != nil {
errors.Errorf("failed to generate in-cluster config: %s", err.Error())
fmt.Fprintf(os.Stderr, "failed to generate in-cluster config: %s", err.Error())
return nil
}

client, err := dynamic.NewForConfig(cfg)
if err != nil {
errors.Errorf("failed to generate in-cluster kubernetes client: %s", err.Error())
fmt.Fprintf(os.Stderr, "failed to generate in-cluster kubernetes client", err.Error())
return nil
}

Expand All @@ -40,12 +40,12 @@ func (p *Patchable) WithMergePatchFrom(name string, original, modified []byte) {

patch, err := strategicpatch.CreateTwoWayMergePatch(original, modified, apiextensions.CustomResourceDefinition{})
if err != nil {
errors.Errorf("failed to create merge patch for %s: %s", name, err.Error())
fmt.Fprintf(os.Stderr, "failed to create merge patch for %s: %s", name, err.Error())
return
}
_, err = p.inner.Patch(context.TODO(), name, k8stypes.MergePatchType, patch, metav1.PatchOptions{})
if err != nil {
errors.Errorf("failed to patch resource %s: %s", name, err.Error())
fmt.Fprintf(os.Stderr, "failed to patch resource %s: %s", name, err.Error())
return
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/patcher/zfsrestore-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ spec:
data. For instance, if the Volume was created with "off" and the
next day the compression was modified to "on", the data written
prior to setting "on" will not be compressed. Default Value: off.'
pattern: ^(on|off|lzjb|zstd|gzip|gzip-[1-9]|zle|lz4|halder)$
pattern: ^(on|off|lzjb|zstd|gzip|gzip-[1-9]|zle|lz4)$
type: string
dedup:
description: 'Deduplication is the process for removing redundant
Expand Down
2 changes: 1 addition & 1 deletion pkg/patcher/zfssnapshot-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ spec:
data. For instance, if the Volume was created with "off" and the
next day the compression was modified to "on", the data written
prior to setting "on" will not be compressed. Default Value: off.'
pattern: ^(on|off|lzjb|zstd|gzip|gzip-[1-9]|zle|lz4|niladri)$
pattern: ^(on|off|lzjb|zstd|gzip|gzip-[1-9]|zle|lz4)$
type: string
dedup:
description: 'Deduplication is the process for removing redundant
Expand Down
2 changes: 1 addition & 1 deletion pkg/patcher/zfsvolume-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ spec:
data. For instance, if the Volume was created with "off" and the
next day the compression was modified to "on", the data written
prior to setting "on" will not be compressed. Default Value: off.'
pattern: ^(on|off|lzjb|zstd|gzip|gzip-[1-9]|zle|lz4|maa)$
pattern: ^(on|off|lzjb|zstd|gzip|gzip-[1-9]|zle|lz4)$
type: string
dedup:
description: 'Deduplication is the process for removing redundant
Expand Down

0 comments on commit b328054

Please sign in to comment.