2019-01-11 15:55:28 +02: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 16:40:33 +02:00
|
|
|
"context"
|
2020-03-05 16:40:55 +02:00
|
|
|
"encoding/json"
|
|
|
|
|
2019-01-11 15:55:28 +02:00
|
|
|
api "k8s.io/api/core/v1"
|
|
|
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-03-05 16:40:55 +02:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2019-01-11 15:55:28 +02:00
|
|
|
k8sclient "k8s.io/client-go/kubernetes"
|
|
|
|
restclient "k8s.io/client-go/rest"
|
2020-05-28 17:43:43 +03:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2019-01-11 15:55:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Implements APIHelpers
|
|
|
|
type K8sHelpers struct {
|
2020-05-28 17:43:43 +03:00
|
|
|
Kubeconfig string
|
2019-01-11 15:55:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h K8sHelpers) GetClient() (*k8sclient.Clientset, error) {
|
|
|
|
// Set up an in-cluster K8S client.
|
2020-05-28 17:43:43 +03:00
|
|
|
var config *restclient.Config
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if h.Kubeconfig == "" {
|
|
|
|
config, err = restclient.InClusterConfig()
|
|
|
|
} else {
|
|
|
|
config, err = clientcmd.BuildConfigFromFlags("", h.Kubeconfig)
|
|
|
|
}
|
2019-01-11 15:55:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-28 17:43:43 +03:00
|
|
|
|
2019-01-11 15:55:28 +02:00
|
|
|
clientset, err := k8sclient.NewForConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return clientset, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h K8sHelpers) GetNode(cli *k8sclient.Clientset, nodeName string) (*api.Node, error) {
|
|
|
|
// Get the node object using node name
|
2020-11-17 16:40:33 +02:00
|
|
|
node, err := cli.CoreV1().Nodes().Get(context.TODO(), nodeName, meta_v1.GetOptions{})
|
2019-01-11 15:55:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2020-05-28 17:12:48 +03:00
|
|
|
func (h K8sHelpers) GetNodes(cli *k8sclient.Clientset) (*api.NodeList, error) {
|
2020-11-17 16:40:33 +02:00
|
|
|
return cli.CoreV1().Nodes().List(context.TODO(), meta_v1.ListOptions{})
|
2020-05-28 17:12:48 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 15:55:28 +02:00
|
|
|
func (h K8sHelpers) UpdateNode(c *k8sclient.Clientset, n *api.Node) error {
|
|
|
|
// Send the updated node to the apiserver.
|
2020-11-17 16:40:33 +02:00
|
|
|
_, err := c.CoreV1().Nodes().Update(context.TODO(), n, meta_v1.UpdateOptions{})
|
2019-01-11 15:55:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-05 16:40:55 +02:00
|
|
|
|
2020-08-13 17:14:27 +03: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 16:40:55 +02:00
|
|
|
}
|
2020-08-13 17:14:27 +03:00
|
|
|
return nil
|
2020-03-05 16:40:55 +02:00
|
|
|
|
|
|
|
}
|