Skip to content

Commit

Permalink
Merge pull request #20 from softonic/feature_abstract_config_improvement
Browse files Browse the repository at this point in the history
With this change we are able to get the host of the destination clust…
  • Loading branch information
santinoncs authored Dec 7, 2020
2 parents 56c2980 + e7253c5 commit 10f64c6
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 6 deletions.
14 changes: 14 additions & 0 deletions api/istio_v1beta1/virtualservice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,26 @@ import (
// NOTE: This type is used only to have a VirtualService runtime.Object (Istio's is not). we are only interested in
// Hosts property. This is not going to be used as CR, could be moved somewherelese

type Destination struct {
Host string `json:"host,omitempty"`
Subset string `json:"subset,omitempty"`
}

type HTTPRouteDestination struct {
Destination *Destination `json:"destination,omitempty"`
}

type HTTPRoute struct {
Route []*HTTPRouteDestination `json:"route,omitempty"`
}

// VirtualServiceSpec defines the desired state of VirtualService
type VirtualServiceSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
Gateways []string `json:"gateways,omitempty"`
Hosts []string `json:"hosts,omitempty"`
Http []*HTTPRoute `json:"http,omitempty"`
}

// VirtualServiceStatus defines the observed state of VirtualService
Expand Down
72 changes: 72 additions & 0 deletions api/istio_v1beta1/zz_generated.deepcopy.go

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

2 changes: 1 addition & 1 deletion api/v1alpha1/ratelimit_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type RateLimitSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
TargetRef v1.ObjectReference `json:"targetRef"`
DestinationCluster string `json:"destinationCluster"`
DestinationCluster string `json:"destinationCluster,omitempty"`
Rate []Rate `json:"rate"`
}

Expand Down
17 changes: 17 additions & 0 deletions config/crd/bases/networking.istio.io_virtualservices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ spec:
items:
type: string
type: array
http:
items:
properties:
route:
items:
properties:
destination:
properties:
host:
type: string
subset:
type: string
type: object
type: object
type: array
type: object
type: array
type: object
status:
description: VirtualServiceStatus defines the observed state of VirtualService
Expand Down
1 change: 0 additions & 1 deletion config/crd/bases/networking.softonic.io_ratelimits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ spec:
type: string
type: object
required:
- destinationCluster
- rate
- targetRef
type: object
Expand Down
59 changes: 55 additions & 4 deletions controllers/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"github.com/ghodss/yaml"
"github.com/softonic/rate-limit-operator/api/istio_v1beta1"
"errors"
networkingv1alpha1 "github.com/softonic/rate-limit-operator/api/v1alpha1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -49,7 +51,7 @@ func (r *RateLimitReconciler) CreateOrUpdateConfigMap(rateLimitInstance *network

var err error

cm := r.generateConfigMap(rateLimitInstance, controllerNamespace, baseName)
cm, err := r.generateConfigMap(rateLimitInstance, controllerNamespace, baseName)
if err != nil {
klog.Infof("Cannot generate %v, Error: %v", cm, err)
return err
Expand Down Expand Up @@ -78,10 +80,12 @@ func (r *RateLimitReconciler) CreateOrUpdateConfigMap(rateLimitInstance *network

}

func (r *RateLimitReconciler) generateConfigMap(rateLimitInstance *networkingv1alpha1.RateLimit, controllerNamespace string, name string) v1.ConfigMap {
func (r *RateLimitReconciler) generateConfigMap(rateLimitInstance *networkingv1alpha1.RateLimit, controllerNamespace string, name string) (v1.ConfigMap, error) {

configMapData := make(map[string]string)

var err error

var output []byte

descriptorOutput := networkingv1alpha1.OutputConfig{}
Expand All @@ -90,6 +94,22 @@ func (r *RateLimitReconciler) generateConfigMap(rateLimitInstance *networkingv1a

descriptorOutput.Domain = name

// get Destination Cluster

nameVirtualService := rateLimitInstance.Spec.TargetRef.Name

var value string

if rateLimitInstance.Spec.DestinationCluster != "" {
value = rateLimitInstance.Spec.DestinationCluster
} else {
value, err = r.getDestinationClusterFromVirtualService("istio-system", nameVirtualService)
if err != nil {
klog.Infof("Cannot generate configmap as we cannot find a host destination cluster")
return v1.ConfigMap{}, err
}
}

for k, dimension := range rateLimitInstance.Spec.Rate {
descriptorOutput.DescriptorsParent[k].Key = dimension.Unit
descriptor := networkingv1alpha1.Descriptors{
Expand All @@ -98,7 +118,7 @@ func (r *RateLimitReconciler) generateConfigMap(rateLimitInstance *networkingv1a
RequestsPerUnit: dimension.RequestPerUnit,
Unit: dimension.Unit,
},
Value: rateLimitInstance.Spec.DestinationCluster,
Value: value,
}
descriptorOutput.DescriptorsParent[k].Descriptors = append(descriptorOutput.DescriptorsParent[k].Descriptors, descriptor)
}
Expand All @@ -123,7 +143,38 @@ func (r *RateLimitReconciler) generateConfigMap(rateLimitInstance *networkingv1a
Data: configMapData,
}

return configMap
return configMap,nil

}

func (r *RateLimitReconciler) getDestinationClusterFromVirtualService(namespace string, nameVirtualService string) (string, error) {

virtualService, err := r.getVirtualService(namespace, nameVirtualService)
if err != nil {
klog.Infof("Virtualservice does not exists")
return "", err
}

subset := ""
destination := ""

for k,routes := range virtualService.Spec.Http {
if routes.Route[k].Destination.Host != "" {
destination = routes.Route[k].Destination.Host
subset = routes.Route[k].Destination.Subset
}
}

if destination == "" {
return "", errors.New("cannot find any suitable destinationCluster")
}

//outbound|80|prod|server.digitaltrends-v1.svc.cluster.local


destinationCluster := "outbound|80|" + subset + "|" + destination

return destinationCluster, errors.New("cannot find any suitable destinationCluster")

}

Expand Down

0 comments on commit 10f64c6

Please sign in to comment.