Skip to content

Commit a5d4618

Browse files
authored
fix/cpu-load (#144)
* Updated clusterrole.yaml to include permissions for coordination.k8s.io leases, modified daemonset.yaml for rolling update strategy and added tolerations, and adjusted resource limits in values.yaml. * Updated main.go to introduce a non-blocking delay with loopTicker for better CPU usage efficiency. * Updated resource requests and limits for CPU and memory in AWS EKS and GCP GKE configurations.
1 parent b6fb907 commit a5d4618

File tree

6 files changed

+53
-42
lines changed

6 files changed

+53
-42
lines changed

chart/templates/clusterrole.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ rules:
1111
verbs: [ "get" ]
1212
- apiGroups: [ "coordination.k8s.io" ]
1313
resources: [ "leases" ]
14-
verbs: [ "create", "get", "delete" ]
14+
verbs: [ "create", "delete", "get" ]
1515
{{- end }}

chart/templates/daemonset.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ spec:
88
selector:
99
matchLabels:
1010
app.kubernetes.io/name: {{ include "kubeip.name" . }}
11+
updateStrategy:
12+
type: RollingUpdate
13+
rollingUpdate:
14+
maxUnavailable: 1
1115
template:
1216
metadata:
1317
labels:
@@ -20,6 +24,11 @@ spec:
2024
{{- if .Values.daemonSet.nodeSelector }}
2125
{{- toYaml .Values.daemonSet.nodeSelector | nindent 8 }}
2226
{{- end }}
27+
tolerations:
28+
- operator: "Exists"
29+
effect: "NoSchedule"
30+
- operator: "Exists"
31+
effect: "NoExecute"
2332
containers:
2433
- name: kubeip
2534
image: "{{ .Values.image.repository }}"

chart/values.yaml

+8-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ serviceAccount:
1717
name: kubeip-service-account
1818
annotations:
1919
gcpServiceAccountEmail: kubeip-service-account@workload-id-117715.iam.gserviceaccount.com
20-
# annotations:
21-
# awsRoleArn: "your-aws-role-arn"
22-
# gcpServiceAccountEmail: "your-google-service-account-email"
23-
20+
# annotations:
21+
# awsRoleArn: "your-aws-role-arn"
22+
# gcpServiceAccountEmail: "your-google-service-account-email"
23+
2424

2525
# Role-Based Access Control (RBAC) configuration.
2626
rbac:
@@ -40,3 +40,7 @@ daemonSet:
4040
resources:
4141
requests:
4242
cpu: 100m
43+
memory: 64Mi
44+
limits:
45+
cpu: 100m
46+
memory: 128Mi

cmd/main.go

+31-35
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func assignAddress(c context.Context, log *logrus.Entry, client kubernetes.Inter
139139
func run(c context.Context, log *logrus.Entry, cfg *config.Config) error {
140140
ctx, cancel := context.WithCancel(c)
141141
defer cancel()
142+
142143
// add debug mode to context
143144
if cfg.DevelopMode {
144145
ctx = context.WithValue(ctx, developModeKey, true)
@@ -167,43 +168,37 @@ func run(c context.Context, log *logrus.Entry, cfg *config.Config) error {
167168
if err != nil {
168169
return errors.Wrap(err, "initializing assigner")
169170
}
170-
// assign static public IP address
171-
errorCh := make(chan error, 1) // buffered channel to avoid goroutine leak
172-
go func() {
173-
defer close(errorCh) // close the channel when the goroutine exits to avoid goroutine leak
174-
e := assignAddress(ctx, log, clientset, assigner, n, cfg)
175-
if e != nil {
176-
errorCh <- e
177-
}
178-
}()
179171

180-
for {
181-
select {
182-
case err = <-errorCh:
183-
if err != nil {
184-
return errors.Wrap(err, "assigning static public IP address")
185-
}
186-
case <-ctx.Done():
187-
log.Infof("kubeip agent gracefully stopped")
188-
if cfg.ReleaseOnExit {
189-
log.Infof("releasing static public IP address")
190-
err = func() error {
191-
releaseCtx, releaseCancel := context.WithTimeout(context.Background(), unassignTimeout) // release the static public IP address within 5 minutes
192-
defer releaseCancel()
193-
// use a different context for releasing the static public IP address since the main context is canceled
194-
if err = assigner.Unassign(releaseCtx, n.Instance, n.Zone); err != nil {
195-
return errors.Wrap(err, "failed to release static public IP address")
196-
}
197-
return nil
198-
}()
199-
if err != nil {
200-
return err //nolint:wrapcheck
201-
}
202-
log.Infof("static public IP address released")
203-
}
204-
return nil
172+
err = assignAddress(ctx, log, clientset, assigner, n, cfg)
173+
if err != nil {
174+
return errors.Wrap(err, "assigning static public IP address")
175+
}
176+
177+
// pause the agent to prevent it from exiting immediately after assigning the static public IP address
178+
// wait for the context to be done: SIGTERM, SIGINT
179+
<-ctx.Done()
180+
log.Infof("shutting down kubeip agent")
181+
182+
// release the static public IP address on exit
183+
if cfg.ReleaseOnExit {
184+
log.Infof("releasing static public IP address")
185+
if releaseErr := releaseIP(assigner, n); releaseErr != nil { //nolint:contextcheck
186+
return releaseErr
205187
}
188+
log.Infof("static public IP address released")
206189
}
190+
return nil
191+
}
192+
193+
func releaseIP(assigner address.Assigner, n *types.Node) error {
194+
releaseCtx, releaseCancel := context.WithTimeout(context.Background(), unassignTimeout)
195+
defer releaseCancel()
196+
197+
if err := assigner.Unassign(releaseCtx, n.Instance, n.Zone); err != nil {
198+
return errors.Wrap(err, "failed to release static public IP address")
199+
}
200+
201+
return nil
207202
}
208203

209204
func runCmd(c *cli.Context) error {
@@ -213,7 +208,8 @@ func runCmd(c *cli.Context) error {
213208
cfg := config.NewConfig(c)
214209

215210
if err := run(ctx, log, cfg); err != nil {
216-
log.Fatalf("eks-lens agent failed: %v", err)
211+
log.WithError(err).Error("error running kubeip agent")
212+
return err
217213
}
218214

219215
return nil

examples/aws/eks.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ resource "kubernetes_daemonset" "kubeip_daemonset" {
260260
}
261261
resources {
262262
requests = {
263-
cpu = "100m"
263+
cpu = "10m"
264+
memory = "32Mi"
264265
}
265266
}
266267
}

examples/gcp/gke.tf

+2-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ resource "kubernetes_daemonset" "kubeip_daemonset" {
336336
}
337337
resources {
338338
requests = {
339-
cpu = "100m"
339+
cpu = "10m"
340+
memory = "32Mi"
340341
}
341342
}
342343
}

0 commit comments

Comments
 (0)