Skip to content

Commit

Permalink
Get VFSContext from caller in ValidateCluster()
Browse files Browse the repository at this point in the history
  • Loading branch information
johngmyers committed Jul 18, 2023
1 parent 7489469 commit 76ed6b9
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 33 deletions.
4 changes: 2 additions & 2 deletions cmd/kops/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/edit_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/kops/validation/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
12 changes: 6 additions & 6 deletions pkg/apis/kops/validation/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -200,20 +200,20 @@ 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
}
allErrs := field.ErrorList{}
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 {
Expand Down Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/client/simple/api/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/client/simple/vfsclientset/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/simple/vfsclientset/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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

Expand Down Expand Up @@ -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
}

Expand Down
12 changes: 7 additions & 5 deletions pkg/client/simple/vfsclientset/commonvfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/client/simple/vfsclientset/instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/helpers_readwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion upup/pkg/fi/cloudup/apply_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions upup/pkg/fi/cloudup/deepvalidate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions upup/pkg/fi/cloudup/populate_cluster_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

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

Expand Down
8 changes: 4 additions & 4 deletions upup/pkg/fi/cloudup/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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())
}
Expand Down

0 comments on commit 76ed6b9

Please sign in to comment.