import ( "k8s.io/apimachinery/pkg/util/validation/field" ) // Validate the "foo.bar.baz" field in a Kubernetes resource func validateFooBarBaz(fooBarBaz string) field.ErrorList { errs := field.ErrorList{} // Use PathChild to traverse to the "foo.bar.baz" field path := field.NewPath("foo").Child("bar").Child("baz") // Validate the value of the field using PathChild if len(fooBarBaz) < 5 { errs = append(errs, field.Invalid(path, fooBarBaz, "must be at least 5 characters")) } return errs }
import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/util/validation/field" ) // Validate the "metadata.labels" field in a Kubernetes resource func validateMetadataLabels(u *unstructured.Unstructured) field.ErrorList { errs := field.ErrorList{} metaPath := field.NewPath("metadata") // Use PathChild to traverse to the "metadata.labels" field labelsPath := metaPath.Child("labels") // Validate the existence of the "labels" field if _, ok := u.Object["metadata"].(map[string]interface{})["labels"]; !ok { errs = append(errs, field.NotFound(labelsPath, "metadata.labels")) } return errs }This code demonstrates how to use PathChild to traverse to the "metadata.labels" field in an Unstructured Kubernetes resource. The function validates the existence of the field and returns a list of any validation errors encountered.