Skip to content

Commit

Permalink
Improve configuration management; Improve the relationship between dr…
Browse files Browse the repository at this point in the history
…iver, controller, & cloud (#1995)

Signed-off-by: torredil <[email protected]>
  • Loading branch information
torredil authored Apr 15, 2024
1 parent f29900c commit 75d142b
Show file tree
Hide file tree
Showing 36 changed files with 800 additions and 2,017 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ test/coverage:
go tool cover -html=filtered_cover.out -o coverage.html
rm cover.out filtered_cover.out

.PHONY: test-sanity
test-sanity:
go test -v -race ./tests/sanity/...
# TODO: Re-enable sanity tests
# sanity tests have been disabled with the removal of NewFakeDriver, which was previously created to instantiate a fake driver utilized for testing.
# to re-enable tests, implement sanity tests creating a new driver instance by injecting mocked dependencies.
#.PHONY: test-sanity
#test-sanity:
# go test -v -race ./tests/sanity/...

.PHONY: tools
tools: bin/aws bin/ct bin/eksctl bin/ginkgo bin/golangci-lint bin/helm bin/kops bin/kubetest2 bin/mockgen bin/shfmt
Expand Down
134 changes: 106 additions & 28 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Kubernetes Authors.
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -18,64 +18,142 @@ package main

import (
"context"
"fmt"
"os"
"strings"
"time"

flag "github.com/spf13/pflag"

"github.com/kubernetes-sigs/aws-ebs-csi-driver/cmd/hooks"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud/metadata"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/driver"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/metrics"
flag "github.com/spf13/pflag"
"k8s.io/component-base/featuregate"
logsapi "k8s.io/component-base/logs/api/v1"
json "k8s.io/component-base/logs/json"

"k8s.io/klog/v2"
)

var (
featureGate = featuregate.NewFeatureGate()
)

func main() {
fs := flag.NewFlagSet("aws-ebs-csi-driver", flag.ExitOnError)

if err := logsapi.RegisterLogFormat(logsapi.JSONLogFormat, json.Factory{}, logsapi.LoggingBetaOptions); err != nil {
klog.ErrorS(err, "failed to register JSON log format")
}

options := GetOptions(fs)
var (
version = fs.Bool("version", false, "Print the version and exit.")
toStderr = fs.Bool("logtostderr", false, "log to standard error instead of files. DEPRECATED: will be removed in a future release.")
args = os.Args[1:]
cmd = string(driver.AllMode)
options = driver.Options{}
)

c := logsapi.NewLoggingConfiguration()
err := logsapi.AddFeatureGates(featureGate)
if err != nil {
klog.ErrorS(err, "failed to add feature gates")
}
logsapi.AddFlags(c, fs)

if len(os.Args) > 1 && !strings.HasPrefix(os.Args[1], "-") {
cmd = os.Args[1]
args = os.Args[2:]
}

switch cmd {
case "pre-stop-hook":
clientset, clientErr := metadata.DefaultKubernetesAPIClient()
if clientErr != nil {
klog.ErrorS(err, "unable to communicate with k8s API")
} else {
err = hooks.PreStop(clientset)
if err != nil {
klog.ErrorS(err, "failed to execute PreStop lifecycle hook")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
}
klog.FlushAndExit(klog.ExitFlushTimeout, 0)
case string(driver.ControllerMode), string(driver.NodeMode), string(driver.AllMode):
options.Mode = driver.Mode(cmd)
default:
klog.Errorf("Unknown driver mode %s: Expected %s, %s, %s, or pre-stop-hook", cmd, driver.ControllerMode, driver.NodeMode, driver.AllMode)
klog.FlushAndExit(klog.ExitFlushTimeout, 0)
}

options.AddFlags(fs)

if err = fs.Parse(args); err != nil {
panic(err)
}

err = logsapi.ValidateAndApply(c, featureGate)
if err != nil {
klog.ErrorS(err, "failed to validate and apply logging configuration")
}

if *version {
versionInfo, versionErr := driver.GetVersionJSON()
if versionErr != nil {
klog.ErrorS(err, "failed to get version")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
fmt.Println(versionInfo)
os.Exit(0)
}

if *toStderr {
klog.SetOutput(os.Stderr)
}

// Start tracing as soon as possible
if options.ServerOptions.EnableOtelTracing {
exporter, err := driver.InitOtelTracing()
if err != nil {
if options.EnableOtelTracing {
exporter, exporterErr := driver.InitOtelTracing()
if exporterErr != nil {
klog.ErrorS(err, "failed to initialize otel tracing")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
// Exporter will flush traces on shutdown
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := exporter.Shutdown(ctx); err != nil {
klog.ErrorS(err, "could not shutdown otel exporter")
if shutdownErr := exporter.Shutdown(ctx); shutdownErr != nil {
klog.ErrorS(exporterErr, "could not shutdown otel exporter")
}
}()
}

if options.ServerOptions.HttpEndpoint != "" {
if options.HttpEndpoint != "" {
r := metrics.InitializeRecorder()
r.InitializeMetricsHandler(options.ServerOptions.HttpEndpoint, "/metrics")
r.InitializeMetricsHandler(options.HttpEndpoint, "/metrics")
}

drv, err := driver.NewDriver(
driver.WithEndpoint(options.ServerOptions.Endpoint),
driver.WithExtraTags(options.ControllerOptions.ExtraTags),
driver.WithExtraVolumeTags(options.ControllerOptions.ExtraVolumeTags),
driver.WithMode(options.DriverMode),
driver.WithVolumeAttachLimit(options.NodeOptions.VolumeAttachLimit),
driver.WithReservedVolumeAttachments(options.NodeOptions.ReservedVolumeAttachments),
driver.WithKubernetesClusterID(options.ControllerOptions.KubernetesClusterID),
driver.WithAwsSdkDebugLog(options.ControllerOptions.AwsSdkDebugLog),
driver.WithWarnOnInvalidTag(options.ControllerOptions.WarnOnInvalidTag),
driver.WithUserAgentExtra(options.ControllerOptions.UserAgentExtra),
driver.WithOtelTracing(options.ServerOptions.EnableOtelTracing),
driver.WithBatching(options.ControllerOptions.Batching),
driver.WithModifyVolumeRequestHandlerTimeout(options.ControllerOptions.ModifyVolumeRequestHandlerTimeout),
)
region := os.Getenv("AWS_REGION")
if region == "" {
klog.V(5).InfoS("[Debug] Retrieving region from metadata service")
cfg := metadata.MetadataServiceConfig{
EC2MetadataClient: metadata.DefaultEC2MetadataClient,
K8sAPIClient: metadata.DefaultKubernetesAPIClient,
}
metadata, metadataErr := metadata.NewMetadataService(cfg, region)
if metadataErr != nil {
klog.ErrorS(metadataErr, "Could not determine region from any metadata service. The region can be manually supplied via the AWS_REGION environment variable.")
panic(err)
}
region = metadata.GetRegion()
}

cloud, err := cloud.NewCloud(region, options.AwsSdkDebugLog, options.UserAgentExtra, options.Batching)
if err != nil {
klog.ErrorS(err, "failed to create cloud service")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}

drv, err := driver.NewDriver(cloud, &options)
if err != nil {
klog.ErrorS(err, "failed to create driver")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
Expand Down
148 changes: 0 additions & 148 deletions cmd/options.go

This file was deleted.

Loading

0 comments on commit 75d142b

Please sign in to comment.