【kubernetes/k8s源码分析】 rook operator discover源码分析

启动命令

    rook discover --discover-interval 60m

启动默认参数

    --discover-interval=1h0m0s, --help=false, --log-flush-frequency=5s, --log-level=INFO

 

定期节点发现新设备,并更新至rook-ceph下的configmap中,命名为local-device-{nodeName}

 

Run函数

   模式一样直接略过,分析Run函数

 

1. updateDeviceCM函数

     

    1.1 probeDevices函数

      DiscoverDevices中的ListDevices函数调用lsblk --all --noheadings --list --output KNAME获得本地块设备

      GetDeviceProperties函数调用lsblk /dev/sda --bytes --nodeps --pairs --output SIZE,ROTA,RO,TYPE,PKNAME得到块设备属性

      GetDiskUUID函数调用sgdisk --print /dev/sda,操作GPT分区的工具,就像fdisk是操作MBR分区,查看分区详情

      GetUdevInfo调用udevadm info --query=property /dev/sda查询设备总线的信息

func probeDevices(context *clusterd.Context) ([]sys.LocalDisk, error) {
	devices := make([]sys.LocalDisk, 0)
	localDevices, err := clusterd.DiscoverDevices(context.Executor)

	for _, device := range localDevices {
		if device == nil {
			continue
		}
		if device.Type == sys.PartType {
			continue
		}

		partitions, _, err := sys.GetDevicePartitions(device.Name, context.Executor)
		if err != nil {
			logger.Infof("failed to check device partitions %s: %v", device.Name, err)
			continue
		}

		// check if there is a file system on the device
		fs, err := sys.GetDeviceFilesystems(device.Name, context.Executor)
		if err != nil {
			logger.Infof("failed to check device filesystem %s: %v", device.Name, err)
			continue
		}
		device.Partitions = partitions
		device.Filesystem = fs
		device.Empty = clusterd.GetDeviceEmpty(device)

		devices = append(devices, *device)
	}

	logger.Infof("available devices: %+v", devices)
	return devices, nil
}

    1.2 updateDeviceCM函数

      创建configmap,把设备信息存入data,调用client-go API创建configmap

  data:
  devices: '[{"name":"sda","parent":"","hasChildren":false,"devLinks":"/dev/disk/by-id/ata-VBOX_HARDDISK_VB4c728feb-b5aac650
    /dev/disk/by-path/pci-0000:00:0d.0-ata-1.0","size":21474836480,"uuid":"36499943-21b5-4af9-82b1-a3b1e1559a05","serial":"VBOX_HARDDISK_VB4c728feb-b5aac650","type":"disk","rotational":true,"readOnly":false,"Partitions":[{"Name":"sda1","Size":1073741824,"Label":"","Filesystem":"xfs"},{"Name":"sda2","Size":2147483648,"Label":"","Filesystem":"swap"},{"Name":"sda3","Size":18252562432,"Label":"","Filesystem":"xfs"}],"filesystem":"","vendor":"","model":"VBOX_HARDDISK","wwn":"","wwnVendorExtension":"","empty":false},{"name":"sdb","parent":"","hasChildren":false,"devLinks":"/dev/disk/by-id/ata-VBOX_HARDDISK_VB7eb9f85c-38b84e54
    /dev/disk/by-path/pci-0000:00:0d.0-ata-2.0","size":21474836480,"uuid":"7e59dc6c-94bc-406c-9d7e-296e7eba2c13","serial":"VBOX_HARDDISK_VB7eb9f85c-38b84e54","type":"disk","rotational":true,"readOnly":false,"Partitions":null,"filesystem":"","vendor":"","model":"VBOX_HARDDISK","wwn":"","wwnVendorExtension":"","empty":true}]'

// the map doesn't exist yet, create it now
cm = &v1.ConfigMap{
	ObjectMeta: metav1.ObjectMeta{
		Name:      cmName,
		Namespace: namespace,
		Labels: map[string]string{
			k8sutil.AppAttr: AppName,
			NodeAttr:        nodeName,
		},
	},
	Data: data,
}

 

2. 定期时间更新device configmap比较好理解

udevEvents := make(chan string)
go udevBlockMonitor(udevEvents, udevEventPeriod)

for {
	select {
	case <-sigc:
		logger.Infof("shutdown signal received, exiting...")
		return nil
	case <-time.After(probeInterval):
		updateDeviceCM(context)
	case _, ok := <-udevEvents:
		if ok {
			logger.Info("trigger probe from udev event")
			updateDeviceCM(context)
		} else {
			logger.Warningf("disabling udev monitoring")
			udevEvents = nil
		}
	}
}

 

 

你可能感兴趣的:(kubernetes,CSI,存储)