Fix golangci-lint errors (#10196)

* Fix golangci-lint errors

Signed-off-by: z1cheng <imchench@gmail.com>

* Fix dupl errors

Signed-off-by: z1cheng <imchench@gmail.com>

* Fix comments

Signed-off-by: z1cheng <imchench@gmail.com>

* Fix errcheck lint errors

Signed-off-by: z1cheng <imchench@gmail.com>

* Fix assert in e2e test

Signed-off-by: z1cheng <imchench@gmail.com>

* Not interrupt the waitForPodsReady

Signed-off-by: z1cheng <imchench@gmail.com>

* Replace string with constant

Signed-off-by: z1cheng <imchench@gmail.com>

* Fix comments

Signed-off-by: z1cheng <imchench@gmail.com>

* Revert write file permision

Signed-off-by: z1cheng <imchench@gmail.com>

---------

Signed-off-by: z1cheng <imchench@gmail.com>
This commit is contained in:
Chen Chen 2023-08-31 15:36:48 +08:00 committed by GitHub
parent 46d87d3462
commit b3060bfbd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
253 changed files with 2434 additions and 2113 deletions

View file

@ -44,7 +44,7 @@ import (
// which the status should check if an update is required.
var UpdateInterval = 60
// Syncer ...
// Syncer is an interface that implements syncer
type Syncer interface {
Run(chan struct{})
@ -56,7 +56,7 @@ type ingressLister interface {
ListIngresses() []*ingress.Ingress
}
// Config ...
// Config is a structure that implements Client interfaces
type Config struct {
Client clientset.Interface
@ -87,7 +87,7 @@ type statusSync struct {
}
// Start starts the loop to keep the status in sync
func (s statusSync) Run(stopCh chan struct{}) {
func (s *statusSync) Run(stopCh chan struct{}) {
go s.syncQueue.Run(time.Second, stopCh)
// trigger initial sync
@ -95,6 +95,7 @@ func (s statusSync) Run(stopCh chan struct{}) {
// when this instance is the leader we need to enqueue
// an item to trigger the update of the Ingress status.
//nolint:staticcheck // TODO: will replace it since wait.PollUntil is deprecated
err := wait.PollUntil(time.Duration(UpdateInterval)*time.Second, func() (bool, error) {
s.syncQueue.EnqueueTask(task.GetDummyObject("sync status"))
return false, nil
@ -106,7 +107,7 @@ func (s statusSync) Run(stopCh chan struct{}) {
// Shutdown stops the sync. In case the instance is the leader it will remove the current IP
// if there is no other instances running.
func (s statusSync) Shutdown() {
func (s *statusSync) Shutdown() {
go s.syncQueue.Shutdown()
if !s.UpdateStatusOnShutdown {
@ -135,7 +136,7 @@ func (s statusSync) Shutdown() {
s.updateStatus([]v1.IngressLoadBalancerIngress{})
}
func (s *statusSync) sync(key interface{}) error {
func (s *statusSync) sync(_ interface{}) error {
if s.syncQueue.IsShuttingDown() {
klog.V(2).InfoS("skipping Ingress status update (shutting down in progress)")
return nil
@ -150,13 +151,13 @@ func (s *statusSync) sync(key interface{}) error {
return nil
}
func (s statusSync) keyfunc(input interface{}) (interface{}, error) {
func (s *statusSync) keyfunc(input interface{}) (interface{}, error) {
return input, nil
}
// NewStatusSyncer returns a new Syncer instance
func NewStatusSyncer(config Config) Syncer {
st := statusSync{
st := &statusSync{
Config: config,
}
st.syncQueue = task.NewCustomTaskQueue(st.sync, st.keyfunc)
@ -229,7 +230,6 @@ func (s *statusSync) runningAddresses() ([]v1.IngressLoadBalancerIngress, error)
}
func (s *statusSync) isRunningMultiplePods() bool {
// As a standard, app.kubernetes.io are "reserved well-known" labels.
// In our case, we add those labels as identifiers of the Ingress
// deployment in this namespace, so we can select it as a set of Ingress instances.
@ -288,7 +288,8 @@ func (s *statusSync) updateStatus(newIngressPoint []v1.IngressLoadBalancerIngres
}
func runUpdate(ing *ingress.Ingress, status []v1.IngressLoadBalancerIngress,
client clientset.Interface) pool.WorkFunc {
client clientset.Interface,
) pool.WorkFunc {
return func(wu pool.WorkUnit) (interface{}, error) {
if wu.IsCancelled() {
return nil, nil
@ -341,7 +342,10 @@ func ingressSliceEqual(lhs, rhs []v1.IngressLoadBalancerIngress) bool {
}
func statusAddressFromService(service string, kubeClient clientset.Interface) ([]v1.IngressLoadBalancerIngress, error) {
ns, name, _ := k8s.ParseNameNS(service)
ns, name, err := k8s.ParseNameNS(service)
if err != nil {
return nil, err
}
svc, err := kubeClient.CoreV1().Services(ns).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
return nil, err
@ -362,15 +366,15 @@ func statusAddressFromService(service string, kubeClient clientset.Interface) ([
IP: svc.Spec.ClusterIP,
}}, nil
}
addrs := make([]v1.IngressLoadBalancerIngress, len(svc.Spec.ExternalIPs))
for i, ip := range svc.Spec.ExternalIPs {
addrs[i] = v1.IngressLoadBalancerIngress{IP: ip}
addrs := make([]v1.IngressLoadBalancerIngress, 0, len(svc.Spec.ExternalIPs))
for _, ip := range svc.Spec.ExternalIPs {
addrs = append(addrs, v1.IngressLoadBalancerIngress{IP: ip})
}
return addrs, nil
case apiv1.ServiceTypeLoadBalancer:
addrs := make([]v1.IngressLoadBalancerIngress, len(svc.Status.LoadBalancer.Ingress))
addrs := make([]v1.IngressLoadBalancerIngress, 0, len(svc.Status.LoadBalancer.Ingress))
for i, ingress := range svc.Status.LoadBalancer.Ingress {
addrs[i] = v1.IngressLoadBalancerIngress{}
addrs = append(addrs, v1.IngressLoadBalancerIngress{})
if ingress.Hostname != "" {
addrs[i].Hostname = ingress.Hostname
}

View file

@ -18,7 +18,6 @@ package status
import (
"context"
"os"
"reflect"
"testing"
"time"
@ -34,6 +33,8 @@ import (
"k8s.io/ingress-nginx/pkg/apis/ingress"
)
const localhost = "127.0.0.1"
func buildLoadBalancerIngressByIP() []networking.IngressLoadBalancerIngress {
return []networking.IngressLoadBalancerIngress{
{
@ -126,17 +127,6 @@ func buildSimpleClientSet() *testclient.Clientset {
// This is commented out as the ServiceStatus.LoadBalancer field expects a LoadBalancerStatus object
// which is incompatible with the current Ingress struct which expects a IngressLoadBalancerStatus object
// TODO: update this service when the ServiceStatus struct gets updated
//{
// ObjectMeta: metav1.ObjectMeta{
// Name: "foo",
// Namespace: apiv1.NamespaceDefault,
// },
// Status: apiv1.ServiceStatus{
// LoadBalancer: apiv1.LoadBalancerStatus{
// Ingress: buildLoadBalancerIngressByIP(),
// },
// },
//},
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo_non_exist",
@ -185,7 +175,8 @@ func buildSimpleClientSet() *testclient.Clientset {
Name: "ingress-controller-leader",
Namespace: apiv1.NamespaceDefault,
},
}}},
},
}},
&networking.IngressList{Items: buildExtensionsIngresses()},
)
}
@ -245,30 +236,33 @@ func buildExtensionsIngresses() []networking.Ingress {
}
}
type testIngressLister struct {
}
type testIngressLister struct{}
func (til *testIngressLister) ListIngresses() []*ingress.Ingress {
var ingresses []*ingress.Ingress
ingresses = append(ingresses, &ingress.Ingress{
Ingress: networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "foo_ingress_non_01",
Namespace: apiv1.NamespaceDefault,
}}})
ingresses = append(ingresses, &ingress.Ingress{
Ingress: networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "foo_ingress_1",
Namespace: apiv1.NamespaceDefault,
},
Status: networking.IngressStatus{
LoadBalancer: networking.IngressLoadBalancerStatus{
Ingress: buildLoadBalancerIngressByIP(),
ingresses = append(ingresses,
&ingress.Ingress{
Ingress: networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "foo_ingress_non_01",
Namespace: apiv1.NamespaceDefault,
},
},
}})
},
&ingress.Ingress{
Ingress: networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "foo_ingress_1",
Namespace: apiv1.NamespaceDefault,
},
Status: networking.IngressStatus{
LoadBalancer: networking.IngressLoadBalancerStatus{
Ingress: buildLoadBalancerIngressByIP(),
},
},
},
},
)
return ingresses
}
@ -290,8 +284,8 @@ func buildStatusSync() statusSync {
func TestStatusActions(t *testing.T) {
// make sure election can be created
os.Setenv("POD_NAME", "foo1")
os.Setenv("POD_NAMESPACE", apiv1.NamespaceDefault)
t.Setenv("POD_NAME", "foo1")
t.Setenv("POD_NAMESPACE", apiv1.NamespaceDefault)
c := Config{
Client: buildSimpleClientSet(),
PublishService: "",
@ -315,7 +309,10 @@ func TestStatusActions(t *testing.T) {
t.Fatalf("expected a valid Sync")
}
fk := fkSync.(statusSync)
fk, ok := fkSync.(*statusSync)
if !ok {
t.Errorf("unexpected type: %T", fkSync)
}
// start it and wait for the election and syn actions
stopCh := make(chan struct{})
@ -366,7 +363,7 @@ func TestStatusActions(t *testing.T) {
}
}
func TestCallback(t *testing.T) {
func TestCallback(_ *testing.T) {
buildStatusSync()
}
@ -375,7 +372,6 @@ func TestKeyfunc(t *testing.T) {
i := "foo_base_pod"
r, err := fk.keyfunc(i)
if err != nil {
t.Fatalf("unexpected error")
}
@ -392,34 +388,36 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
}{
"service type ClusterIP": {
testclient.NewSimpleClientset(
&apiv1.PodList{Items: []apiv1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
Spec: apiv1.PodSpec{
NodeName: "foo_node",
},
Status: apiv1.PodStatus{
Phase: apiv1.PodRunning,
&apiv1.PodList{
Items: []apiv1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
Spec: apiv1.PodSpec{
NodeName: "foo_node",
},
Status: apiv1.PodStatus{
Phase: apiv1.PodRunning,
},
},
},
},
},
&apiv1.ServiceList{Items: []apiv1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
Spec: apiv1.ServiceSpec{
Type: apiv1.ServiceTypeClusterIP,
ClusterIP: "1.1.1.1",
&apiv1.ServiceList{
Items: []apiv1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
Spec: apiv1.ServiceSpec{
Type: apiv1.ServiceTypeClusterIP,
ClusterIP: "1.1.1.1",
},
},
},
},
},
),
[]networking.IngressLoadBalancerIngress{
{IP: "1.1.1.1"},
@ -428,19 +426,20 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
},
"service type NodePort": {
testclient.NewSimpleClientset(
&apiv1.ServiceList{Items: []apiv1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
Spec: apiv1.ServiceSpec{
Type: apiv1.ServiceTypeNodePort,
ClusterIP: "1.1.1.1",
&apiv1.ServiceList{
Items: []apiv1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
Spec: apiv1.ServiceSpec{
Type: apiv1.ServiceTypeNodePort,
ClusterIP: "1.1.1.1",
},
},
},
},
},
),
[]networking.IngressLoadBalancerIngress{
{IP: "1.1.1.1"},
@ -449,19 +448,20 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
},
"service type ExternalName": {
testclient.NewSimpleClientset(
&apiv1.ServiceList{Items: []apiv1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
Spec: apiv1.ServiceSpec{
Type: apiv1.ServiceTypeExternalName,
ExternalName: "foo.bar",
&apiv1.ServiceList{
Items: []apiv1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
Spec: apiv1.ServiceSpec{
Type: apiv1.ServiceTypeExternalName,
ExternalName: "foo.bar",
},
},
},
},
},
),
[]networking.IngressLoadBalancerIngress{
{Hostname: "foo.bar"},
@ -470,35 +470,36 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
},
"service type LoadBalancer": {
testclient.NewSimpleClientset(
&apiv1.ServiceList{Items: []apiv1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
Spec: apiv1.ServiceSpec{
Type: apiv1.ServiceTypeLoadBalancer,
},
Status: apiv1.ServiceStatus{
LoadBalancer: apiv1.LoadBalancerStatus{
Ingress: []apiv1.LoadBalancerIngress{
{
IP: "10.0.0.1",
},
{
IP: "",
Hostname: "foo",
},
{
IP: "10.0.0.2",
Hostname: "10-0-0-2.cloudprovider.example.net",
&apiv1.ServiceList{
Items: []apiv1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
Spec: apiv1.ServiceSpec{
Type: apiv1.ServiceTypeLoadBalancer,
},
Status: apiv1.ServiceStatus{
LoadBalancer: apiv1.LoadBalancerStatus{
Ingress: []apiv1.LoadBalancerIngress{
{
IP: "10.0.0.1",
},
{
IP: "",
Hostname: "foo",
},
{
IP: "10.0.0.2",
Hostname: "10-0-0-2.cloudprovider.example.net",
},
},
},
},
},
},
},
},
),
[]networking.IngressLoadBalancerIngress{
{IP: "10.0.0.1"},
@ -512,28 +513,29 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
},
"service type LoadBalancer with same externalIP and ingress IP": {
testclient.NewSimpleClientset(
&apiv1.ServiceList{Items: []apiv1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
Spec: apiv1.ServiceSpec{
Type: apiv1.ServiceTypeLoadBalancer,
ExternalIPs: []string{"10.0.0.1"},
},
Status: apiv1.ServiceStatus{
LoadBalancer: apiv1.LoadBalancerStatus{
Ingress: []apiv1.LoadBalancerIngress{
{
IP: "10.0.0.1",
&apiv1.ServiceList{
Items: []apiv1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
Spec: apiv1.ServiceSpec{
Type: apiv1.ServiceTypeLoadBalancer,
ExternalIPs: []string{"10.0.0.1"},
},
Status: apiv1.ServiceStatus{
LoadBalancer: apiv1.LoadBalancerStatus{
Ingress: []apiv1.LoadBalancerIngress{
{
IP: "10.0.0.1",
},
},
},
},
},
},
},
},
),
[]networking.IngressLoadBalancerIngress{
{IP: "10.0.0.1"},
@ -542,15 +544,16 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
},
"invalid service type": {
testclient.NewSimpleClientset(
&apiv1.ServiceList{Items: []apiv1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
&apiv1.ServiceList{
Items: []apiv1.Service{
{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: apiv1.NamespaceDefault,
},
},
},
},
},
),
nil,
true,
@ -559,7 +562,6 @@ func TestRunningAddressesWithPublishService(t *testing.T) {
for title, tc := range testCases {
t.Run(title, func(t *testing.T) {
fk := buildStatusSync()
fk.Config.Client = tc.fakeClient
@ -587,7 +589,11 @@ func TestRunningAddressesWithPods(t *testing.T) {
fk := buildStatusSync()
fk.PublishService = ""
r, _ := fk.runningAddresses()
r, err := fk.runningAddresses()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if r == nil {
t.Fatalf("returned nil but expected valid []networking.IngressLoadBalancerIngress")
}
@ -603,9 +609,12 @@ func TestRunningAddressesWithPods(t *testing.T) {
func TestRunningAddressesWithPublishStatusAddress(t *testing.T) {
fk := buildStatusSync()
fk.PublishStatusAddress = "127.0.0.1"
fk.PublishStatusAddress = localhost
ra, _ := fk.runningAddresses()
ra, err := fk.runningAddresses()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if ra == nil {
t.Fatalf("returned nil but expected valid []networking.IngressLoadBalancerIngress")
}
@ -614,8 +623,8 @@ func TestRunningAddressesWithPublishStatusAddress(t *testing.T) {
t.Errorf("returned %v but expected %v", rl, 1)
}
rv := ra[0]
if rv.IP != "127.0.0.1" {
t.Errorf("returned %v but expected %v", rv, networking.IngressLoadBalancerIngress{IP: "127.0.0.1"})
if rv.IP != localhost {
t.Errorf("returned %v but expected %v", rv, networking.IngressLoadBalancerIngress{IP: localhost})
}
}
@ -623,7 +632,10 @@ func TestRunningAddressesWithPublishStatusAddresses(t *testing.T) {
fk := buildStatusSync()
fk.PublishStatusAddress = "127.0.0.1,1.1.1.1"
ra, _ := fk.runningAddresses()
ra, err := fk.runningAddresses()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if ra == nil {
t.Fatalf("returned nil but expected valid []networking.IngressLoadBalancerIngress")
}
@ -633,8 +645,8 @@ func TestRunningAddressesWithPublishStatusAddresses(t *testing.T) {
}
rv := ra[0]
rv2 := ra[1]
if rv.IP != "127.0.0.1" {
t.Errorf("returned %v but expected %v", rv, networking.IngressLoadBalancerIngress{IP: "127.0.0.1"})
if rv.IP != localhost {
t.Errorf("returned %v but expected %v", rv, networking.IngressLoadBalancerIngress{IP: localhost})
}
if rv2.IP != "1.1.1.1" {
t.Errorf("returned %v but expected %v", rv2, networking.IngressLoadBalancerIngress{IP: "1.1.1.1"})
@ -645,7 +657,10 @@ func TestRunningAddressesWithPublishStatusAddressesAndSpaces(t *testing.T) {
fk := buildStatusSync()
fk.PublishStatusAddress = "127.0.0.1, 1.1.1.1"
ra, _ := fk.runningAddresses()
ra, err := fk.runningAddresses()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if ra == nil {
t.Fatalf("returned nil but expected valid []networking.IngressLoadBalancerIngresst")
}
@ -655,8 +670,8 @@ func TestRunningAddressesWithPublishStatusAddressesAndSpaces(t *testing.T) {
}
rv := ra[0]
rv2 := ra[1]
if rv.IP != "127.0.0.1" {
t.Errorf("returned %v but expected %v", rv, networking.IngressLoadBalancerIngress{IP: "127.0.0.1"})
if rv.IP != localhost {
t.Errorf("returned %v but expected %v", rv, networking.IngressLoadBalancerIngress{IP: localhost})
}
if rv2.IP != "1.1.1.1" {
t.Errorf("returned %v but expected %v", rv2, networking.IngressLoadBalancerIngress{IP: "1.1.1.1"})