Skip to content

Commit 3e33b61

Browse files
authored
Merge pull request #589 from jetstack/VC-35568-events-to-pod
VC-35568: Feature: Send errors to the pod's event to increase visibility
2 parents 64a9255 + bc2958c commit 3e33b61

File tree

5 files changed

+170
-61
lines changed

5 files changed

+170
-61
lines changed

deploy/charts/venafi-kubernetes-agent/templates/deployment.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ spec:
8989
{{- with .Values.volumeMounts }}
9090
{{- toYaml . | nindent 12 }}
9191
{{- end }}
92+
env:
93+
- name: POD_NAMESPACE
94+
valueFrom:
95+
fieldRef:
96+
fieldPath: metadata.namespace
97+
- name: POD_NAME
98+
valueFrom:
99+
fieldRef:
100+
fieldPath: metadata.name
101+
- name: POD_UID
102+
valueFrom:
103+
fieldRef:
104+
fieldPath: metadata.uid
105+
- name: POD_NODE
106+
valueFrom:
107+
fieldRef:
108+
fieldPath: spec.nodeName
92109
{{- if .Values.metrics.enabled }}
93110
ports:
94111
- containerPort: 8081

deploy/charts/venafi-kubernetes-agent/templates/rbac.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
---
22
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: Role
4+
metadata:
5+
name: {{ include "venafi-kubernetes-agent.fullname" . }}-event-emitted
6+
labels:
7+
{{- include "venafi-kubernetes-agent.labels" . | nindent 4 }}
8+
rules:
9+
- apiGroups: [""]
10+
resources: ["events"]
11+
verbs: ["create"]
12+
---
13+
apiVersion: rbac.authorization.k8s.io/v1
14+
kind: RoleBinding
15+
metadata:
16+
name: {{ include "venafi-kubernetes-agent.fullname" . }}-event-emitted
17+
labels:
18+
{{- include "venafi-kubernetes-agent.labels" . | nindent 4 }}
19+
roleRef:
20+
apiGroup: rbac.authorization.k8s.io
21+
kind: Role
22+
name: {{ include "venafi-kubernetes-agent.fullname" . }}-event-emitted
23+
subjects:
24+
- kind: ServiceAccount
25+
name: {{ include "venafi-kubernetes-agent.serviceAccountName" . }}
26+
namespace: {{ .Release.Namespace }}
27+
---
28+
apiVersion: rbac.authorization.k8s.io/v1
329
kind: ClusterRoleBinding
430
metadata:
531
name: {{ include "venafi-kubernetes-agent.fullname" . }}-cluster-viewer

pkg/agent/config.go

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ import (
2323
"github.com/jetstack/preflight/pkg/version"
2424
)
2525

26-
const (
27-
inClusterNamespacePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
28-
)
29-
3026
// Config wraps the options for a run of the agent.
3127
type Config struct {
3228
// Deprecated: Schedule doesn't do anything. Use `period` instead.
@@ -154,9 +150,8 @@ type AgentCmdFlags struct {
154150
// InstallNS (--install-namespace) is the namespace in which the agent is
155151
// running in. Only needed when running the agent outside of Kubernetes.
156152
//
157-
// May be left empty when running in Kubernetes. In this case, the namespace
158-
// is read from the file
159-
// /var/run/secrets/kubernetes.io/serviceaccount/namespace.
153+
// May be left empty when running in Kubernetes. In Kubernetes, the
154+
// namespace is read from the environment variable `POD_NAMESPACE`.
160155
InstallNS string
161156

162157
// Profiling (--enable-pprof) enables the pprof server.
@@ -273,8 +268,7 @@ func InitAgentCmdFlags(c *cobra.Command, cfg *AgentCmdFlags) {
273268
"install-namespace",
274269
"",
275270
"For testing purposes. Namespace in which the agent is running. "+
276-
"Only needed with the "+string(VenafiCloudVenafiConnection)+" mode"+
277-
"when running the agent outside of Kubernetes.",
271+
"Only needed when running the agent outside of Kubernetes.",
278272
)
279273
c.PersistentFlags().BoolVarP(
280274
&cfg.Profiling,
@@ -314,6 +308,7 @@ type CombinedConfig struct {
314308
BackoffMaxTime time.Duration
315309
StrictMode bool
316310
OneShot bool
311+
InstallNS string
317312

318313
// Used by JetstackSecureOAuth, JetstackSecureAPIToken, and
319314
// VenafiCloudKeypair. Ignored in VenafiCloudVenafiConnection mode.
@@ -330,7 +325,6 @@ type CombinedConfig struct {
330325
// VenafiCloudVenafiConnection mode only.
331326
VenConnName string
332327
VenConnNS string
333-
InstallNS string
334328

335329
// Only used for testing purposes.
336330
OutputPath string
@@ -530,20 +524,20 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
530524
res.StrictMode = flags.StrictMode
531525
}
532526

533-
// Validation of --venafi-connection, --venafi-connection-namespace, and
534-
// --install-namespace.
535-
if res.AuthMode == VenafiCloudVenafiConnection {
536-
var installNS string = flags.InstallNS
537-
if flags.InstallNS == "" {
538-
var err error
539-
installNS, err = getInClusterNamespace()
540-
if err != nil {
541-
errs = multierror.Append(errs, fmt.Errorf("could not guess which namespace the agent is running in: %w", err))
542-
}
527+
// Validation of --install-namespace.
528+
var installNS string = flags.InstallNS
529+
if flags.InstallNS == "" {
530+
var err error
531+
installNS, err = getInClusterNamespace()
532+
if err != nil {
533+
errs = multierror.Append(errs, fmt.Errorf("could not guess which namespace the agent is running in: %w", err))
543534
}
544-
res.InstallNS = installNS
545-
res.VenConnName = flags.VenConnName
535+
}
536+
res.InstallNS = installNS
546537

538+
// Validation of --venafi-connection and --venafi-connection-namespace.
539+
if res.AuthMode == VenafiCloudVenafiConnection {
540+
res.VenConnName = flags.VenConnName
547541
var venConnNS string = flags.VenConnNS
548542
if flags.VenConnNS == "" {
549543
venConnNS = installNS
@@ -727,21 +721,12 @@ func createCredentialClient(log *log.Logger, credentials client.Credentials, cfg
727721

728722
// Inspired by the controller-runtime project.
729723
func getInClusterNamespace() (string, error) {
730-
// Check whether the namespace file exists.
731-
// If not, we are not running in cluster so can't guess the namespace.
732-
_, err := os.Stat(inClusterNamespacePath)
733-
if os.IsNotExist(err) {
734-
return "", fmt.Errorf("not running in cluster, please use --install-namespace to specify the namespace in which the agent is running")
735-
}
736-
if err != nil {
737-
return "", fmt.Errorf("error checking namespace file: %w", err)
724+
ns := os.Getenv("POD_NAMESPACE")
725+
if ns != "" {
726+
return ns, nil
738727
}
739728

740-
namespace, err := os.ReadFile(inClusterNamespacePath)
741-
if err != nil {
742-
return "", fmt.Errorf("error reading namespace file: %w", err)
743-
}
744-
return string(namespace), nil
729+
return "", fmt.Errorf("POD_NAMESPACE env var not set, meaning that you are probably not running in cluster. Please use --install-namespace or POD_NAMESPACE to specify the namespace in which the agent is running.")
745730
}
746731

747732
func reMarshal(rawConfig interface{}, config datagatherer.Config) error {

0 commit comments

Comments
 (0)