func()

in internal/instanceinfo/instanceinforeader.go [131:211]


func (r *Reader) ReadDiskMapping(ctx context.Context, config *configpb.Configuration) (*compute.Instance, *instancepb.InstanceProperties, error) {
	if config.GetBareMetal() {
		return nil, nil, fmt.Errorf("bare Metal configured, cannot get instance information from the Compute API")
	}

	cp := config.GetCloudProperties()
	if cp == nil {
		return nil, nil, fmt.Errorf("no Metadata Cloud Properties found, cannot collect instance information from the Compute API")

	}

	// Nil check before dereferencing to avoid panics.
	if r.dm == nil || r.gceService == nil {
		log.CtxLogger(ctx).Debug("")
		return nil, nil, fmt.Errorf("disk mapper and GCE service must be non-nil to read instance info")
	}

	projectID, zone, instanceID := cp.GetProjectId(), cp.GetZone(), cp.GetInstanceId()
	var instance *compute.Instance
	var err error
	instance, err = r.gceService.GetInstance(projectID, zone, instanceID)
	if err != nil {
		return nil, nil, fmt.Errorf("could not get instance info from the Compute API, error: %v", err)
	}

	builder := instancepb.InstanceProperties{
		MachineType:       instance.MachineType,
		CpuPlatform:       instance.CpuPlatform,
		CreationTimestamp: instance.CreationTimestamp,
	}

	diskNames := []string{}
	for _, disk := range instance.Disks {
		source, diskName := disk.Source, disk.DeviceName
		if source != "" {
			s := strings.Split(source, "/")
			diskName = s[len(s)-1]
		}
		diskNames = append(diskNames, diskName)
	}
	f := r.createDiskFilter(diskNames)
	disks, err := r.gceService.ListDisks(projectID, zone, f)
	if err != nil {
		log.Logger.Errorw("Could not get disk info from the Compute API", "project", projectID, "zone", zone, "filter", f, "error", err)
	}

	for _, disk := range instance.Disks {
		source, diskName := disk.Source, disk.DeviceName
		if source != "" {
			s := strings.Split(source, "/")
			diskName = s[len(s)-1]
		}

		mapping, err := r.dm.ForDeviceName(ctx, disk.DeviceName)
		if err != nil {
			log.CtxLogger(ctx).Warnw("No mapping for instance disk", "disk", disk, "error", err)
			mapping = "unknown"
		}
		log.CtxLogger(ctx).Debugw("Instance disk is mapped to device name", "devicename", disk.DeviceName, "mapping", mapping)
		diskData := r.getDiskData(disks, diskName)
		var pIops int64 = 0
		var pThroughput int64 = 0
		if diskData != nil {
			pIops = diskData.ProvisionedIops
			pThroughput = diskData.ProvisionedThroughput
		}
		builder.Disks = append(builder.Disks, &instancepb.Disk{
			Type:                  disk.Type,
			DeviceType:            r.getDeviceType(disk.Type, diskData),
			DeviceName:            disk.DeviceName,
			IsLocalSsd:            disk.Type == "SCRATCH",
			DiskName:              diskName,
			Mapping:               mapping,
			ProvisionedIops:       pIops,
			ProvisionedThroughput: pThroughput,
		})
	}

	log.CtxLogger(ctx).Debugw("Instance properties:", "instanceProperties", instance)
	return instance, &builder, nil
}