Skip to content

Commit

Permalink
Add support to merge config from flavor
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Jan 22, 2024
1 parent 9b286b0 commit 81f55c1
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 1 deletion.
5 changes: 5 additions & 0 deletions api/v1alpha1/rpaasflavor_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type RpaasFlavorSpec struct {
// +optional
InstanceTemplate *RpaasInstanceSpec `json:"instanceTemplate,omitempty"`

// Config defines some NGINX configurations values that can be used in the
// configuration template.
// +optional
Config *NginxConfig `json:"config,omitempty"`

// Default defines if the flavor should be applied by default on
// every service instance. Default flavors cannot be listed on RpaasFlavorList.
// +optional
Expand Down
40 changes: 39 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

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

77 changes: 77 additions & 0 deletions config/crd/bases/extensions.tsuru.io_rpaasflavors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,83 @@ spec:
spec:
description: RpaasFlavorSpec defines the desired state of RpaasFlavor
properties:
config:
description: Config defines some NGINX configurations values that
can be used in the configuration template.
properties:
cacheEnabled:
type: boolean
cacheInactive:
type: string
cacheLoaderFiles:
type: integer
cachePath:
type: string
cacheSize:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
cacheZoneSize:
anyOf:
- type: integer
- type: string
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
httpListenOptions:
type: string
httpsListenOptions:
type: string
logAdditionalFields:
additionalProperties:
type: string
type: object
logAdditionalHeaders:
items:
type: string
type: array
logFormat:
type: string
logFormatEscape:
type: string
logFormatName:
type: string
mapHashBucketSize:
type: integer
mapHashMaxSize:
type: integer
resolverAddresses:
items:
type: string
type: array
resolverTTL:
type: string
syslogEnabled:
type: boolean
syslogFacility:
type: string
syslogServerAddress:
type: string
syslogTag:
type: string
templateExtraVars:
additionalProperties:
type: string
type: object
upstreamKeepalive:
type: integer
user:
type: string
vtsEnabled:
type: boolean
vtsStatusHistogramBuckets:
type: string
workerConnections:
type: integer
workerProcesses:
type: integer
type: object
creationOnly:
description: CreationOnly defines if the flavor could be used only
in the moment of creation of instance
Expand Down
61 changes: 61 additions & 0 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,52 @@ func (r *RpaasInstanceReconciler) mergeInstanceWithFlavors(ctx context.Context,
return instance, nil
}

func (r *RpaasInstanceReconciler) mergePlanWithFlavors(ctx context.Context, instance *v1alpha1.RpaasInstance, plan *v1alpha1.RpaasPlan) (*v1alpha1.RpaasPlan, error) {
defaultFlavors, err := r.listDefaultFlavors(ctx, instance)
if err != nil {
return nil, err
}

for _, defaultFlavor := range defaultFlavors {
if defaultFlavor.Spec.Config == nil {
continue
}
if err := mergePlanWithFlavor(plan, defaultFlavor); err != nil {
return nil, err
}
}

for _, flavorName := range instance.Spec.Flavors {
flavorObjectKey := types.NamespacedName{
Name: flavorName,
Namespace: instance.Namespace,
}

if instance.Spec.PlanNamespace != "" {
flavorObjectKey.Namespace = instance.Spec.PlanNamespace
}

var flavor v1alpha1.RpaasFlavor
if err := r.Client.Get(ctx, flavorObjectKey, &flavor); err != nil {
return nil, err
}

if flavor.Spec.Default {
continue
}

if flavor.Spec.Config == nil {
continue
}

if err := mergePlanWithFlavor(plan, flavor); err != nil {
return nil, err
}
}

return plan, nil
}

func mergeInstanceWithFlavor(instance *v1alpha1.RpaasInstance, flavor v1alpha1.RpaasFlavor) error {
if flavor.Spec.InstanceTemplate == nil {
return nil
Expand All @@ -220,6 +266,21 @@ func mergeInstanceWithFlavor(instance *v1alpha1.RpaasInstance, flavor v1alpha1.R
return nil
}

func mergePlanWithFlavor(plan *v1alpha1.RpaasPlan, flavor v1alpha1.RpaasFlavor) error {
if flavor.Spec.Config == nil {
return nil
}

mergedPlanSpec, err := mergePlans(plan.Spec, v1alpha1.RpaasPlanSpec{
Config: *flavor.Spec.Config,
})
if err != nil {
return err
}
plan.Spec = mergedPlanSpec
return nil
}

func (r *RpaasInstanceReconciler) listDefaultFlavors(ctx context.Context, instance *v1alpha1.RpaasInstance) ([]v1alpha1.RpaasFlavor, error) {
flavorList := &v1alpha1.RpaasFlavorList{}
flavorNamespace := instance.Namespace
Expand Down
5 changes: 5 additions & 0 deletions controllers/rpaasinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
}

plan, err = r.mergePlanWithFlavors(ctx, instance.DeepCopy(), plan)
if err != nil {
return reconcile.Result{}, err
}

instanceMergedWithFlavors, err := r.mergeWithFlavors(ctx, instance.DeepCopy())
if err != nil {
return reconcile.Result{}, nil
Expand Down

0 comments on commit 81f55c1

Please sign in to comment.