Skip to content

Commit

Permalink
Reduce string constant duplication
Browse files Browse the repository at this point in the history
Newer versions of goconst detect more instances of string duplication.
Address that by providing pod construction functions for common pods
(API server, controller manager, proxy). Disable goconst on the
generated embedded YAMLs file (it doesn't seem useful to avoid string
duplication there).

Signed-off-by: Stephen Kitt <skitt@redhat.com>
  • Loading branch information
skitt authored and tpantelis committed Oct 25, 2023
1 parent a088bf5 commit 1b296be
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,8 @@ issues:
- path: pkg/metrics/service-monitor.go
linters:
- goheader

# Ignore duplicate strings in generated code
- path: pkg/embeddedyamls/yamls.go
linters:
- goconst
4 changes: 2 additions & 2 deletions pkg/discovery/network/calico_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ var _ = Describe("Calico Network", func() {
JustBeforeEach(func() {
initObjs = []client.Object{
calicoCfgMap,
fakePod("kube-apiserver", []string{"kube-apiserver", "--service-cluster-ip-range=" + testServiceCIDR}, []v1.EnvVar{}),
fakePod("kube-controller-manager", []string{"kube-controller-manager", "--cluster-cidr=" + testPodCIDR}, []v1.EnvVar{}),
fakeKubeAPIServerPod(),
fakeKubeControllerManagerPod(),
}

client := newTestClient(initObjs...)
Expand Down
2 changes: 1 addition & 1 deletion pkg/discovery/network/canal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var _ = Describe("Canal Flannel Network", func() {
It("Should return the ClusterNetwork structure with the pod CIDR and the service CIDR", func() {
clusterNet := testDiscoverWith(
&canalFlannelCfgMap,
fakePod("kube-apiserver", []string{"kube-apiserver", "--service-cluster-ip-range=" + testServiceCIDR}, []v1.EnvVar{}),
fakeKubeAPIServerPod(),
)
Expect(clusterNet).NotTo(BeNil())
Expect(clusterNet.NetworkPlugin).To(Equal(cni.CanalFlannel))
Expand Down
12 changes: 6 additions & 6 deletions pkg/discovery/network/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ var _ = Describe("Generic Network", func() {

BeforeEach(func() {
clusterNet = testDiscoverGenericWith(
fakePod("kube-controller-manager", []string{"kube-controller-manager", "--cluster-cidr=" + testPodCIDR}, []corev1.EnvVar{}),
fakeKubeControllerManagerPod(),
)
Expect(clusterNet).NotTo(BeNil())
})
Expand All @@ -137,7 +137,7 @@ var _ = Describe("Generic Network", func() {

BeforeEach(func() {
clusterNet = testDiscoverGenericWith(
fakePod("kube-proxy", []string{"kube-proxy", "--cluster-cidr=" + testPodCIDR}, []corev1.EnvVar{}),
fakeKubeProxyPod(),
)
Expect(clusterNet).NotTo(BeNil())
})
Expand All @@ -160,7 +160,7 @@ var _ = Describe("Generic Network", func() {

BeforeEach(func() {
clusterNet = testDiscoverGenericWith(
fakePod("kube-apiserver", []string{"kube-apiserver", "--service-cluster-ip-range=" + testServiceCIDR}, []corev1.EnvVar{}),
fakeKubeAPIServerPod(),
)
Expect(clusterNet).NotTo(BeNil())
})
Expand All @@ -183,8 +183,8 @@ var _ = Describe("Generic Network", func() {

BeforeEach(func() {
clusterNet = testDiscoverGenericWith(
fakePod("kube-proxy", []string{"kube-proxy", "--cluster-cidr=" + testPodCIDR}, []corev1.EnvVar{}),
fakePod("kube-apiserver", []string{"kube-apiserver", "--service-cluster-ip-range=" + testServiceCIDR}, []corev1.EnvVar{}),
fakeKubeProxyPod(),
fakeKubeAPIServerPod(),
)
Expect(clusterNet).NotTo(BeNil())
})
Expand Down Expand Up @@ -265,7 +265,7 @@ var _ = Describe("Generic Network", func() {
BeforeEach(func() {
clusterNet = testDiscoverGenericWith(
fakeNode("node1", testPodCIDR),
fakePod("kube-apiserver", []string{"kube-apiserver", "--service-cluster-ip-range=" + testServiceCIDR}, []corev1.EnvVar{}),
fakeKubeAPIServerPod(),
)
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/discovery/network/kindnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var _ = Describe("Kindnet CNI", func() {
BeforeEach(func() {
clusterNet = testDiscoverNetwork(
fakePod("kindnet", []string{"kindnet"}, []v1.EnvVar{{Name: "POD_SUBNET", Value: testPodCIDR}}),
fakePod("kube-apiserver", []string{"kube-apiserver", "--service-cluster-ip-range=" + testServiceCIDR}, []v1.EnvVar{}),
fakeKubeAPIServerPod(),
)
Expect(clusterNet).NotTo(BeNil())
})
Expand Down
12 changes: 12 additions & 0 deletions pkg/discovery/network/network_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ func fakePodWithNamespace(namespace, name, component string, command []string, e
}
}

func fakeKubeAPIServerPod() *v1.Pod {
return fakePod("kube-apiserver", []string{"kube-apiserver", "--service-cluster-ip-range=" + testServiceCIDR}, []v1.EnvVar{})
}

func fakeKubeControllerManagerPod() *v1.Pod {
return fakePod("kube-controller-manager", []string{"kube-controller-manager", "--cluster-cidr=" + testPodCIDR}, []v1.EnvVar{})
}

func fakeKubeProxyPod() *v1.Pod {
return fakePod("kube-proxy", []string{"kube-proxy", "--cluster-cidr=" + testPodCIDR}, []v1.EnvVar{})
}

func fakeService(namespace, name, component string) *v1.Service {
return &v1.Service{
ObjectMeta: v1meta.ObjectMeta{
Expand Down
2 changes: 1 addition & 1 deletion pkg/discovery/network/weavenet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var _ = Describe("Weave Network", func() {
BeforeEach(func() {
clusterNet = testDiscoverNetwork(
fakePod("weave-net", []string{"weave-net"}, []v1.EnvVar{{Name: "IPALLOC_RANGE", Value: testPodCIDR}}),
fakePod("kube-apiserver", []string{"kube-apiserver", "--service-cluster-ip-range=" + testServiceCIDR}, []v1.EnvVar{}),
fakeKubeAPIServerPod(),
)
Expect(clusterNet).NotTo(BeNil())
})
Expand Down

0 comments on commit 1b296be

Please sign in to comment.