Skip to content

Commit 8238750

Browse files
authored
controller and eventhandlers (#5)
* controller and eventhandlers - basic controller framework and eventhandlers - k8s utilities - finalizer manager - command line configuration * follow up changes for eventhandlers and controllers - refactor resolver - update controller-runtime to v0.15.0 - update eventhandlers * check deletion timestamp for namespace
1 parent eda20ad commit 8238750

File tree

22 files changed

+1558
-574
lines changed

22 files changed

+1558
-574
lines changed

cmd/main.go

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"flag"
2120
"os"
2221

22+
"github.com/go-logr/logr"
23+
"github.com/spf13/pflag"
24+
"go.uber.org/zap/zapcore"
25+
2326
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2427
// to ensure that exec-entrypoint and run can make use of them.
2528
_ "k8s.io/client-go/plugin/pkg/client/auth"
@@ -31,8 +34,12 @@ import (
3134
"sigs.k8s.io/controller-runtime/pkg/healthz"
3235
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3336

34-
networkingv1alpha1 "github.com/aws/amazon-network-policy-controller-k8s/api/v1alpha1"
35-
"github.com/aws/amazon-network-policy-controller-k8s/internal/controller"
37+
policyinfo "github.com/aws/amazon-network-policy-controller-k8s/api/v1alpha1"
38+
"github.com/aws/amazon-network-policy-controller-k8s/internal/controllers"
39+
"github.com/aws/amazon-network-policy-controller-k8s/pkg/config"
40+
"github.com/aws/amazon-network-policy-controller-k8s/pkg/k8s"
41+
"github.com/aws/amazon-network-policy-controller-k8s/pkg/policyendpoints"
42+
"github.com/aws/amazon-network-policy-controller-k8s/version"
3643
//+kubebuilder:scaffold:imports
3744
)
3845

@@ -44,58 +51,52 @@ var (
4451
func init() {
4552
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
4653

47-
utilruntime.Must(networkingv1alpha1.AddToScheme(scheme))
54+
utilruntime.Must(policyinfo.AddToScheme(scheme))
4855
//+kubebuilder:scaffold:scheme
4956
}
5057

5158
func main() {
52-
var metricsAddr string
53-
var enableLeaderElection bool
54-
var probeAddr string
55-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
56-
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
57-
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
58-
"Enable leader election for controller manager. "+
59-
"Enabling this will ensure there is only one active controller manager.")
60-
opts := zap.Options{
61-
Development: true,
59+
infoLogger := getLoggerWithLogLevel("info")
60+
infoLogger.Info("version",
61+
"GitVersion", version.GitVersion,
62+
"GitCommit", version.GitCommit,
63+
"BuildDate", version.BuildDate,
64+
)
65+
controllerCFG, err := loadControllerConfig()
66+
if err != nil {
67+
infoLogger.Error(err, "unable to load controller config")
68+
os.Exit(1)
6269
}
63-
opts.BindFlags(flag.CommandLine)
64-
flag.Parse()
65-
66-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
67-
68-
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
69-
Scheme: scheme,
70-
MetricsBindAddress: metricsAddr,
71-
Port: 9443,
72-
HealthProbeBindAddress: probeAddr,
73-
LeaderElection: enableLeaderElection,
74-
LeaderElectionID: "9d29aaa6.k8s.aws",
75-
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
76-
// when the Manager ends. This requires the binary to immediately end when the
77-
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
78-
// speeds up voluntary leader transitions as the new leader don't have to wait
79-
// LeaseDuration time first.
80-
//
81-
// In the default scaffold provided, the program ends immediately after
82-
// the manager stops, so would be fine to enable this option. However,
83-
// if you are doing or is intended to do any operation such as perform cleanups
84-
// after the manager stops then its usage might be unsafe.
85-
// LeaderElectionReleaseOnCancel: true,
86-
})
70+
ctrlLogger := getLoggerWithLogLevel(controllerCFG.LogLevel)
71+
ctrl.SetLogger(ctrlLogger)
72+
73+
restCFG, err := config.BuildRestConfig(controllerCFG.RuntimeConfig)
8774
if err != nil {
88-
setupLog.Error(err, "unable to start manager")
75+
setupLog.Error(err, "unable to build REST config")
8976
os.Exit(1)
9077
}
78+
rtOpts := config.BuildRuntimeOptions(controllerCFG.RuntimeConfig, scheme)
9179

92-
if err = (&controller.PolicyEndpointReconciler{
93-
Client: mgr.GetClient(),
94-
Scheme: mgr.GetScheme(),
95-
}).SetupWithManager(mgr); err != nil {
96-
setupLog.Error(err, "unable to create controller", "controller", "PolicyEndpoint")
80+
mgr, err := ctrl.NewManager(restCFG, rtOpts)
81+
if err != nil {
82+
setupLog.Error(err, "unable to create controller manager")
9783
os.Exit(1)
9884
}
85+
ctx := ctrl.SetupSignalHandler()
86+
enablePolicyController := true
87+
policyEndpointsManager := policyendpoints.NewPolicyEndpointsManager(mgr.GetClient(),
88+
controllerCFG.EndpointChunkSize, ctrl.Log.WithName("endpoints-manager"))
89+
finalizerManager := k8s.NewDefaultFinalizerManager(mgr.GetClient(), ctrl.Log.WithName("finalizer-manager"))
90+
policyController := controllers.NewPolicyReconciler(mgr.GetClient(), policyEndpointsManager,
91+
controllerCFG, finalizerManager, ctrl.Log.WithName("controllers").WithName("policy"))
92+
if enablePolicyController {
93+
setupLog.Info("Network Policy controller is enabled, starting watches")
94+
if err := policyController.SetupWithManager(ctx, mgr); err != nil {
95+
setupLog.Error(err, "unable to create controller", "controller", "policy")
96+
os.Exit(1)
97+
}
98+
}
99+
99100
//+kubebuilder:scaffold:builder
100101

101102
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
@@ -106,10 +107,41 @@ func main() {
106107
setupLog.Error(err, "unable to set up ready check")
107108
os.Exit(1)
108109
}
109-
110-
setupLog.Info("starting manager")
111-
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
112-
setupLog.Error(err, "problem running manager")
110+
setupLog.Info("starting controller manager")
111+
if err := mgr.Start(ctx); err != nil {
112+
setupLog.Error(err, "problem running controller manager")
113113
os.Exit(1)
114114
}
115+
setupLog.Info("controller manager stopped")
116+
117+
}
118+
119+
// loadControllerConfig loads the controller configuration
120+
func loadControllerConfig() (config.ControllerConfig, error) {
121+
controllerConfig := config.ControllerConfig{}
122+
fs := pflag.NewFlagSet("", pflag.ExitOnError)
123+
controllerConfig.BindFlags(fs)
124+
125+
if err := fs.Parse(os.Args); err != nil {
126+
return controllerConfig, err
127+
}
128+
129+
return controllerConfig, nil
130+
}
131+
132+
// getLoggerWithLogLevel returns logger with specific log level.
133+
func getLoggerWithLogLevel(logLevel string) logr.Logger {
134+
var zapLevel zapcore.Level
135+
switch logLevel {
136+
case "info":
137+
zapLevel = zapcore.InfoLevel
138+
case "debug":
139+
zapLevel = zapcore.DebugLevel
140+
default:
141+
zapLevel = zapcore.InfoLevel
142+
}
143+
return zap.New(zap.UseDevMode(false),
144+
zap.Level(zapLevel),
145+
zap.StacktraceLevel(zapcore.FatalLevel),
146+
)
115147
}

config/rbac/role.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,30 @@ metadata:
55
creationTimestamp: null
66
name: manager-role
77
rules:
8+
- apiGroups:
9+
- ""
10+
resources:
11+
- namespaces
12+
verbs:
13+
- get
14+
- list
15+
- watch
16+
- apiGroups:
17+
- ""
18+
resources:
19+
- pods
20+
verbs:
21+
- get
22+
- list
23+
- watch
24+
- apiGroups:
25+
- ""
26+
resources:
27+
- services
28+
verbs:
29+
- get
30+
- list
31+
- watch
832
- apiGroups:
933
- networking.k8s.aws
1034
resources:
@@ -31,3 +55,13 @@ rules:
3155
- get
3256
- patch
3357
- update
58+
- apiGroups:
59+
- networking.k8s.io
60+
resources:
61+
- networkpolicies
62+
verbs:
63+
- get
64+
- list
65+
- patch
66+
- update
67+
- watch

go.mod

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,76 @@ module github.com/aws/amazon-network-policy-controller-k8s
33
go 1.19
44

55
require (
6-
github.com/onsi/ginkgo/v2 v2.6.0
7-
github.com/onsi/gomega v1.24.1
8-
k8s.io/apimachinery v0.26.1
9-
k8s.io/client-go v0.26.1
10-
sigs.k8s.io/controller-runtime v0.14.4
6+
github.com/go-logr/logr v1.2.4
7+
github.com/google/go-cmp v0.5.9
8+
github.com/onsi/ginkgo/v2 v2.9.5
9+
github.com/onsi/gomega v1.27.7
10+
github.com/samber/lo v1.38.1
11+
github.com/spf13/pflag v1.0.5
12+
github.com/stretchr/testify v1.8.1
13+
go.uber.org/zap v1.24.0
14+
k8s.io/api v0.27.2
15+
k8s.io/apimachinery v0.27.2
16+
k8s.io/client-go v0.27.2
17+
sigs.k8s.io/controller-runtime v0.15.0
1118
)
1219

1320
require (
1421
github.com/beorn7/perks v1.0.1 // indirect
15-
github.com/cespare/xxhash/v2 v2.1.2 // indirect
22+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
1623
github.com/davecgh/go-spew v1.1.1 // indirect
1724
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
25+
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
1826
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
1927
github.com/fsnotify/fsnotify v1.6.0 // indirect
20-
github.com/go-logr/logr v1.2.3 // indirect
21-
github.com/go-logr/zapr v1.2.3 // indirect
22-
github.com/go-openapi/jsonpointer v0.19.5 // indirect
23-
github.com/go-openapi/jsonreference v0.20.0 // indirect
24-
github.com/go-openapi/swag v0.19.14 // indirect
28+
github.com/go-logr/zapr v1.2.4 // indirect
29+
github.com/go-openapi/jsonpointer v0.19.6 // indirect
30+
github.com/go-openapi/jsonreference v0.20.1 // indirect
31+
github.com/go-openapi/swag v0.22.3 // indirect
32+
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
2533
github.com/gogo/protobuf v1.3.2 // indirect
2634
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
27-
github.com/golang/protobuf v1.5.2 // indirect
35+
github.com/golang/protobuf v1.5.3 // indirect
2836
github.com/google/gnostic v0.5.7-v3refs // indirect
29-
github.com/google/go-cmp v0.5.9 // indirect
3037
github.com/google/gofuzz v1.1.0 // indirect
31-
github.com/google/uuid v1.1.2 // indirect
38+
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
39+
github.com/google/uuid v1.3.0 // indirect
3240
github.com/imdario/mergo v0.3.6 // indirect
3341
github.com/josharian/intern v1.0.0 // indirect
3442
github.com/json-iterator/go v1.1.12 // indirect
35-
github.com/mailru/easyjson v0.7.6 // indirect
36-
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
43+
github.com/mailru/easyjson v0.7.7 // indirect
44+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
3745
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3846
github.com/modern-go/reflect2 v1.0.2 // indirect
3947
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4048
github.com/pkg/errors v0.9.1 // indirect
41-
github.com/prometheus/client_golang v1.14.0 // indirect
42-
github.com/prometheus/client_model v0.3.0 // indirect
43-
github.com/prometheus/common v0.37.0 // indirect
44-
github.com/prometheus/procfs v0.8.0 // indirect
45-
github.com/spf13/pflag v1.0.5 // indirect
49+
github.com/pmezard/go-difflib v1.0.0 // indirect
50+
github.com/prometheus/client_golang v1.15.1 // indirect
51+
github.com/prometheus/client_model v0.4.0 // indirect
52+
github.com/prometheus/common v0.42.0 // indirect
53+
github.com/prometheus/procfs v0.9.0 // indirect
4654
go.uber.org/atomic v1.7.0 // indirect
4755
go.uber.org/multierr v1.6.0 // indirect
48-
go.uber.org/zap v1.24.0 // indirect
49-
golang.org/x/net v0.3.1-0.20221206200815-1e63c2f08a10 // indirect
50-
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
51-
golang.org/x/sys v0.3.0 // indirect
52-
golang.org/x/term v0.3.0 // indirect
53-
golang.org/x/text v0.5.0 // indirect
56+
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
57+
golang.org/x/net v0.10.0 // indirect
58+
golang.org/x/oauth2 v0.5.0 // indirect
59+
golang.org/x/sys v0.8.0 // indirect
60+
golang.org/x/term v0.8.0 // indirect
61+
golang.org/x/text v0.9.0 // indirect
5462
golang.org/x/time v0.3.0 // indirect
55-
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
63+
golang.org/x/tools v0.9.1 // indirect
64+
gomodules.xyz/jsonpatch/v2 v2.3.0 // indirect
5665
google.golang.org/appengine v1.6.7 // indirect
57-
google.golang.org/protobuf v1.28.1 // indirect
66+
google.golang.org/protobuf v1.30.0 // indirect
5867
gopkg.in/inf.v0 v0.9.1 // indirect
5968
gopkg.in/yaml.v2 v2.4.0 // indirect
6069
gopkg.in/yaml.v3 v3.0.1 // indirect
61-
k8s.io/api v0.26.1 // indirect
62-
k8s.io/apiextensions-apiserver v0.26.1 // indirect
63-
k8s.io/component-base v0.26.1 // indirect
64-
k8s.io/klog/v2 v2.80.1 // indirect
65-
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
66-
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 // indirect
67-
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
70+
k8s.io/apiextensions-apiserver v0.27.2 // indirect
71+
k8s.io/component-base v0.27.2 // indirect
72+
k8s.io/klog/v2 v2.90.1 // indirect
73+
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
74+
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
75+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
6876
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
6977
sigs.k8s.io/yaml v1.3.0 // indirect
7078
)

0 commit comments

Comments
 (0)