...

Source file src/kubevirt.io/client-go/kubecli/profiler.go

Documentation: kubevirt.io/client-go/kubecli

     1  /*
     2   * This file is part of the KubeVirt project
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * Copyright 2018 Red Hat, Inc.
    17   *
    18   */
    19  
    20  package kubecli
    21  
    22  import (
    23  	"context"
    24  	"encoding/json"
    25  	"fmt"
    26  	"net/url"
    27  
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  	"k8s.io/client-go/rest"
    30  
    31  	v1 "kubevirt.io/api/core/v1"
    32  )
    33  
    34  func (k *kubevirt) ClusterProfiler() *ClusterProfiler {
    35  	return &ClusterProfiler{
    36  		restClient: k.restClient,
    37  		resource:   "cluster-profiler",
    38  	}
    39  }
    40  
    41  type ClusterProfiler struct {
    42  	restClient *rest.RESTClient
    43  	resource   string
    44  }
    45  
    46  func (v *ClusterProfiler) preferredVersion() (string, error) {
    47  	var group metav1.APIGroup
    48  	// First, find out which version to query
    49  	uri := ApiGroupName
    50  	result := v.restClient.Get().AbsPath(uri).Do(context.Background())
    51  	if data, err := result.Raw(); err != nil {
    52  		connErr, isConnectionErr := err.(*url.Error)
    53  
    54  		if isConnectionErr {
    55  			return "", connErr.Err
    56  		}
    57  
    58  		return "", err
    59  	} else if err = json.Unmarshal(data, &group); err != nil {
    60  		return "", err
    61  	}
    62  
    63  	return group.PreferredVersion.GroupVersion, nil
    64  
    65  }
    66  
    67  func (v *ClusterProfiler) Start() error {
    68  	preferredVersion, err := v.preferredVersion()
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	// Now, query the preferred version
    74  	uri := fmt.Sprintf("/apis/%s/start-cluster-profiler", preferredVersion)
    75  
    76  	return v.restClient.Get().AbsPath(uri).Do(context.Background()).Error()
    77  }
    78  
    79  func (v *ClusterProfiler) Stop() error {
    80  	preferredVersion, err := v.preferredVersion()
    81  	if err != nil {
    82  
    83  		return fmt.Errorf("error encountered while detecting preferred version: %v", err)
    84  	}
    85  
    86  	// Now, query the preferred version
    87  	uri := fmt.Sprintf("/apis/%s/stop-cluster-profiler", preferredVersion)
    88  
    89  	return v.restClient.Get().AbsPath(uri).Do(context.Background()).Error()
    90  }
    91  
    92  // Dump returns at most cpRequest.PageSize profiler results. To fetch results from all kubevirt pods
    93  // Dump should be called with Continue fields set to Continue field value from the response to a previous request.
    94  // This should be repeated until Continue or ComponentsResult field in ClusterProfilerResponse is empty.
    95  func (v *ClusterProfiler) Dump(cpRequest *v1.ClusterProfilerRequest) (*v1.ClusterProfilerResults, error) {
    96  	preferredVersion, err := v.preferredVersion()
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  
   101  	if cpRequest == nil {
   102  		return nil, fmt.Errorf("request body can't be nil")
   103  	}
   104  
   105  	bytes, err := json.Marshal(cpRequest)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	// Now, query the preferred version
   111  	uri := fmt.Sprintf("/apis/%s/dump-cluster-profiler", preferredVersion)
   112  
   113  	var profileResults v1.ClusterProfilerResults
   114  
   115  	result := v.restClient.Get().AbsPath(uri).Body(bytes).Do(context.Background())
   116  	if data, err := result.Raw(); err != nil {
   117  		connErr, isConnectionErr := err.(*url.Error)
   118  
   119  		if isConnectionErr {
   120  			return nil, connErr.Err
   121  		}
   122  
   123  		return nil, err
   124  	} else if len(data) == 0 {
   125  		return &profileResults, nil
   126  	} else if err = json.Unmarshal(data, &profileResults); err != nil {
   127  		return nil, err
   128  	}
   129  
   130  	return &profileResults, nil
   131  }
   132  

View as plain text