-
Notifications
You must be signed in to change notification settings - Fork 741
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
entry point script migration to golang #1726
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
62c18f9
Use miminal distro image
jayanthvn 2bb5477
Cleanup go.mod and go.sum
jayanthvn 9dc06a1
Update for UT
jayanthvn 19d90f2
revert back to al2
jayanthvn 870da86
Merge branch 'master' into minimal_distro
jayanthvn 4a62282
Merge branch 'master' into minimal_distro
jayanthvn f083b67
code refactor
jayanthvn 3b587c9
fix typo
jayanthvn 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 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 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,168 @@ | ||
// 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. | ||
|
||
// The aws-node initialization | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/aws/amazon-vpc-cni-k8s/utils/cp" | ||
"github.com/aws/amazon-vpc-cni-k8s/utils/imds" | ||
"github.com/aws/amazon-vpc-cni-k8s/utils/sysctl" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
const ( | ||
defaultHostCNIBinPath = "/host/opt/cni/bin" | ||
metadataLocalIP = "local-ipv4" | ||
metadataMAC = "mac" | ||
|
||
envDisableIPv4TcpEarlyDemux = "DISABLE_TCP_EARLY_DEMUX" | ||
envEnableIPv6 = "ENABLE_IPv6" | ||
envHostCniBinPath = "HOST_CNI_BIN_PATH" | ||
) | ||
|
||
func getEnv(env, def string) string { | ||
if val, ok := os.LookupEnv(env); ok { | ||
return val | ||
} | ||
return def | ||
} | ||
|
||
func main() { | ||
os.Exit(_main()) | ||
} | ||
|
||
func _main() int { | ||
log.Debug("Started Initialization") | ||
pluginBins := []string{"loopback", "portmap", "bandwidth", "aws-cni-support.sh"} | ||
var err error | ||
for _, plugin := range pluginBins { | ||
if _, err = os.Stat(plugin); err != nil { | ||
log.WithError(err).Fatalf("Required executable : %s not found\n", plugin) | ||
return 1 | ||
} | ||
} | ||
|
||
hostCNIBinPath := getEnv(envHostCniBinPath, defaultHostCNIBinPath) | ||
|
||
log.Infof("Copying CNI plugin binaries ...") | ||
err = cp.InstallBinaries(pluginBins, hostCNIBinPath) | ||
if err != nil { | ||
log.WithError(err).Errorf("Failed to install binaries") | ||
return 1 | ||
} | ||
|
||
log.Infof("Copied all CNI plugin binaries to %s\n", hostCNIBinPath) | ||
|
||
var hostIP string | ||
hostIP, err = imds.GetMetaData("local-ipv4") | ||
if err != nil { | ||
log.WithError(err).Fatalf("aws-vpc-cni init failed\n") | ||
return 1 | ||
} | ||
|
||
var primaryMAC string | ||
primaryMAC, err = imds.GetMetaData("mac") | ||
if err != nil { | ||
log.WithError(err).Fatalf("aws-vpc-cni init failed\n") | ||
return 1 | ||
} | ||
|
||
log.Infof("Found hostIP %s and primaryMAC %s", hostIP, primaryMAC) | ||
|
||
links, err := netlink.LinkList() | ||
if err != nil { | ||
log.WithError(err).Fatalf("Failed to list links\n") | ||
return 1 | ||
} | ||
|
||
var primaryIF string | ||
for _, link := range links { | ||
if link.Attrs().HardwareAddr.String() == primaryMAC { | ||
primaryIF = link.Attrs().Name | ||
break | ||
} | ||
} | ||
|
||
if primaryIF == "" { | ||
log.Errorf("Failed to retrieve primary IF") | ||
return 1 | ||
} | ||
|
||
log.Infof("Found primaryIF %s", primaryIF) | ||
sys := sysctl.New() | ||
entry := "net/ipv4/conf/" + primaryIF + "/rp_filter" | ||
err = sys.SetSysctl(entry, 2) | ||
if err != nil { | ||
log.WithError(err).Fatalf("Failed to set rp_filter for %s\n", primaryIF) | ||
return 1 | ||
} | ||
|
||
val, _ := sys.GetSysctl(entry) | ||
log.Infof("Updated entry for %d", val) | ||
|
||
disableIPv4EarlyDemux := getEnv(envDisableIPv4TcpEarlyDemux, "false") | ||
entry = "net/ipv4/tcp_early_demux" | ||
if disableIPv4EarlyDemux == "true" { | ||
err = sys.SetSysctl(entry, 0) | ||
if err != nil { | ||
log.WithError(err).Fatalf("Failed to disable tcp_early_demux\n") | ||
return 1 | ||
} | ||
} else { | ||
err = sys.SetSysctl(entry, 1) | ||
if err != nil { | ||
log.WithError(err).Fatalf("Failed to enable tcp_early_demux\n") | ||
return 1 | ||
} | ||
} | ||
|
||
val, _ = sys.GetSysctl(entry) | ||
log.Infof("Updated entry for %d", val) | ||
|
||
enableIPv6 := getEnv(envEnableIPv6, "false") | ||
jdn5126 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if enableIPv6 == "true" { | ||
entry = "net/ipv6/conf/all/disable_ipv6" | ||
err = sys.SetSysctl(entry, 0) | ||
if err != nil { | ||
log.WithError(err).Fatalf("Failed to enable disable_ipv6\n") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe "Failed to set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I can add it. |
||
return 1 | ||
} | ||
val, _ = sys.GetSysctl(entry) | ||
log.Infof("Updated entry for %d", val) | ||
|
||
entry = "net/ipv6/conf/all/forwarding" | ||
err = sys.SetSysctl(entry, 1) | ||
if err != nil { | ||
log.WithError(err).Fatalf("Failed to enable ipv6 forwarding\n") | ||
return 1 | ||
} | ||
val, _ = sys.GetSysctl(entry) | ||
log.Infof("Updated entry for %d", val) | ||
|
||
entry = "net/ipv6/conf/" + primaryIF + "/accept_ra" | ||
err = sys.SetSysctl(entry, 2) | ||
if err != nil { | ||
log.WithError(err).Fatalf("Failed to enable ipv6 accept_ra\n") | ||
return 1 | ||
} | ||
val, _ = sys.GetSysctl(entry) | ||
log.Infof("Updated entry for %d", val) | ||
} | ||
|
||
log.Infof("CNI init container done") | ||
return 0 | ||
} |
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.
If the environment variable is not present, then we would be leaving default IPv6 settings. Should this explicitly disable IPv6 when env var is not present?
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.
Yes false is the default.
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.
But that default could change on AL2. I think we should call these sysctls either way to be safe