mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
fix golint errors in pkg/controller/service
This commit is contained in:
parent
9fa1bc8003
commit
a18de39166
@ -86,7 +86,6 @@ pkg/controller/replication/config/v1alpha1
|
|||||||
pkg/controller/resourcequota
|
pkg/controller/resourcequota
|
||||||
pkg/controller/resourcequota/config/v1alpha1
|
pkg/controller/resourcequota/config/v1alpha1
|
||||||
pkg/controller/route
|
pkg/controller/route
|
||||||
pkg/controller/service
|
|
||||||
pkg/controller/service/config/v1alpha1
|
pkg/controller/service/config/v1alpha1
|
||||||
pkg/controller/serviceaccount/config/v1alpha1
|
pkg/controller/serviceaccount/config/v1alpha1
|
||||||
pkg/controller/statefulset
|
pkg/controller/statefulset
|
||||||
|
@ -100,9 +100,9 @@ type serviceCache struct {
|
|||||||
serviceMap map[string]*cachedService
|
serviceMap map[string]*cachedService
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServiceController keeps cloud provider service resources
|
// Controller keeps cloud provider service resources
|
||||||
// (like load balancers) in sync with the registry.
|
// (like load balancers) in sync with the registry.
|
||||||
type ServiceController struct {
|
type Controller struct {
|
||||||
cloud cloudprovider.Interface
|
cloud cloudprovider.Interface
|
||||||
knownHosts []*v1.Node
|
knownHosts []*v1.Node
|
||||||
servicesToUpdate []*v1.Service
|
servicesToUpdate []*v1.Service
|
||||||
@ -128,7 +128,7 @@ func New(
|
|||||||
serviceInformer coreinformers.ServiceInformer,
|
serviceInformer coreinformers.ServiceInformer,
|
||||||
nodeInformer coreinformers.NodeInformer,
|
nodeInformer coreinformers.NodeInformer,
|
||||||
clusterName string,
|
clusterName string,
|
||||||
) (*ServiceController, error) {
|
) (*Controller, error) {
|
||||||
broadcaster := record.NewBroadcaster()
|
broadcaster := record.NewBroadcaster()
|
||||||
broadcaster.StartLogging(klog.Infof)
|
broadcaster.StartLogging(klog.Infof)
|
||||||
broadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: kubeClient.CoreV1().Events("")})
|
broadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: kubeClient.CoreV1().Events("")})
|
||||||
@ -140,7 +140,7 @@ func New(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
s := &ServiceController{
|
s := &Controller{
|
||||||
cloud: cloud,
|
cloud: cloud,
|
||||||
knownHosts: []*v1.Node{},
|
knownHosts: []*v1.Node{},
|
||||||
kubeClient: kubeClient,
|
kubeClient: kubeClient,
|
||||||
@ -192,7 +192,7 @@ func New(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// obj could be an *v1.Service, or a DeletionFinalStateUnknown marker item.
|
// obj could be an *v1.Service, or a DeletionFinalStateUnknown marker item.
|
||||||
func (s *ServiceController) enqueueService(obj interface{}) {
|
func (s *Controller) enqueueService(obj interface{}) {
|
||||||
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
|
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
runtime.HandleError(fmt.Errorf("couldn't get key for object %#v: %v", obj, err))
|
runtime.HandleError(fmt.Errorf("couldn't get key for object %#v: %v", obj, err))
|
||||||
@ -211,7 +211,7 @@ func (s *ServiceController) enqueueService(obj interface{}) {
|
|||||||
//
|
//
|
||||||
// It's an error to call Run() more than once for a given ServiceController
|
// It's an error to call Run() more than once for a given ServiceController
|
||||||
// object.
|
// object.
|
||||||
func (s *ServiceController) Run(stopCh <-chan struct{}, workers int) {
|
func (s *Controller) Run(stopCh <-chan struct{}, workers int) {
|
||||||
defer runtime.HandleCrash()
|
defer runtime.HandleCrash()
|
||||||
defer s.queue.ShutDown()
|
defer s.queue.ShutDown()
|
||||||
|
|
||||||
@ -233,12 +233,12 @@ func (s *ServiceController) Run(stopCh <-chan struct{}, workers int) {
|
|||||||
|
|
||||||
// worker runs a worker thread that just dequeues items, processes them, and marks them done.
|
// worker runs a worker thread that just dequeues items, processes them, and marks them done.
|
||||||
// It enforces that the syncHandler is never invoked concurrently with the same key.
|
// It enforces that the syncHandler is never invoked concurrently with the same key.
|
||||||
func (s *ServiceController) worker() {
|
func (s *Controller) worker() {
|
||||||
for s.processNextWorkItem() {
|
for s.processNextWorkItem() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServiceController) processNextWorkItem() bool {
|
func (s *Controller) processNextWorkItem() bool {
|
||||||
key, quit := s.queue.Get()
|
key, quit := s.queue.Get()
|
||||||
if quit {
|
if quit {
|
||||||
return false
|
return false
|
||||||
@ -256,7 +256,7 @@ func (s *ServiceController) processNextWorkItem() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServiceController) init() error {
|
func (s *Controller) init() error {
|
||||||
if s.cloud == nil {
|
if s.cloud == nil {
|
||||||
return fmt.Errorf("WARNING: no cloud provider provided, services of type LoadBalancer will fail")
|
return fmt.Errorf("WARNING: no cloud provider provided, services of type LoadBalancer will fail")
|
||||||
}
|
}
|
||||||
@ -272,7 +272,7 @@ func (s *ServiceController) init() error {
|
|||||||
|
|
||||||
// processServiceCreateOrUpdate operates loadbalancers for the incoming service accordingly.
|
// processServiceCreateOrUpdate operates loadbalancers for the incoming service accordingly.
|
||||||
// Returns an error if processing the service update failed.
|
// Returns an error if processing the service update failed.
|
||||||
func (s *ServiceController) processServiceCreateOrUpdate(service *v1.Service, key string) error {
|
func (s *Controller) processServiceCreateOrUpdate(service *v1.Service, key string) error {
|
||||||
// TODO(@MrHohn): Remove the cache once we get rid of the non-finalizer deletion
|
// TODO(@MrHohn): Remove the cache once we get rid of the non-finalizer deletion
|
||||||
// path. Ref https://github.com/kubernetes/enhancements/issues/980.
|
// path. Ref https://github.com/kubernetes/enhancements/issues/980.
|
||||||
cachedService := s.cache.getOrCreate(key)
|
cachedService := s.cache.getOrCreate(key)
|
||||||
@ -310,7 +310,7 @@ const (
|
|||||||
// syncLoadBalancerIfNeeded ensures that service's status is synced up with loadbalancer
|
// syncLoadBalancerIfNeeded ensures that service's status is synced up with loadbalancer
|
||||||
// i.e. creates loadbalancer for service if requested and deletes loadbalancer if the service
|
// i.e. creates loadbalancer for service if requested and deletes loadbalancer if the service
|
||||||
// doesn't want a loadbalancer no more. Returns whatever error occurred.
|
// doesn't want a loadbalancer no more. Returns whatever error occurred.
|
||||||
func (s *ServiceController) syncLoadBalancerIfNeeded(service *v1.Service, key string) (loadBalancerOperation, error) {
|
func (s *Controller) syncLoadBalancerIfNeeded(service *v1.Service, key string) (loadBalancerOperation, error) {
|
||||||
// Note: It is safe to just call EnsureLoadBalancer. But, on some clouds that requires a delete & create,
|
// Note: It is safe to just call EnsureLoadBalancer. But, on some clouds that requires a delete & create,
|
||||||
// which may involve service interruption. Also, we would like user-friendly events.
|
// which may involve service interruption. Also, we would like user-friendly events.
|
||||||
|
|
||||||
@ -385,7 +385,7 @@ func (s *ServiceController) syncLoadBalancerIfNeeded(service *v1.Service, key st
|
|||||||
return op, nil
|
return op, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServiceController) ensureLoadBalancer(service *v1.Service) (*v1.LoadBalancerStatus, error) {
|
func (s *Controller) ensureLoadBalancer(service *v1.Service) (*v1.LoadBalancerStatus, error) {
|
||||||
nodes, err := s.nodeLister.ListWithPredicate(getNodeConditionPredicate())
|
nodes, err := s.nodeLister.ListWithPredicate(getNodeConditionPredicate())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -485,7 +485,7 @@ func needsCleanup(service *v1.Service) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// needsUpdate checks if load balancer needs to be updated due to change in attributes.
|
// needsUpdate checks if load balancer needs to be updated due to change in attributes.
|
||||||
func (s *ServiceController) needsUpdate(oldService *v1.Service, newService *v1.Service) bool {
|
func (s *Controller) needsUpdate(oldService *v1.Service, newService *v1.Service) bool {
|
||||||
if !wantsLoadBalancer(oldService) && !wantsLoadBalancer(newService) {
|
if !wantsLoadBalancer(oldService) && !wantsLoadBalancer(newService) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -658,7 +658,7 @@ func getNodeConditionPredicate() corelisters.NodeConditionPredicate {
|
|||||||
|
|
||||||
// nodeSyncLoop handles updating the hosts pointed to by all load
|
// nodeSyncLoop handles updating the hosts pointed to by all load
|
||||||
// balancers whenever the set of nodes in the cluster changes.
|
// balancers whenever the set of nodes in the cluster changes.
|
||||||
func (s *ServiceController) nodeSyncLoop() {
|
func (s *Controller) nodeSyncLoop() {
|
||||||
newHosts, err := s.nodeLister.ListWithPredicate(getNodeConditionPredicate())
|
newHosts, err := s.nodeLister.ListWithPredicate(getNodeConditionPredicate())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
runtime.HandleError(fmt.Errorf("Failed to retrieve current set of nodes from node lister: %v", err))
|
runtime.HandleError(fmt.Errorf("Failed to retrieve current set of nodes from node lister: %v", err))
|
||||||
@ -688,7 +688,7 @@ func (s *ServiceController) nodeSyncLoop() {
|
|||||||
// updateLoadBalancerHosts updates all existing load balancers so that
|
// updateLoadBalancerHosts updates all existing load balancers so that
|
||||||
// they will match the list of hosts provided.
|
// they will match the list of hosts provided.
|
||||||
// Returns the list of services that couldn't be updated.
|
// Returns the list of services that couldn't be updated.
|
||||||
func (s *ServiceController) updateLoadBalancerHosts(services []*v1.Service, hosts []*v1.Node) (servicesToRetry []*v1.Service) {
|
func (s *Controller) updateLoadBalancerHosts(services []*v1.Service, hosts []*v1.Node) (servicesToRetry []*v1.Service) {
|
||||||
for _, service := range services {
|
for _, service := range services {
|
||||||
func() {
|
func() {
|
||||||
if service == nil {
|
if service == nil {
|
||||||
@ -705,7 +705,7 @@ func (s *ServiceController) updateLoadBalancerHosts(services []*v1.Service, host
|
|||||||
|
|
||||||
// Updates the load balancer of a service, assuming we hold the mutex
|
// Updates the load balancer of a service, assuming we hold the mutex
|
||||||
// associated with the service.
|
// associated with the service.
|
||||||
func (s *ServiceController) lockedUpdateLoadBalancerHosts(service *v1.Service, hosts []*v1.Node) error {
|
func (s *Controller) lockedUpdateLoadBalancerHosts(service *v1.Service, hosts []*v1.Node) error {
|
||||||
if !wantsLoadBalancer(service) {
|
if !wantsLoadBalancer(service) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -749,7 +749,7 @@ func loadBalancerIPsAreEqual(oldService, newService *v1.Service) bool {
|
|||||||
// syncService will sync the Service with the given key if it has had its expectations fulfilled,
|
// syncService will sync the Service with the given key if it has had its expectations fulfilled,
|
||||||
// meaning it did not expect to see any more of its pods created or deleted. This function is not meant to be
|
// meaning it did not expect to see any more of its pods created or deleted. This function is not meant to be
|
||||||
// invoked concurrently with the same key.
|
// invoked concurrently with the same key.
|
||||||
func (s *ServiceController) syncService(key string) error {
|
func (s *Controller) syncService(key string) error {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
defer func() {
|
defer func() {
|
||||||
klog.V(4).Infof("Finished syncing service %q (%v)", key, time.Since(startTime))
|
klog.V(4).Infof("Finished syncing service %q (%v)", key, time.Since(startTime))
|
||||||
@ -775,7 +775,7 @@ func (s *ServiceController) syncService(key string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServiceController) processServiceDeletion(key string) error {
|
func (s *Controller) processServiceDeletion(key string) error {
|
||||||
cachedService, ok := s.cache.get(key)
|
cachedService, ok := s.cache.get(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
// Cache does not contains the key means:
|
// Cache does not contains the key means:
|
||||||
@ -792,7 +792,7 @@ func (s *ServiceController) processServiceDeletion(key string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ServiceController) processLoadBalancerDelete(service *v1.Service, key string) error {
|
func (s *Controller) processLoadBalancerDelete(service *v1.Service, key string) error {
|
||||||
// delete load balancer info only if the service type is LoadBalancer
|
// delete load balancer info only if the service type is LoadBalancer
|
||||||
if !wantsLoadBalancer(service) {
|
if !wantsLoadBalancer(service) {
|
||||||
return nil
|
return nil
|
||||||
@ -807,7 +807,7 @@ func (s *ServiceController) processLoadBalancerDelete(service *v1.Service, key s
|
|||||||
}
|
}
|
||||||
|
|
||||||
// addFinalizer patches the service to add finalizer.
|
// addFinalizer patches the service to add finalizer.
|
||||||
func (s *ServiceController) addFinalizer(service *v1.Service) error {
|
func (s *Controller) addFinalizer(service *v1.Service) error {
|
||||||
if servicehelper.HasLBFinalizer(service) {
|
if servicehelper.HasLBFinalizer(service) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -822,7 +822,7 @@ func (s *ServiceController) addFinalizer(service *v1.Service) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// removeFinalizer patches the service to remove finalizer.
|
// removeFinalizer patches the service to remove finalizer.
|
||||||
func (s *ServiceController) removeFinalizer(service *v1.Service) error {
|
func (s *Controller) removeFinalizer(service *v1.Service) error {
|
||||||
if !servicehelper.HasLBFinalizer(service) {
|
if !servicehelper.HasLBFinalizer(service) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -849,11 +849,10 @@ func removeString(slice []string, s string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// patchStatus patches the service with the given LoadBalancerStatus.
|
// patchStatus patches the service with the given LoadBalancerStatus.
|
||||||
func (s *ServiceController) patchStatus(service *v1.Service, previousStatus, newStatus *v1.LoadBalancerStatus) error {
|
func (s *Controller) patchStatus(service *v1.Service, previousStatus, newStatus *v1.LoadBalancerStatus) error {
|
||||||
if servicehelper.LoadBalancerStatusEqual(previousStatus, newStatus) {
|
if servicehelper.LoadBalancerStatusEqual(previousStatus, newStatus) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make a copy so we don't mutate the shared informer cache.
|
// Make a copy so we don't mutate the shared informer cache.
|
||||||
updated := service.DeepCopy()
|
updated := service.DeepCopy()
|
||||||
updated.Status.LoadBalancer = *newStatus
|
updated.Status.LoadBalancer = *newStatus
|
||||||
|
@ -67,7 +67,7 @@ func defaultExternalService() *v1.Service {
|
|||||||
|
|
||||||
func alwaysReady() bool { return true }
|
func alwaysReady() bool { return true }
|
||||||
|
|
||||||
func newController() (*ServiceController, *fakecloud.Cloud, *fake.Clientset) {
|
func newController() (*Controller, *fakecloud.Cloud, *fake.Clientset) {
|
||||||
cloud := &fakecloud.Cloud{}
|
cloud := &fakecloud.Cloud{}
|
||||||
cloud.Region = region
|
cloud.Region = region
|
||||||
|
|
||||||
@ -677,7 +677,7 @@ func TestProcessServiceCreateOrUpdateK8sError(t *testing.T) {
|
|||||||
|
|
||||||
func TestSyncService(t *testing.T) {
|
func TestSyncService(t *testing.T) {
|
||||||
|
|
||||||
var controller *ServiceController
|
var controller *Controller
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
testName string
|
testName string
|
||||||
@ -756,19 +756,19 @@ func TestSyncService(t *testing.T) {
|
|||||||
|
|
||||||
func TestProcessServiceDeletion(t *testing.T) {
|
func TestProcessServiceDeletion(t *testing.T) {
|
||||||
|
|
||||||
var controller *ServiceController
|
var controller *Controller
|
||||||
var cloud *fakecloud.Cloud
|
var cloud *fakecloud.Cloud
|
||||||
// Add a global svcKey name
|
// Add a global svcKey name
|
||||||
svcKey := "external-balancer"
|
svcKey := "external-balancer"
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
testName string
|
testName string
|
||||||
updateFn func(*ServiceController) // Update function used to manipulate srv and controller values
|
updateFn func(*Controller) // Update function used to manipulate srv and controller values
|
||||||
expectedFn func(svcErr error) error // Function to check if the returned value is expected
|
expectedFn func(svcErr error) error // Function to check if the returned value is expected
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
testName: "If a non-existent service is deleted",
|
testName: "If a non-existent service is deleted",
|
||||||
updateFn: func(controller *ServiceController) {
|
updateFn: func(controller *Controller) {
|
||||||
// Does not do anything
|
// Does not do anything
|
||||||
},
|
},
|
||||||
expectedFn: func(svcErr error) error {
|
expectedFn: func(svcErr error) error {
|
||||||
@ -777,7 +777,7 @@ func TestProcessServiceDeletion(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
testName: "If cloudprovided failed to delete the service",
|
testName: "If cloudprovided failed to delete the service",
|
||||||
updateFn: func(controller *ServiceController) {
|
updateFn: func(controller *Controller) {
|
||||||
|
|
||||||
svc := controller.cache.getOrCreate(svcKey)
|
svc := controller.cache.getOrCreate(svcKey)
|
||||||
svc.state = defaultExternalService()
|
svc.state = defaultExternalService()
|
||||||
@ -797,7 +797,7 @@ func TestProcessServiceDeletion(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
testName: "If delete was successful",
|
testName: "If delete was successful",
|
||||||
updateFn: func(controller *ServiceController) {
|
updateFn: func(controller *Controller) {
|
||||||
|
|
||||||
testSvc := defaultExternalService()
|
testSvc := defaultExternalService()
|
||||||
controller.enqueueService(testSvc)
|
controller.enqueueService(testSvc)
|
||||||
@ -1221,7 +1221,7 @@ func TestAddFinalizer(t *testing.T) {
|
|||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.desc, func(t *testing.T) {
|
t.Run(tc.desc, func(t *testing.T) {
|
||||||
c := fake.NewSimpleClientset()
|
c := fake.NewSimpleClientset()
|
||||||
s := &ServiceController{
|
s := &Controller{
|
||||||
kubeClient: c,
|
kubeClient: c,
|
||||||
}
|
}
|
||||||
if _, err := s.kubeClient.CoreV1().Services(tc.svc.Namespace).Create(tc.svc); err != nil {
|
if _, err := s.kubeClient.CoreV1().Services(tc.svc.Namespace).Create(tc.svc); err != nil {
|
||||||
@ -1275,7 +1275,7 @@ func TestRemoveFinalizer(t *testing.T) {
|
|||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.desc, func(t *testing.T) {
|
t.Run(tc.desc, func(t *testing.T) {
|
||||||
c := fake.NewSimpleClientset()
|
c := fake.NewSimpleClientset()
|
||||||
s := &ServiceController{
|
s := &Controller{
|
||||||
kubeClient: c,
|
kubeClient: c,
|
||||||
}
|
}
|
||||||
if _, err := s.kubeClient.CoreV1().Services(tc.svc.Namespace).Create(tc.svc); err != nil {
|
if _, err := s.kubeClient.CoreV1().Services(tc.svc.Namespace).Create(tc.svc); err != nil {
|
||||||
@ -1375,7 +1375,7 @@ func TestPatchStatus(t *testing.T) {
|
|||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.desc, func(t *testing.T) {
|
t.Run(tc.desc, func(t *testing.T) {
|
||||||
c := fake.NewSimpleClientset()
|
c := fake.NewSimpleClientset()
|
||||||
s := &ServiceController{
|
s := &Controller{
|
||||||
kubeClient: c,
|
kubeClient: c,
|
||||||
}
|
}
|
||||||
if _, err := s.kubeClient.CoreV1().Services(tc.svc.Namespace).Create(tc.svc); err != nil {
|
if _, err := s.kubeClient.CoreV1().Services(tc.svc.Namespace).Create(tc.svc); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user