github.com/kubewharf/katalyst-core@v0.5.3/pkg/metaserver/agent/metric/provisioner/malachite/client/client.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     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  
    17  package client
    18  
    19  import (
    20  	"fmt"
    21  	"sync"
    22  
    23  	"github.com/kubewharf/katalyst-core/pkg/metaserver/agent/pod"
    24  )
    25  
    26  const (
    27  	malachiteServicePort = 9002
    28  
    29  	CgroupResource     = "cgroup/groups"
    30  	CgroupPathParamKey = "cgroup_user_path"
    31  
    32  	SystemIOResource      = "system/io"
    33  	SystemNetResource     = "system/network"
    34  	SystemMemoryResource  = "system/memory"
    35  	SystemComputeResource = "system/compute"
    36  )
    37  
    38  type SystemResourceKind int
    39  
    40  const (
    41  	Compute SystemResourceKind = iota
    42  	Memory
    43  	IO
    44  	Net
    45  )
    46  
    47  type MalachiteClient struct {
    48  	// those fields are for testing
    49  	sync.RWMutex
    50  	urls             map[string]string
    51  	relativePathFunc *func(podUID, containerId string) (string, error)
    52  
    53  	fetcher pod.PodFetcher
    54  }
    55  
    56  func NewMalachiteClient(fetcher pod.PodFetcher) *MalachiteClient {
    57  	urls := make(map[string]string)
    58  	for _, path := range []string{
    59  		CgroupResource,
    60  		SystemIOResource,
    61  		SystemNetResource,
    62  		SystemComputeResource,
    63  		SystemMemoryResource,
    64  	} {
    65  		urls[path] = fmt.Sprintf("http://localhost:%d/api/v1/%s", malachiteServicePort, path)
    66  	}
    67  
    68  	return &MalachiteClient{
    69  		fetcher: fetcher,
    70  		urls:    urls,
    71  	}
    72  }
    73  
    74  // SetURL is used to implement UT for
    75  func (c *MalachiteClient) SetURL(urls map[string]string) {
    76  	c.Lock()
    77  	defer c.Unlock()
    78  	c.urls = urls
    79  }