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

add lease config #664

Merged
merged 1 commit into from
Aug 14, 2024
Merged
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
120 changes: 73 additions & 47 deletions cmd/terway-controlplane/terway-controlplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,53 +137,7 @@
os.Exit(1)
}

ws := wh.NewServer(wh.Options{
Port: cfg.WebhookPort,
CertDir: cfg.CertDir,
})
options := ctrl.Options{
Scheme: scheme,
HealthProbeBindAddress: cfg.HealthzBindAddress,
WebhookServer: ws,
LeaderElection: cfg.LeaderElection,
LeaderElectionID: cfg.ControllerName,
LeaderElectionNamespace: cfg.ControllerNamespace,
LeaderElectionResourceLock: "leases",
MetricsBindAddress: cfg.MetricsBindAddress,
Cache: cache.Options{
ByObject: map[client.Object]cache.ByObject{
&corev1.Node{}: {
Transform: func(i interface{}) (interface{}, error) {
if node, ok := i.(*corev1.Node); ok {
node.Status.Images = nil
node.Status.VolumesInUse = nil
node.Status.VolumesAttached = nil
return node, nil
}
return nil, fmt.Errorf("unexpected type %T", i)
},
},
&corev1.Pod{}: {
Transform: func(i interface{}) (interface{}, error) {
if pod, ok := i.(*corev1.Pod); ok {
pod.Spec.Volumes = nil
pod.Spec.EphemeralContainers = nil
pod.Spec.SecurityContext = nil
pod.Spec.ImagePullSecrets = nil
pod.Spec.Tolerations = nil
pod.Spec.ReadinessGates = nil
pod.Spec.PreemptionPolicy = nil
pod.Status.InitContainerStatuses = nil
pod.Status.ContainerStatuses = nil
pod.Status.EphemeralContainerStatuses = nil
return pod, nil
}
return nil, fmt.Errorf("unexpected type %T", i)
},
},
},
},
}
options := newOption(cfg)

Check warning on line 140 in cmd/terway-controlplane/terway-controlplane.go

View check run for this annotation

Codecov / codecov/patch

cmd/terway-controlplane/terway-controlplane.go#L140

Added line #L140 was not covered by tests

if !cfg.DisableWebhook {
err = cert.SyncCert(ctx, directClient, cfg.ControllerNamespace, cfg.ControllerName, cfg.ClusterDomain, cfg.CertDir)
Expand Down Expand Up @@ -283,6 +237,78 @@
wg.Wait()
}

func newOption(cfg *controlplane.Config) ctrl.Options {
ws := wh.NewServer(wh.Options{
Port: cfg.WebhookPort,
CertDir: cfg.CertDir,
})
options := ctrl.Options{
Scheme: scheme,
HealthProbeBindAddress: cfg.HealthzBindAddress,
WebhookServer: ws,
LeaderElection: cfg.LeaderElection,
LeaderElectionID: cfg.ControllerName,
LeaderElectionNamespace: cfg.ControllerNamespace,
LeaderElectionResourceLock: "leases",
MetricsBindAddress: cfg.MetricsBindAddress,
Cache: cache.Options{
ByObject: map[client.Object]cache.ByObject{
&corev1.Node{}: {
Transform: func(i interface{}) (interface{}, error) {
if node, ok := i.(*corev1.Node); ok {
node.Status.Images = nil
node.Status.VolumesInUse = nil
node.Status.VolumesAttached = nil
return node, nil

Check warning on line 262 in cmd/terway-controlplane/terway-controlplane.go

View check run for this annotation

Codecov / codecov/patch

cmd/terway-controlplane/terway-controlplane.go#L258-L262

Added lines #L258 - L262 were not covered by tests
}
return nil, fmt.Errorf("unexpected type %T", i)

Check warning on line 264 in cmd/terway-controlplane/terway-controlplane.go

View check run for this annotation

Codecov / codecov/patch

cmd/terway-controlplane/terway-controlplane.go#L264

Added line #L264 was not covered by tests
},
},
&corev1.Pod{}: {
Transform: func(i interface{}) (interface{}, error) {
if pod, ok := i.(*corev1.Pod); ok {
pod.Spec.Volumes = nil
pod.Spec.EphemeralContainers = nil
pod.Spec.SecurityContext = nil
pod.Spec.ImagePullSecrets = nil
pod.Spec.Tolerations = nil
pod.Spec.ReadinessGates = nil
pod.Spec.PreemptionPolicy = nil
pod.Status.InitContainerStatuses = nil
pod.Status.ContainerStatuses = nil
pod.Status.EphemeralContainerStatuses = nil
return pod, nil

Check warning on line 280 in cmd/terway-controlplane/terway-controlplane.go

View check run for this annotation

Codecov / codecov/patch

cmd/terway-controlplane/terway-controlplane.go#L268-L280

Added lines #L268 - L280 were not covered by tests
}
return nil, fmt.Errorf("unexpected type %T", i)

Check warning on line 282 in cmd/terway-controlplane/terway-controlplane.go

View check run for this annotation

Codecov / codecov/patch

cmd/terway-controlplane/terway-controlplane.go#L282

Added line #L282 was not covered by tests
},
},
},
},
}

if cfg.LeaseDuration != "" {
d, err := time.ParseDuration(cfg.LeaseDuration)
if err == nil {
options.LeaseDuration = &d
}
}

if cfg.RenewDeadline != "" {
d, err := time.ParseDuration(cfg.RenewDeadline)
if err == nil {
options.RenewDeadline = &d
}
}

if cfg.RetryPeriod != "" {
d, err := time.ParseDuration(cfg.RetryPeriod)
if err == nil {
options.RetryPeriod = &d
}
}
return options
}

// initOpenTelemetry bootstraps the OpenTelemetry pipeline.
// If it does not return an error, make sure to call shutdown for proper cleanup.
func initOpenTelemetry(ctx context.Context, serviceName, serviceVersion string, cfg *controlplane.Config) (*trace.TracerProvider, error) {
Expand Down
34 changes: 34 additions & 0 deletions cmd/terway-controlplane/terway-controlplane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package main
import (
"context"
"testing"
"time"

"github.com/samber/lo"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

Expand Down Expand Up @@ -122,3 +124,35 @@ func Test_detectMultiIP(t *testing.T) {
})
}
}

func Test_newOption(t *testing.T) {
type args struct {
cfg *controlplane.Config
}
tests := []struct {
name string
args args
checkFunc func(t *testing.T, opt ctrl.Options)
}{
{
name: "new cfg",
args: args{
cfg: &controlplane.Config{
LeaseDuration: "2m",
RenewDeadline: "2m",
RetryPeriod: "2m",
},
},
checkFunc: func(t *testing.T, opt ctrl.Options) {
assert.Equal(t, 2*time.Minute, *opt.LeaseDuration)
assert.Equal(t, 2*time.Minute, *opt.RenewDeadline)
assert.Equal(t, 2*time.Minute, *opt.RetryPeriod)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.checkFunc(t, newOption(tt.args.cfg))
})
}
}
8 changes: 6 additions & 2 deletions types/controlplane/config_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ import (

type Config struct {
// controller config
LeaseLockName string `json:"leaseLockName" validate:"required" mod:"default=terway-controller-lock"`
LeaseLockNamespace string `json:"leaseLockNamespace" validate:"required" mod:"default=kube-system"`
LeaseLockName string `json:"leaseLockName" validate:"required" mod:"default=terway-controller-lock"`
LeaseLockNamespace string `json:"leaseLockNamespace" validate:"required" mod:"default=kube-system"`
LeaseDuration string `json:"leaseDuration"`
RenewDeadline string `json:"renewDeadline"`
RetryPeriod string `json:"retryPeriod"`

ControllerNamespace string `json:"controllerNamespace" validate:"required" mod:"default=kube-system"`
ControllerName string `json:"controllerName" validate:"required" mod:"default=terway-controlplane"`

Expand Down
Loading