Skip to content

Commit 16de9dd

Browse files
committed
pass machine-type and disable-autoconfig flag to gcsfuse
1 parent 9158317 commit 16de9dd

4 files changed

Lines changed: 60 additions & 3 deletions

File tree

cmd/sidecar_mounter/main.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ import (
2828
"syscall"
2929
"time"
3030

31+
driver "github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/csi_driver"
3132
sidecarmounter "github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/sidecar_mounter"
3233
"github.com/googlecloudplatform/gcs-fuse-csi-driver/pkg/webhook"
3334
"k8s.io/klog/v2"
3435
)
3536

3637
var (
37-
gcsfusePath = flag.String("gcsfuse-path", "/gcsfuse", "gcsfuse path")
38-
volumeBasePath = flag.String("volume-base-path", webhook.SidecarContainerTmpVolumeMountPath+"/.volumes", "volume base path")
39-
_ = flag.Int("grace-period", 0, "grace period for gcsfuse termination. This flag has been deprecated, has no effect and will be removed in the future.")
38+
gcsfusePath = flag.String("gcsfuse-path", "/gcsfuse", "gcsfuse path")
39+
volumeBasePath = flag.String("volume-base-path", webhook.SidecarContainerTmpVolumeMountPath+"/.volumes", "volume base path")
40+
disableAutoConfig = flag.Bool("disable-autoconfig", true, "disable defaulting configurations")
41+
_ = flag.Int("grace-period", 0, "grace period for gcsfuse termination. This flag has been deprecated, has no effect and will be removed in the future.")
4042
// This is set at compile time.
4143
version = "unknown"
4244
)
@@ -55,13 +57,28 @@ func main() {
5557
mounter := sidecarmounter.New(*gcsfusePath)
5658
ctx, cancel := context.WithCancel(context.Background())
5759

60+
machineType := ""
61+
volumePath := *volumeBasePath + driver.MachineTypePath
62+
klog.Infof("Checking if machine-type file exists: %v", volumePath)
63+
if _, err := os.Stat(volumePath); err == nil {
64+
machineTypeBytes, err := os.ReadFile(volumePath)
65+
if err != nil {
66+
klog.Fatalf("failed to read machine-type file: %v", err)
67+
}
68+
machineType = string(machineTypeBytes)
69+
}
70+
5871
for _, sp := range socketPaths {
5972
// sleep 1.5 seconds before launch the next gcsfuse to avoid
6073
// 1. different gcsfuse logs mixed together.
6174
// 2. memory usage peak.
6275
time.Sleep(1500 * time.Millisecond)
6376
mc := sidecarmounter.NewMountConfig(sp)
6477
if mc != nil {
78+
// TODO: Pass machine-type to gcsfuse binary
79+
klog.Infof("Setting machine type to gcsfuse binary %v", machineType)
80+
mc.FlagMap["machine-type"] = machineType
81+
mc.FlagMap["disable-autoconfig"] = strconv.FormatBool(*disableAutoConfig)
6582
if err := mounter.Mount(ctx, mc); err != nil {
6683
mc.ErrWriter.WriteMsg(fmt.Sprintf("failed to mount bucket %q for volume %q: %v\n", mc.BucketName, mc.VolumeName, err))
6784
}

pkg/cloud_provider/clientset/clientset.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type Clientset struct {
5959
}
6060

6161
const GkeMetaDataServerKey = "iam.gke.io/gke-metadata-server-enabled"
62+
const MachineTypeKey = "node.kubernetes.io/instance-type"
6263

6364
func (c *Clientset) ConfigureNodeLister(nodeName string) {
6465
trim := func(obj interface{}) (interface{}, error) {
@@ -81,6 +82,10 @@ func (c *Clientset) ConfigureNodeLister(nodeName string) {
8182
if ok {
8283
newLabels[GkeMetaDataServerKey] = isGkeMetaDataServerEnabled
8384
}
85+
machineType, ok := nodeObj.ObjectMeta.Labels[MachineTypeKey]
86+
if ok {
87+
newLabels[MachineTypeKey] = machineType
88+
}
8489

8590
nodeObj.Spec = corev1.NodeSpec{}
8691
nodeObj.Status = corev1.NodeStatus{}

pkg/csi_driver/node.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ func (s *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublish
181181
}
182182
}
183183

184+
machineType, ok := node.Labels[clientset.MachineTypeKey]
185+
if ok {
186+
klog.V(1).Infof("Putting machine-type file to %v: %v", targetPath, machineType)
187+
if err := putMachineTypeFile(machineType, targetPath); err != nil {
188+
return nil, status.Error(codes.Internal, err.Error())
189+
}
190+
}
191+
184192
// Check if there is any error from the gcsfuse
185193
code, err := checkGcsFuseErr(isInitContainer, pod, targetPath)
186194
if code != codes.OK {

pkg/csi_driver/utils.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const (
6666
VolumeContextKeyEphemeral = "csi.storage.k8s.io/ephemeral"
6767
VolumeContextKeyBucketName = "bucketName"
6868
tokenServerSidecarMinVersion = "v1.12.2-gke.0" // #nosec G101
69+
MachineTypePath = "/machine-type"
6970
)
7071

7172
func NewVolumeCapabilityAccessMode(mode csi.VolumeCapability_AccessMode_Mode) *csi.VolumeCapability_AccessMode {
@@ -484,3 +485,29 @@ func isSidecarVersionSupportedForTokenServer(imageName string) bool {
484485

485486
return false
486487
}
488+
489+
func putMachineTypeFile(machineType string, targetPath string) error {
490+
emptyDirBasePath, err := util.PrepareEmptyDir(targetPath, true)
491+
if err != nil {
492+
return fmt.Errorf("failed to get emptyDir path: %w", err)
493+
}
494+
495+
machineTypeFilePath := filepath.Dir(emptyDirBasePath) + MachineTypePath
496+
497+
f, err := os.Create(machineTypeFilePath)
498+
if err != nil {
499+
return fmt.Errorf("failed to put the machine-type file: %w", err)
500+
}
501+
if _, err := f.WriteString(machineType); err != nil {
502+
return fmt.Errorf("failed to write machine-type file: %w", err)
503+
}
504+
505+
f.Close()
506+
507+
err = os.Chown(machineTypeFilePath, webhook.NobodyUID, webhook.NobodyGID)
508+
if err != nil {
509+
return fmt.Errorf("failed to change ownership on the machine-type file: %w", err)
510+
}
511+
512+
return nil
513+
}

0 commit comments

Comments
 (0)