Skip to content

Commit 959023a

Browse files
Resolved merge conflicts
2 parents d76a2fb + 0708c51 commit 959023a

38 files changed

Lines changed: 2219 additions & 19 deletions

cmd/csi_driver/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"time"
2929

3030
"cloud.google.com/go/profiler"
31+
"github.com/KimMachineGun/automemlimit/memlimit"
3132
"github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/cloud_provider/auth"
3233
"github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/cloud_provider/clientset"
3334
"github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/cloud_provider/metadata"
@@ -70,6 +71,8 @@ var (
7071
enableSidecarBucketAccessCheck = flag.Bool("enable-sidecar-bucket-access-check", false, "Enable bucket access check on sidecar, this does not disable bucket access check in node driver.")
7172
enableCloudProfilerForDriver = flag.Bool("enable-cloud-profiler-for-driver", false, "Enable cloud profiler to collect analysis data.")
7273
assumeGoodSidecarVersion = flag.Bool("assume-good-sidecar-version", false, "Assume the sidecar version is compatible with all features in the running version of the driver.")
74+
enableAutoGoMemLimit = flag.Bool("enable-auto-gomemlimit", false, "Automatically set GOMEMLIMIT to a percentage of the container's cgroup memory limit.")
75+
autoGoMemLimitRatio = flag.Float64("auto-gomemlimit-ratio", util.GoMemLimitCgroupPercentage, "The ratio of the container's cgroup memory limit to set as GOMEMLIMIT when enable-auto-gomemlimit is enabled.")
7376

7477
// GCSFuse kernel params feature.
7578
enableGCSFuseKernelParams = flag.Bool("enable-gcsfuse-kernel-params", false, "Enable gcsfuse kernel params feature.")
@@ -101,6 +104,20 @@ func main() {
101104
klog.InitFlags(nil)
102105
flag.Parse()
103106

107+
if *enableAutoGoMemLimit {
108+
// Sets GOMEMLIMIT to a percentage of the cgroup limit.
109+
// If cgroup limits are unavailable, it falls back to doing nothing
110+
// (no limit).
111+
if _, err := memlimit.SetGoMemLimitWithOpts(
112+
memlimit.WithRatio(*autoGoMemLimitRatio),
113+
memlimit.WithProvider(memlimit.FromCgroup),
114+
); err != nil {
115+
// This can happen if the gcs-fuse-csi-driver container memory
116+
// limit is not set.
117+
klog.Warningf("Failed to automatically set GOMEMLIMIT: %v", err)
118+
}
119+
}
120+
104121
// All CSI sidecars use http-endpoint for metrics and health checks.
105122
// Example: https://gke-internal.googlesource.com/third_party/kubernetes-csi/livenessprobe/+/refs/heads/master/cmd/livenessprobe/main.go#113
106123
// At some point, we should replace "metrics-endpoint" with "http-endpoint".
@@ -221,6 +238,10 @@ func main() {
221238
EnableGcsfuseProfilesInternal: *enableGcsfuseProfilesInternal,
222239
},
223240
EnableGCSFuseKernelParams: *enableGCSFuseKernelParams,
241+
GoMemLimitOptions: &driver.GoMemLimitOptions{
242+
EnableAutoGoMemLimit: *enableAutoGoMemLimit,
243+
AutoGoMemLimitRatio: *autoGoMemLimitRatio,
244+
},
224245
}
225246

226247
var mounter mount.Interface

cmd/sidecar_mounter/gcsfuse_binary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
gs://gke-release-staging/gcsfuse/v3.9.0-gke.0/gcsfuse_bin
1+
gs://gke-release-staging/gcsfuse/v3.9.1-gke.0/gcsfuse_bin

cmd/webhook/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import (
2222
"net/http"
2323
"time"
2424

25+
"github.com/KimMachineGun/automemlimit/memlimit"
2526
"github.com/go-logr/logr"
27+
"github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/util"
2628
wh "github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/webhook"
2729
"k8s.io/apimachinery/pkg/runtime"
2830
"k8s.io/apimachinery/pkg/util/version"
@@ -56,6 +58,8 @@ var (
5658
metadataSidecarImage = flag.String("metadata-sidecar-image", "", "The metadata prefetch sidecar container image.")
5759
injectSAVol = flag.Bool("should-inject-sa-vol", false, "Inject projected service account volume when true")
5860
enableGcsfuseProfiles = flag.Bool("enable-gcsfuse-profiles", false, "Enable gcsfuse profiles when true")
61+
enableAutoGoMemLimit = flag.Bool("enable-auto-gomemlimit", false, "Automatically set GOMEMLIMIT to a percentage of the container's cgroup memory limit.")
62+
autoGoMemLimitRatio = flag.Float64("auto-gomemlimit-ratio", util.GoMemLimitCgroupPercentage, "The ratio of the container's cgroup memory limit to set as GOMEMLIMIT when enable-auto-gomemlimit is enabled.")
5963
metadataMemoryRequest = flag.String("metadata-sidecar-memory-request", "10Mi", "Flag to use default value for gcsfuse memory prefetch sidecar container memory request.")
6064
metadataMemoryLimit = flag.String("metadata-sidecar-memory-limit", "10Mi", "Flag to use default value for gcsfuse memory prefetch sidecar container memory limit.")
6165
metadataPrefetchCPURequest = flag.String("metadata-sidecar-cpu-request", "10m", "The default cpu request for gcsfuse memory prefetch sidecar container cpu request.")
@@ -78,6 +82,20 @@ func main() {
7882
// This line prevents controller-runtime from complaining about log.SetLogger never being called
7983
log.SetLogger(logr.New(log.NullLogSink{}))
8084

85+
if *enableAutoGoMemLimit {
86+
// Sets GOMEMLIMIT to a percentage of the cgroup limit.
87+
// If cgroup limits are unavailable, it falls back to doing nothing
88+
// (no limit).
89+
if _, err := memlimit.SetGoMemLimitWithOpts(
90+
memlimit.WithRatio(*autoGoMemLimitRatio),
91+
memlimit.WithProvider(memlimit.FromCgroup),
92+
); err != nil {
93+
// This can happen if the gcs-fuse-csi-driver-webhook container
94+
// memory limit is not set.
95+
klog.Warningf("Failed to automatically set GOMEMLIMIT: %v", err)
96+
}
97+
}
98+
8199
klog.Infof("Running Google Cloud Storage FUSE CSI driver admission webhook version %v, sidecar container image %v", webhookVersion, *sidecarImage)
82100

83101
// Load webhook config

deploy/base/controller/controller.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ spec:
4545
- "--cluster-location=$(CLUSTER_LOCATION)"
4646
- "--project-number=$(PROJECT_NUMBER)"
4747
- "--http-endpoint=:29633"
48+
- "--enable-auto-gomemlimit=true"
49+
- "--auto-gomemlimit-ratio=0.95"
4850
ports:
4951
- containerPort: 29633
5052
name: http-endpoint

deploy/base/node/node.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ spec:
6262
- --enable-gcsfuse-profiles-internal=true
6363
- --stream-metrics-export=true
6464
- --enable-cloud-profiler-for-driver=true
65+
- --enable-auto-gomemlimit=true
66+
- --auto-gomemlimit-ratio=0.95
6567
ports:
6668
- containerPort: 9920
6769
name: metrics

deploy/base/webhook/deployment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ spec:
6161
- --health-probe-bind-address=:22031
6262
- --should-inject-sa-vol=true
6363
- --enable-gcsfuse-profiles=true
64+
- --enable-auto-gomemlimit=true
65+
- --auto-gomemlimit-ratio=0.95
6466
env:
6567
- name: SIDECAR_IMAGE_PULL_POLICY
6668
value: "IfNotPresent"

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
cloud.google.com/go/monitoring v1.24.2
99
cloud.google.com/go/profiler v0.4.3
1010
cloud.google.com/go/storage v1.56.3
11+
github.com/KimMachineGun/automemlimit v0.7.5
1112
github.com/container-storage-interface/spec v1.11.0
1213
github.com/distribution/reference v0.6.0
1314
github.com/go-logr/logr v1.4.3
@@ -89,6 +90,7 @@ require (
8990
github.com/onsi/ginkgo/v2 v2.27.0 // indirect
9091
github.com/onsi/gomega v1.38.2 // indirect
9192
github.com/opencontainers/go-digest v1.0.0 // indirect
93+
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
9294
github.com/pkg/errors v0.9.1 // indirect
9395
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
9496
github.com/prometheus/procfs v0.15.1 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock v0
3030
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/cloudmock v0.53.0/go.mod h1:jUZ5LYlw40WMd07qxcQJD5M40aUxrfwqQX1g7zxYnrQ=
3131
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.53.0 h1:Ron4zCA/yk6U7WOBXhTJcDpsUBG9npumK6xw2auFltQ=
3232
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.53.0/go.mod h1:cSgYe11MCNYunTnRXrKiR/tHc0eoKjICUuWpNZoVCOo=
33+
github.com/KimMachineGun/automemlimit v0.7.5 h1:RkbaC0MwhjL1ZuBKunGDjE/ggwAX43DwZrJqVwyveTk=
34+
github.com/KimMachineGun/automemlimit v0.7.5/go.mod h1:QZxpHaGOQoYvFhv/r4u3U0JTC2ZcOwbSr11UZF46UBM=
3335
github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0=
3436
github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
3537
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
@@ -157,6 +159,8 @@ github.com/onsi/gomega v1.38.2 h1:eZCjf2xjZAqe+LeWvKb5weQ+NcPwX84kqJ0cZNxok2A=
157159
github.com/onsi/gomega v1.38.2/go.mod h1:W2MJcYxRGV63b418Ai34Ud0hEdTVXq9NW9+Sx6uXf3k=
158160
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
159161
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
162+
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
163+
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
160164
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
161165
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
162166
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 h1:GFCKgmp0tecUJ0sJuv4pzYCqS9+RGSn52M3FUwPs+uo=

pkg/csi_driver/gcs_fuse_driver.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@ type FeatureGCSFuseProfiles struct {
4545
EnableGcsfuseProfilesInternal bool
4646
}
4747

48+
type GoMemLimitOptions struct {
49+
EnableAutoGoMemLimit bool
50+
AutoGoMemLimitRatio float64
51+
}
52+
4853
type GCSDriverFeatureOptions struct {
4954
EnableGCSFuseKernelParams bool
5055
FeatureGCSFuseProfiles *FeatureGCSFuseProfiles
56+
GoMemLimitOptions *GoMemLimitOptions
5157
}
5258

5359
type GCSDriverConfig struct {

pkg/csi_driver/node.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,42 @@ func (s *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublish
217217
})
218218
}
219219

220+
// Check if the user explicitly provided GOMEMLIMIT flags in their
221+
// volume's mountOptions.
222+
userProvidedAutoMemLimit := false
223+
userProvidedRatio := false
224+
for _, opt := range args.fuseMountOptions {
225+
if opt == util.EnableAutoGoMemLimitConst || strings.HasPrefix(opt, util.EnableAutoGoMemLimitConst+"=") {
226+
userProvidedAutoMemLimit = true
227+
}
228+
if strings.HasPrefix(opt, util.AutoGoMemLimitRatioConst+"=") {
229+
userProvidedRatio = true
230+
}
231+
}
232+
233+
// Inject driver defaults if the sidecar supports the GOMEMLIMIT feature.
234+
// We evaluate the ratio independently so users who manually opt-in via
235+
// mountOptions without specifying a ratio still receive the driver's
236+
// default.
237+
if s.driver.isSidecarVersionSupportedForGivenFeature(gcsFuseSidecarImage, SidecarAutoGoMemLimitMinVersion) {
238+
var extraOpts []string
239+
enableAutoGoMemLimit := s.driver.config.FeatureOptions.GoMemLimitOptions != nil && s.driver.config.FeatureOptions.GoMemLimitOptions.EnableAutoGoMemLimit
240+
if enableAutoGoMemLimit && !userProvidedAutoMemLimit {
241+
extraOpts = append(extraOpts, util.EnableAutoGoMemLimitConst+"=true")
242+
}
243+
// We check this to prevent injecting the ratio and adding unnecessary
244+
// noise to the mount options when the feature is completely disabled.
245+
sidecarFeatureWillBeEnabled := enableAutoGoMemLimit || userProvidedAutoMemLimit
246+
if !userProvidedRatio && sidecarFeatureWillBeEnabled && s.driver.config.FeatureOptions.GoMemLimitOptions != nil {
247+
autoGoMemLimitRatio := s.driver.config.FeatureOptions.GoMemLimitOptions.AutoGoMemLimitRatio
248+
extraOpts = append(extraOpts, util.AutoGoMemLimitRatioConst+"="+strconv.FormatFloat(autoGoMemLimitRatio, 'f', -1, 64))
249+
}
250+
251+
if len(extraOpts) > 0 {
252+
args.fuseMountOptions = joinMountOptions(args.fuseMountOptions, extraOpts)
253+
}
254+
}
255+
220256
node, err := s.k8sClients.GetNode(s.driver.config.NodeID)
221257
if err != nil {
222258
return nil, status.Errorf(codes.NotFound, "failed to get node: %v", err)

0 commit comments

Comments
 (0)