Skip to content

Commit

Permalink
Discard reverse port if collision detected (#2304)
Browse files Browse the repository at this point in the history
* Discard reverse port if collision detected

* Added unit test

* Added license
  • Loading branch information
AgustinRamiroDiaz committed Mar 7, 2022
1 parent ef58009 commit c1e1ab0
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pkg/cmd/init/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ func SetDevDefaultsFromApp(ctx context.Context, dev *model.Dev, app apps.App, co
setResourcesFromPod(dev, pod, container)
}

return setForwardsFromPod(ctx, dev, pod, c)
if err := setForwardsFromPod(ctx, dev, pod, c); err != nil {
return err
}

discardReverseIfCollision(dev)
return nil
}

func getRunningPod(ctx context.Context, app apps.App, container string, c kubernetes.Interface) (*apiv1.Pod, error) {
Expand Down Expand Up @@ -161,6 +166,19 @@ func setForwardsFromPod(ctx context.Context, dev *model.Dev, pod *apiv1.Pod, c *
return nil
}

// discardReverseIfCollision discards the reverse forward port if there is a collision with a forwarded port
func discardReverseIfCollision(dev *model.Dev) {
seenPorts := map[int]bool{}
for _, forward := range dev.Forward {
seenPorts[forward.Local] = true
}
for i, reverse := range dev.Reverse {
if seenPorts[reverse.Local] {
dev.Reverse = append(dev.Reverse[:i], dev.Reverse[i+1:]...)
}
}
}

func setNameAndLabelsFromApp(dev *model.Dev, app apps.App) {
for _, l := range componentLabels {
component := app.ObjectMeta().Labels[l]
Expand Down
96 changes: 96 additions & 0 deletions pkg/cmd/init/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2022 The Okteto 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 init

import (
"reflect"
"testing"

"github.com/okteto/okteto/pkg/model"
)

func TestDiscardReverseIfCollision(t *testing.T) {
tests := []struct {
name string
devInput *model.Dev
devOutput *model.Dev
}{
{
name: "no collision",
devInput: &model.Dev{
Forward: []model.Forward{
{
Local: 8000,
Remote: 8000,
},
},
Reverse: []model.Reverse{
{
Local: 8001,
Remote: 8001,
},
},
},
devOutput: &model.Dev{
Forward: []model.Forward{
{
Local: 8000,
Remote: 8000,
},
},
Reverse: []model.Reverse{
{
Local: 8001,
Remote: 8001,
},
},
},
},
{
name: "collision",
devInput: &model.Dev{
Forward: []model.Forward{
{
Local: 8000,
Remote: 8000,
},
},
Reverse: []model.Reverse{
{
Local: 8000,
Remote: 8000,
},
},
},
devOutput: &model.Dev{
Forward: []model.Forward{
{
Local: 8000,
Remote: 8000,
},
},
Reverse: []model.Reverse{},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
discardReverseIfCollision(test.devInput)
if !reflect.DeepEqual(test.devInput, test.devOutput) {
t.Errorf("expected %v got %v", test.devOutput, test.devInput)
}
})
}
}
13 changes: 13 additions & 0 deletions pkg/okteto/context_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2022 The Okteto 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 okteto

import (
Expand Down

0 comments on commit c1e1ab0

Please sign in to comment.