From 76ed6b9e27ff3bff50685e9469441b84ca544435 Mon Sep 17 00:00:00 2001 From: John Gardiner Myers Date: Mon, 17 Jul 2023 22:20:57 -0700 Subject: [PATCH] Get VFSContext from caller in ValidateCluster() --- cmd/kops/create_cluster.go | 4 ++-- cmd/kops/edit_cluster.go | 2 +- pkg/apis/kops/validation/cluster.go | 5 +++-- pkg/apis/kops/validation/legacy.go | 12 ++++++------ pkg/client/simple/api/clientset.go | 2 +- pkg/client/simple/vfsclientset/clientset.go | 2 +- pkg/client/simple/vfsclientset/cluster.go | 8 ++++---- pkg/client/simple/vfsclientset/commonvfs.go | 12 +++++++----- pkg/client/simple/vfsclientset/instancegroup.go | 2 +- pkg/commands/helpers_readwrite.go | 2 +- upup/pkg/fi/cloudup/apply_cluster.go | 2 +- upup/pkg/fi/cloudup/deepvalidate_test.go | 5 +++-- upup/pkg/fi/cloudup/populate_cluster_spec.go | 4 ++-- upup/pkg/fi/cloudup/validation_test.go | 8 ++++---- 14 files changed, 37 insertions(+), 33 deletions(-) diff --git a/cmd/kops/create_cluster.go b/cmd/kops/create_cluster.go index 83866e703cce9..df717e06686c8 100644 --- a/cmd/kops/create_cluster.go +++ b/cmd/kops/create_cluster.go @@ -651,7 +651,7 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr } strict := false - err = validation.DeepValidate(cluster, instanceGroups, strict, nil) + err = validation.DeepValidate(cluster, instanceGroups, strict, clientset.VFSContext(), nil) if err != nil { return err } @@ -691,7 +691,7 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr fullInstanceGroups = append(fullInstanceGroups, fullGroup) } - err = validation.DeepValidate(fullCluster, fullInstanceGroups, true, nil) + err = validation.DeepValidate(fullCluster, fullInstanceGroups, true, clientset.VFSContext(), nil) if err != nil { return fmt.Errorf("validation of the full cluster and instance group specs failed: %w", err) } diff --git a/cmd/kops/edit_cluster.go b/cmd/kops/edit_cluster.go index c0198fc37131d..6cbc3a68e925e 100644 --- a/cmd/kops/edit_cluster.go +++ b/cmd/kops/edit_cluster.go @@ -266,7 +266,7 @@ func updateCluster(ctx context.Context, clientset simple.Clientset, oldCluster, 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 { return fmt.Sprintf("validation failed: %s", err), nil } diff --git a/pkg/apis/kops/validation/cluster.go b/pkg/apis/kops/validation/cluster.go index 91680a4b33860..e94bc369fa0c3 100644 --- a/pkg/apis/kops/validation/cluster.go +++ b/pkg/apis/kops/validation/cluster.go @@ -23,10 +23,11 @@ import ( "k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/kops/pkg/apis/kops" "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 { - allErrs := ValidateCluster(obj, false) +func ValidateClusterUpdate(obj *kops.Cluster, status *kops.ClusterStatus, old *kops.Cluster, vfsContext *vfs.VFSContext) field.ErrorList { + allErrs := ValidateCluster(obj, false, vfsContext) // Validate etcd cluster changes { diff --git a/pkg/apis/kops/validation/legacy.go b/pkg/apis/kops/validation/legacy.go index 172ef61af9b66..9338cb0884d53 100644 --- a/pkg/apis/kops/validation/legacy.go +++ b/pkg/apis/kops/validation/legacy.go @@ -33,7 +33,7 @@ import ( // legacy contains validation functions that don't match the apimachinery style // 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") allErrs := field.ErrorList{} @@ -200,12 +200,12 @@ func ValidateCluster(c *kops.Cluster, strict bool) field.ErrorList { allErrs = append(allErrs, newValidateCluster(c, strict)...) 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 } -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 { return nil } @@ -213,7 +213,7 @@ func validateServiceAccountIssuerDiscovery(c *kops.Cluster, said *kops.ServiceAc saidStore := said.DiscoveryStore if saidStore != "" { saidStoreField := fieldSpec.Child("serviceAccountIssuerDiscovery", "discoveryStore") - base, err := vfs.Context.BuildVfsPath(saidStore) + base, err := vfsContext.BuildVfsPath(saidStore) if err != nil { allErrs = append(allErrs, field.Invalid(saidStoreField, saidStore, "not a valid VFS path")) } 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 -func DeepValidate(c *kops.Cluster, groups []*kops.InstanceGroup, strict bool, cloud fi.Cloud) error { - if errs := ValidateCluster(c, strict); len(errs) != 0 { +func DeepValidate(c *kops.Cluster, groups []*kops.InstanceGroup, strict bool, vfsContext *vfs.VFSContext, cloud fi.Cloud) error { + if errs := ValidateCluster(c, strict, vfsContext); len(errs) != 0 { return errs.ToAggregate() } diff --git a/pkg/client/simple/api/clientset.go b/pkg/client/simple/api/clientset.go index 82a0c4ef55456..050f1c103a679 100644 --- a/pkg/client/simple/api/clientset.go +++ b/pkg/client/simple/api/clientset.go @@ -81,7 +81,7 @@ func (c *RESTClientset) UpdateCluster(ctx context.Context, cluster *kops.Cluster if err != nil { 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 } diff --git a/pkg/client/simple/vfsclientset/clientset.go b/pkg/client/simple/vfsclientset/clientset.go index d967634432132..f92edba4581eb 100644 --- a/pkg/client/simple/vfsclientset/clientset.go +++ b/pkg/client/simple/vfsclientset/clientset.go @@ -45,7 +45,7 @@ func (c *VFSClientset) VFSContext() *vfs.VFSContext { } 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 diff --git a/pkg/client/simple/vfsclientset/cluster.go b/pkg/client/simple/vfsclientset/cluster.go index 27a1da5f7dd82..a4e1937331966 100644 --- a/pkg/client/simple/vfsclientset/cluster.go +++ b/pkg/client/simple/vfsclientset/cluster.go @@ -41,9 +41,9 @@ type ClusterVFS struct { commonVFS } -func newClusterVFS(basePath vfs.Path) *ClusterVFS { +func newClusterVFS(vfsContext *vfs.VFSContext, basePath vfs.Path) *ClusterVFS { c := &ClusterVFS{} - c.init("Cluster", basePath, StoreVersion) + c.init("Cluster", vfsContext, basePath, StoreVersion) 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) { 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() } @@ -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) } - 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 } diff --git a/pkg/client/simple/vfsclientset/commonvfs.go b/pkg/client/simple/vfsclientset/commonvfs.go index fe3d074940bb9..cfbf119ac6ec6 100644 --- a/pkg/client/simple/vfsclientset/commonvfs.go +++ b/pkg/client/simple/vfsclientset/commonvfs.go @@ -41,13 +41,14 @@ var StoreVersion = v1alpha2.SchemeGroupVersion type ValidationFunction func(o runtime.Object) error type commonVFS struct { - kind string - basePath vfs.Path - encoder runtime.Encoder - validate ValidationFunction + kind string + vfsContext *vfs.VFSContext + basePath vfs.Path + encoder runtime.Encoder + 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 yaml, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), "application/yaml") 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.kind = kind + c.vfsContext = vfsContext c.basePath = basePath } diff --git a/pkg/client/simple/vfsclientset/instancegroup.go b/pkg/client/simple/vfsclientset/instancegroup.go index ce02c086a38c7..0fbae1d8ae05a 100644 --- a/pkg/client/simple/vfsclientset/instancegroup.go +++ b/pkg/client/simple/vfsclientset/instancegroup.go @@ -52,7 +52,7 @@ func newInstanceGroupVFS(c *VFSClientset, cluster *kopsapi.Cluster) *InstanceGro cluster: cluster, 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 { return validation.ValidateInstanceGroup(o.(*kopsapi.InstanceGroup), nil, false).ToAggregate() } diff --git a/pkg/commands/helpers_readwrite.go b/pkg/commands/helpers_readwrite.go index 7876b38bc8f58..a00365b903488 100644 --- a/pkg/commands/helpers_readwrite.go +++ b/pkg/commands/helpers_readwrite.go @@ -46,7 +46,7 @@ func UpdateCluster(ctx context.Context, clientset simple.Clientset, cluster *kop return err } - err = validation.DeepValidate(fullCluster, instanceGroups, true, nil) + err = validation.DeepValidate(fullCluster, instanceGroups, true, clientset.VFSContext(), nil) if err != nil { return err } diff --git a/upup/pkg/fi/cloudup/apply_cluster.go b/upup/pkg/fi/cloudup/apply_cluster.go index 596bd45202d4a..fa54f2bde73db 100644 --- a/upup/pkg/fi/cloudup/apply_cluster.go +++ b/upup/pkg/fi/cloudup/apply_cluster.go @@ -297,7 +297,7 @@ func (c *ApplyClusterCmd) Run(ctx context.Context) error { 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 { return err } diff --git a/upup/pkg/fi/cloudup/deepvalidate_test.go b/upup/pkg/fi/cloudup/deepvalidate_test.go index 22e5ac0873fdc..d1d36f42d614f 100644 --- a/upup/pkg/fi/cloudup/deepvalidate_test.go +++ b/upup/pkg/fi/cloudup/deepvalidate_test.go @@ -24,6 +24,7 @@ import ( kopsapi "k8s.io/kops/pkg/apis/kops" "k8s.io/kops/pkg/apis/kops/validation" "k8s.io/kops/upup/pkg/fi" + "k8s.io/kops/util/pkg/vfs" ) 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, buildMinimalNodeInstanceGroup(subnet.Name)) } - err := validation.DeepValidate(c, groups, true, nil) + err := validation.DeepValidate(c, groups, true, vfs.Context, nil) if err != nil { 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) { - err := validation.DeepValidate(c, groups, true, nil) + err := validation.DeepValidate(c, groups, true, vfs.Context, nil) if err == nil { t.Fatalf("Expected error %q from DeepValidate (strict=true), not no error raised", message) } diff --git a/upup/pkg/fi/cloudup/populate_cluster_spec.go b/upup/pkg/fi/cloudup/populate_cluster_spec.go index d48aa2470a066..42216755a6171 100644 --- a/upup/pkg/fi/cloudup/populate_cluster_spec.go +++ b/upup/pkg/fi/cloudup/populate_cluster_spec.go @@ -75,7 +75,7 @@ func PopulateClusterSpec(ctx context.Context, clientset simple.Clientset, cluste // struct is falling through.. // @kris-nova 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() } @@ -314,7 +314,7 @@ func (c *populateClusterSpec) run(ctx context.Context, clientset simple.Clientse *fullCluster = *cluster 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()) } diff --git a/upup/pkg/fi/cloudup/validation_test.go b/upup/pkg/fi/cloudup/validation_test.go index 721e00dc76c41..4c70ecde488c4 100644 --- a/upup/pkg/fi/cloudup/validation_test.go +++ b/upup/pkg/fi/cloudup/validation_test.go @@ -86,11 +86,11 @@ func buildDefaultCluster(t *testing.T) *api.Cluster { func TestValidateFull_Default_Validates(t *testing.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) 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()) } } @@ -200,7 +200,7 @@ func TestValidate_ContainerRegistry_and_ContainerProxy_exclusivity(t *testing.T) } 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 { 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) { - errs := validation.ValidateCluster(c, false) + errs := validation.ValidateCluster(c, false, vfs.Context) if len(errs) != 0 { t.Fatalf("Unexpected error from Validate: %v", errs.ToAggregate()) }