Skip to content

Commit

Permalink
feat(): Adding aliases to service export (#192)
Browse files Browse the repository at this point in the history
There is a requirement to identify exported services with arbitrary names
instead of slice.local names. This can be achieved by providing an alias field
in the service export spec section so that the users can give any name that
they desire to the service being exported from a cluster.

Added a new field to hold alias names to the service import and export CRDs.

Signed-off-by: Bharath Horatti <bharath@aveshasystems.com>
  • Loading branch information
bharath-avesha authored and Rahul-D78 committed Apr 14, 2023
1 parent 0d21f5a commit 0bf20b1
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 13 deletions.
7 changes: 7 additions & 0 deletions api/v1beta1/serviceexport_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ type ServiceExportSpec struct {
IngressEnabled bool `json:"ingressEnabled,omitempty"`
// Ports which should be exposed through the service
Ports []ServicePort `json:"ports"`
// Alias names for the exported service. The service could be addressed by the alias names
// in addition to the slice.local name.
Aliases []string `json:"aliases,omitempty"`
}

// ExportStatus is the status of Service Discovery reconciliation
Expand Down Expand Up @@ -109,6 +112,9 @@ type ServiceExportStatus struct {
IngressGwEnabled bool `json:"ingressGwEnabled,omitempty"`
// IngressGwPod contains ingress gateway pod info
IngressGwPod IngressGwPod `json:"ingressGwPod,omitempty"`
// Alias names for the exported service. The service could be addressed by the alias names
// in addition to the slice.local name.
Aliases []string `json:"aliases,omitempty"`
}

// +kubebuilder:object:root=true
Expand All @@ -118,6 +124,7 @@ type ServiceExportStatus struct {
// +kubebuilder:printcolumn:name="Port(s)",type=string,JSONPath=`.status.exposedPorts`
// +kubebuilder:printcolumn:name="Endpoints",type=integer,JSONPath=`.status.availableEndpoints`
// +kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.exportStatus`
// +kubebuilder:printcolumn:name="Alias",type=string,JSONPath=`.spec.aliases`
// +kubebuilder:resource:path=serviceexports,singular=serviceexport,shortName=svcex

// ServiceExport is the Schema for the serviceexports API
Expand Down
4 changes: 4 additions & 0 deletions api/v1beta1/serviceimport_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type ServiceImportSpec struct {
DNSName string `json:"dnsName"`
// Ports which should be exposed through the service
Ports []ServicePort `json:"ports"`
// Alias names for the exported service. The service could be addressed by the alias names
// in addition to the slice.local name.
Aliases []string `json:"aliases,omitempty"`
}

// ImportStatus is the status of Service Discovery reconciliation
Expand Down Expand Up @@ -83,6 +86,7 @@ type ServiceImportStatus struct {
// +kubebuilder:printcolumn:name="Port(s)",type=string,JSONPath=`.status.exposedPorts`
// +kubebuilder:printcolumn:name="Endpoints",type=integer,JSONPath=`.status.availableEndpoints`
// +kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.importStatus`
// +kubebuilder:printcolumn:name="Alias",type=string,JSONPath=`.spec.aliases`
// +kubebuilder:resource:path=serviceimports,singular=serviceimport,shortName=svcim

// ServiceImport is the Schema for the serviceimports API
Expand Down
15 changes: 15 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions config/crd/bases/networking.kubeslice.io_serviceexports.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ spec:
- jsonPath: .status.exportStatus
name: Status
type: string
- jsonPath: .spec.aliases
name: Alias
type: string
name: v1beta1
schema:
openAPIV3Schema:
Expand All @@ -54,6 +57,12 @@ spec:
spec:
description: ServiceExportSpec defines the desired state of ServiceExport
properties:
aliases:
description: Alias names for the exported service. The service could
be addressed by the alias names in addition to the slice.local name.
items:
type: string
type: array
ingressEnabled:
description: IngressEnabled denotes whether the traffic should be
proxied through an ingress gateway
Expand Down Expand Up @@ -135,6 +144,12 @@ spec:
status:
description: ServiceExportStatus defines the observed state of ServiceExport
properties:
aliases:
description: Alias names for the exported service. The service could
be addressed by the alias names in addition to the slice.local name.
items:
type: string
type: array
availableEndpoints:
description: AvailableEndpoints shows the number of available endpoints
type: integer
Expand Down
9 changes: 9 additions & 0 deletions config/crd/bases/networking.kubeslice.io_serviceimports.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ spec:
- jsonPath: .status.importStatus
name: Status
type: string
- jsonPath: .spec.aliases
name: Alias
type: string
name: v1beta1
schema:
openAPIV3Schema:
Expand All @@ -51,6 +54,12 @@ spec:
spec:
description: ServiceImportSpec defines the desired state of ServiceImport
properties:
aliases:
description: Alias names for the exported service. The service could
be addressed by the alias names in addition to the slice.local name.
items:
type: string
type: array
dnsName:
description: DNSName shows the FQDN to reach the service
type: string
Expand Down
10 changes: 10 additions & 0 deletions controllers/serviceexport/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ func (r Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resul
return res, nil
}

res, err, requeue = r.ReconcileAliases(ctx, serviceexport)
if err != nil {
return ctrl.Result{}, err
}
if requeue {
log.Info("aliases reconciled")
debugLog.Info("requeuing after aliases reconcile", "res", res, "er", err)
return res, nil
}

res, err, requeue = r.SyncSvcExportStatus(ctx, serviceexport)
if err != nil {
return ctrl.Result{}, err
Expand Down
49 changes: 49 additions & 0 deletions controllers/serviceexport/serviceexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,52 @@ func (r *Reconciler) SyncSvcExportStatus(ctx context.Context, serviceexport *kub

return ctrl.Result{Requeue: true}, nil, true
}

// ReconcileAliases reconciles serviceexport aliases
func (r *Reconciler) ReconcileAliases(
ctx context.Context, serviceexport *kubeslicev1beta1.ServiceExport) (ctrl.Result, error, bool) {
log := logger.FromContext(ctx).WithValues("type", "aliases")

if len(serviceexport.Spec.Aliases) == 0 && len(serviceexport.Status.Aliases) == 0 {
return ctrl.Result{}, nil, false
}

// Check if an update is needed to the alias info in the status
updateNeeded := false

// Status does not contain alias info. Could be the first iteration of the reconcile loop
if len(serviceexport.Status.Aliases) == 0 {
updateNeeded = true
}

for _, aliasInStatus := range serviceexport.Status.Aliases {
if !aliasRecordedInStatus(aliasInStatus, &serviceexport.Spec.Aliases) {
updateNeeded = true
break
}
}

if updateNeeded {
serviceexport.Status.Aliases = serviceexport.Spec.Aliases
serviceexport.Status.LastSync = 0
err := r.Status().Update(ctx, serviceexport)
if err != nil {
log.Error(err, "Failed to update serviceexport ports")
return ctrl.Result{}, err, true
}
log.Info("serviceexport status updated with aliases", ":", serviceexport.Status.Aliases)
return ctrl.Result{Requeue: true}, nil, true
}

return ctrl.Result{}, nil, false
}

func aliasRecordedInStatus(aliasInStatus string, aliasListInSpec *[]string) bool {
for _, alias := range *aliasListInSpec {
if alias == aliasInStatus {
return true
}
}

return false
}
19 changes: 14 additions & 5 deletions controllers/slice/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,9 @@ func (r *SliceReconciler) deleteAnnotationsAndLabels(ctx context.Context, slice
if ok && v == fmt.Sprintf("kernel://vl3-service-%s/nsm0", slice.Name) {
delete(podannotations, "networkservicemesh.io")
}
}
statusannotations := pod.ObjectMeta.GetAnnotations()
if statusannotations != nil {
_, ok := statusannotations["kubeslice.io/status"]
_, ok = podannotations["kubeslice.io/status"]
if ok {
delete(statusannotations, "kubeslice.io/status")
delete(podannotations, "kubeslice.io/status")
}
}
if err := r.Update(ctx, &pod); err != nil {
Expand Down Expand Up @@ -482,6 +479,10 @@ func (r *SliceReconciler) deleteAnnotationsAndLabels(ctx context.Context, slice
if ok && v == fmt.Sprintf("kernel://vl3-service-%s/nsm0", slice.Name) {
delete(podannotations, "networkservicemesh.io")
}
_, ok = podannotations["kubeslice.io/status"]
if ok {
delete(podannotations, "kubeslice.io/status")
}
}
deployannotations := deploy.ObjectMeta.GetAnnotations()
if deployannotations != nil {
Expand Down Expand Up @@ -529,6 +530,10 @@ func (r *SliceReconciler) deleteAnnotationsAndLabels(ctx context.Context, slice
if ok && v == fmt.Sprintf("kernel://vl3-service-%s/nsm0", slice.Name) {
delete(podannotations, "networkservicemesh.io")
}
_, ok = podannotations["kubeslice.io/status"]
if ok {
delete(podannotations, "kubeslice.io/status")
}
}
deployannotations := statefulset.ObjectMeta.GetAnnotations()
if deployannotations != nil {
Expand Down Expand Up @@ -576,6 +581,10 @@ func (r *SliceReconciler) deleteAnnotationsAndLabels(ctx context.Context, slice
if ok && v == fmt.Sprintf("kernel://vl3-service-%s/nsm0", slice.Name) {
delete(podannotations, "networkservicemesh.io")
}
_, ok = podannotations["kubeslice.io/status"]
if ok {
delete(podannotations, "kubeslice.io/status")
}
}
deployannotations := daemonset.ObjectMeta.GetAnnotations()
if deployannotations != nil {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ require (
github.com/go-logr/logr v1.2.3
github.com/go-logr/zapr v1.2.3
github.com/golang/protobuf v1.5.2
github.com/google/go-cmp v0.5.9
github.com/kubeslice/apis v0.1.3
github.com/google/go-cmp v0.5.8
github.com/kubeslice/apis v0.1.4
github.com/kubeslice/gateway-sidecar v0.2.0
github.com/kubeslice/kubeslice-monitoring v0.1.6
github.com/kubeslice/netops v0.1.3
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,10 @@ github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kubeslice/apis v0.1.3 h1:A7KIKRSKxmJe8EkfsCoNuJhGRb8t8okdzAtjpCjFcOw=
github.com/kubeslice/apis v0.1.3/go.mod h1:YDSfpIsQM+FtQPaZVGNCTZnlp3viWuQhkjJjIHQdaYs=
github.com/kubeslice/apis v0.1.1 h1:141adRMtxZFlx6m8BF8bWhusuh/mjtEsO/HctQuyX48=
github.com/kubeslice/apis v0.1.1/go.mod h1:YDSfpIsQM+FtQPaZVGNCTZnlp3viWuQhkjJjIHQdaYs=
github.com/kubeslice/apis v0.1.4 h1:v8DZ9skXybEnyYsEZD9VlaZsFG4jmqNKpJSIir+IVCc=
github.com/kubeslice/apis v0.1.4/go.mod h1:YDSfpIsQM+FtQPaZVGNCTZnlp3viWuQhkjJjIHQdaYs=
github.com/kubeslice/gateway-sidecar v0.2.0 h1:Ja3fIUivuSjUFQ4lPCt79ATq99BxslvAFYUwV9Urpy4=
github.com/kubeslice/gateway-sidecar v0.2.0/go.mod h1:nM1+Wjud2vk44cUg+9iwBbWTpqI+2Ecbn9NuaHEs9aY=
github.com/kubeslice/kubeslice-monitoring v0.1.6 h1:/Vvv9CuXAQoZLR2+/yO+zuk6tk4nQwcaxA++6FYsQTY=
Expand Down
2 changes: 2 additions & 0 deletions pkg/hub/controllers/serviceimport_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func getMeshServiceImportObj(svcim *spokev1alpha1.WorkerServiceImport) *kubeslic
Slice: svcim.Spec.SliceName,
DNSName: svcim.Spec.ServiceName + "." + svcim.Spec.ServiceNamespace + ".svc.slice.local",
Ports: getMeshServiceImportPortList(svcim),
Aliases: svcim.Spec.Aliases,
},
}
}
Expand Down Expand Up @@ -146,6 +147,7 @@ func (r *ServiceImportReconciler) Reconcile(ctx context.Context, req reconcile.R
}

meshSvcIm.Spec.Ports = getMeshServiceImportPortList(svcim)
meshSvcIm.Spec.Aliases = svcim.Spec.Aliases
err = r.MeshClient.Update(ctx, meshSvcIm)
if err != nil {
log.Error(err, "unable to update service import in spoke cluster", "serviceimport", svcim.Name)
Expand Down
3 changes: 3 additions & 0 deletions pkg/hub/hubclient/hubclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ func getHubServiceExportObj(serviceexport *kubeslicev1beta1.ServiceExport) *hubv
SliceName: serviceexport.Spec.Slice,
ServiceDiscoveryEndpoints: getHubServiceDiscoveryEps(serviceexport),
ServiceDiscoveryPorts: getHubServiceDiscoveryPorts(serviceexport),
Aliases: serviceexport.Spec.Aliases,
},
}
}
Expand Down Expand Up @@ -321,6 +322,7 @@ func (hubClient *HubClientConfig) UpdateServiceExportEndpointForIngressGw(ctx co
SliceName: serviceexport.Spec.Slice,
ServiceDiscoveryEndpoints: []hubv1alpha1.ServiceDiscoveryEndpoint{getHubServiceDiscoveryEpForIngressGw(ep)},
ServiceDiscoveryPorts: getHubServiceDiscoveryPorts(serviceexport),
Aliases: serviceexport.Spec.Aliases,
},
}
err = hubClient.Create(ctx, hubSvcExObj)
Expand All @@ -334,6 +336,7 @@ func (hubClient *HubClientConfig) UpdateServiceExportEndpointForIngressGw(ctx co

hubSvcEx.Spec.ServiceDiscoveryEndpoints = []hubv1alpha1.ServiceDiscoveryEndpoint{getHubServiceDiscoveryEpForIngressGw(ep)}
hubSvcEx.Spec.ServiceDiscoveryPorts = getHubServiceDiscoveryPorts(serviceexport)
hubSvcEx.Spec.Aliases = serviceexport.Spec.Aliases

err = hubClient.Update(ctx, hubSvcEx)
if err != nil {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ github.com/imdario/mergo
# github.com/json-iterator/go v1.1.12
## explicit; go 1.12
github.com/json-iterator/go
# github.com/kubeslice/apis v0.1.3
## explicit; go 1.16
# github.com/kubeslice/apis v0.1.4
## explicit
github.com/kubeslice/apis/pkg/controller/v1alpha1
github.com/kubeslice/apis/pkg/worker/v1alpha1
# github.com/kubeslice/gateway-sidecar v0.2.0
Expand Down

0 comments on commit 0bf20b1

Please sign in to comment.