func()

in clusterloader2/pkg/measurement/common/resource_usage.go [60:143]


func (e *resourceUsageMetricMeasurement) Execute(config *measurement.Config) ([]measurement.Summary, error) {
	provider := config.ClusterFramework.GetClusterConfig().Provider
	if !provider.Features().SupportResourceUsageMetering {
		klog.Warningf("fetching resource usage metrics is not possible for provider %q", provider.Name())
		return nil, nil
	}

	action, err := util.GetString(config.Params, "action")
	if err != nil {
		return nil, err
	}

	switch action {
	case "start":
		provider := config.ClusterFramework.GetClusterConfig().Provider
		host, err := util.GetStringOrDefault(config.Params, "host", config.ClusterFramework.GetClusterConfig().GetMasterIP())
		if err != nil {
			return nil, err
		}
		namespace, err := util.GetStringOrDefault(config.Params, "namespace", "kube-system")
		if err != nil {
			return nil, err
		}
		constraintsPath, err := util.GetStringOrDefault(config.Params, "resourceConstraints", "")
		if err != nil {
			return nil, err
		}
		if constraintsPath != "" {
			mapping := make(map[string]interface{})
			mapping["Nodes"] = config.ClusterFramework.GetClusterConfig().Nodes
			if err = config.TemplateProvider.TemplateInto(constraintsPath, mapping, &e.resourceConstraints); err != nil {
				return nil, fmt.Errorf("resource constraints reading error: %v", err)
			}
			for _, constraint := range e.resourceConstraints {
				if constraint.CPUConstraint == 0 {
					constraint.CPUConstraint = math.MaxFloat64
				}
				if constraint.MemoryConstraint == 0 {
					constraint.MemoryConstraint = math.MaxUint64
				}
			}
		}

		// Compute the node based on the cluster size.
		nodeCount := config.ClusterFramework.GetClusterConfig().Nodes
		nodesSet := gatherers.AllNodes
		if nodeCount > maxNodeCountForAllNodes {
			nodesSet = gatherers.MasterAndNonDaemons
		}

		klog.V(2).Infof("%s: starting resource usage collecting (mode %#v)...", e, nodesSet)
		e.gatherer, err = gatherers.NewResourceUsageGatherer(config.ClusterFramework.GetClientSets().GetClient(), host, config.ClusterFramework.GetClusterConfig().KubeletPort,
			provider, gatherers.ResourceGathererOptions{
				InKubemark:                        provider.Features().IsKubemarkProvider,
				Nodes:                             nodesSet,
				ResourceDataGatheringPeriod:       60 * time.Second,
				MasterResourceDataGatheringPeriod: 10 * time.Second,
			}, namespace)
		if err != nil {
			return nil, err
		}
		go e.gatherer.StartGatheringData()
		return nil, nil
	case "gather":
		if e.gatherer == nil {
			klog.Errorf("%s: gatherer not initialized", e)
			return nil, nil
		}
		klog.V(2).Infof("%s: gathering resource usage...", e)
		summary, err := e.gatherer.StopAndSummarize([]int{50, 90, 99, 100})
		if err != nil {
			return nil, err
		}
		content, err := util.PrettyPrintJSON(summary)
		if err != nil {
			return nil, err
		}
		resourceSummary := measurement.CreateSummary(resourceUsageMetricName, "json", content)
		return []measurement.Summary{resourceSummary}, e.verifySummary(summary)

	default:
		return nil, fmt.Errorf("unknown action %v", action)
	}
}