Skip to content
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
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

.PHONY: all dist check clean \
lint format check-format vet docker-vet \
build-linux docker docker-init \
build-linux build-init docker docker-init \
unit-test unit-test-race build-docker-test docker-func-test \
build-metrics docker-metrics \
metrics-unit-test docker-metrics-test
Expand Down Expand Up @@ -98,7 +98,7 @@ DOCKER_BUILD_FLAGS = --build-arg GOARCH="$(ARCH)" \
$(DOCKER_ARGS)

# Default to building an executable using the host's Go toolchain.
.DEFAULT_GOAL = build-linux
.DEFAULT_GOAL = build-linux && build-init

# Build both CNI and metrics helper container images.
all: docker docker-init docker-metrics ## Builds Init, CNI and metrics helper container images.
Expand All @@ -118,6 +118,14 @@ build-linux: ## Build the VPC CNI plugin agent using the host's Go toolchain.
go build $(VENDOR_OVERRIDE_FLAG) $(BUILD_FLAGS) -o grpc-health-probe ./cmd/grpc-health-probe
go build $(VENDOR_OVERRIDE_FLAG) $(BUILD_FLAGS) -o egress-v4-cni ./cmd/egress-v4-cni-plugin

build-aws-vpc-cni-init: BUILD_FLAGS = $(BUILD_MODE) -ldflags '-s -w $(LDFLAGS)'
build-aws-vpc-cni-init: ## Build the VPC CNI init container using the host's Go toolchain.
go build $(VENDOR_OVERRIDE_FLAG) $(BUILD_FLAGS) -o aws-vpc-cni-init ./cmd/aws-vpc-cni-init

build-aws-vpc-cni: BUILD_FLAGS = $(BUILD_MODE) -ldflags '-s -w $(LDFLAGS)'
build-aws-vpc-cni: ## Build the VPC CNI container using the host's Go toolchain.
go build $(VENDOR_OVERRIDE_FLAG) $(BUILD_FLAGS) -o aws-vpc-cni ./cmd/aws-vpc-cni

# Build VPC CNI plugin & agent container image.
docker: setup-ec2-sdk-override ## Build VPC CNI plugin & agent container image.
docker build $(DOCKER_BUILD_FLAGS) \
Expand Down
168 changes: 168 additions & 0 deletions cmd/aws-vpc-cni-init/main.go
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")
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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

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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe "Failed to set disable_ipv6 to 0"

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}
Loading