Skip to content

Commit 65cd51c

Browse files
committed
Refactor merge logic and improve secret validation
Add helper functions for security item detection (isSecurityItem, isSecurityAnnotation) Extract filter/extract operations into reusable functions for volumes, annotations, and mounts Improve SPP validation to fail-closed (reject on parse errors instead of allowing) Refactor parseValidSppsFromSecret to reuse ptp4lConf parser for consistency Add detailed comments explaining secret hash change detection mechanism Downgrade controller-gen from v0.19.0 to v0.15.0 for compatibility Add kubebuilder marker to skip auto-generation for Ptp4lConf type Regenerate CRDs and RBAC with ptpSecretName field support
1 parent 8b14b1d commit 65cd51c

18 files changed

+424
-309
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ ENVTEST ?= $(LOCALBIN)/setup-envtest
155155

156156
## Tool Versions
157157
KUSTOMIZE_VERSION ?= v4.5.7
158-
CONTROLLER_TOOLS_VERSION ?= v0.19.0
158+
CONTROLLER_TOOLS_VERSION ?= v0.15.0
159159

160160
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
161161
.PHONY: kustomize

api/v1/ptpconfig_webhook.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type ptp4lConf struct {
7171
sections map[string]ptp4lConfSection
7272
}
7373

74+
// +kubebuilder:object:generate=false
7475
// Ptp4lConf is a public wrapper for ptp4lConf
7576
type Ptp4lConf struct {
7677
conf ptp4lConf
@@ -390,9 +391,12 @@ func (r *PtpConfig) validateSppInSecret(profile PtpProfile, secret *corev1.Secre
390391
// Parse the secret data to find valid SPP values
391392
validSpps, err := parseValidSppsFromSecret(secret)
392393
if err != nil {
393-
ptpconfiglog.Error(err, "failed to parse SPPs from secret", "secret", secret.Name, "profile", profileName)
394-
// Fail open - don't block if we can't parse
395-
return nil
394+
return fmt.Errorf("failed to parse SPPs from secret '%s' for profile '%s': %v", secret.Name, profileName, err)
395+
}
396+
397+
// If no SPPs found in secret, that's an error
398+
if len(validSpps) == 0 {
399+
return fmt.Errorf("no SPPs defined in secret '%s'. Secret must contain 'spp <number>' lines in security associations", secret.Name)
396400
}
397401

398402
// Check if the specified SPP exists in the secret
@@ -412,33 +416,25 @@ func parseValidSppsFromSecret(secret *corev1.Secret) ([]string, error) {
412416

413417
// Iterate through all keys in the secret
414418
for key, value := range secret.Data {
415-
// Parse the security configuration
419+
// Parse the security configuration using existing ptp4l parser
416420
content := string(value)
417-
lines := strings.Split(content, "\n")
418-
419-
// Look for any line starting with "spp <number>"
420-
for _, line := range lines {
421-
line = strings.TrimSpace(line)
422-
423-
// Skip empty lines and comments
424-
if line == "" || strings.HasPrefix(line, "#") {
425-
continue
426-
}
421+
conf := &ptp4lConf{}
422+
if err := conf.populatePtp4lConf(&content, nil); err != nil {
423+
// If parsing fails, return error (strict validation)
424+
return nil, fmt.Errorf("failed to parse security file format: %v", err)
425+
}
427426

428-
// Check if line starts with "spp " (case-insensitive)
429-
if strings.HasPrefix(strings.ToLower(line), "spp ") {
430-
parts := strings.Fields(line)
431-
if len(parts) >= 2 {
432-
sppNumber := parts[1]
433-
// Add if not already in list
434-
if !contains(validSpps, sppNumber) {
435-
validSpps = append(validSpps, sppNumber)
436-
}
427+
// Extract SPP values from all [security_association] sections
428+
for sectionName, section := range conf.sections {
429+
// Look for security_association sections or any section with spp
430+
if sppValue, exists := section.options["spp"]; exists {
431+
// Found an spp value
432+
if !contains(validSpps, sppValue) {
433+
validSpps = append(validSpps, sppValue)
434+
ptpconfiglog.Info("found SPP in secret", "section", sectionName, "spp", sppValue, "key", key)
437435
}
438436
}
439437
}
440-
441-
ptpconfiglog.Info("parsed SPPs from secret", "key", key, "spps", validSpps)
442438
}
443439

444440
return validSpps, nil

bundle/manifests/ptp-operator.clusterserviceversion.yaml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ metadata:
6161
categories: Networking
6262
certified: "false"
6363
containerImage: quay.io/openshift/origin-ptp-operator:4.21
64-
createdAt: "2025-11-03T12:52:29Z"
64+
createdAt: "2025-11-18T22:08:29Z"
6565
description: This software enables configuration of Precision Time Protocol(PTP)
6666
on Kubernetes. It detects hardware capable PTP devices on each node, and configures
6767
linuxptp processes such as ptp4l, phc2sys and timemaster.
@@ -280,6 +280,24 @@ spec:
280280
- rolebindings
281281
verbs:
282282
- '*'
283+
- apiGroups:
284+
- ""
285+
resources:
286+
- secrets
287+
verbs:
288+
- get
289+
- list
290+
- watch
291+
- apiGroups:
292+
- apps
293+
resources:
294+
- daemonsets
295+
verbs:
296+
- get
297+
- list
298+
- patch
299+
- update
300+
- watch
283301
- apiGroups:
284302
- config.openshift.io
285303
resources:

bundle/manifests/ptp.openshift.io_ptpconfigs.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ spec:
9090
maximum: 65
9191
minimum: 1
9292
type: integer
93+
ptpSecretName:
94+
type: string
9395
ptpSettings:
9496
additionalProperties:
9597
type: string

config/crd/bases/ptp.openshift.io_hardwareconfigs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.19.0
6+
controller-gen.kubebuilder.io/version: v0.15.0
77
name: hardwareconfigs.ptp.openshift.io
88
spec:
99
group: ptp.openshift.io

config/crd/bases/ptp.openshift.io_nodeptpdevices.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.19.0
6+
controller-gen.kubebuilder.io/version: v0.15.0
77
name: nodeptpdevices.ptp.openshift.io
88
spec:
99
group: ptp.openshift.io

config/crd/bases/ptp.openshift.io_ptpconfigs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.19.0
6+
controller-gen.kubebuilder.io/version: v0.15.0
77
name: ptpconfigs.ptp.openshift.io
88
spec:
99
group: ptp.openshift.io

config/crd/bases/ptp.openshift.io_ptpoperatorconfigs.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.19.0
6+
controller-gen.kubebuilder.io/version: v0.15.0
77
name: ptpoperatorconfigs.ptp.openshift.io
88
spec:
99
group: ptp.openshift.io

config/rbac/role.yaml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ rules:
7272
- ptp.openshift.io
7373
resources:
7474
- ptpconfigs
75-
- ptpoperatorconfigs
7675
verbs:
7776
- create
7877
- delete
@@ -85,13 +84,37 @@ rules:
8584
- ptp.openshift.io
8685
resources:
8786
- ptpconfigs/finalizers
88-
- ptpoperatorconfigs/finalizers
8987
verbs:
9088
- update
9189
- apiGroups:
9290
- ptp.openshift.io
9391
resources:
9492
- ptpconfigs/status
93+
verbs:
94+
- get
95+
- patch
96+
- update
97+
- apiGroups:
98+
- ptp.openshift.io
99+
resources:
100+
- ptpoperatorconfigs
101+
verbs:
102+
- create
103+
- delete
104+
- get
105+
- list
106+
- patch
107+
- update
108+
- watch
109+
- apiGroups:
110+
- ptp.openshift.io
111+
resources:
112+
- ptpoperatorconfigs/finalizers
113+
verbs:
114+
- update
115+
- apiGroups:
116+
- ptp.openshift.io
117+
resources:
95118
- ptpoperatorconfigs/status
96119
verbs:
97120
- get

manifests/stable/ptp-operator.clusterserviceversion.yaml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ metadata:
6161
categories: Networking
6262
certified: "false"
6363
containerImage: quay.io/openshift/origin-ptp-operator:4.21
64-
createdAt: "2025-11-03T12:52:29Z"
64+
createdAt: "2025-11-18T22:08:29Z"
6565
description: This software enables configuration of Precision Time Protocol(PTP)
6666
on Kubernetes. It detects hardware capable PTP devices on each node, and configures
6767
linuxptp processes such as ptp4l, phc2sys and timemaster.
@@ -280,6 +280,24 @@ spec:
280280
- rolebindings
281281
verbs:
282282
- '*'
283+
- apiGroups:
284+
- ""
285+
resources:
286+
- secrets
287+
verbs:
288+
- get
289+
- list
290+
- watch
291+
- apiGroups:
292+
- apps
293+
resources:
294+
- daemonsets
295+
verbs:
296+
- get
297+
- list
298+
- patch
299+
- update
300+
- watch
283301
- apiGroups:
284302
- config.openshift.io
285303
resources:

0 commit comments

Comments
 (0)