-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
592 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
pkg/liqo-controller-manager/ip-controller/exposition.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2019-2023 The Liqo Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ipctrl | ||
|
||
import ( | ||
"context" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
discoveryv1 "k8s.io/api/discovery/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/klog/v2" | ||
"k8s.io/utils/pointer" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
|
||
ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1" | ||
"github.com/liqotech/liqo/pkg/consts" | ||
) | ||
|
||
// handleAssociatedService creates, updates or deletes the service associated to the IP. | ||
func (r *IPReconciler) handleAssociatedService(ctx context.Context, ip *ipamv1alpha1.IP) error { | ||
// Service associated to the IP | ||
svc := v1.Service{ObjectMeta: metav1.ObjectMeta{ | ||
Name: ip.Name, | ||
Namespace: ip.Namespace, | ||
}} | ||
svcMutateFn := func() error { | ||
svc.SetLabels(labels.Merge(svc.GetLabels(), ip.Spec.ServiceTemplate.Metadata.GetLabels())) | ||
svc.SetAnnotations(labels.Merge(svc.GetAnnotations(), ip.Spec.ServiceTemplate.Metadata.GetAnnotations())) | ||
svc.Spec = ip.Spec.ServiceTemplate.Spec | ||
return controllerutil.SetControllerReference(ip, &svc, r.Scheme) | ||
} | ||
|
||
// EndpointSlice associated to the Service | ||
eps := discoveryv1.EndpointSlice{ObjectMeta: metav1.ObjectMeta{ | ||
Name: ip.Name, | ||
Namespace: ip.Namespace, | ||
}} | ||
epsMutateFn := func() error { | ||
eps.SetLabels(labels.Merge(eps.GetLabels(), labels.Set{discoveryv1.LabelServiceName: svc.Name})) | ||
eps.SetAnnotations(labels.Merge(eps.GetAnnotations(), labels.Set{consts.VKSkipUnmapIPAnnotationKey: "true"})) | ||
eps.AddressType = discoveryv1.AddressTypeIPv4 | ||
eps.Endpoints = []discoveryv1.Endpoint{ | ||
{ | ||
Addresses: []string{ip.Spec.IP}, | ||
Conditions: discoveryv1.EndpointConditions{ | ||
Ready: pointer.Bool(true), | ||
}, | ||
}, | ||
} | ||
var ports []discoveryv1.EndpointPort | ||
for i := range ip.Spec.ServiceTemplate.Spec.Ports { | ||
ports = append(ports, discoveryv1.EndpointPort{ | ||
Name: &ip.Spec.ServiceTemplate.Spec.Ports[i].Name, | ||
Protocol: &ip.Spec.ServiceTemplate.Spec.Ports[i].Protocol, | ||
Port: &ip.Spec.ServiceTemplate.Spec.Ports[i].Port, | ||
AppProtocol: ip.Spec.ServiceTemplate.Spec.Ports[i].AppProtocol, | ||
}) | ||
} | ||
eps.Ports = ports | ||
|
||
return controllerutil.SetControllerReference(ip, &eps, r.Scheme) | ||
} | ||
|
||
// Create service and endpointslice if the template is defined | ||
if ip.Spec.ServiceTemplate != nil { | ||
if err := enforceResource(ctx, r.Client, &svc, svcMutateFn, "service"); err != nil { | ||
return err | ||
} | ||
if err := enforceResource(ctx, r.Client, &eps, epsMutateFn, "endpointslice"); err != nil { | ||
return err | ||
} | ||
} else { | ||
// Service spec is not defined, delete the associated service and endpointslices if previously created | ||
if err := ensureResourceAbsence(ctx, r.Client, &svc, "service"); err != nil { | ||
return err | ||
} | ||
if err := ensureResourceAbsence(ctx, r.Client, &eps, "endpointslice"); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// enforceResource ensures that the given resource exists. | ||
// It either creates or update the resource. | ||
func enforceResource(ctx context.Context, r client.Client, obj client.Object, mutateFn controllerutil.MutateFn, resourceKind string) error { | ||
op, err := controllerutil.CreateOrUpdate(ctx, r, obj, mutateFn) | ||
if err != nil { | ||
klog.Errorf("error while creating/updating %s %q (operation: %s): %v", resourceKind, obj.GetName(), op, err) | ||
return err | ||
} | ||
klog.Infof("%s %q correctly enforced (operation: %s)", resourceKind, obj.GetName(), op) | ||
return nil | ||
} | ||
|
||
// ensureResourceAbsence ensures that the given resource does not exist. | ||
// If the resource does not exist, it does nothing. | ||
func ensureResourceAbsence(ctx context.Context, r client.Client, obj client.Object, resourceKind string) error { | ||
err := r.Get(ctx, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()}, obj) | ||
switch { | ||
case err != nil && !apierrors.IsNotFound(err): | ||
klog.Errorf("error while getting %s %q: %v", resourceKind, obj.GetName(), err) | ||
return err | ||
case apierrors.IsNotFound(err): | ||
// The resource does not exist, do nothing. | ||
klog.V(6).Infof("%s %q does not exist. Nothing to do", resourceKind, obj.GetName()) | ||
default: | ||
if err := r.Delete(ctx, obj); err != nil { | ||
klog.Errorf("error while deleting %s %q: %v", resourceKind, obj.GetName(), err) | ||
return err | ||
} | ||
klog.Infof("%s %q correctly deleted", resourceKind, obj.GetName()) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters