diff --git a/PROJECT b/PROJECT index 6ceb9cbb9..0ccb6e4b5 100644 --- a/PROJECT +++ b/PROJECT @@ -29,11 +29,4 @@ resources: kind: RpaasFlavor path: github.com/tsuru/rpaas-operator/api/v1alpha1 version: v1alpha1 -- api: - crdVersion: v1 - domain: tsuru.io - group: extensions - kind: RpaasPortAllocation - path: github.com/tsuru/rpaas-operator/api/v1alpha1 - version: v1alpha1 version: "3" \ No newline at end of file diff --git a/api/v1alpha1/rpaasportallocation_types.go b/api/v1alpha1/rpaasportallocation_types.go deleted file mode 100644 index d9dd52dcb..000000000 --- a/api/v1alpha1/rpaasportallocation_types.go +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright 2020 tsuru authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package v1alpha1 - -import ( - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" -) - -// RpaasPortAllocationSpec defines the state of port allocations -type RpaasPortAllocationSpec struct { - Ports []AllocatedPort `json:"ports,omitempty"` -} - -type AllocatedPort struct { - Port int32 `json:"port,omitempty"` - Owner NamespacedOwner `json:"owner,omitempty"` -} - -type NamespacedOwner struct { - Namespace string `json:"namespace,omitempty"` - RpaasName string `json:"rpaasName,omitempty"` - UID types.UID `json:"uid,omitempty"` -} - -// +kubebuilder:object:root=true -// +kubebuilder:resource:scope=Cluster - -// RpaasPortAllocation is the Schema for the Rpaasportallocations API -type RpaasPortAllocation struct { - metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` - - Spec RpaasPortAllocationSpec `json:"spec,omitempty"` -} - -// +kubebuilder:object:root=true - -// NginxList contains a list of Nginx -type RpaasPortAllocationList struct { - metav1.TypeMeta `json:",inline"` - metav1.ListMeta `json:"metadata,omitempty"` - Items []RpaasPortAllocation `json:"items"` -} - -func init() { - SchemeBuilder.Register(&RpaasPortAllocation{}, &RpaasPortAllocationList{}) -} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 224f156b3..ce50785ed 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -15,21 +15,7 @@ import ( runtime "k8s.io/apimachinery/pkg/runtime" ) -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *AllocatedPort) DeepCopyInto(out *AllocatedPort) { - *out = *in - out.Owner = in.Owner -} -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AllocatedPort. -func (in *AllocatedPort) DeepCopy() *AllocatedPort { - if in == nil { - return nil - } - out := new(AllocatedPort) - in.DeepCopyInto(out) - return out -} // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *AllowedUpstream) DeepCopyInto(out *AllowedUpstream) { @@ -230,20 +216,7 @@ func (in *Location) DeepCopy() *Location { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *NamespacedOwner) DeepCopyInto(out *NamespacedOwner) { - *out = *in -} -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NamespacedOwner. -func (in *NamespacedOwner) DeepCopy() *NamespacedOwner { - if in == nil { - return nil - } - out := new(NamespacedOwner) - in.DeepCopyInto(out) - return out -} // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *NginxConfig) DeepCopyInto(out *NginxConfig) { @@ -685,83 +658,6 @@ func (in *RpaasPlanSpec) DeepCopy() *RpaasPlanSpec { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *RpaasPortAllocation) DeepCopyInto(out *RpaasPortAllocation) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) - in.Spec.DeepCopyInto(&out.Spec) -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RpaasPortAllocation. -func (in *RpaasPortAllocation) DeepCopy() *RpaasPortAllocation { - if in == nil { - return nil - } - out := new(RpaasPortAllocation) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *RpaasPortAllocation) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *RpaasPortAllocationList) DeepCopyInto(out *RpaasPortAllocationList) { - *out = *in - out.TypeMeta = in.TypeMeta - in.ListMeta.DeepCopyInto(&out.ListMeta) - if in.Items != nil { - in, out := &in.Items, &out.Items - *out = make([]RpaasPortAllocation, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RpaasPortAllocationList. -func (in *RpaasPortAllocationList) DeepCopy() *RpaasPortAllocationList { - if in == nil { - return nil - } - out := new(RpaasPortAllocationList) - in.DeepCopyInto(out) - return out -} - -// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. -func (in *RpaasPortAllocationList) DeepCopyObject() runtime.Object { - if c := in.DeepCopy(); c != nil { - return c - } - return nil -} - -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *RpaasPortAllocationSpec) DeepCopyInto(out *RpaasPortAllocationSpec) { - *out = *in - if in.Ports != nil { - in, out := &in.Ports, &out.Ports - *out = make([]AllocatedPort, len(*in)) - copy(*out, *in) - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RpaasPortAllocationSpec. -func (in *RpaasPortAllocationSpec) DeepCopy() *RpaasPortAllocationSpec { - if in == nil { - return nil - } - out := new(RpaasPortAllocationSpec) - in.DeepCopyInto(out) - return out -} // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *TLSSessionResumption) DeepCopyInto(out *TLSSessionResumption) { diff --git a/config/crd/bases/extensions.tsuru.io_rpaasportallocations.yaml b/config/crd/bases/extensions.tsuru.io_rpaasportallocations.yaml deleted file mode 100644 index 0dd6ca1db..000000000 --- a/config/crd/bases/extensions.tsuru.io_rpaasportallocations.yaml +++ /dev/null @@ -1,70 +0,0 @@ - ---- -apiVersion: apiextensions.k8s.io/v1 -kind: CustomResourceDefinition -metadata: - annotations: - controller-gen.kubebuilder.io/version: v0.7.0 - creationTimestamp: null - name: rpaasportallocations.extensions.tsuru.io -spec: - group: extensions.tsuru.io - names: - kind: RpaasPortAllocation - listKind: RpaasPortAllocationList - plural: rpaasportallocations - singular: rpaasportallocation - scope: Cluster - versions: - - name: v1alpha1 - schema: - openAPIV3Schema: - description: RpaasPortAllocation is the Schema for the Rpaasportallocations - API - properties: - apiVersion: - description: 'APIVersion defines the versioned schema of this representation - of an object. Servers should convert recognized schemas to the latest - internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' - type: string - kind: - description: 'Kind is a string value representing the REST resource this - object represents. Servers may infer this from the endpoint the client - submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' - type: string - metadata: - type: object - spec: - description: RpaasPortAllocationSpec defines the state of port allocations - properties: - ports: - items: - properties: - owner: - properties: - namespace: - type: string - rpaasName: - type: string - uid: - description: UID is a type that holds unique ID values, - including UUIDs. Because we don't ONLY use UUIDs, this - is an alias to string. Being a type captures intent and - helps make sure that UIDs and names do not get conflated. - type: string - type: object - port: - format: int32 - type: integer - type: object - type: array - type: object - type: object - served: true - storage: true -status: - acceptedNames: - kind: "" - plural: "" - conditions: [] - storedVersions: [] diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index 0dde3bdb4..9dbb28d3c 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -5,5 +5,4 @@ resources: - bases/extensions.tsuru.io_rpaasinstances.yaml - bases/extensions.tsuru.io_rpaasplans.yaml - bases/extensions.tsuru.io_rpaasflavors.yaml -- bases/extensions.tsuru.io_rpaasportallocations.yaml # +kubebuilder:scaffold:crdkustomizeresource diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 90dce7194..d9c97c112 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -95,16 +95,6 @@ rules: - get - list - watch -- apiGroups: - - extensions.tsuru.io - resources: - - rpaasportallocations - verbs: - - create - - get - - list - - update - - watch - apiGroups: - nginx.tsuru.io resources: diff --git a/controllers/controller.go b/controllers/controller.go index ba8b9e775..c17b0a2ce 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -20,7 +20,6 @@ import ( "github.com/sirupsen/logrus" nginxv1alpha1 "github.com/tsuru/nginx-operator/api/v1alpha1" nginxk8s "github.com/tsuru/nginx-operator/pkg/k8s" - "github.com/willf/bitset" appsv1 "k8s.io/api/apps/v1" autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2" batchv1 "k8s.io/api/batch/v1" @@ -28,7 +27,6 @@ import ( corev1 "k8s.io/api/core/v1" policyv1beta1 "k8s.io/api/policy/v1beta1" "k8s.io/apimachinery/pkg/api/equality" - "k8s.io/apimachinery/pkg/api/errors" k8sErrors "k8s.io/apimachinery/pkg/api/errors" k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" @@ -1499,116 +1497,6 @@ func (_ rpaasMergoTransformers) Transformer(t reflect.Type) func(reflect.Value, return nil } -func portBelongsTo(port extensionsv1alpha1.AllocatedPort, instance *extensionsv1alpha1.RpaasInstance) bool { - if instance == nil { - return false - } - return instance.UID == port.Owner.UID && port.Owner.Namespace == instance.Namespace && port.Owner.RpaasName == instance.Name -} - -func (r *RpaasInstanceReconciler) reconcileDedicatedPorts(ctx context.Context, instance *extensionsv1alpha1.RpaasInstance, portCount int) ([]int, error) { - allocation := extensionsv1alpha1.RpaasPortAllocation{ - ObjectMeta: metav1.ObjectMeta{ - Name: defaultPortAllocationResource, - }, - } - - err := r.Client.Get(ctx, types.NamespacedName{Name: allocation.Name}, &allocation) - if err != nil { - if !errors.IsNotFound(err) { - return nil, err - } - - err = r.Client.Create(ctx, &allocation) - if err != nil { - return nil, err - } - } - - var newPorts []extensionsv1alpha1.AllocatedPort - var usedSet bitset.BitSet - var instancePorts []int - highestPortUsed := r.PortRangeMin - 1 - - // Loop through all allocated ports and remove ports from removed Nginx - // resources or from resources that have AllocateContainerPorts==false (or nil). - for _, port := range allocation.Spec.Ports { - if port.Port > highestPortUsed { - highestPortUsed = port.Port - } - var rpaas extensionsv1alpha1.RpaasInstance - err = r.Client.Get(ctx, types.NamespacedName{ - Namespace: port.Owner.Namespace, - Name: port.Owner.RpaasName, - }, &rpaas) - if err != nil { - if errors.IsNotFound(err) { - continue - } - return nil, err - } - if portBelongsTo(port, instance) { - if !v1alpha1.BoolValue(instance.Spec.AllocateContainerPorts) { - continue - } - instancePorts = append(instancePorts, int(port.Port)) - } - if portBelongsTo(port, &rpaas) { - usedSet.Set(uint(port.Port)) - newPorts = append(newPorts, port) - } - } - - // If we should allocate ports and none are allocated yet we have to look - // for available ports and allocate them. - if instance != nil && v1alpha1.BoolValue(instance.Spec.AllocateContainerPorts) { - - if r.PortRangeMin >= r.PortRangeMax { - return nil, fmt.Errorf("unable to allocate container ports, range is invalid: min: %d, max: %d", r.PortRangeMin, r.PortRangeMax) - } - - for port := highestPortUsed + 1; port != highestPortUsed; port++ { - if len(instancePorts) >= portCount { - break - } - - if port > r.PortRangeMax { - port = r.PortRangeMin - 1 - continue - } - - if usedSet.Test(uint(port)) { - continue - } - - usedSet.Set(uint(port)) - newPorts = append(newPorts, extensionsv1alpha1.AllocatedPort{ - Port: int32(port), - Owner: extensionsv1alpha1.NamespacedOwner{ - Namespace: instance.Namespace, - RpaasName: instance.Name, - UID: instance.UID, - }, - }) - instancePorts = append(instancePorts, int(port)) - } - - if len(instancePorts) < portCount { - return nil, fmt.Errorf("unable to allocate container ports, wanted %d, allocated %d", portCount, len(instancePorts)) - } - } - - if !reflect.DeepEqual(allocation.Spec.Ports, newPorts) { - allocation.Spec.Ports = newPorts - err = r.Client.Update(ctx, &allocation) - if err != nil { - return nil, err - } - } - - return instancePorts, nil -} - func labelsForRpaasInstance(instance *extensionsv1alpha1.RpaasInstance) map[string]string { return map[string]string{ "rpaas.extensions.tsuru.io/instance-name": instance.Name, diff --git a/controllers/controller_test.go b/controllers/controller_test.go index 2df18251f..43f6c375f 100644 --- a/controllers/controller_test.go +++ b/controllers/controller_test.go @@ -29,7 +29,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/reconcile" "github.com/tsuru/rpaas-operator/api/v1alpha1" - "github.com/tsuru/rpaas-operator/internal/config" extensionsruntime "github.com/tsuru/rpaas-operator/pkg/runtime" ) @@ -1759,553 +1758,6 @@ func newRpaasFlavor() *v1alpha1.RpaasFlavor { } } -func TestReconcileNginx_reconcileDedicatedPorts(t *testing.T) { - tests := []struct { - name string - rpaas *v1alpha1.RpaasInstance - objects []runtime.Object - portMin int32 - portMax int32 - assertion func(t *testing.T, err error, ports []int, portAlloc v1alpha1.RpaasPortAllocationSpec) - }{ - { - name: "creates empty port allocation", - portMin: 20000, - portMax: 30000, - rpaas: &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-rpaas", - Namespace: "default", - }, - Spec: v1alpha1.RpaasInstanceSpec{}, - Status: v1alpha1.RpaasInstanceStatus{}, - }, - assertion: func(t *testing.T, err error, ports []int, portAlloc v1alpha1.RpaasPortAllocationSpec) { - assert.NoError(t, err) - assert.Nil(t, ports) - assert.Equal(t, v1alpha1.RpaasPortAllocationSpec{}, portAlloc) - }, - }, - { - name: "allocate ports if requested", - portMin: 20000, - portMax: 30000, - rpaas: &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-rpaas", - Namespace: "default", - UID: "1337", - }, - Spec: v1alpha1.RpaasInstanceSpec{ - AllocateContainerPorts: v1alpha1.Bool(true), - }, - Status: v1alpha1.RpaasInstanceStatus{}, - }, - assertion: func(t *testing.T, err error, ports []int, portAlloc v1alpha1.RpaasPortAllocationSpec) { - assert.NoError(t, err) - assert.Equal(t, []int{20000, 20001}, ports) - assert.Equal(t, v1alpha1.RpaasPortAllocationSpec{ - Ports: []v1alpha1.AllocatedPort{ - { - Port: 20000, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "my-rpaas", - UID: "1337", - }, - }, - { - Port: 20001, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "my-rpaas", - UID: "1337", - }, - }, - }, - }, portAlloc) - }, - }, - { - name: "skip already allocated when allocating new ports", - portMin: 20000, - portMax: 30000, - rpaas: &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-rpaas", - Namespace: "default", - UID: "1337", - }, - Spec: v1alpha1.RpaasInstanceSpec{ - AllocateContainerPorts: v1alpha1.Bool(true), - }, - Status: v1alpha1.RpaasInstanceStatus{}, - }, - objects: []runtime.Object{ - &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "other-nginx", - Namespace: "default", - UID: "1337-af", - }, - }, - &v1alpha1.RpaasPortAllocation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "default", - }, - Spec: v1alpha1.RpaasPortAllocationSpec{ - Ports: []v1alpha1.AllocatedPort{ - { - Port: 20000, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-nginx", - UID: "1337-af", - }, - }, - { - Port: 20002, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-nginx", - UID: "1337-af", - }, - }, - }, - }, - }, - }, - assertion: func(t *testing.T, err error, ports []int, portAlloc v1alpha1.RpaasPortAllocationSpec) { - assert.NoError(t, err) - assert.Equal(t, []int{20003, 20004}, ports) - assert.Equal(t, v1alpha1.RpaasPortAllocationSpec{ - Ports: []v1alpha1.AllocatedPort{ - { - Port: 20000, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-nginx", - UID: "1337-af", - }, - }, - { - Port: 20002, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-nginx", - UID: "1337-af", - }, - }, - { - Port: 20003, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "my-rpaas", - UID: "1337", - }, - }, - { - Port: 20004, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "my-rpaas", - UID: "1337", - }, - }, - }, - }, portAlloc) - }, - }, - { - name: "reuse previous allocations", - portMin: 20000, - portMax: 30000, - rpaas: &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-rpaas", - Namespace: "default", - UID: "1337", - }, - Spec: v1alpha1.RpaasInstanceSpec{ - AllocateContainerPorts: v1alpha1.Bool(true), - }, - Status: v1alpha1.RpaasInstanceStatus{}, - }, - objects: []runtime.Object{ - &v1alpha1.RpaasPortAllocation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "default", - }, - Spec: v1alpha1.RpaasPortAllocationSpec{ - Ports: []v1alpha1.AllocatedPort{ - { - Port: 20000, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "my-rpaas", - UID: "1337", - }, - }, - { - Port: 20002, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "my-rpaas", - UID: "1337", - }, - }, - }, - }, - }, - }, - assertion: func(t *testing.T, err error, ports []int, portAlloc v1alpha1.RpaasPortAllocationSpec) { - assert.NoError(t, err) - assert.Equal(t, []int{20000, 20002}, ports) - assert.Equal(t, v1alpha1.RpaasPortAllocationSpec{ - Ports: []v1alpha1.AllocatedPort{ - { - Port: 20000, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "my-rpaas", - UID: "1337", - }, - }, - { - Port: 20002, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "my-rpaas", - UID: "1337", - }, - }, - }, - }, portAlloc) - }, - }, - { - name: "remove allocations for removed objects", - portMin: 20000, - portMax: 30000, - rpaas: &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-rpaas", - Namespace: "default", - UID: "1337", - }, - Spec: v1alpha1.RpaasInstanceSpec{}, - Status: v1alpha1.RpaasInstanceStatus{}, - }, - objects: []runtime.Object{ - &v1alpha1.RpaasPortAllocation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "default", - }, - Spec: v1alpha1.RpaasPortAllocationSpec{ - Ports: []v1alpha1.AllocatedPort{ - { - Port: 20000, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-nginx", - UID: "1337-af", - }, - }, - { - Port: 20002, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-nginx", - UID: "1337-af", - }, - }, - }, - }, - }, - }, - assertion: func(t *testing.T, err error, ports []int, portAlloc v1alpha1.RpaasPortAllocationSpec) { - assert.NoError(t, err) - assert.Nil(t, ports) - assert.Equal(t, v1alpha1.RpaasPortAllocationSpec{}, portAlloc) - }, - }, - { - name: "remove allocations for objects not matching UID", - portMin: 20000, - portMax: 30000, - rpaas: &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-rpaas", - Namespace: "default", - UID: "1337", - }, - Spec: v1alpha1.RpaasInstanceSpec{}, - Status: v1alpha1.RpaasInstanceStatus{}, - }, - objects: []runtime.Object{ - &v1alpha1.RpaasPortAllocation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "default", - }, - Spec: v1alpha1.RpaasPortAllocationSpec{ - Ports: []v1alpha1.AllocatedPort{ - { - Port: 20000, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "my-rpaas", - UID: "1337-af", - }, - }, - { - Port: 20002, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "my-rpaas", - UID: "1337-af", - }, - }, - }, - }, - }, - }, - assertion: func(t *testing.T, err error, ports []int, portAlloc v1alpha1.RpaasPortAllocationSpec) { - assert.NoError(t, err) - assert.Nil(t, ports) - assert.Equal(t, v1alpha1.RpaasPortAllocationSpec{}, portAlloc) - }, - }, - { - name: "loops around looking for ports", - portMin: 10, - portMax: 13, - rpaas: &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-rpaas", - Namespace: "default", - UID: "1234", - }, - Spec: v1alpha1.RpaasInstanceSpec{ - AllocateContainerPorts: v1alpha1.Bool(true), - }, - Status: v1alpha1.RpaasInstanceStatus{}, - }, - objects: []runtime.Object{ - &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "other-rpaas", - Namespace: "default", - UID: "1337", - }, - Spec: v1alpha1.RpaasInstanceSpec{ - AllocateContainerPorts: v1alpha1.Bool(true), - }, - }, - &v1alpha1.RpaasPortAllocation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "default", - }, - Spec: v1alpha1.RpaasPortAllocationSpec{ - Ports: []v1alpha1.AllocatedPort{ - { - Port: 10, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-rpaas", - UID: "1337", - }, - }, - { - Port: 12, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-rpaas", - UID: "1337", - }, - }, - }, - }, - }, - }, - assertion: func(t *testing.T, err error, ports []int, portAlloc v1alpha1.RpaasPortAllocationSpec) { - assert.NoError(t, err) - assert.Equal(t, []int{13, 11}, ports) - assert.Equal(t, v1alpha1.RpaasPortAllocationSpec{ - Ports: []v1alpha1.AllocatedPort{ - { - Port: 10, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-rpaas", - UID: "1337", - }, - }, - { - Port: 12, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-rpaas", - UID: "1337", - }, - }, - { - Port: 13, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "my-rpaas", - UID: "1234", - }, - }, - { - Port: 11, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "my-rpaas", - UID: "1234", - }, - }, - }, - }, portAlloc) - }, - }, - { - name: "errors if no ports are available", - portMin: 10, - portMax: 11, - rpaas: &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-rpaas", - Namespace: "default", - UID: "1234", - }, - Spec: v1alpha1.RpaasInstanceSpec{ - AllocateContainerPorts: v1alpha1.Bool(true), - }, - Status: v1alpha1.RpaasInstanceStatus{}, - }, - objects: []runtime.Object{ - &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "other-rpaas", - Namespace: "default", - UID: "1337", - }, - Spec: v1alpha1.RpaasInstanceSpec{ - AllocateContainerPorts: v1alpha1.Bool(true), - }, - }, - &v1alpha1.RpaasPortAllocation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "default", - }, - Spec: v1alpha1.RpaasPortAllocationSpec{ - Ports: []v1alpha1.AllocatedPort{ - { - Port: 10, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-rpaas", - UID: "1337", - }, - }, - { - Port: 11, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-rpaas", - UID: "1337", - }, - }, - }, - }, - }, - }, - assertion: func(t *testing.T, err error, ports []int, portAlloc v1alpha1.RpaasPortAllocationSpec) { - assert.Nil(t, ports) - assert.EqualError(t, err, `unable to allocate container ports, wanted 2, allocated 0`) - }, - }, - { - name: "errors with empty port range", - portMin: 0, - portMax: 0, - rpaas: &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "my-rpaas", - Namespace: "default", - UID: "1234", - }, - Spec: v1alpha1.RpaasInstanceSpec{ - AllocateContainerPorts: v1alpha1.Bool(true), - }, - Status: v1alpha1.RpaasInstanceStatus{}, - }, - objects: []runtime.Object{ - &v1alpha1.RpaasInstance{ - ObjectMeta: metav1.ObjectMeta{ - Name: "other-rpaas", - Namespace: "default", - UID: "1337", - }, - Spec: v1alpha1.RpaasInstanceSpec{ - AllocateContainerPorts: v1alpha1.Bool(true), - }, - }, - &v1alpha1.RpaasPortAllocation{ - ObjectMeta: metav1.ObjectMeta{ - Name: "default", - }, - Spec: v1alpha1.RpaasPortAllocationSpec{ - Ports: []v1alpha1.AllocatedPort{ - { - Port: 10, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-rpaas", - UID: "1337", - }, - }, - { - Port: 11, - Owner: v1alpha1.NamespacedOwner{ - Namespace: "default", - RpaasName: "other-rpaas", - UID: "1337", - }, - }, - }, - }, - }, - }, - assertion: func(t *testing.T, err error, ports []int, portAlloc v1alpha1.RpaasPortAllocationSpec) { - assert.Nil(t, ports) - assert.EqualError(t, err, `unable to allocate container ports, range is invalid: min: 0, max: 0`) - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := config.Init() - require.NoError(t, err) - resources := []runtime.Object{tt.rpaas} - if tt.objects != nil { - resources = append(resources, tt.objects...) - } - reconciler := newRpaasInstanceReconciler(resources...) - reconciler.PortRangeMin = tt.portMin - reconciler.PortRangeMax = tt.portMax - ports, err := reconciler.reconcileDedicatedPorts(context.Background(), tt.rpaas, 2) - var allocation v1alpha1.RpaasPortAllocation - allocErr := reconciler.Client.Get(context.Background(), types.NamespacedName{ - Name: defaultPortAllocationResource, - }, &allocation) - require.NoError(t, allocErr) - tt.assertion(t, err, ports, allocation.Spec) - }) - } -} - func TestReconcile(t *testing.T) { rpaas := &v1alpha1.RpaasInstance{ ObjectMeta: metav1.ObjectMeta{ diff --git a/controllers/rpaasinstance_controller.go b/controllers/rpaasinstance_controller.go index 3f8736135..3d251589a 100644 --- a/controllers/rpaasinstance_controller.go +++ b/controllers/rpaasinstance_controller.go @@ -8,7 +8,6 @@ import ( "context" "fmt" "reflect" - "sort" "strconv" cmv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" @@ -52,7 +51,6 @@ type RpaasInstanceReconciler struct { // +kubebuilder:rbac:groups=nginx.tsuru.io,resources=nginxes,verbs=get;list;watch;create;update -// +kubebuilder:rbac:groups=extensions.tsuru.io,resources=rpaasportallocations,verbs=get;list;watch;create;update // +kubebuilder:rbac:groups=extensions.tsuru.io,resources=rpaasflavors,verbs=get;list;watch // +kubebuilder:rbac:groups=extensions.tsuru.io,resources=rpaasplans,verbs=get;list;watch // +kubebuilder:rbac:groups=extensions.tsuru.io,resources=rpaasinstances,verbs=get;list;watch;update;patch @@ -63,8 +61,7 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques instance, err := r.getRpaasInstance(ctx, req.NamespacedName) if k8serrors.IsNotFound(err) { - _, err = r.reconcileDedicatedPorts(ctx, nil, 0) - return reconcile.Result{}, err + return reconcile.Result{}, nil } if err != nil { @@ -108,39 +105,12 @@ func (r *RpaasInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques return reconcile.Result{}, err } - dedicatedPorts, err := r.reconcileDedicatedPorts(ctx, instance, 3) - if err != nil { - return reconcile.Result{}, err - } - - if len(dedicatedPorts) == 0 { - // nginx-operator will allocate http and https ports by hostNetwork setting - instanceMergedWithFlavors.Spec.PodTemplate.Ports = []corev1.ContainerPort{ - { - Name: nginx.PortNameManagement, - ContainerPort: nginx.DefaultManagePort, - Protocol: corev1.ProtocolTCP, - }, - } - } else if len(dedicatedPorts) == 3 { - sort.Ints(dedicatedPorts) - instanceMergedWithFlavors.Spec.PodTemplate.Ports = []corev1.ContainerPort{ - { - Name: nginx.PortNameHTTP, - ContainerPort: int32(dedicatedPorts[0]), - Protocol: corev1.ProtocolTCP, - }, - { - Name: nginx.PortNameHTTPS, - ContainerPort: int32(dedicatedPorts[1]), - Protocol: corev1.ProtocolTCP, - }, - { - Name: nginx.PortNameManagement, - ContainerPort: int32(dedicatedPorts[2]), - Protocol: corev1.ProtocolTCP, - }, - } + instanceMergedWithFlavors.Spec.PodTemplate.Ports = []corev1.ContainerPort{ + { + Name: nginx.PortNameManagement, + ContainerPort: nginx.DefaultManagePort, + Protocol: corev1.ProtocolTCP, + }, } rendered, err := r.renderTemplate(ctx, instanceMergedWithFlavors, plan) diff --git a/go.mod b/go.mod index e72d56072..7778934f4 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,6 @@ require ( github.com/tsuru/nginx-operator v0.13.1 github.com/uber/jaeger-client-go v2.25.0+incompatible github.com/urfave/cli/v2 v2.3.0 - github.com/willf/bitset v1.1.11 golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e golang.org/x/net v0.0.0-20220225172249-27dd8689420f golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 diff --git a/go.sum b/go.sum index 3ad1a275a..baa2afe71 100644 --- a/go.sum +++ b/go.sum @@ -1102,7 +1102,6 @@ github.com/vishvananda/netns v0.0.0-20180720170159-13995c7128cc/go.mod h1:ZjcWmF github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0= github.com/willf/bitset v1.1.11-0.20200630133818-d5bec3311243/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4= -github.com/willf/bitset v1.1.11 h1:N7Z7E9UvjW+sGsEl7k/SJrvY2reP1A07MrGuCjIOjRE= github.com/willf/bitset v1.1.11/go.mod h1:83CECat5yLh5zVOf4P1ErAgKA5UDvKtgyUABdr3+MjI= github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=