-
Notifications
You must be signed in to change notification settings - Fork 56
Add support to configure branch ENI cooldown period via configmap #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
90b90d3
Add support to configure branch ENI cooldown period via configmap
sushrk 5338739
support configurable branch ENI cooldown period
haouc da40ae9
moving error check out from CM update
haouc 60641d0
Fix logs and remove mutex lock in Get function
sushrk 2f60f7d
Update to go1.21.5
sushrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
mocks/amazon-vcp-resource-controller-k8s/pkg/provider/branch/cooldown/mock_cooldown.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file 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 cooldown | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"sync" | ||
"time" | ||
|
||
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/config" | ||
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/k8s" | ||
"github.com/go-logr/logr" | ||
) | ||
|
||
// Global variable for CoolDownPeriod allows packages to Get and Set the coolDown period | ||
var coolDown *cooldown | ||
|
||
type cooldown struct { | ||
mu sync.RWMutex | ||
sushrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// CoolDownPeriod is the period to wait before deleting the branch ENI for propagation of ip tables rule for deleted pod | ||
coolDownPeriod time.Duration | ||
} | ||
|
||
type CoolDown interface { | ||
GetCoolDownPeriod() time.Duration | ||
SetCoolDownPeriod(time.Duration) | ||
} | ||
|
||
const ( | ||
DefaultCoolDownPeriod = time.Second * 60 | ||
) | ||
|
||
// Initialize coolDown period by setting the value in configmap or to default | ||
func InitCoolDownPeriod(k8sApi k8s.K8sWrapper, log logr.Logger) { | ||
coolDown = &cooldown{} | ||
coolDownPeriod, err := GetVpcCniConfigMapCoolDownPeriodOrDefault(k8sApi, log) | ||
if err != nil { | ||
log.Info("setting coolDown period to default", "cool down period", coolDownPeriod) | ||
} | ||
coolDown.coolDownPeriod = coolDownPeriod | ||
} | ||
|
||
func GetCoolDown() CoolDown { | ||
return coolDown | ||
} | ||
|
||
func GetVpcCniConfigMapCoolDownPeriodOrDefault(k8sApi k8s.K8sWrapper, log logr.Logger) (time.Duration, error) { | ||
vpcCniConfigMap, err := k8sApi.GetConfigMap(config.VpcCniConfigMapName, config.KubeSystemNamespace) | ||
if err == nil && vpcCniConfigMap.Data != nil { | ||
if val, ok := vpcCniConfigMap.Data[config.BranchENICooldownPeriodKey]; ok { | ||
coolDownPeriodInt, err := strconv.Atoi(val) | ||
if err != nil { | ||
log.Error(err, "failed to parse branch ENI coolDown period", "cool down period", val) | ||
} else { | ||
return time.Second * time.Duration(coolDownPeriodInt), nil | ||
} | ||
} | ||
} | ||
// If configmap not found, or configmap data not found, or error in parsing coolDown period, return default coolDown period and error | ||
return DefaultCoolDownPeriod, fmt.Errorf("failed to get coolDown period period:%v", err) | ||
sushrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (c *cooldown) GetCoolDownPeriod() time.Duration { | ||
c.mu.Lock() | ||
sushrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defer c.mu.Unlock() | ||
return c.coolDownPeriod | ||
} | ||
|
||
func (c *cooldown) SetCoolDownPeriod(newCoolDownPeriod time.Duration) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
c.coolDownPeriod = newCoolDownPeriod | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file 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 cooldown | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
mock_k8s "github.com/aws/amazon-vpc-resource-controller-k8s/mocks/amazon-vcp-resource-controller-k8s/pkg/k8s" | ||
"github.com/aws/amazon-vpc-resource-controller-k8s/pkg/config" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
) | ||
|
||
var log = zap.New(zap.UseDevMode(true)).WithName("cooldown test") | ||
var ( | ||
mockConfigMap30s = &corev1.ConfigMap{ | ||
TypeMeta: metav1.TypeMeta{}, | ||
ObjectMeta: metav1.ObjectMeta{Name: config.VpcCniConfigMapName, Namespace: config.KubeSystemNamespace}, | ||
Data: map[string]string{config.BranchENICooldownPeriodKey: "30"}, | ||
} | ||
mockConfigMapNilData = &corev1.ConfigMap{ | ||
TypeMeta: metav1.TypeMeta{}, | ||
ObjectMeta: metav1.ObjectMeta{Name: config.VpcCniConfigMapName, Namespace: config.KubeSystemNamespace}, | ||
Data: map[string]string{}, | ||
} | ||
mockConfigMapErrData = &corev1.ConfigMap{ | ||
TypeMeta: metav1.TypeMeta{}, | ||
ObjectMeta: metav1.ObjectMeta{Name: config.VpcCniConfigMapName, Namespace: config.KubeSystemNamespace}, | ||
Data: map[string]string{config.BranchENICooldownPeriodKey: "aaa"}, | ||
} | ||
) | ||
|
||
func TestCoolDown_InitCoolDownPeriod(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
type args struct { | ||
vpcCniConfigMap *corev1.ConfigMap | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
expectedCoolDown time.Duration | ||
err error | ||
}{ | ||
{ | ||
name: "VpcCniConfigMap_Exists, verifies cooldown period is set to configmap value when exists", | ||
args: args{vpcCniConfigMap: mockConfigMap30s}, | ||
expectedCoolDown: time.Second * 30, | ||
err: nil, | ||
}, | ||
{ | ||
name: "VpcCniConfigMap_NotExists, verifies cool down period is set to default when configmap does not exist", | ||
args: args{}, | ||
expectedCoolDown: time.Second * 60, | ||
err: fmt.Errorf("mock error"), | ||
}, | ||
{ | ||
name: "VpcCniConfigMap_Exists_NilData, verifies cool period is set to default when configmap data does not exist", | ||
args: args{vpcCniConfigMap: mockConfigMapNilData}, | ||
expectedCoolDown: time.Second * 60, | ||
err: nil, | ||
}, | ||
{ | ||
name: "VpcCniConfigMap_Exists_ErrData, verifies cool period is set to default when configmap data is incorrect", | ||
args: args{vpcCniConfigMap: mockConfigMapErrData}, | ||
expectedCoolDown: time.Second * 60, | ||
err: nil, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
ctrl = gomock.NewController(t) | ||
defer ctrl.Finish() | ||
}) | ||
mockK8sApi := mock_k8s.NewMockK8sWrapper(ctrl) | ||
mockK8sApi.EXPECT().GetConfigMap(config.VpcCniConfigMapName, config.KubeSystemNamespace).Return(test.args.vpcCniConfigMap, test.err) | ||
InitCoolDownPeriod(mockK8sApi, log) | ||
assert.Equal(t, test.expectedCoolDown, coolDown.coolDownPeriod) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this defaulted to 30s since if first time get fails what will be the value?