Skip to content

Commit ba9d97d

Browse files
committed
annotation tests
made the service to LB type
1 parent e0af696 commit ba9d97d

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

pkg/annotations/gcp.go

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package annotations
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
configv1 "github.com/openshift/api/config/v1"
9+
corev1 "k8s.io/api/core/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"sigs.k8s.io/controller-runtime/pkg/client"
12+
"sigs.k8s.io/controller-runtime/pkg/client/config"
13+
14+
"sigs.k8s.io/controller-runtime/pkg/envtest/komega"
15+
16+
"github.com/openshift/cluster-api-actuator-pkg/pkg/framework"
17+
18+
g "github.com/onsi/ginkgo/v2"
19+
o "github.com/onsi/gomega"
20+
)
21+
22+
var (
23+
annotationsToTest = map[string]string{
24+
"traffic-policy.network.alpha.openshift.io/local-with-fallback": "true",
25+
"alpha.cloud.google.com/load-balancer-backend-share": "",
26+
"networking.gke.io/internal-load-balancer-allow-global-access": "true",
27+
"networking.gke.io/internal-load-balancer-subnet": "",
28+
"cloud.google.com/network-tier": "Standard",
29+
}
30+
)
31+
32+
var cl client.Client
33+
34+
var _ = g.Describe("Service Annotation tests GCP", framework.LabelCCM, framework.LabelDisruptive, g.Ordered, func() {
35+
var (
36+
ctx context.Context
37+
platform configv1.PlatformType
38+
namespace string
39+
createdServices []string
40+
)
41+
42+
g.BeforeAll(func() {
43+
cfg, err := config.GetConfig()
44+
o.Expect(err).ToNot(o.HaveOccurred(), "Failed to GetConfig")
45+
46+
cl, err = client.New(cfg, client.Options{})
47+
o.Expect(err).NotTo(o.HaveOccurred(), "Failed to create Kubernetes client for test")
48+
komega.SetClient(cl)
49+
ctx = framework.GetContext()
50+
platform, err = framework.GetPlatform(ctx, cl)
51+
fmt.Println("platform is ", platform)
52+
o.Expect(err).ToNot(o.HaveOccurred(), "Failed to get platform")
53+
if platform != configv1.GCPPlatformType {
54+
g.Skip("Skipping GCP E2E tests")
55+
}
56+
57+
namespace = "default"
58+
})
59+
60+
g.AfterAll(func() {
61+
for _, svcName := range createdServices {
62+
service := &corev1.Service{
63+
ObjectMeta: metav1.ObjectMeta{
64+
Name: svcName,
65+
Namespace: namespace,
66+
},
67+
}
68+
_ = cl.Delete(ctx, service)
69+
}
70+
})
71+
72+
g.It("should validate IP changes only for specific annotations", func() {
73+
g.By("Create service without annotations")
74+
service := &corev1.Service{
75+
ObjectMeta: metav1.ObjectMeta{
76+
Name: "test-service-ip-validation",
77+
Namespace: namespace,
78+
},
79+
Spec: corev1.ServiceSpec{
80+
Type: corev1.ServiceTypeLoadBalancer,
81+
Selector: map[string]string{"app": "test"},
82+
Ports: []corev1.ServicePort{{
83+
Port: 80,
84+
}},
85+
},
86+
}
87+
o.Expect(cl.Create(ctx, service)).To(o.Succeed())
88+
createdServices = append(createdServices, service.Name)
89+
90+
g.By("Verify LoadBalancer service creation")
91+
var lastIngressIP string
92+
o.Eventually(func() (string, error) {
93+
updatedService := &corev1.Service{}
94+
err := cl.Get(ctx, client.ObjectKey{Name: service.Name, Namespace: namespace}, updatedService)
95+
if err != nil {
96+
return "", err
97+
}
98+
if len(updatedService.Status.LoadBalancer.Ingress) > 0 {
99+
lastIngressIP = updatedService.Status.LoadBalancer.Ingress[0].IP
100+
return lastIngressIP, nil
101+
}
102+
103+
return "", nil
104+
}, 2*time.Minute, 10*time.Second).ShouldNot(o.BeEmpty(), "LoadBalancer service did not get an external IP")
105+
106+
// Add and remove annotations alternately
107+
for key, value := range annotationsToTest {
108+
// Add annotation
109+
g.By(fmt.Sprintf("Adding annotation: %s=%s", key, value))
110+
latestService := &corev1.Service{}
111+
o.Expect(cl.Get(ctx, client.ObjectKey{Name: service.Name, Namespace: namespace}, latestService)).To(o.Succeed())
112+
113+
if latestService.Annotations == nil {
114+
latestService.Annotations = make(map[string]string)
115+
}
116+
latestService.Annotations[key] = value
117+
o.Expect(cl.Update(ctx, latestService)).To(o.Succeed())
118+
119+
// Validate IP change only for relevant annotations
120+
if key == "cloud.google.com/network-tier" {
121+
g.By(fmt.Sprintf("Validating Ingress IP change after annotation update: %s=%s", key, value))
122+
o.Eventually(func() (string, error) {
123+
updatedService := &corev1.Service{}
124+
err := cl.Get(ctx, client.ObjectKey{Name: service.Name, Namespace: namespace}, updatedService)
125+
if err != nil {
126+
return "", err
127+
}
128+
if len(updatedService.Status.LoadBalancer.Ingress) > 0 {
129+
return updatedService.Status.LoadBalancer.Ingress[0].IP, nil
130+
}
131+
132+
return "", nil
133+
}, 4*time.Minute, 10*time.Second).ShouldNot(o.Equal(lastIngressIP), "Ingress IP did not change after annotation update")
134+
}
135+
136+
// Remove annotation
137+
g.By(fmt.Sprintf("Removing annotation: %s", key))
138+
o.Expect(cl.Get(ctx, client.ObjectKey{Name: service.Name, Namespace: namespace}, latestService)).To(o.Succeed())
139+
delete(latestService.Annotations, key)
140+
o.Expect(cl.Update(ctx, latestService)).To(o.Succeed())
141+
142+
g.By("Validate IP change only for relevant annotations")
143+
if key == "cloud.google.com/network-tier" {
144+
g.By(fmt.Sprintf("Validating Ingress IP change after annotation removal: %s", key))
145+
o.Eventually(func() (string, error) {
146+
updatedService := &corev1.Service{}
147+
err := cl.Get(ctx, client.ObjectKey{Name: service.Name, Namespace: namespace}, updatedService)
148+
if err != nil {
149+
return "", err
150+
}
151+
if len(updatedService.Status.LoadBalancer.Ingress) > 0 {
152+
return updatedService.Status.LoadBalancer.Ingress[0].IP, nil
153+
}
154+
155+
return "", nil
156+
}, 4*time.Minute, 10*time.Second).ShouldNot(o.Equal(lastIngressIP), "Ingress IP did not change after annotation removal")
157+
}
158+
}
159+
})
160+
})

pkg/e2e_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
gcpv1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
2020
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2121

22+
_ "github.com/openshift/cluster-api-actuator-pkg/pkg/annotations"
2223
_ "github.com/openshift/cluster-api-actuator-pkg/pkg/autoscaler"
2324
_ "github.com/openshift/cluster-api-actuator-pkg/pkg/capi"
2425
_ "github.com/openshift/cluster-api-actuator-pkg/pkg/infra"
@@ -51,6 +52,10 @@ func init() {
5152
klog.Fatal(err)
5253
}
5354

55+
if err := gcpv1.AddToScheme(scheme.Scheme); err != nil {
56+
klog.Fatal(err)
57+
}
58+
5459
if err := awsv1.AddToScheme(scheme.Scheme); err != nil {
5560
klog.Fatal(err)
5661
}

0 commit comments

Comments
 (0)