Skip to content

Commit

Permalink
Merge pull request #15662 from johngmyers/vfscontext-2
Browse files Browse the repository at this point in the history
More VFSContext refactoring
  • Loading branch information
k8s-ci-robot committed Jul 18, 2023
2 parents 34f9c2c + 76ed6b9 commit aff52d8
Show file tree
Hide file tree
Showing 33 changed files with 97 additions and 86 deletions.
2 changes: 1 addition & 1 deletion cmd/kops/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func RunCreate(ctx context.Context, f *util.Factory, out io.Writer, c *CreateOpt

// Adding a PerformAssignments() call here as the user might be trying to use
// the new `-f` feature, with an old cluster definition.
err = cloudup.PerformAssignments(v, cloud)
err = cloudup.PerformAssignments(v, vfsContext, cloud)
if err != nil {
return fmt.Errorf("error populating configuration: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/kops/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,13 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
return err
}

err = cloudup.PerformAssignments(cluster, cloud)
err = cloudup.PerformAssignments(cluster, clientset.VFSContext(), cloud)
if err != nil {
return fmt.Errorf("error populating configuration: %v", err)
}

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/create_instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func RunCreateInstanceGroup(ctx context.Context, f *util.Factory, out io.Writer,
return err
}

channel, err := cloudup.ChannelForCluster(cluster)
channel, err := cloudup.ChannelForCluster(clientset.VFSContext(), cluster)
if err != nil {
klog.Warningf("%v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kops/edit_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func updateCluster(ctx context.Context, clientset simple.Clientset, oldCluster,
return "", err
}

err = cloudup.PerformAssignments(newCluster, cloud)
err = cloudup.PerformAssignments(newCluster, clientset.VFSContext(), cloud)
if err != nil {
return "", fmt.Errorf("error populating configuration: %v", err)
}
Expand All @@ -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
10 changes: 5 additions & 5 deletions cmd/kops/edit_instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ func RunEditInstanceGroup(ctx context.Context, f *util.Factory, out io.Writer, o
return err
}

channel, err := cloudup.ChannelForCluster(cluster)
clientset, err := f.KopsClient()
if err != nil {
klog.Warningf("%v", err)
return err
}

clientset, err := f.KopsClient()
channel, err := cloudup.ChannelForCluster(clientset.VFSContext(), cluster)
if err != nil {
return err
klog.Warningf("%v", err)
}

oldGroup, err := clientset.InstanceGroupsFor(cluster).Get(ctx, groupName, metav1.GetOptions{})
Expand Down Expand Up @@ -290,7 +290,7 @@ func updateInstanceGroup(ctx context.Context, clientset simple.Clientset, channe

// We need the full cluster spec to perform deep validation
// Note that we don't write it back though
err = cloudup.PerformAssignments(cluster, cloud)
err = cloudup.PerformAssignments(cluster, clientset.VFSContext(), cloud)
if err != nil {
return "", fmt.Errorf("error populating configuration: %v", err)
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/kops/get_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"k8s.io/kops/pkg/commands/commandutils"
"k8s.io/kops/pkg/kopscodecs"
"k8s.io/kops/util/pkg/tables"
"k8s.io/kops/util/pkg/vfs"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
Expand Down Expand Up @@ -157,7 +158,7 @@ func RunGetClusters(ctx context.Context, f commandutils.Factory, out io.Writer,

if options.FullSpec {
var err error
clusters, err = fullClusterSpecs(ctx, clusters)
clusters, err = fullClusterSpecs(ctx, client.VFSContext(), clusters)
if err != nil {
return err
}
Expand Down Expand Up @@ -278,10 +279,10 @@ func fullOutputYAML(out io.Writer, args ...runtime.Object) error {
return nil
}

func fullClusterSpecs(ctx context.Context, clusters []*kopsapi.Cluster) ([]*kopsapi.Cluster, error) {
func fullClusterSpecs(ctx context.Context, vfsContext *vfs.VFSContext, clusters []*kopsapi.Cluster) ([]*kopsapi.Cluster, error) {
var fullSpecs []*kopsapi.Cluster
for _, cluster := range clusters {
configBase, err := registry.ConfigBase(cluster)
configBase, err := registry.ConfigBase(vfsContext, cluster)
if err != nil {
return nil, fmt.Errorf("error reading full cluster spec for %q: %v", cluster.ObjectMeta.Name, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func RunReplace(ctx context.Context, f *util.Factory, out io.Writer, c *ReplaceO
return fmt.Errorf("cluster %v does not exist (try adding --force flag)", clusterName)
}

err = cloudup.PerformAssignments(v, cloud)
err = cloudup.PerformAssignments(v, vfsContext, cloud)
if err != nil {
return fmt.Errorf("error populating configuration: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/toolbox_instance-selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func retrieveClusterRefs(ctx context.Context, f commandutils.Factory, clusterNam
return nil, nil, nil, err
}

channel, err := cloudup.ChannelForCluster(cluster)
channel, err := cloudup.ChannelForCluster(clientset.VFSContext(), cluster)
if err != nil {
return nil, nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/toolbox_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func RunToolBoxTemplate(f commandutils.Factory, out io.Writer, options *ToolboxT
}
}

channel, err := kopsapi.LoadChannel(options.channel)
channel, err := kopsapi.LoadChannel(f.VFSContext(), options.channel)
if err != nil {
return fmt.Errorf("error loading channel %q: %v", options.channel, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/upgrade_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func RunUpgradeCluster(ctx context.Context, f *util.Factory, out io.Writer, opti
})
}

channel, err := kopsapi.LoadChannel(channelLocation)
channel, err := kopsapi.LoadChannel(f.VFSContext(), channelLocation)
if err != nil {
return fmt.Errorf("error loading channel %q: %v", channelLocation, err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/kops-api-example/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func up(vfsContext *vfs.VFSContext, ctx context.Context) error {
return err
}

if err := cloudup.PerformAssignments(cluster, cloud); err != nil {
if err := cloudup.PerformAssignments(cluster, vfsContext, cloud); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion nodeup/pkg/model/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func BuildNodeupModelContext(model *testutils.Model) (*NodeupModelContext, error
return nil, fmt.Errorf("error from BuildCloud: %v", err)
}

err = cloudup.PerformAssignments(model.Cluster, cloud)
err = cloudup.PerformAssignments(model.Cluster, vfs.Context, cloud)
if err != nil {
return nil, fmt.Errorf("error from PerformAssignments: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/kops/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func ResolveChannel(location string) (*url.URL, error) {
}

// LoadChannel loads a Channel object from the specified VFS location
func LoadChannel(location string) (*Channel, error) {
func LoadChannel(vfsContext *vfs.VFSContext, location string) (*Channel, error) {
resolvedURL, err := ResolveChannel(location)
if err != nil {
return nil, err
Expand All @@ -141,7 +141,7 @@ func LoadChannel(location string) (*Channel, error) {
resolved := resolvedURL.String()

klog.V(2).Infof("Loading channel from %q", resolved)
channelBytes, err := vfs.Context.ReadFile(resolved)
channelBytes, err := vfsContext.ReadFile(resolved)
if err != nil {
return nil, fmt.Errorf("error reading channel %q: %v", resolved, err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/kops/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ const (
PathKopsVersionUpdated = "kops-version.txt"
)

func ConfigBase(c *api.Cluster) (vfs.Path, error) {
func ConfigBase(vfsContext *vfs.VFSContext, c *api.Cluster) (vfs.Path, error) {
if c.Spec.ConfigBase == "" {
return nil, field.Required(field.NewPath("spec", "configBase"), "")
}
configBase, err := vfs.Context.BuildVfsPath(c.Spec.ConfigBase)
configBase, err := vfsContext.BuildVfsPath(c.Spec.ConfigBase)
if err != nil {
return nil, fmt.Errorf("error parsing ConfigBase %q: %v", c.Spec.ConfigBase, err)
}
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
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -126,7 +126,7 @@ func (c *RESTClientset) SSHCredentialStore(cluster *kops.Cluster) (fi.SSHCredent
}

func (c *RESTClientset) DeleteCluster(ctx context.Context, cluster *kops.Cluster) error {
configBase, err := registry.ConfigBase(cluster)
configBase, err := registry.ConfigBase(c.VFSContext(), cluster)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions 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 Expand Up @@ -87,7 +87,7 @@ func (c *VFSClientset) AddonsFor(cluster *kops.Cluster) simple.AddonsClient {

func (c *VFSClientset) SecretStore(cluster *kops.Cluster) (fi.SecretStore, error) {
if cluster.Spec.SecretStore == "" {
configBase, err := registry.ConfigBase(cluster)
configBase, err := registry.ConfigBase(c.VFSContext(), cluster)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -120,7 +120,7 @@ func (c *VFSClientset) SSHCredentialStore(cluster *kops.Cluster) (fi.SSHCredenti

func (c *VFSClientset) pkiPath(cluster *kops.Cluster) (vfs.Path, error) {
if cluster.Spec.KeyStore == "" {
configBase, err := registry.ConfigBase(cluster)
configBase, err := registry.ConfigBase(c.VFSContext(), cluster)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func (c *VFSClientset) DeleteCluster(ctx context.Context, cluster *kops.Cluster)
}
}

configBase, err := registry.ConfigBase(cluster)
configBase, err := registry.ConfigBase(c.VFSContext(), cluster)
if err != nil {
return err
}
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
6 changes: 5 additions & 1 deletion pkg/commands/commandutils/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ limitations under the License.

package commandutils

import "k8s.io/kops/pkg/client/simple"
import (
"k8s.io/kops/pkg/client/simple"
"k8s.io/kops/util/pkg/vfs"
)

type Factory interface {
KopsClient() (simple.Clientset, error)
VFSContext() *vfs.VFSContext
}
Loading

0 comments on commit aff52d8

Please sign in to comment.