-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add multi-namespace support #124
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,5 @@ | ||||||||||||||
apiVersion: rbac.authorization.k8s.io/v1 | ||||||||||||||
kind: Role | ||||||||||||||
kind: ClusterRole | ||||||||||||||
metadata: | ||||||||||||||
name: coder-logstream-kube-role | ||||||||||||||
rules: | ||||||||||||||
|
@@ -18,16 +18,17 @@ metadata: | |||||||||||||
labels: {{ toYaml .Values.serviceAccount.labels | nindent 4 }} | ||||||||||||||
--- | ||||||||||||||
apiVersion: rbac.authorization.k8s.io/v1 | ||||||||||||||
kind: RoleBinding | ||||||||||||||
kind: ClusterRoleBinding | ||||||||||||||
metadata: | ||||||||||||||
name: coder-logstream-kube-rolebinding | ||||||||||||||
roleRef: | ||||||||||||||
apiGroup: rbac.authorization.k8s.io | ||||||||||||||
kind: Role | ||||||||||||||
kind: ClusterRole | ||||||||||||||
name: coder-logstream-kube-role | ||||||||||||||
subjects: | ||||||||||||||
- kind: ServiceAccount | ||||||||||||||
name: {{ .Values.serviceAccount.name | quote }} | ||||||||||||||
namespace: {{ .Release.Namespace }} | ||||||||||||||
--- | ||||||||||||||
apiVersion: apps/v1 | ||||||||||||||
kind: Deployment | ||||||||||||||
|
@@ -75,8 +76,8 @@ spec: | |||||||||||||
env: | ||||||||||||||
- name: CODER_URL | ||||||||||||||
value: {{ .Values.url }} | ||||||||||||||
- name: CODER_NAMESPACE | ||||||||||||||
value: {{ .Values.namespace | default .Release.Namespace }} | ||||||||||||||
- name: CODER_NAMESPACES | ||||||||||||||
value: {{ if .Values.namespaces }}{{ join "," .Values.namespaces }}{{ else }}{{ end }} | ||||||||||||||
|
- name: CODER_NAMESPACES | |
value: {{ if .Values.namespaces }}{{ join "," .Values.namespaces }}{{ else }}{{ end }} | |
{{ if .Values.namespaces -}} | |
- name: CODER_NAMESPACES | |
value: "{{ join "," .Values.namespaces }}" | |
{{ end -}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
# url -- The URL of your Coder deployment. Must prefix with http or https | ||
url: "" | ||
|
||
# namespace -- The namespace to searching for Pods within. | ||
# If unspecified, this defaults to the Helm namespace. | ||
namespace: "" | ||
# namespace -- List of namespaces to search for Pods within. | ||
# If unspecified or empty it will watch all namespaces. | ||
namespaces: [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is breaking so make sure you inform customers in the release notes. |
||
|
||
# volumes -- A list of extra volumes to add to the coder-logstream pod. | ||
volumes: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ type podEventLoggerOptions struct { | |
logDebounce time.Duration | ||
|
||
// The following fields are optional! | ||
namespace string | ||
namespaces []string | ||
fieldSelector string | ||
labelSelector string | ||
} | ||
|
@@ -78,7 +78,18 @@ func newPodEventLogger(ctx context.Context, opts podEventLoggerOptions) (*podEve | |
}, | ||
} | ||
|
||
return reporter, reporter.init() | ||
// If no namespaces are provided, we listen for events in all namespaces. | ||
if len(opts.namespaces) == 0 { | ||
reporter.initNamespace("") | ||
} else { | ||
for _, namespace := range opts.namespaces { | ||
if err := reporter.initNamespace(namespace); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
|
||
return reporter, nil | ||
} | ||
|
||
type podEventLogger struct { | ||
|
@@ -96,21 +107,21 @@ type podEventLogger struct { | |
} | ||
|
||
// init starts the informer factory and registers event handlers. | ||
func (p *podEventLogger) init() error { | ||
func (p *podEventLogger) initNamespace(namespace string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should include a sentence in the method comment explaining the |
||
// We only track events that happen after the reporter starts. | ||
// This is to prevent us from sending duplicate events. | ||
startTime := time.Now() | ||
|
||
go p.lq.work(p.ctx) | ||
|
||
podFactory := informers.NewSharedInformerFactoryWithOptions(p.client, 0, informers.WithNamespace(p.namespace), informers.WithTweakListOptions(func(lo *v1.ListOptions) { | ||
podFactory := informers.NewSharedInformerFactoryWithOptions(p.client, 0, informers.WithNamespace(namespace), informers.WithTweakListOptions(func(lo *v1.ListOptions) { | ||
lo.FieldSelector = p.fieldSelector | ||
lo.LabelSelector = p.labelSelector | ||
})) | ||
eventFactory := podFactory | ||
if p.fieldSelector != "" || p.labelSelector != "" { | ||
// Events cannot filter on labels and fields! | ||
eventFactory = informers.NewSharedInformerFactoryWithOptions(p.client, 0, informers.WithNamespace(p.namespace)) | ||
eventFactory = informers.NewSharedInformerFactoryWithOptions(p.client, 0, informers.WithNamespace(namespace)) | ||
} | ||
|
||
// We listen for Pods and Events in the informer factory. | ||
|
@@ -277,7 +288,7 @@ func (p *podEventLogger) init() error { | |
|
||
p.logger.Info(p.ctx, "listening for pod events", | ||
slog.F("coder_url", p.coderURL.String()), | ||
slog.F("namespace", p.namespace), | ||
slog.F("namespace", namespace), | ||
slog.F("field_selector", p.fieldSelector), | ||
slog.F("label_selector", p.labelSelector), | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If namespaces are specified, this should probably be a for loop to create the role and role binding in each namespace, rather than granting full cluster permissions
You should probably make the permissions block a reusable template though