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
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)
}

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
}
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
}

View File

@ -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
{

View File

@ -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()
}

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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
}

View File

@ -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()
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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())
}

View File

@ -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())
}