From 3e4b70fb6ef04e9d32f524ac21b1a117a4fcb3bf Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Tue, 23 Jan 2024 20:46:52 +0200 Subject: [PATCH] Drop pkg/apihelper The code is now unused. --- pkg/apihelper/apihelpers.go | 53 -------- pkg/apihelper/k8shelpers.go | 112 ----------------- pkg/apihelper/mock_APIHelpers.go | 205 ------------------------------- 3 files changed, 370 deletions(-) delete mode 100644 pkg/apihelper/apihelpers.go delete mode 100644 pkg/apihelper/k8shelpers.go delete mode 100644 pkg/apihelper/mock_APIHelpers.go diff --git a/pkg/apihelper/apihelpers.go b/pkg/apihelper/apihelpers.go deleted file mode 100644 index c458d67fc8..0000000000 --- a/pkg/apihelper/apihelpers.go +++ /dev/null @@ -1,53 +0,0 @@ -/* -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 - -//go:generate mockery --name=APIHelpers --inpackage - -import ( - topologyclientset "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/generated/clientset/versioned" - corev1 "k8s.io/api/core/v1" - k8sclient "k8s.io/client-go/kubernetes" - "sigs.k8s.io/node-feature-discovery/pkg/utils" -) - -// APIHelpers represents a set of API helpers for Kubernetes -type APIHelpers interface { - // GetClient returns a client - GetClient() (*k8sclient.Clientset, error) - - // GetNode returns the Kubernetes node on which this container is running. - GetNode(*k8sclient.Clientset, string) (*corev1.Node, error) - - // GetNodes returns all the nodes in the cluster - GetNodes(*k8sclient.Clientset) (*corev1.NodeList, error) - - // UpdateNode updates the node via the API server using a client. - UpdateNode(*k8sclient.Clientset, *corev1.Node) error - - // PatchNode updates the node object via the API server using a client. - PatchNode(*k8sclient.Clientset, string, []utils.JsonPatch) error - - // PatchNodeStatus updates the node status via the API server using a client. - PatchNodeStatus(*k8sclient.Clientset, string, []utils.JsonPatch) error - - // GetTopologyClient returns a topologyclientset - GetTopologyClient() (*topologyclientset.Clientset, error) - - // GetPod returns the Kubernetes pod in a namepace with a name. - GetPod(*k8sclient.Clientset, string, string) (*corev1.Pod, error) -} diff --git a/pkg/apihelper/k8shelpers.go b/pkg/apihelper/k8shelpers.go deleted file mode 100644 index 8d8f494e0e..0000000000 --- a/pkg/apihelper/k8shelpers.go +++ /dev/null @@ -1,112 +0,0 @@ -/* -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 ( - "context" - "encoding/json" - - topologyclientset "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/generated/clientset/versioned" - corev1 "k8s.io/api/core/v1" - meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" - k8sclient "k8s.io/client-go/kubernetes" - restclient "k8s.io/client-go/rest" - "sigs.k8s.io/node-feature-discovery/pkg/utils" -) - -// K8sHelpers implements APIHelpers -type K8sHelpers struct { - Kubeconfig *restclient.Config -} - -// GetClient creates and returns a new clientset from given config -func (h K8sHelpers) GetClient() (*k8sclient.Clientset, error) { - clientset, err := k8sclient.NewForConfig(h.Kubeconfig) - if err != nil { - return nil, err - } - return clientset, nil -} - -func (h K8sHelpers) GetTopologyClient() (*topologyclientset.Clientset, error) { - topologyClient, err := topologyclientset.NewForConfig(h.Kubeconfig) - if err != nil { - return nil, err - } - return topologyClient, nil -} - -// GetNode retrieves one node object. -func (h K8sHelpers) GetNode(cli *k8sclient.Clientset, nodeName string) (*corev1.Node, error) { - // Get the node object using node name - node, err := cli.CoreV1().Nodes().Get(context.TODO(), nodeName, meta_v1.GetOptions{}) - if err != nil { - return nil, err - } - - return node, nil -} - -// GetNodes retrieves all the node objects. -func (h K8sHelpers) GetNodes(cli *k8sclient.Clientset) (*corev1.NodeList, error) { - return cli.CoreV1().Nodes().List(context.TODO(), meta_v1.ListOptions{}) -} - -// UpdateNode sends updated node object to the apiserver -func (h K8sHelpers) UpdateNode(c *k8sclient.Clientset, n *corev1.Node) error { - // Send the updated node to the apiserver. - _, err := c.CoreV1().Nodes().Update(context.TODO(), n, meta_v1.UpdateOptions{}) - if err != nil { - return err - } - - return nil -} - -func (h K8sHelpers) PatchNode(c *k8sclient.Clientset, nodeName string, patches []utils.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 []utils.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 - } - return nil - -} - -func (h K8sHelpers) GetPod(cli *k8sclient.Clientset, namespace string, podName string) (*corev1.Pod, error) { - // 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 -} diff --git a/pkg/apihelper/mock_APIHelpers.go b/pkg/apihelper/mock_APIHelpers.go deleted file mode 100644 index 46341ae2df..0000000000 --- a/pkg/apihelper/mock_APIHelpers.go +++ /dev/null @@ -1,205 +0,0 @@ -// Code generated by mockery v2.32.0. DO NOT EDIT. - -package apihelper - -import ( - mock "github.com/stretchr/testify/mock" - kubernetes "k8s.io/client-go/kubernetes" - - utils "sigs.k8s.io/node-feature-discovery/pkg/utils" - - v1 "k8s.io/api/core/v1" - - versioned "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/generated/clientset/versioned" -) - -// MockAPIHelpers is an autogenerated mock type for the APIHelpers type -type MockAPIHelpers struct { - mock.Mock -} - -// GetClient provides a mock function with given fields: -func (_m *MockAPIHelpers) GetClient() (*kubernetes.Clientset, error) { - ret := _m.Called() - - var r0 *kubernetes.Clientset - var r1 error - if rf, ok := ret.Get(0).(func() (*kubernetes.Clientset, error)); ok { - return rf() - } - if rf, ok := ret.Get(0).(func() *kubernetes.Clientset); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*kubernetes.Clientset) - } - } - - if rf, ok := ret.Get(1).(func() error); ok { - r1 = rf() - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// GetNode provides a mock function with given fields: _a0, _a1 -func (_m *MockAPIHelpers) GetNode(_a0 *kubernetes.Clientset, _a1 string) (*v1.Node, error) { - ret := _m.Called(_a0, _a1) - - var r0 *v1.Node - var r1 error - if rf, ok := ret.Get(0).(func(*kubernetes.Clientset, string) (*v1.Node, error)); ok { - return rf(_a0, _a1) - } - if rf, ok := ret.Get(0).(func(*kubernetes.Clientset, string) *v1.Node); ok { - r0 = rf(_a0, _a1) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*v1.Node) - } - } - - if rf, ok := ret.Get(1).(func(*kubernetes.Clientset, string) error); ok { - r1 = rf(_a0, _a1) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// GetNodes provides a mock function with given fields: _a0 -func (_m *MockAPIHelpers) GetNodes(_a0 *kubernetes.Clientset) (*v1.NodeList, error) { - ret := _m.Called(_a0) - - var r0 *v1.NodeList - var r1 error - if rf, ok := ret.Get(0).(func(*kubernetes.Clientset) (*v1.NodeList, error)); ok { - return rf(_a0) - } - if rf, ok := ret.Get(0).(func(*kubernetes.Clientset) *v1.NodeList); ok { - r0 = rf(_a0) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*v1.NodeList) - } - } - - if rf, ok := ret.Get(1).(func(*kubernetes.Clientset) error); ok { - r1 = rf(_a0) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// GetPod provides a mock function with given fields: _a0, _a1, _a2 -func (_m *MockAPIHelpers) GetPod(_a0 *kubernetes.Clientset, _a1 string, _a2 string) (*v1.Pod, error) { - ret := _m.Called(_a0, _a1, _a2) - - var r0 *v1.Pod - var r1 error - if rf, ok := ret.Get(0).(func(*kubernetes.Clientset, string, string) (*v1.Pod, error)); ok { - return rf(_a0, _a1, _a2) - } - if rf, ok := ret.Get(0).(func(*kubernetes.Clientset, string, string) *v1.Pod); ok { - r0 = rf(_a0, _a1, _a2) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*v1.Pod) - } - } - - if rf, ok := ret.Get(1).(func(*kubernetes.Clientset, string, string) error); ok { - r1 = rf(_a0, _a1, _a2) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// GetTopologyClient provides a mock function with given fields: -func (_m *MockAPIHelpers) GetTopologyClient() (*versioned.Clientset, error) { - ret := _m.Called() - - var r0 *versioned.Clientset - var r1 error - if rf, ok := ret.Get(0).(func() (*versioned.Clientset, error)); ok { - return rf() - } - if rf, ok := ret.Get(0).(func() *versioned.Clientset); ok { - r0 = rf() - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*versioned.Clientset) - } - } - - if rf, ok := ret.Get(1).(func() error); ok { - r1 = rf() - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// PatchNode provides a mock function with given fields: _a0, _a1, _a2 -func (_m *MockAPIHelpers) PatchNode(_a0 *kubernetes.Clientset, _a1 string, _a2 []utils.JsonPatch) error { - ret := _m.Called(_a0, _a1, _a2) - - var r0 error - if rf, ok := ret.Get(0).(func(*kubernetes.Clientset, string, []utils.JsonPatch) error); ok { - r0 = rf(_a0, _a1, _a2) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// PatchNodeStatus provides a mock function with given fields: _a0, _a1, _a2 -func (_m *MockAPIHelpers) PatchNodeStatus(_a0 *kubernetes.Clientset, _a1 string, _a2 []utils.JsonPatch) error { - ret := _m.Called(_a0, _a1, _a2) - - var r0 error - if rf, ok := ret.Get(0).(func(*kubernetes.Clientset, string, []utils.JsonPatch) error); ok { - r0 = rf(_a0, _a1, _a2) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// UpdateNode provides a mock function with given fields: _a0, _a1 -func (_m *MockAPIHelpers) UpdateNode(_a0 *kubernetes.Clientset, _a1 *v1.Node) error { - ret := _m.Called(_a0, _a1) - - var r0 error - if rf, ok := ret.Get(0).(func(*kubernetes.Clientset, *v1.Node) error); ok { - r0 = rf(_a0, _a1) - } else { - r0 = ret.Error(0) - } - - return r0 -} - -// NewMockAPIHelpers creates a new instance of MockAPIHelpers. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -// The first argument is typically a *testing.T value. -func NewMockAPIHelpers(t interface { - mock.TestingT - Cleanup(func()) -}) *MockAPIHelpers { - mock := &MockAPIHelpers{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -}