Skip to content

Commit 95fd84b

Browse files
committed
refine some code
1 parent 8519a6c commit 95fd84b

File tree

7 files changed

+59
-78
lines changed

7 files changed

+59
-78
lines changed

controllers/gateway_controller.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"k8s.io/apimachinery/pkg/runtime"
2626
ctrl "sigs.k8s.io/controller-runtime"
2727
"sigs.k8s.io/controller-runtime/pkg/client"
28-
"sigs.k8s.io/controller-runtime/pkg/log"
2928

3029
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
3130

@@ -48,15 +47,15 @@ type GatewayReconciler struct {
4847
// For more details, check Reconcile and its Result here:
4948
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
5049
func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
51-
zlog := log.FromContext(ctx)
5250
lctx := context.WithValue(ctx, utils.CtxKey_Logger, utils.NewLog(uuid.New().String(), "debug"))
51+
slog := utils.LogFromContext(lctx)
5352
if !pkg.ActiveSIGs.SyncedAtStart {
5453
<-time.After(100 * time.Millisecond)
5554
return ctrl.Result{Requeue: true}, nil
5655
}
5756
var obj gatewayv1beta1.Gateway
5857

59-
zlog.V(1).Info("handling " + req.NamespacedName.String())
58+
slog.Debugf("handling " + req.NamespacedName.String())
6059
if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
6160
if client.IgnoreNotFound(err) == nil {
6261
// delete resources
@@ -80,7 +79,7 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
8079
}
8180

8281
func handleDeletingGateway(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
83-
zlog := log.FromContext(ctx)
82+
slog := utils.LogFromContext(ctx)
8483

8584
gw := pkg.ActiveSIGs.GetGateway(req.NamespacedName.String())
8685
// Only when we know all the gateways can we know exactly which routes need to be cleared because of this gateway event.
@@ -97,7 +96,7 @@ func handleDeletingGateway(ctx context.Context, req ctrl.Request) (ctrl.Result,
9796
return ctrl.Result{}, err
9897
}
9998

100-
zlog.V(1).Info("handling + deleting " + req.NamespacedName.String())
99+
slog.Debugf("handling + deleting " + req.NamespacedName.String())
101100
pkg.ActiveSIGs.UnsetGateway(req.NamespacedName.String())
102101

103102
if ncfgs, err = pkg.ParseGatewayRelatedForClass(string(gw.Spec.GatewayClassName), gws); err != nil {
@@ -132,10 +131,10 @@ func handleDeletingGateway(ctx context.Context, req ctrl.Request) (ctrl.Result,
132131
}
133132

134133
func handleUpsertingGateway(ctx context.Context, obj *gatewayv1beta1.Gateway) (ctrl.Result, error) {
135-
zlog := log.FromContext(ctx)
134+
slog := utils.LogFromContext(ctx)
136135

137136
reqnsn := utils.Keyname(obj.Namespace, obj.Name)
138-
zlog.V(1).Info("handling + upserting " + reqnsn)
137+
slog.Debugf("handling + upserting " + reqnsn)
139138

140139
ogw := pkg.ActiveSIGs.GetGateway(reqnsn)
141140
if ogw == nil {
@@ -152,7 +151,7 @@ func handleUpsertingGateway(ctx context.Context, obj *gatewayv1beta1.Gateway) (c
152151
opcfgs, npcfgs := map[string]interface{}{}, map[string]interface{}{}
153152
ocfgs, err = pkg.ParseGatewayRelatedForClass(string(ogw.Spec.GatewayClassName), []*gatewayv1beta1.Gateway{ogw})
154153
if err != nil {
155-
zlog.Error(err, "handling + upserting + parse related ocfgs "+reqnsn)
154+
slog.Errorf("handling + upserting + parse related ocfgs: %s %s", reqnsn, err.Error())
156155
return ctrl.Result{}, err
157156
}
158157
opcfgs, err = pkg.ParseServicesRelatedForAll()
@@ -164,7 +163,7 @@ func handleUpsertingGateway(ctx context.Context, obj *gatewayv1beta1.Gateway) (c
164163

165164
ncfgs, err = pkg.ParseGatewayRelatedForClass(string(ngw.Spec.GatewayClassName), []*gatewayv1beta1.Gateway{ngw})
166165
if err != nil {
167-
zlog.Error(err, "handling + upserting + parse related ncfgs "+reqnsn)
166+
slog.Errorf("handling + upserting + parse related ncfgs: %s %s", reqnsn, err.Error())
168167
return ctrl.Result{}, err
169168
}
170169
npcfgs, err = pkg.ParseServicesRelatedForAll()

controllers/gatewayclass_controller.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"k8s.io/apimachinery/pkg/runtime"
2828
ctrl "sigs.k8s.io/controller-runtime"
2929
"sigs.k8s.io/controller-runtime/pkg/client"
30-
"sigs.k8s.io/controller-runtime/pkg/log"
3130

3231
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3332
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
@@ -48,8 +47,8 @@ type GatewayClassReconciler struct {
4847
// For more details, check Reconcile and its Result here:
4948
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
5049
func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
51-
zlog := log.FromContext(ctx)
5250
lctx := context.WithValue(ctx, utils.CtxKey_Logger, utils.NewLog(uuid.New().String(), "debug"))
51+
slog := utils.LogFromContext(lctx)
5352
if req.Namespace != "" {
5453
return ctrl.Result{}, fmt.Errorf("gateway class namespace must be ''")
5554
}
@@ -60,7 +59,7 @@ func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request
6059
}
6160

6261
var obj gatewayv1beta1.GatewayClass
63-
zlog.V(1).Info("handling gatewayclass " + req.Name)
62+
slog.Debugf("handling gatewayclass " + req.Name)
6463
if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
6564
if client.IgnoreNotFound(err) == nil {
6665
defer pkg.ActiveSIGs.UnsetGatewayClass(req.Name)
@@ -85,10 +84,10 @@ func (r *GatewayClassReconciler) Reconcile(ctx context.Context, req ctrl.Request
8584
// 1.671457016981118e+09 DEBUG handling gatewayclass bigip {"controller": "gatewayclass", "controllerGroup": "gateway.networking.k8s.io", "controllerKind": "GatewayClass", "GatewayClass": {"name":"bigip"}, "namespace": "", "name": "bigip", "reconcileID": "bcd01fdd-8dfc-4be9-b3ab-c3d3a3fdb67e"}
8685
// 1.6714570170091689e+09 ERROR unable to update status {"controller": "gatewayclass", "controllerGroup": "gateway.networking.k8s.io", "controllerKind": "GatewayClass", "GatewayClass": {"name":"bigip"}, "namespace": "", "name": "bigip", "reconcileID": "bcd01fdd-8dfc-4be9-b3ab-c3d3a3fdb67e", "error": "Operation cannot be fulfilled on gatewayclasses.gateway.networking.k8s.io \"bigip\": the object has been modified; please apply your changes to the latest version and try again"}
8786
if err := r.Status().Update(ctx, ngwc); err != nil {
88-
zlog.V(1).Error(err, "unable to update status")
87+
slog.Errorf("unable to update status: %s", err.Error())
8988
return ctrl.Result{}, err
9089
} else {
91-
zlog.V(1).Info("status updated")
90+
slog.Debugf("status updated")
9291
}
9392

9493
// upsert gatewayclass
@@ -105,8 +104,8 @@ func (r *GatewayClassReconciler) SetupWithManager(mgr ctrl.Manager) error {
105104
}
106105

107106
func handleDeletingGatewayClass(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
108-
zlog := log.FromContext(ctx)
109-
zlog.V(1).Info("deleting gatewayclass " + req.Name)
107+
slog := utils.LogFromContext(ctx)
108+
slog.Debugf("deleting gatewayclass " + req.Name)
110109

111110
gwc := pkg.ActiveSIGs.GetGatewayClass(req.Name)
112111
if gwc == nil {
@@ -156,14 +155,14 @@ func handleDeletingGatewayClass(ctx context.Context, req ctrl.Request) (ctrl.Res
156155
}
157156

158157
func handleUpsertingGatewayClass(ctx context.Context, obj *gatewayv1beta1.GatewayClass) (ctrl.Result, error) {
159-
zlog := log.FromContext(ctx)
158+
slog := utils.LogFromContext(ctx)
160159

161160
reqn := utils.Keyname(obj.Namespace, obj.Name)
162-
zlog.V(1).Info("upserting gatewayclass " + reqn)
161+
slog.Debugf("upserting gatewayclass " + reqn)
163162
ngwc := obj.DeepCopy()
164163

165164
if ngwc.Spec.ControllerName != gatewayv1beta1.GatewayController(pkg.ActiveSIGs.ControllerName) {
166-
zlog.V(1).Info("ignore this gwc " + reqn + " as its controllerName does not match " + pkg.ActiveSIGs.ControllerName)
165+
slog.Debugf("ignore this gwc " + reqn + " as its controllerName does not match " + pkg.ActiveSIGs.ControllerName)
167166
return ctrl.Result{}, nil
168167
}
169168

controllers/httproute_controller.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"k8s.io/apimachinery/pkg/runtime"
2828
ctrl "sigs.k8s.io/controller-runtime"
2929
"sigs.k8s.io/controller-runtime/pkg/client"
30-
"sigs.k8s.io/controller-runtime/pkg/log"
3130

3231
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
3332
)
@@ -47,16 +46,16 @@ type HttpRouteReconciler struct {
4746
// For more details, check Reconcile and its Result here:
4847
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
4948
func (r *HttpRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
50-
zlog := log.FromContext(ctx)
5149
lctx := context.WithValue(ctx, utils.CtxKey_Logger, utils.NewLog(uuid.New().String(), "debug"))
50+
slog := utils.LogFromContext(lctx)
5251
if !pkg.ActiveSIGs.SyncedAtStart {
5352
<-time.After(100 * time.Millisecond)
5453
return ctrl.Result{Requeue: true}, nil
5554
}
5655

5756
var obj gatewayv1beta1.HTTPRoute
5857

59-
zlog.V(1).Info("handling " + req.NamespacedName.String())
58+
slog.Debugf("handling " + req.NamespacedName.String())
6059
if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
6160
if client.IgnoreNotFound(err) == nil {
6261
// delete resources
@@ -85,7 +84,6 @@ func (r *HttpRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
8584
}
8685

8786
func handleDeletingHTTPRoute(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
88-
// zlog := log.FromContext(ctx)
8987
hr := pkg.ActiveSIGs.GetHTTPRoute(req.NamespacedName.String())
9088
gws := pkg.ActiveSIGs.GatewayRefsOf(hr)
9189
drs := map[string]*pkg.DeployRequest{}
@@ -158,9 +156,9 @@ func handleDeletingHTTPRoute(ctx context.Context, req ctrl.Request) (ctrl.Result
158156
}
159157

160158
func handleUpsertingHTTPRoute(ctx context.Context, obj *gatewayv1beta1.HTTPRoute) (ctrl.Result, error) {
161-
zlog := log.FromContext(ctx)
159+
slog := utils.LogFromContext(ctx)
162160
reqnsn := utils.Keyname(obj.Namespace, obj.Name)
163-
zlog.V(1).Info("upserting " + reqnsn)
161+
slog.Debugf("upserting " + reqnsn)
164162

165163
hr := pkg.ActiveSIGs.GetHTTPRoute(reqnsn)
166164
gws := pkg.ActiveSIGs.GatewayRefsOf(hr)

controllers/v1_controller.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"k8s.io/apimachinery/pkg/runtime"
3030
ctrl "sigs.k8s.io/controller-runtime"
3131
"sigs.k8s.io/controller-runtime/pkg/client"
32-
"sigs.k8s.io/controller-runtime/pkg/log"
3332

3433
v1 "k8s.io/api/core/v1"
3534
)
@@ -52,9 +51,8 @@ type NodeReconciler struct {
5251
func (r *EndpointsReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
5352
lctx := context.WithValue(ctx, utils.CtxKey_Logger, utils.NewLog(uuid.New().String(), "debug"))
5453
var obj v1.Endpoints
55-
// zlog := log.FromContext(ctx)
5654
// // too many logs.
57-
// zlog.V(1).Info("endpoint event: " + req.NamespacedName.String())
55+
// slog.Debugf("endpoint event: " + req.NamespacedName.String())
5856
if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
5957
if client.IgnoreNotFound(err) == nil {
6058
defer pkg.ActiveSIGs.UnsetEndpoints(req.NamespacedName.String())
@@ -69,11 +67,10 @@ func (r *EndpointsReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
6967
}
7068

7169
func (r *ServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
72-
7370
var obj v1.Service
74-
zlog := log.FromContext(ctx)
7571
lctx := context.WithValue(ctx, utils.CtxKey_Logger, utils.NewLog(uuid.New().String(), "debug"))
76-
zlog.V(1).Info("Service event: " + req.NamespacedName.String())
72+
slog := utils.LogFromContext(lctx)
73+
slog.Debugf("Service event: " + req.NamespacedName.String())
7774
if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
7875
if client.IgnoreNotFound(err) == nil {
7976
defer pkg.ActiveSIGs.UnsetService(req.NamespacedName.String())
@@ -98,8 +95,6 @@ func (r *NodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
9895
ncfgs := map[string]interface{}{}
9996

10097
var obj v1.Node
101-
// zlog := log.FromContext(ctx)
102-
// zlog.V(1).Info("resource event: " + req.NamespacedName.String())
10398
if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
10499
if client.IgnoreNotFound(err) == nil {
105100
k8s.NodeCache.Unset(req.Name)

main.go

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2929
// to ensure that exec-entrypoint and run can make use of them.
3030

31+
"github.com/google/uuid"
3132
"github.com/prometheus/client_golang/prometheus"
3233
"github.com/prometheus/client_golang/prometheus/promhttp"
3334
"gopkg.in/yaml.v3"
@@ -72,8 +73,6 @@ func main() {
7273
credsDir string
7374
confDir string
7475
controllerName string
75-
// mode string
76-
// vxlanTunnelName string
7776
)
7877

7978
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
@@ -94,25 +93,11 @@ func main() {
9493
flag.Parse()
9594

9695
pkg.ActiveSIGs.ControllerName = controllerName
97-
98-
// TODO: the filenames must be 'bigip-kubernetes-gateway-config' and 'password'
99-
if confDir == "" || credsDir == "" {
100-
err := fmt.Errorf("missing BIG-IP credential/configuration parameters")
101-
setupLog.Error(err, "Missing BIG-IP credentials info: %s", err.Error())
102-
panic(err)
103-
}
104-
if err := getCredentials(&pkg.BIPPassword, credsDir); err != nil {
105-
panic(err)
106-
}
107-
if err := getConfigs(&pkg.BIPConfigs, confDir); err != nil {
108-
panic(err)
109-
}
110-
if err := setupBIGIPs(); err != nil {
111-
panic(err)
96+
if err := setupBIGIPs(credsDir, confDir); err != nil {
97+
setupLog.Error(err, "failed to setup BIG-IPs")
98+
os.Exit(1)
11299
}
113-
114100
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
115-
116101
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
117102
Scheme: scheme,
118103
MetricsBindAddress: metricsAddr,
@@ -141,15 +126,9 @@ func main() {
141126
prometheus.MustRegister(utils.FunctionDurationTimeCostTotal)
142127
prometheus.MustRegister(f5_bigip.BIGIPiControlTimeCostCount)
143128
prometheus.MustRegister(f5_bigip.BIGIPiControlTimeCostTotal)
144-
145129
mgr.AddMetricsExtraHandler("/stats", promhttp.Handler())
146130

147-
stopCh := make(chan struct{})
148-
149-
go pkg.Deployer(stopCh, pkg.BIGIPs)
150-
151131
setupReconcilers(mgr)
152-
153132
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
154133
setupLog.Error(err, "unable to set up health check")
155134
os.Exit(1)
@@ -159,6 +138,8 @@ func main() {
159138
os.Exit(1)
160139
}
161140

141+
stopCh := make(chan struct{})
142+
go pkg.Deployer(stopCh, pkg.BIGIPs)
162143
go pkg.ActiveSIGs.SyncAllResources(mgr)
163144
go applyNodeConfigsAtStart()
164145

@@ -167,7 +148,6 @@ func main() {
167148
setupLog.Error(err, "problem running manager")
168149
os.Exit(1)
169150
}
170-
171151
}
172152

173153
func getCredentials(bigipPassword *string, credsDir string) error {
@@ -210,6 +190,7 @@ func applyNodeConfigsAtStart() {
210190
}
211191
}
212192

193+
lctx := context.WithValue(context.TODO(), utils.CtxKey_Logger, utils.NewLog(uuid.New().String(), "debug"))
213194
for _, c := range pkg.BIPConfigs {
214195
if ncfgs, err := pkg.ParseNodeConfigs(&c); err != nil {
215196
setupLog.Error(err, "unable to parse nodes config for net setup")
@@ -225,7 +206,7 @@ func applyNodeConfigsAtStart() {
225206
To: &ncfgs,
226207
StatusFunc: func() {},
227208
Partition: "Common",
228-
Context: context.WithValue(context.TODO(), pkg.CtxKey_SpecifiedBIGIP, url),
209+
Context: context.WithValue(lctx, pkg.CtxKey_SpecifiedBIGIP, url),
229210
}
230211
}
231212
}
@@ -261,7 +242,15 @@ func setupReconcilers(mgr manager.Manager) {
261242
}
262243
}
263244

264-
func setupBIGIPs() error {
245+
func setupBIGIPs(credsDir, confDir string) error {
246+
// TODO: the filenames must be 'bigip-kubernetes-gateway-config' and 'password'
247+
if err := getCredentials(&pkg.BIPPassword, credsDir); err != nil {
248+
return err
249+
}
250+
if err := getConfigs(&pkg.BIPConfigs, confDir); err != nil {
251+
return err
252+
}
253+
265254
errs := []string{}
266255
for i, c := range pkg.BIPConfigs {
267256
if c.Management == nil {

0 commit comments

Comments
 (0)