Skip to content

Commit d261767

Browse files
authored
Merge pull request #1 from andrewsykim/clusterset-ip-from-service-import
Assign predefined clustersetIP to service status
2 parents 2b8d599 + a3047f2 commit d261767

File tree

7 files changed

+91
-4
lines changed

7 files changed

+91
-4
lines changed

config/rbac/role.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ rules:
1717
- patch
1818
- update
1919
- watch
20+
- apiGroups:
21+
- ""
22+
resources:
23+
- services/status
24+
verbs:
25+
- create
26+
- get
27+
- list
28+
- patch
29+
- update
30+
- watch
2031
- apiGroups:
2132
- discovery.k8s.io
2233
resources:

demo/demo.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ run "${k1} get endpointslice -n demo ${ep_1} -o yaml | ./edit-meta --metadata '{
5555
run "${k2} get endpointslice -n demo ${ep_2} -o yaml | ./edit-meta --metadata '{name: imported-${ep_2}, namespace: demo, labels: {multicluster.kubernetes.io/service-name: serve}}' > yaml/slice-2.tmp"
5656
run "${k1} apply -f yaml/serviceimport.yaml -f yaml/slice-1.tmp -f yaml/slice-2.tmp"
5757
run "${k2} apply -f yaml/serviceimport.yaml -f yaml/slice-1.tmp -f yaml/slice-2.tmp"
58+
run "${k1} apply -f yaml/serviceimport-with-vip.yaml -f yaml/slice-1.tmp -f yaml/slice-2.tmp"
59+
run "${k2} apply -f yaml/serviceimport-with-vip.yaml -f yaml/slice-1.tmp -f yaml/slice-2.tmp"
5860

5961
desc "See what we've created..."
6062
run "${k1} get -n demo serviceimports"
@@ -77,4 +79,4 @@ desc "See for yourself"
7779
desc "Cluster 1: kubectl --kubeconfig ${kubeconfig1} -n demo"
7880
desc "Cluster 2: kubectl --kubeconfig ${kubeconfig2} -n demo"
7981
desc "(Enter to exit)"
80-
read -s
82+
read -s

demo/yaml/serviceimport-with-vip.yaml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: multicluster.x-k8s.io/v1alpha1
2+
kind: ServiceImport
3+
metadata:
4+
name: serve-with-vip
5+
namespace: demo
6+
spec:
7+
type: ClusterSetIP
8+
ips:
9+
- 1.2.3.4
10+
ports:
11+
- port: 80
12+
protocol: TCP

demo/yaml/serviceimport.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ spec:
77
type: ClusterSetIP
88
ports:
99
- port: 80
10-
protocol: TCP
10+
protocol: TCP

pkg/controllers/service.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ func (r *ServiceReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
6565
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: importName}, &svcImport); err != nil {
6666
return ctrl.Result{}, err
6767
}
68-
if len(svcImport.Spec.IPs) == 1 && svcImport.Spec.IPs[0] == service.Spec.ClusterIP {
68+
69+
if len(svcImport.Spec.IPs) > 0 {
6970
return ctrl.Result{}, nil
7071
}
72+
7173
svcImport.Spec.IPs = []string{service.Spec.ClusterIP}
7274
if err := r.Client.Update(ctx, &svcImport); err != nil {
7375
return ctrl.Result{}, err

pkg/controllers/serviceimport.go

+27
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func shouldIgnoreImport(svcImport *v1alpha1.ServiceImport) bool {
6060
return false
6161
}
6262

63+
// +kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch
64+
// +kubebuilder:rbac:groups=core,resources=services/status,verbs=get;list;watch;create;update;patch
65+
6366
// Reconcile the changes.
6467
func (r *ServiceImportReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
6568
ctx, cancel := context.WithCancel(context.Background())
@@ -92,6 +95,7 @@ func (r *ServiceImportReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
9295
} else if !apierrors.IsNotFound(err) {
9396
return ctrl.Result{}, err
9497
}
98+
9599
svc = v1.Service{
96100
ObjectMeta: metav1.ObjectMeta{
97101
Namespace: req.Namespace,
@@ -114,6 +118,29 @@ func (r *ServiceImportReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
114118
return ctrl.Result{}, err
115119
}
116120
log.Info("created service")
121+
122+
if len(svcImport.Spec.IPs) == 0 {
123+
return ctrl.Result{}, nil
124+
}
125+
126+
// update loadbalanacer status with provided clustersetIPs
127+
ingress := []v1.LoadBalancerIngress{}
128+
for _, ip := range svcImport.Spec.IPs {
129+
ingress = append(ingress, v1.LoadBalancerIngress{
130+
IP: ip,
131+
})
132+
}
133+
134+
svc.Status = v1.ServiceStatus{
135+
LoadBalancer: v1.LoadBalancerStatus{
136+
Ingress: ingress,
137+
},
138+
}
139+
140+
if err := r.Client.Status().Update(ctx, &svc); err != nil {
141+
return ctrl.Result{}, err
142+
}
143+
117144
return ctrl.Result{}, nil
118145
}
119146

pkg/controllers/serviceimport_test.go

+34-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ var _ = Describe("ServiceImport", func() {
138138
Ports: []v1alpha1.ServicePort{
139139
{Port: 80},
140140
},
141-
IPs: []string{"10.42.42.42"},
142141
},
143142
}
144143
Expect(k8s.Create(ctx, &serviceImport)).To(Succeed())
@@ -158,4 +157,38 @@ var _ = Describe("ServiceImport", func() {
158157
}, 10).Should(Equal(s.Spec.ClusterIP))
159158
}, 15)
160159
})
160+
Context("created with existing clustersetIP", func() {
161+
BeforeEach(func() {
162+
serviceName = types.NamespacedName{Namespace: testNS, Name: fmt.Sprintf("svc-%v", rand.Uint64())}
163+
derivedServiceName = types.NamespacedName{Namespace: testNS, Name: derivedName(serviceName)}
164+
serviceImport = v1alpha1.ServiceImport{
165+
ObjectMeta: metav1.ObjectMeta{
166+
Namespace: testNS,
167+
Name: serviceName.Name,
168+
},
169+
Spec: v1alpha1.ServiceImportSpec{
170+
Type: v1alpha1.ClusterSetIP,
171+
Ports: []v1alpha1.ServicePort{
172+
{Port: 80},
173+
},
174+
IPs: []string{"10.42.42.42"},
175+
},
176+
}
177+
Expect(k8s.Create(ctx, &serviceImport)).To(Succeed())
178+
})
179+
It("updates service loadbalancer status with service import IPs", func() {
180+
var svcImport v1alpha1.ServiceImport
181+
var s v1.Service
182+
Eventually(func() error {
183+
return k8s.Get(ctx, derivedServiceName, &s)
184+
}, 10).Should(Succeed())
185+
Eventually(func() string {
186+
Expect(k8s.Get(ctx, serviceName, &svcImport)).To(Succeed())
187+
if len(svcImport.Spec.IPs) > 0 {
188+
return svcImport.Spec.IPs[0]
189+
}
190+
return ""
191+
}, 10).Should(Equal(s.Status.LoadBalancer.Ingress[0].IP))
192+
}, 15)
193+
})
161194
})

0 commit comments

Comments
 (0)