Skip to content

Commit 8760dcb

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

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

pkg/annotations/gcp.go

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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+
"networking.gke.io/load-balancer-type": "Internal",
29+
"cloud.google.com/network-tier": "Standard",
30+
}
31+
)
32+
33+
var cl client.Client
34+
35+
var _ = g.Describe("Service Annotation tests GCP", framework.LabelCCM, framework.LabelDisruptive, g.Ordered, func() {
36+
var (
37+
ctx context.Context
38+
platform configv1.PlatformType
39+
namespace string
40+
createdServices []string
41+
)
42+
43+
g.BeforeAll(func() {
44+
cfg, err := config.GetConfig()
45+
o.Expect(err).ToNot(o.HaveOccurred(), "Failed to GetConfig")
46+
47+
cl, err = client.New(cfg, client.Options{})
48+
o.Expect(err).NotTo(o.HaveOccurred(), "Failed to create Kubernetes client for test")
49+
komega.SetClient(cl)
50+
ctx = framework.GetContext()
51+
platform, err = framework.GetPlatform(ctx, cl)
52+
fmt.Println("platform is ", platform)
53+
o.Expect(err).ToNot(o.HaveOccurred(), "Failed to get platform")
54+
if platform != configv1.GCPPlatformType {
55+
g.Skip("Skipping GCP E2E tests")
56+
}
57+
58+
namespace = "default"
59+
})
60+
61+
g.AfterAll(func() {
62+
// Cleanup created services if necessary
63+
for _, svcName := range createdServices {
64+
service := &corev1.Service{
65+
ObjectMeta: metav1.ObjectMeta{
66+
Name: svcName,
67+
Namespace: namespace,
68+
},
69+
}
70+
_ = cl.Delete(ctx, service)
71+
}
72+
})
73+
74+
g.It("should add and remove annotations alternately with validation", func() {
75+
g.By("Create service without annotations")
76+
service := &corev1.Service{
77+
ObjectMeta: metav1.ObjectMeta{
78+
Name: "test-service-add-remove-annotations",
79+
Namespace: namespace,
80+
},
81+
Spec: corev1.ServiceSpec{
82+
Type: corev1.ServiceTypeLoadBalancer,
83+
Selector: map[string]string{"app": "test"},
84+
Ports: []corev1.ServicePort{{
85+
Port: 80,
86+
}},
87+
},
88+
}
89+
o.Expect(cl.Create(ctx, service)).To(o.Succeed())
90+
createdServices = append(createdServices, service.Name)
91+
92+
g.By("Verify LoadBalancer service creation")
93+
o.Eventually(func() (string, error) {
94+
updatedService := &corev1.Service{}
95+
err := cl.Get(ctx, client.ObjectKey{Name: service.Name, Namespace: namespace}, updatedService)
96+
if err != nil {
97+
return "", err
98+
}
99+
if len(updatedService.Status.LoadBalancer.Ingress) > 0 {
100+
return updatedService.Status.LoadBalancer.Ingress[0].IP, 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+
g.By("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+
// Log the Ingress IP after adding annotation
120+
g.By(fmt.Sprintf("Validating Ingress IP after adding annotation: %s=%s", key, value))
121+
var ingressIP string
122+
o.Eventually(func() string {
123+
updatedService := &corev1.Service{}
124+
err := cl.Get(ctx, client.ObjectKey{Name: service.Name, Namespace: namespace}, updatedService)
125+
o.Expect(err).ToNot(o.HaveOccurred(), "Failed to fetch service after adding annotation")
126+
if len(updatedService.Status.LoadBalancer.Ingress) > 0 {
127+
ingressIP = updatedService.Status.LoadBalancer.Ingress[0].IP
128+
return ingressIP
129+
}
130+
131+
return ""
132+
}, 1*time.Minute, 5*time.Second).ShouldNot(o.BeEmpty(), "Ingress IP did not update after adding annotation")
133+
fmt.Printf("Ingress IP after adding annotation %s=%s: %s\n", key, value, ingressIP)
134+
135+
// Remove annotation
136+
g.By(fmt.Sprintf("Removing annotation: %s", key))
137+
o.Expect(cl.Get(ctx, client.ObjectKey{Name: service.Name, Namespace: namespace}, latestService)).To(o.Succeed())
138+
delete(latestService.Annotations, key)
139+
o.Expect(cl.Update(ctx, latestService)).To(o.Succeed())
140+
141+
// Log the Ingress IP after removing annotation
142+
g.By(fmt.Sprintf("Validating Ingress IP after removing annotation: %s", key))
143+
o.Eventually(func() string {
144+
updatedService := &corev1.Service{}
145+
err := cl.Get(ctx, client.ObjectKey{Name: service.Name, Namespace: namespace}, updatedService)
146+
o.Expect(err).ToNot(o.HaveOccurred(), "Failed to fetch service after removing annotation")
147+
if len(updatedService.Status.LoadBalancer.Ingress) > 0 {
148+
ingressIP = updatedService.Status.LoadBalancer.Ingress[0].IP
149+
return ingressIP
150+
}
151+
152+
return ""
153+
}, 1*time.Minute, 5*time.Second).ShouldNot(o.BeEmpty(), "Ingress IP did not update after removing annotation")
154+
fmt.Printf("Ingress IP after removing annotation %s: %s\n", key, ingressIP)
155+
}
156+
})
157+
})

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)