Get VFSContext from caller in ValidateCluster()

This commit is contained in:
John Gardiner Myers 2023-07-17 22:20:57 -07:00
parent 7489469414
commit 76ed6b9e27
14 changed files with 37 additions and 33 deletions

View File

@ -651,7 +651,7 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
} }
strict := false strict := false
err = validation.DeepValidate(cluster, instanceGroups, strict, nil) err = validation.DeepValidate(cluster, instanceGroups, strict, clientset.VFSContext(), nil)
if err != nil { if err != nil {
return err return err
} }
@ -691,7 +691,7 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
fullInstanceGroups = append(fullInstanceGroups, fullGroup) fullInstanceGroups = append(fullInstanceGroups, fullGroup)
} }
err = validation.DeepValidate(fullCluster, fullInstanceGroups, true, nil) err = validation.DeepValidate(fullCluster, fullInstanceGroups, true, clientset.VFSContext(), nil)
if err != nil { if err != nil {
return fmt.Errorf("validation of the full cluster and instance group specs failed: %w", err) return fmt.Errorf("validation of the full cluster and instance group specs failed: %w", err)
} }

View File

@ -266,7 +266,7 @@ func updateCluster(ctx context.Context, clientset simple.Clientset, oldCluster,
return fmt.Sprintf("error populating cluster spec: %s", err), nil return fmt.Sprintf("error populating cluster spec: %s", err), nil
} }
err = validation.DeepValidate(fullCluster, instanceGroups, true, cloud) err = validation.DeepValidate(fullCluster, instanceGroups, true, clientset.VFSContext(), cloud)
if err != nil { if err != nil {
return fmt.Sprintf("validation failed: %s", err), nil return fmt.Sprintf("validation failed: %s", err), nil
} }

View File

@ -23,10 +23,11 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kops/pkg/apis/kops" "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi" "k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/util/pkg/vfs"
) )
func ValidateClusterUpdate(obj *kops.Cluster, status *kops.ClusterStatus, old *kops.Cluster) field.ErrorList { func ValidateClusterUpdate(obj *kops.Cluster, status *kops.ClusterStatus, old *kops.Cluster, vfsContext *vfs.VFSContext) field.ErrorList {
allErrs := ValidateCluster(obj, false) allErrs := ValidateCluster(obj, false, vfsContext)
// Validate etcd cluster changes // Validate etcd cluster changes
{ {

View File

@ -33,7 +33,7 @@ import (
// legacy contains validation functions that don't match the apimachinery style // legacy contains validation functions that don't match the apimachinery style
// ValidateCluster is responsible for checking the validity of the Cluster spec // ValidateCluster is responsible for checking the validity of the Cluster spec
func ValidateCluster(c *kops.Cluster, strict bool) field.ErrorList { func ValidateCluster(c *kops.Cluster, strict bool, vfsContext *vfs.VFSContext) field.ErrorList {
fieldSpec := field.NewPath("spec") fieldSpec := field.NewPath("spec")
allErrs := field.ErrorList{} allErrs := field.ErrorList{}
@ -200,12 +200,12 @@ func ValidateCluster(c *kops.Cluster, strict bool) field.ErrorList {
allErrs = append(allErrs, newValidateCluster(c, strict)...) allErrs = append(allErrs, newValidateCluster(c, strict)...)
said := c.Spec.ServiceAccountIssuerDiscovery said := c.Spec.ServiceAccountIssuerDiscovery
allErrs = append(allErrs, validateServiceAccountIssuerDiscovery(c, said, fieldSpec.Child("serviceAccountIssuerDiscovery"))...) allErrs = append(allErrs, validateServiceAccountIssuerDiscovery(c, said, fieldSpec.Child("serviceAccountIssuerDiscovery"), vfsContext)...)
return allErrs return allErrs
} }
func validateServiceAccountIssuerDiscovery(c *kops.Cluster, said *kops.ServiceAccountIssuerDiscoveryConfig, fieldSpec *field.Path) field.ErrorList { func validateServiceAccountIssuerDiscovery(c *kops.Cluster, said *kops.ServiceAccountIssuerDiscoveryConfig, fieldSpec *field.Path, vfsContext *vfs.VFSContext) field.ErrorList {
if said == nil { if said == nil {
return nil return nil
} }
@ -213,7 +213,7 @@ func validateServiceAccountIssuerDiscovery(c *kops.Cluster, said *kops.ServiceAc
saidStore := said.DiscoveryStore saidStore := said.DiscoveryStore
if saidStore != "" { if saidStore != "" {
saidStoreField := fieldSpec.Child("serviceAccountIssuerDiscovery", "discoveryStore") saidStoreField := fieldSpec.Child("serviceAccountIssuerDiscovery", "discoveryStore")
base, err := vfs.Context.BuildVfsPath(saidStore) base, err := vfsContext.BuildVfsPath(saidStore)
if err != nil { if err != nil {
allErrs = append(allErrs, field.Invalid(saidStoreField, saidStore, "not a valid VFS path")) allErrs = append(allErrs, field.Invalid(saidStoreField, saidStore, "not a valid VFS path"))
} else { } else {
@ -256,8 +256,8 @@ func validateSubnetCIDR(networkCIDRs []*net.IPNet, subnetCIDR *net.IPNet) bool {
} }
// DeepValidate is responsible for validating the instancegroups within the cluster spec // DeepValidate is responsible for validating the instancegroups within the cluster spec
func DeepValidate(c *kops.Cluster, groups []*kops.InstanceGroup, strict bool, cloud fi.Cloud) error { func DeepValidate(c *kops.Cluster, groups []*kops.InstanceGroup, strict bool, vfsContext *vfs.VFSContext, cloud fi.Cloud) error {
if errs := ValidateCluster(c, strict); len(errs) != 0 { if errs := ValidateCluster(c, strict, vfsContext); len(errs) != 0 {
return errs.ToAggregate() return errs.ToAggregate()
} }

View File

@ -81,7 +81,7 @@ func (c *RESTClientset) UpdateCluster(ctx context.Context, cluster *kops.Cluster
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := validation.ValidateClusterUpdate(cluster, status, old).ToAggregate(); err != nil { if err := validation.ValidateClusterUpdate(cluster, status, old, c.VFSContext()).ToAggregate(); err != nil {
return nil, err return nil, err
} }

View File

@ -45,7 +45,7 @@ func (c *VFSClientset) VFSContext() *vfs.VFSContext {
} }
func (c *VFSClientset) clusters() *ClusterVFS { func (c *VFSClientset) clusters() *ClusterVFS {
return newClusterVFS(c.basePath) return newClusterVFS(c.VFSContext(), c.basePath)
} }
// GetCluster implements the GetCluster method of simple.Clientset for a VFS-backed state store // GetCluster implements the GetCluster method of simple.Clientset for a VFS-backed state store

View File

@ -41,9 +41,9 @@ type ClusterVFS struct {
commonVFS commonVFS
} }
func newClusterVFS(basePath vfs.Path) *ClusterVFS { func newClusterVFS(vfsContext *vfs.VFSContext, basePath vfs.Path) *ClusterVFS {
c := &ClusterVFS{} c := &ClusterVFS{}
c.init("Cluster", basePath, StoreVersion) c.init("Cluster", vfsContext, basePath, StoreVersion)
return c return c
} }
@ -103,7 +103,7 @@ func (c *ClusterVFS) List(options metav1.ListOptions) (*api.ClusterList, error)
func (r *ClusterVFS) Create(c *api.Cluster) (*api.Cluster, error) { func (r *ClusterVFS) Create(c *api.Cluster) (*api.Cluster, error) {
ctx := context.TODO() ctx := context.TODO()
if errs := validation.ValidateCluster(c, false); len(errs) != 0 { if errs := validation.ValidateCluster(c, false, r.vfsContext); len(errs) != 0 {
return nil, errs.ToAggregate() return nil, errs.ToAggregate()
} }
@ -143,7 +143,7 @@ func (r *ClusterVFS) Update(c *api.Cluster, status *api.ClusterStatus) (*api.Clu
return nil, errors.NewNotFound(schema.GroupResource{Group: api.GroupName, Resource: "Cluster"}, clusterName) return nil, errors.NewNotFound(schema.GroupResource{Group: api.GroupName, Resource: "Cluster"}, clusterName)
} }
if err := validation.ValidateClusterUpdate(c, status, old).ToAggregate(); err != nil { if err := validation.ValidateClusterUpdate(c, status, old, r.vfsContext).ToAggregate(); err != nil {
return nil, err return nil, err
} }

View File

@ -42,12 +42,13 @@ type ValidationFunction func(o runtime.Object) error
type commonVFS struct { type commonVFS struct {
kind string kind string
vfsContext *vfs.VFSContext
basePath vfs.Path basePath vfs.Path
encoder runtime.Encoder encoder runtime.Encoder
validate ValidationFunction validate ValidationFunction
} }
func (c *commonVFS) init(kind string, basePath vfs.Path, storeVersion runtime.GroupVersioner) { func (c *commonVFS) init(kind string, vfsContext *vfs.VFSContext, basePath vfs.Path, storeVersion runtime.GroupVersioner) {
codecs := kopscodecs.Codecs codecs := kopscodecs.Codecs
yaml, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), "application/yaml") yaml, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), "application/yaml")
if !ok { if !ok {
@ -56,6 +57,7 @@ func (c *commonVFS) init(kind string, basePath vfs.Path, storeVersion runtime.Gr
c.encoder = codecs.EncoderForVersion(yaml.Serializer, storeVersion) c.encoder = codecs.EncoderForVersion(yaml.Serializer, storeVersion)
c.kind = kind c.kind = kind
c.vfsContext = vfsContext
c.basePath = basePath c.basePath = basePath
} }

View File

@ -52,7 +52,7 @@ func newInstanceGroupVFS(c *VFSClientset, cluster *kopsapi.Cluster) *InstanceGro
cluster: cluster, cluster: cluster,
clusterName: clusterName, clusterName: clusterName,
} }
r.init(kind, c.basePath.Join(clusterName, "instancegroup"), StoreVersion) r.init(kind, c.VFSContext(), c.basePath.Join(clusterName, "instancegroup"), StoreVersion)
r.validate = func(o runtime.Object) error { r.validate = func(o runtime.Object) error {
return validation.ValidateInstanceGroup(o.(*kopsapi.InstanceGroup), nil, false).ToAggregate() return validation.ValidateInstanceGroup(o.(*kopsapi.InstanceGroup), nil, false).ToAggregate()
} }

View File

@ -46,7 +46,7 @@ func UpdateCluster(ctx context.Context, clientset simple.Clientset, cluster *kop
return err return err
} }
err = validation.DeepValidate(fullCluster, instanceGroups, true, nil) err = validation.DeepValidate(fullCluster, instanceGroups, true, clientset.VFSContext(), nil)
if err != nil { if err != nil {
return err return err
} }

View File

@ -297,7 +297,7 @@ func (c *ApplyClusterCmd) Run(ctx context.Context) error {
cloud := c.Cloud cloud := c.Cloud
err = validation.DeepValidate(c.Cluster, c.InstanceGroups, true, cloud) err = validation.DeepValidate(c.Cluster, c.InstanceGroups, true, c.Clientset.VFSContext(), cloud)
if err != nil { if err != nil {
return err return err
} }

View File

@ -24,6 +24,7 @@ import (
kopsapi "k8s.io/kops/pkg/apis/kops" kopsapi "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/validation" "k8s.io/kops/pkg/apis/kops/validation"
"k8s.io/kops/upup/pkg/fi" "k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/util/pkg/vfs"
) )
func TestDeepValidate_OK(t *testing.T) { func TestDeepValidate_OK(t *testing.T) {
@ -33,7 +34,7 @@ func TestDeepValidate_OK(t *testing.T) {
groups = append(groups, buildMinimalMasterInstanceGroup(subnet.Name)) groups = append(groups, buildMinimalMasterInstanceGroup(subnet.Name))
groups = append(groups, buildMinimalNodeInstanceGroup(subnet.Name)) groups = append(groups, buildMinimalNodeInstanceGroup(subnet.Name))
} }
err := validation.DeepValidate(c, groups, true, nil) err := validation.DeepValidate(c, groups, true, vfs.Context, nil)
if err != nil { if err != nil {
t.Fatalf("Expected no error from DeepValidate, got %v", err) t.Fatalf("Expected no error from DeepValidate, got %v", err)
} }
@ -174,7 +175,7 @@ func TestDeepValidate_MissingEtcdMember(t *testing.T) {
} }
func expectErrorFromDeepValidate(t *testing.T, c *kopsapi.Cluster, groups []*kopsapi.InstanceGroup, message string) { func expectErrorFromDeepValidate(t *testing.T, c *kopsapi.Cluster, groups []*kopsapi.InstanceGroup, message string) {
err := validation.DeepValidate(c, groups, true, nil) err := validation.DeepValidate(c, groups, true, vfs.Context, nil)
if err == nil { if err == nil {
t.Fatalf("Expected error %q from DeepValidate (strict=true), not no error raised", message) t.Fatalf("Expected error %q from DeepValidate (strict=true), not no error raised", message)
} }

View File

@ -75,7 +75,7 @@ func PopulateClusterSpec(ctx context.Context, clientset simple.Clientset, cluste
// struct is falling through.. // struct is falling through..
// @kris-nova // @kris-nova
func (c *populateClusterSpec) run(ctx context.Context, clientset simple.Clientset) error { func (c *populateClusterSpec) run(ctx context.Context, clientset simple.Clientset) error {
if errs := validation.ValidateCluster(c.InputCluster, false); len(errs) != 0 { if errs := validation.ValidateCluster(c.InputCluster, false, clientset.VFSContext()); len(errs) != 0 {
return errs.ToAggregate() return errs.ToAggregate()
} }
@ -314,7 +314,7 @@ func (c *populateClusterSpec) run(ctx context.Context, clientset simple.Clientse
*fullCluster = *cluster *fullCluster = *cluster
fullCluster.Spec = *completed fullCluster.Spec = *completed
if errs := validation.ValidateCluster(fullCluster, true); len(errs) != 0 { if errs := validation.ValidateCluster(fullCluster, true, clientset.VFSContext()); len(errs) != 0 {
return fmt.Errorf("completed cluster failed validation: %v", errs.ToAggregate()) return fmt.Errorf("completed cluster failed validation: %v", errs.ToAggregate())
} }

View File

@ -86,11 +86,11 @@ func buildDefaultCluster(t *testing.T) *api.Cluster {
func TestValidateFull_Default_Validates(t *testing.T) { func TestValidateFull_Default_Validates(t *testing.T) {
c := buildDefaultCluster(t) c := buildDefaultCluster(t)
if errs := validation.ValidateCluster(c, false); len(errs) != 0 { if errs := validation.ValidateCluster(c, false, vfs.Context); len(errs) != 0 {
klog.Infof("Cluster: %v", c) klog.Infof("Cluster: %v", c)
t.Fatalf("Validate gave unexpected error (strict=false): %v", errs.ToAggregate()) t.Fatalf("Validate gave unexpected error (strict=false): %v", errs.ToAggregate())
} }
if errs := validation.ValidateCluster(c, true); len(errs) != 0 { if errs := validation.ValidateCluster(c, true, vfs.Context); len(errs) != 0 {
t.Fatalf("Validate gave unexpected error (strict=true): %v", errs.ToAggregate()) t.Fatalf("Validate gave unexpected error (strict=true): %v", errs.ToAggregate())
} }
} }
@ -200,7 +200,7 @@ func TestValidate_ContainerRegistry_and_ContainerProxy_exclusivity(t *testing.T)
} }
func expectErrorFromValidate(t *testing.T, c *api.Cluster, message string) { func expectErrorFromValidate(t *testing.T, c *api.Cluster, message string) {
errs := validation.ValidateCluster(c, false) errs := validation.ValidateCluster(c, false, vfs.Context)
if len(errs) == 0 { if len(errs) == 0 {
t.Fatalf("Expected error from Validate") t.Fatalf("Expected error from Validate")
} }
@ -211,7 +211,7 @@ func expectErrorFromValidate(t *testing.T, c *api.Cluster, message string) {
} }
func expectNoErrorFromValidate(t *testing.T, c *api.Cluster) { func expectNoErrorFromValidate(t *testing.T, c *api.Cluster) {
errs := validation.ValidateCluster(c, false) errs := validation.ValidateCluster(c, false, vfs.Context)
if len(errs) != 0 { if len(errs) != 0 {
t.Fatalf("Unexpected error from Validate: %v", errs.ToAggregate()) t.Fatalf("Unexpected error from Validate: %v", errs.ToAggregate())
} }