2019-01-11 13:55:28 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Kubernetes Authors.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package apihelper
|
|
|
|
|
|
|
|
import (
|
2020-11-17 14:40:33 +00:00
|
|
|
"context"
|
2020-03-05 14:40:55 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2021-05-13 10:51:15 +00:00
|
|
|
topologyclientset "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/generated/clientset/versioned"
|
2022-10-14 12:28:52 +00:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2019-01-11 13:55:28 +00:00
|
|
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-03-05 14:40:55 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2019-01-11 13:55:28 +00:00
|
|
|
k8sclient "k8s.io/client-go/kubernetes"
|
|
|
|
restclient "k8s.io/client-go/rest"
|
2020-05-28 14:43:43 +00:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2019-01-11 13:55:28 +00:00
|
|
|
)
|
|
|
|
|
2022-03-03 01:12:46 +00:00
|
|
|
// K8sHelpers implements APIHelpers
|
2019-01-11 13:55:28 +00:00
|
|
|
type K8sHelpers struct {
|
2021-06-08 13:17:38 +00:00
|
|
|
Kubeconfig *restclient.Config
|
2019-01-11 13:55:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 01:12:46 +00:00
|
|
|
// GetClient creates and returns a new clientset from given config
|
2019-01-11 13:55:28 +00:00
|
|
|
func (h K8sHelpers) GetClient() (*k8sclient.Clientset, error) {
|
2021-06-08 13:17:38 +00:00
|
|
|
clientset, err := k8sclient.NewForConfig(h.Kubeconfig)
|
2019-01-11 13:55:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return clientset, nil
|
|
|
|
}
|
|
|
|
|
2021-05-13 10:51:15 +00:00
|
|
|
func (h K8sHelpers) GetTopologyClient() (*topologyclientset.Clientset, error) {
|
2021-06-08 13:17:38 +00:00
|
|
|
topologyClient, err := topologyclientset.NewForConfig(h.Kubeconfig)
|
2021-05-13 10:51:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return topologyClient, nil
|
|
|
|
}
|
|
|
|
|
2022-03-03 01:12:46 +00:00
|
|
|
// GetNode retrieves one node object.
|
2022-10-14 12:28:52 +00:00
|
|
|
func (h K8sHelpers) GetNode(cli *k8sclient.Clientset, nodeName string) (*corev1.Node, error) {
|
2019-01-11 13:55:28 +00:00
|
|
|
// Get the node object using node name
|
2020-11-17 14:40:33 +00:00
|
|
|
node, err := cli.CoreV1().Nodes().Get(context.TODO(), nodeName, meta_v1.GetOptions{})
|
2019-01-11 13:55:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2022-03-03 01:12:46 +00:00
|
|
|
// GetNodes retrieves all the node objects.
|
2022-10-14 12:28:52 +00:00
|
|
|
func (h K8sHelpers) GetNodes(cli *k8sclient.Clientset) (*corev1.NodeList, error) {
|
2020-11-17 14:40:33 +00:00
|
|
|
return cli.CoreV1().Nodes().List(context.TODO(), meta_v1.ListOptions{})
|
2020-05-28 14:12:48 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 01:12:46 +00:00
|
|
|
// UpdateNode sends updated node object to the apiserver
|
2022-10-14 12:28:52 +00:00
|
|
|
func (h K8sHelpers) UpdateNode(c *k8sclient.Clientset, n *corev1.Node) error {
|
2019-01-11 13:55:28 +00:00
|
|
|
// Send the updated node to the apiserver.
|
2020-11-17 14:40:33 +00:00
|
|
|
_, err := c.CoreV1().Nodes().Update(context.TODO(), n, meta_v1.UpdateOptions{})
|
2019-01-11 13:55:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-05 14:40:55 +00:00
|
|
|
|
2020-08-13 14:14:27 +00:00
|
|
|
func (h K8sHelpers) PatchNode(c *k8sclient.Clientset, nodeName string, patches []JsonPatch) error {
|
|
|
|
if len(patches) > 0 {
|
|
|
|
data, err := json.Marshal(patches)
|
|
|
|
if err == nil {
|
|
|
|
_, err = c.CoreV1().Nodes().Patch(context.TODO(), nodeName, types.JSONPatchType, data, meta_v1.PatchOptions{})
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h K8sHelpers) PatchNodeStatus(c *k8sclient.Clientset, nodeName string, patches []JsonPatch) error {
|
|
|
|
if len(patches) > 0 {
|
|
|
|
data, err := json.Marshal(patches)
|
|
|
|
if err == nil {
|
|
|
|
_, err = c.CoreV1().Nodes().Patch(context.TODO(), nodeName, types.JSONPatchType, data, meta_v1.PatchOptions{}, "status")
|
|
|
|
}
|
|
|
|
return err
|
2020-03-05 14:40:55 +00:00
|
|
|
}
|
2020-08-13 14:14:27 +00:00
|
|
|
return nil
|
2020-03-05 14:40:55 +00:00
|
|
|
|
|
|
|
}
|
2021-07-13 23:50:05 +00:00
|
|
|
|
2022-10-14 12:28:52 +00:00
|
|
|
func (h K8sHelpers) GetPod(cli *k8sclient.Clientset, namespace string, podName string) (*corev1.Pod, error) {
|
2021-07-13 23:50:05 +00:00
|
|
|
// Get the node object using pod name
|
|
|
|
pod, err := cli.CoreV1().Pods(namespace).Get(context.TODO(), podName, meta_v1.GetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pod, nil
|
|
|
|
}
|
2021-06-08 13:17:38 +00:00
|
|
|
|
2022-03-03 01:12:46 +00:00
|
|
|
// GetKubeconfig returns the kubeconfig for the cluster
|
2021-06-08 13:17:38 +00:00
|
|
|
func GetKubeconfig(path string) (*restclient.Config, error) {
|
|
|
|
if path == "" {
|
|
|
|
return restclient.InClusterConfig()
|
|
|
|
}
|
|
|
|
return clientcmd.BuildConfigFromFlags("", path)
|
|
|
|
}
|