Skip to content

Commit 4fe5f05

Browse files
committed
prefactor: centralize Contrast runtime class matching
1 parent 0808763 commit 4fe5f05

6 files changed

Lines changed: 33 additions & 23 deletions

File tree

cli/cmd/generate.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,17 +284,17 @@ func runGenerate(cmd *cobra.Command, args []string) error {
284284
return nil
285285
}
286286

287-
// mapCCWorkloads applies the given function to all workloads with the 'contrast-cc' runtime class.
287+
// mapContrastWorkloads applies the given function to all workloads with a Contrast runtime class.
288288
// The callback receives an apply configuration together with the file path and index the unstructured object has in the file map.
289289
// Changes to the apply configuration are not applied to the original unstructured object.
290-
func mapCCWorkloads(fileMap map[string][]*unstructured.Unstructured, f func(res any, path string, idx int) (any, error)) error {
290+
func mapContrastWorkloads(fileMap map[string][]*unstructured.Unstructured, f func(res any, path string, idx int) (any, error)) error {
291291
for path, resources := range fileMap {
292292
for idx, r := range resources {
293293
applyConfig, err := kuberesource.UnstructuredToApplyConfiguration(r)
294294
if err != nil {
295295
continue
296296
}
297-
if !isCCWorkload(applyConfig) {
297+
if !isContrastWorkload(applyConfig) {
298298
continue
299299
}
300300
changed, err := f(applyConfig, path, idx)
@@ -313,11 +313,9 @@ func mapCCWorkloads(fileMap map[string][]*unstructured.Unstructured, f func(res
313313
return nil
314314
}
315315

316-
func isCCWorkload(resource any) (ret bool) {
316+
func isContrastWorkload(resource any) (ret bool) {
317317
kuberesource.MapPodSpec(resource, func(spec *applycorev1.PodSpecApplyConfiguration) *applycorev1.PodSpecApplyConfiguration {
318-
if spec != nil && spec.RuntimeClassName != nil && strings.HasPrefix(*spec.RuntimeClassName, "contrast-cc") {
319-
ret = true
320-
}
318+
ret = kuberesource.IsContrastPod(spec)
321319
return spec
322320
})
323321
return ret
@@ -339,7 +337,7 @@ func isCoordinator(resource any) bool {
339337
func runVerifiers(fileMap map[string][]*unstructured.Unstructured, verifiers []verifier.Verifier) error {
340338
var findings error
341339
for _, v := range verifiers {
342-
_ = mapCCWorkloads(fileMap, func(res any, path string, idx int) (any, error) {
340+
_ = mapContrastWorkloads(fileMap, func(res any, path string, idx int) (any, error) {
343341
if err := v.Verify(res); err != nil {
344342
findings = errors.Join(findings, fmt.Errorf("failed to verify resource %q in file %q: %w", fileMap[path][idx].GetName(), path, err))
345343
}
@@ -406,7 +404,7 @@ func extractTargets(paths []string, configFile io.Writer, logger *slog.Logger) (
406404
applyConfig, err := kuberesource.UnstructuredToApplyConfiguration(object)
407405
if err != nil {
408406
logger.Warn("Could not convert resource into ApplyConfiguration", "path", path, "err", err)
409-
} else if isCCWorkload(applyConfig) {
407+
} else if isContrastWorkload(applyConfig) {
410408
containsCC = true
411409
if isCoordinator(applyConfig) {
412410
r, ok := applyConfig.(*applyappsv1.StatefulSetApplyConfiguration)
@@ -454,7 +452,7 @@ func generatePolicies(ctx context.Context, flags *generateFlags, fileMap map[str
454452
}
455453
}()
456454

457-
return mapCCWorkloads(fileMap, func(res any, path string, idx int) (any, error) {
455+
return mapContrastWorkloads(fileMap, func(res any, path string, idx int) (any, error) {
458456
initdataAnno, err := runner.Run(ctx, res, extraPath, logger)
459457
if err != nil {
460458
return nil, fmt.Errorf("failed to generate policy for %q in %q: %w", fileMap[path][idx].GetName(), path, err)
@@ -496,7 +494,7 @@ func patchTargets(fileMap map[string][]*unstructured.Unstructured, imageReplacem
496494
return fmt.Errorf("parsing release image definitions %s: %w", ReleaseImageReplacements, err)
497495
}
498496
}
499-
return mapCCWorkloads(fileMap, func(res any, _ string, _ int) (any, error) {
497+
return mapContrastWorkloads(fileMap, func(res any, _ string, _ int) (any, error) {
500498
if flags.insecureEnableDebugShell {
501499
if _, err := kuberesource.AddDebugShell(res, kuberesource.DebugShell()); err != nil {
502500
return nil, fmt.Errorf("injecting debug shell container: %w", err)

cli/cmd/policies.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
func manipulateInitdata(fileMap map[string][]*unstructured.Unstructured, manipulators ...func(*initdata.Initdata) error) error {
21-
return mapCCWorkloads(fileMap, func(res any, path string, _ int) (resource any, retErr error) {
21+
return mapContrastWorkloads(fileMap, func(res any, path string, _ int) (resource any, retErr error) {
2222
return kuberesource.MapPodSpecWithMeta(res, func(meta *applymetav1.ObjectMetaApplyConfiguration, spec *applycorev1.PodSpecApplyConfiguration) (*applymetav1.ObjectMetaApplyConfiguration, *applycorev1.PodSpecApplyConfiguration) {
2323
if meta == nil {
2424
return meta, spec
@@ -60,7 +60,7 @@ func manipulateInitdata(fileMap map[string][]*unstructured.Unstructured, manipul
6060

6161
func policiesFromKubeResources(fileMap map[string][]*unstructured.Unstructured) ([]deployment, error) {
6262
var deployments []deployment
63-
if err := mapCCWorkloads(fileMap, func(res any, path string, idx int) (any, error) {
63+
if err := mapContrastWorkloads(fileMap, func(res any, path string, idx int) (any, error) {
6464
name := fileMap[path][idx].GetName()
6565
namespace := orDefault(fileMap[path][idx].GetNamespace(), "default")
6666
gvk := fileMap[path][idx].GetObjectKind().GroupVersionKind()

cli/verifier/image_ref_valid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (v *ImageRefValid) Verify(toVerify any) error {
2525
kuberesource.MapPodSpec(toVerify, func(
2626
spec *applycorev1.PodSpecApplyConfiguration,
2727
) *applycorev1.PodSpecApplyConfiguration {
28-
if spec == nil || spec.RuntimeClassName == nil || !strings.HasPrefix(*spec.RuntimeClassName, "contrast-cc") {
28+
if !kuberesource.IsContrastPod(spec) {
2929
return spec
3030
}
3131

cli/verifier/no_shared_fs_mount.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package verifier
66
import (
77
"errors"
88
"fmt"
9-
"strings"
109

1110
"github.com/edgelesssys/contrast/internal/kuberesource"
1211

@@ -25,7 +24,7 @@ func (v *NoSharedFSMount) Verify(toVerify any) error {
2524
// get all volume mounts that are referenced in containers
2625
isNonCC := false
2726
kuberesource.MapPodSpec(toVerify, func(spec *applycorev1.PodSpecApplyConfiguration) *applycorev1.PodSpecApplyConfiguration {
28-
if spec == nil || spec.RuntimeClassName == nil || !strings.HasPrefix(*spec.RuntimeClassName, "contrast-cc") {
27+
if !kuberesource.IsContrastPod(spec) {
2928
// this isn't a confidential pod so we don't need to check further
3029
isNonCC = true
3130
return spec

cli/verifier/versions_match.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (v *VersionsMatch) Verify(toVerify any) error {
3434
meta *applymetav1.ObjectMetaApplyConfiguration,
3535
spec *applycorev1.PodSpecApplyConfiguration,
3636
) (*applymetav1.ObjectMetaApplyConfiguration, *applycorev1.PodSpecApplyConfiguration) {
37-
if spec == nil || spec.RuntimeClassName == nil || !strings.HasPrefix(*spec.RuntimeClassName, "contrast-cc") {
37+
if !kuberesource.IsContrastPod(spec) {
3838
return meta, spec
3939
}
4040

internal/kuberesource/mutators.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ const (
3434
imageStoreSizeAnnotationKey = "contrast.edgeless.systems/image-store-size"
3535
)
3636

37+
// contrastRuntimeClassPrefixes lists runtime class prefixes that identify Contrast pods.
38+
var contrastRuntimeClassPrefixes = []string{"contrast-cc"}
39+
40+
// IsContrastPod reports whether a pod uses a Contrast runtime.
41+
func IsContrastPod(spec *applycorev1.PodSpecApplyConfiguration) bool {
42+
if spec == nil || spec.RuntimeClassName == nil {
43+
return false
44+
}
45+
return slices.ContainsFunc(contrastRuntimeClassPrefixes, func(p string) bool {
46+
return strings.HasPrefix(*spec.RuntimeClassName, p)
47+
})
48+
}
49+
3750
// AddInitializer adds an initializer and its shared volume to the resource.
3851
//
3952
// If the resource does not contain a PodSpec, this function does nothing.
@@ -46,7 +59,7 @@ func AddInitializer(
4659
if meta != nil && meta.Annotations[skipInitializerAnnotationKey] == "true" {
4760
return meta, spec
4861
}
49-
if spec == nil || spec.RuntimeClassName == nil || !strings.HasPrefix(*spec.RuntimeClassName, "contrast-cc") {
62+
if !IsContrastPod(spec) {
5063
return meta, spec
5164
}
5265
if meta != nil && meta.Annotations[securePVAnnotationKey] != "" {
@@ -173,7 +186,7 @@ func AddServiceMesh(
173186
serviceMeshProxy *applycorev1.ContainerApplyConfiguration,
174187
) (res any, retErr error) {
175188
res = MapPodSpecWithMeta(resource, func(meta *applymetav1.ObjectMetaApplyConfiguration, spec *applycorev1.PodSpecApplyConfiguration) (*applymetav1.ObjectMetaApplyConfiguration, *applycorev1.PodSpecApplyConfiguration) {
176-
if spec == nil || spec.RuntimeClassName == nil || !strings.HasPrefix(*spec.RuntimeClassName, "contrast-cc") {
189+
if !IsContrastPod(spec) {
177190
return meta, spec
178191
}
179192

@@ -230,7 +243,7 @@ func AddDebugShell(
230243
debugShell *applycorev1.ContainerApplyConfiguration,
231244
) (any, error) {
232245
return MapPodSpec(resource, func(spec *applycorev1.PodSpecApplyConfiguration) *applycorev1.PodSpecApplyConfiguration {
233-
if spec == nil || spec.RuntimeClassName == nil || !strings.HasPrefix(*spec.RuntimeClassName, "contrast-cc") {
246+
if !IsContrastPod(spec) {
234247
return spec
235248
}
236249

@@ -319,7 +332,7 @@ func AddDmesg(resources []any) []any {
319332
WithPrivileged(true).SecurityContextApplyConfiguration)
320333

321334
addDmesg := func(spec *applycorev1.PodSpecApplyConfiguration) *applycorev1.PodSpecApplyConfiguration {
322-
if spec == nil || spec.RuntimeClassName == nil || !strings.HasPrefix(*spec.RuntimeClassName, "contrast-cc") {
335+
if !IsContrastPod(spec) {
323336
return spec
324337
}
325338
spec.Containers = append(spec.Containers, *dmesgContainer)
@@ -380,7 +393,7 @@ func AddImageStore(resources []any) []any {
380393

381394
addPvc := func(meta *applymetav1.ObjectMetaApplyConfiguration, spec *applycorev1.PodSpecApplyConfiguration,
382395
) (*applymetav1.ObjectMetaApplyConfiguration, *applycorev1.PodSpecApplyConfiguration) {
383-
if spec == nil || spec.RuntimeClassName == nil || !strings.HasPrefix(*spec.RuntimeClassName, "contrast-cc") {
396+
if !IsContrastPod(spec) {
384397
return meta, spec
385398
}
386399

@@ -733,7 +746,7 @@ func PatchNodeSelector(resources []any) []any {
733746
var out []any
734747
for _, resource := range resources {
735748
out = append(out, MapPodSpec(resource, func(spec *applycorev1.PodSpecApplyConfiguration) *applycorev1.PodSpecApplyConfiguration {
736-
if spec == nil || spec.RuntimeClassName == nil || !strings.HasPrefix(*spec.RuntimeClassName, "contrast-cc") {
749+
if !IsContrastPod(spec) {
737750
return spec
738751
}
739752
spec = spec.WithNodeSelector(map[string]string{

0 commit comments

Comments
 (0)