Skip to content

Commit bc8f58f

Browse files
committed
Use Patch operation for PV updates
1 parent 7a6acd9 commit bc8f58f

File tree

14 files changed

+531
-63
lines changed

14 files changed

+531
-63
lines changed

.golangci.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1+
version: 2
2+
3+
run:
4+
# Skip files and directories
5+
skip-files:
6+
- ".*_test\\.go$" # Skip all test files
7+
skip-dirs:
8+
- "tests/e2e" # Skip e2e test directory
9+
110
linters:
211
disable-all: true
312
enable:
413
- errcheck
5-
- gosimple
614
- govet
715
- ineffassign
816
- staticcheck
9-
- typecheck
1017
- unused
1118
- misspell
1219
- lll
20+
1321
linters-settings:
1422
lll:
1523
# max line length, lines longer will be reported. Default is 120.

hack/check-golangci-lint.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ shift $((OPTIND-1))
5353

5454
export GOOS=linux
5555
if [ ! "${DO_DOCKER-}" ]; then
56-
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$(go env GOPATH)"/bin v1.64.8
56+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$(go env GOPATH)"/bin v2.6.2
5757
"$(go env GOPATH)"/bin/golangci-lint run -v --timeout=1200s
5858
else
59-
docker run --rm -v "$(pwd)":/app -w /app golangci/golangci-lint:v1.64.8 golangci-lint run -v --timeout=1200s
59+
docker run --rm -v "$(pwd)":/app -w /app golangci/golangci-lint:v2.6.2 golangci-lint run -v --timeout=1200s
6060
fi

pkg/csi/service/common/commonco/k8sorchestrator/k8sorchestrator.go

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package k8sorchestrator
1919
import (
2020
"context"
2121
"crypto/sha256"
22+
"encoding/json"
2223
"errors"
2324
"fmt"
2425
"os"
@@ -44,6 +45,7 @@ import (
4445
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
4546
"k8s.io/apimachinery/pkg/runtime"
4647
k8stypes "k8s.io/apimachinery/pkg/types"
48+
"k8s.io/apimachinery/pkg/util/strategicpatch"
4749
"k8s.io/client-go/informers"
4850
clientset "k8s.io/client-go/kubernetes"
4951
restclient "k8s.io/client-go/rest"
@@ -2173,15 +2175,36 @@ func (c *K8sOrchestrator) PreLinkedCloneCreateAction(ctx context.Context, pvcNam
21732175
return err
21742176
}
21752177

2176-
if linkedClonePVC.Labels == nil {
2177-
linkedClonePVC.Labels = make(map[string]string)
2178+
oldData, err := json.Marshal(linkedClonePVC)
2179+
if err != nil {
2180+
log.Errorf("Failed to marshal PVC %s/%s: %v", pvcNamespace, pvcName, err)
2181+
return err
2182+
}
2183+
2184+
newPVC := linkedClonePVC.DeepCopy()
2185+
if newPVC.Labels == nil {
2186+
newPVC.Labels = make(map[string]string)
21782187
}
21792188
// Add label
2180-
if _, ok := linkedClonePVC.Labels[common.AnnKeyLinkedClone]; !ok {
2181-
linkedClonePVC.Labels[common.LinkedClonePVCLabel] = linkedClonePVC.Annotations[common.AttributeIsLinkedClone]
2189+
if _, ok := newPVC.Labels[common.AnnKeyLinkedClone]; !ok {
2190+
newPVC.Labels[common.LinkedClonePVCLabel] = newPVC.Annotations[common.AttributeIsLinkedClone]
2191+
}
2192+
2193+
newData, err := json.Marshal(newPVC)
2194+
if err != nil {
2195+
log.Errorf("Failed to marshal updated PVC %s/%s with labels: %v", pvcNamespace, pvcName, err)
2196+
return err
21822197
}
21832198

2184-
_, err = c.k8sClient.CoreV1().PersistentVolumeClaims(pvcNamespace).Update(ctx, linkedClonePVC, metav1.UpdateOptions{})
2199+
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, linkedClonePVC)
2200+
if err != nil {
2201+
log.Errorf("Error creating two way merge patch for PVC %s/%s with error: %v", pvcNamespace, pvcName, err)
2202+
return err
2203+
}
2204+
2205+
_, err = c.k8sClient.CoreV1().PersistentVolumeClaims(pvcNamespace).Patch(ctx, linkedClonePVC.Name,
2206+
k8stypes.StrategicMergePatchType,
2207+
patchBytes, metav1.PatchOptions{})
21852208
if err != nil {
21862209
log.Errorf("failed to add linked clone label for PVC %s/%s. Error: %+v, retrying...",
21872210
pvcNamespace, pvcName, err)
@@ -2229,11 +2252,36 @@ func (c *K8sOrchestrator) UpdatePersistentVolumeLabel(ctx context.Context,
22292252
if err != nil {
22302253
return fmt.Errorf("error getting PV %s from API server: %w", pvName, err)
22312254
}
2232-
if pv.Labels == nil {
2233-
pv.Labels = make(map[string]string)
2255+
2256+
oldData, err := json.Marshal(pv)
2257+
if err != nil {
2258+
errMsg := fmt.Sprintf("Failed to marshal PV %s: %v", pvName, err)
2259+
log.Error(errMsg)
2260+
return err
2261+
}
2262+
2263+
newPV := pv.DeepCopy()
2264+
if newPV.Labels == nil {
2265+
newPV.Labels = make(map[string]string)
2266+
}
2267+
newPV.Labels[key] = value
2268+
2269+
newData, err := json.Marshal(newPV)
2270+
if err != nil {
2271+
errMsg := fmt.Sprintf("Failed to marshal updated PV %s with labels: %v", pvName, err)
2272+
log.Error(errMsg)
2273+
return err
22342274
}
2235-
pv.Labels[key] = value
2236-
_, err = c.k8sClient.CoreV1().PersistentVolumes().Update(ctx, pv, metav1.UpdateOptions{})
2275+
2276+
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, pv)
2277+
if err != nil {
2278+
errMsg := fmt.Sprintf("Error creating two way merge patch for PV %s with error: %v", pvName, err)
2279+
log.Error(errMsg)
2280+
return err
2281+
}
2282+
2283+
_, err = c.k8sClient.CoreV1().PersistentVolumes().Patch(ctx, pv.Name, k8stypes.StrategicMergePatchType,
2284+
patchBytes, metav1.PatchOptions{})
22372285
if err != nil {
22382286
errMsg := fmt.Sprintf("error updating PV %s with labels %s/%s. Error: %v", pvName, key, value, err)
22392287
log.Error(errMsg)

pkg/csi/service/common/commonco/k8sorchestrator/k8sorchestrator_helper.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"time"
2424

2525
k8stypes "k8s.io/apimachinery/pkg/types"
26+
"k8s.io/apimachinery/pkg/util/strategicpatch"
2627
"k8s.io/apimachinery/pkg/util/wait"
2728

2829
v1 "k8s.io/api/core/v1"
@@ -83,17 +84,38 @@ func (c *K8sOrchestrator) updatePVCAnnotations(ctx context.Context,
8384
return err
8485
}
8586

87+
oldData, err := json.Marshal(pvcObj)
88+
if err != nil {
89+
log.Errorf("failed to marshal PVC %s/%s: %v", pvcNamespace, pvcName, err)
90+
return err
91+
}
92+
93+
newPVC := pvcObj.DeepCopy()
8694
for key, val := range annotations {
8795
// If value is not set, remove the annotation.
8896
if val == "" {
89-
delete(pvcObj.ObjectMeta.Annotations, key)
97+
delete(newPVC.ObjectMeta.Annotations, key)
9098
log.Debugf("Removing annotation %s on pvc %s/%s", key, pvcNamespace, pvcName)
9199
} else {
92-
metav1.SetMetaDataAnnotation(&pvcObj.ObjectMeta, key, val)
100+
metav1.SetMetaDataAnnotation(&newPVC.ObjectMeta, key, val)
93101
log.Debugf("Updating annotation %s on pvc %s/%s to value: %s", key, pvcNamespace, pvcName, val)
94102
}
95103
}
96-
_, err = c.k8sClient.CoreV1().PersistentVolumeClaims(pvcNamespace).Update(ctx, pvcObj, metav1.UpdateOptions{})
104+
105+
newData, err := json.Marshal(newPVC)
106+
if err != nil {
107+
log.Errorf("failed to marshal updated PVC %s/%s with annotations: %v", pvcNamespace, pvcName, err)
108+
return err
109+
}
110+
111+
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, pvcObj)
112+
if err != nil {
113+
log.Errorf("error creating two way merge patch for PVC %s/%s: %v", pvcNamespace, pvcName, err)
114+
return err
115+
}
116+
117+
_, err = c.k8sClient.CoreV1().PersistentVolumeClaims(pvcNamespace).Patch(ctx, pvcObj.Name,
118+
k8stypes.StrategicMergePatchType, patchBytes, metav1.PatchOptions{})
97119
if err != nil {
98120
log.Errorf("failed to update pvc annotations %s/%s with err:%+v", pvcNamespace, pvcName, err)
99121
return err

pkg/csi/service/wcpguest/controller.go

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package wcpguest
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"fmt"
2223
"math"
2324
"net/http"
@@ -48,6 +49,7 @@ import (
4849
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4950
"k8s.io/apimachinery/pkg/fields"
5051
"k8s.io/apimachinery/pkg/types"
52+
"k8s.io/apimachinery/pkg/util/strategicpatch"
5153
"k8s.io/apimachinery/pkg/watch"
5254
clientset "k8s.io/client-go/kubernetes"
5355
"k8s.io/client-go/rest"
@@ -622,10 +624,37 @@ func (c *controller) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequ
622624
if finalizer == cnsoperatortypes.CNSVolumeFinalizer {
623625
log.Infof("Removing %q finalizer from PersistentVolumeClaim with name: %q on namespace: %q",
624626
cnsoperatortypes.CNSVolumeFinalizer, svPVC.Name, svPVC.Namespace)
625-
svPVC.ObjectMeta.Finalizers = slices.Delete(svPVC.ObjectMeta.Finalizers, i, i+1)
627+
628+
oldData, err := json.Marshal(svPVC)
629+
if err != nil {
630+
msg := fmt.Sprintf("failed to marshal supervisor PVC %q in %q namespace. Error: %+v",
631+
req.VolumeId, c.supervisorNamespace, err)
632+
log.Error(msg)
633+
return nil, csifault.CSIInternalFault, status.Error(codes.Internal, msg)
634+
}
635+
636+
newPVC := svPVC.DeepCopy()
637+
newPVC.ObjectMeta.Finalizers = slices.Delete(newPVC.ObjectMeta.Finalizers, i, i+1)
638+
639+
newData, err := json.Marshal(newPVC)
640+
if err != nil {
641+
msg := fmt.Sprintf("failed to marshal updated supervisor PVC %q in %q namespace. Error: %+v",
642+
req.VolumeId, c.supervisorNamespace, err)
643+
log.Error(msg)
644+
return nil, csifault.CSIInternalFault, status.Error(codes.Internal, msg)
645+
}
646+
647+
patchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldData, newData, svPVC)
648+
if err != nil {
649+
msg := fmt.Sprintf("error creating two way merge patch for supervisor PVC %q in %q namespace. Error: %+v",
650+
req.VolumeId, c.supervisorNamespace, err)
651+
log.Error(msg)
652+
return nil, csifault.CSIInternalFault, status.Error(codes.Internal, msg)
653+
}
654+
626655
// Update the instance after removing finalizer
627-
_, err := c.supervisorClient.CoreV1().PersistentVolumeClaims(c.supervisorNamespace).Update(ctx, svPVC,
628-
metav1.UpdateOptions{})
656+
_, err = c.supervisorClient.CoreV1().PersistentVolumeClaims(c.supervisorNamespace).Patch(ctx,
657+
svPVC.Name, types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{})
629658
if err != nil {
630659
msg := fmt.Sprintf("failed to update supervisor PVC %q in %q namespace. Error: %+v",
631660
req.VolumeId, c.supervisorNamespace, err)
@@ -1466,14 +1495,38 @@ func (c *controller) ControllerExpandVolume(ctx context.Context, req *csi.Contro
14661495
switch (gcPvcRequestSize).Cmp(svPvcRequestSize) {
14671496
case 1:
14681497
// Update requested storage in SV PVC spec
1469-
svPvcClone := svPVC.DeepCopy()
1470-
svPvcClone.Spec.Resources.Requests[corev1.ResourceName(corev1.ResourceStorage)] = *gcPvcRequestSize
1498+
oldExpandData, err := json.Marshal(svPVC)
1499+
if err != nil {
1500+
msg := fmt.Sprintf("failed to marshal supervisor PVC for expansion %q in %q namespace. Error: %+v",
1501+
volumeID, c.supervisorNamespace, err)
1502+
log.Error(msg)
1503+
return nil, csifault.CSIInternalFault, status.Error(codes.Internal, msg)
1504+
}
1505+
1506+
newExpandPVC := svPVC.DeepCopy()
1507+
newExpandPVC.Spec.Resources.Requests[corev1.ResourceName(corev1.ResourceStorage)] = *gcPvcRequestSize
1508+
1509+
newExpandData, err := json.Marshal(newExpandPVC)
1510+
if err != nil {
1511+
msg := fmt.Sprintf("failed to marshal updated supervisor PVC for expansion %q in %q namespace. Error: %+v",
1512+
volumeID, c.supervisorNamespace, err)
1513+
log.Error(msg)
1514+
return nil, csifault.CSIInternalFault, status.Error(codes.Internal, msg)
1515+
}
1516+
1517+
expandPatchBytes, err := strategicpatch.CreateTwoWayMergePatch(oldExpandData, newExpandData, svPVC)
1518+
if err != nil {
1519+
msg := fmt.Sprintf("error creating two way merge patch for supervisor PVC expansion %q in %q namespace. Error: %+v",
1520+
volumeID, c.supervisorNamespace, err)
1521+
log.Error(msg)
1522+
return nil, csifault.CSIInternalFault, status.Error(codes.Internal, msg)
1523+
}
14711524

14721525
// Make an update call to SV API server
14731526
log.Infof("Increasing the size of supervisor PVC %s in namespace %s to %s",
14741527
volumeID, c.supervisorNamespace, gcPvcRequestSize.String())
1475-
svPVC, err = c.supervisorClient.CoreV1().PersistentVolumeClaims(c.supervisorNamespace).Update(
1476-
ctx, svPvcClone, metav1.UpdateOptions{})
1528+
svPVC, err = c.supervisorClient.CoreV1().PersistentVolumeClaims(c.supervisorNamespace).Patch(
1529+
ctx, svPVC.Name, types.StrategicMergePatchType, expandPatchBytes, metav1.PatchOptions{})
14771530
if err != nil {
14781531
msg := fmt.Sprintf("failed to update supervisor PVC %q in %q namespace. Error: %+v",
14791532
volumeID, c.supervisorNamespace, err)

0 commit comments

Comments
 (0)