Skip to content

Commit

Permalink
Merge pull request #14960 from johngmyers/vfscontext
Browse files Browse the repository at this point in the history
Add VFSContext to various clientsets
  • Loading branch information
k8s-ci-robot committed Jul 15, 2023
2 parents 141a040 + 1130480 commit ef284b1
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 40 deletions.
5 changes: 3 additions & 2 deletions cmd/kops/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"k8s.io/kops/pkg/kubemanifest"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/util/pkg/text"
"k8s.io/kops/util/pkg/vfs"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
Expand Down Expand Up @@ -102,6 +101,8 @@ func RunCreate(ctx context.Context, f *util.Factory, out io.Writer, c *CreateOpt
return err
}

vfsContext := f.VFSContext()

clusterName := ""
// var cSpec = false
var sb bytes.Buffer
Expand All @@ -118,7 +119,7 @@ func RunCreate(ctx context.Context, f *util.Factory, out io.Writer, c *CreateOpt
return err
}
} else {
contents, err = vfs.Context.ReadFile(f)
contents, err = vfsContext.ReadFile(f)
if err != nil {
return fmt.Errorf("error reading file %q: %v", f, err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/kops/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
kopsapi "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/kopscodecs"
"k8s.io/kops/util/pkg/text"
"k8s.io/kops/util/pkg/vfs"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
Expand Down Expand Up @@ -91,7 +90,7 @@ func RunDelete(ctx context.Context, factory *util.Factory, out io.Writer, d *Del
return fmt.Errorf("reading from stdin: %v", err)
}
} else {
contents, err = vfs.Context.ReadFile(f)
contents, err = factory.VFSContext().ReadFile(f)
if err != nil {
return fmt.Errorf("reading file %q: %v", f, err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/kops/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"k8s.io/kops/pkg/kopscodecs"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/util/pkg/text"
"k8s.io/kops/util/pkg/vfs"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
Expand Down Expand Up @@ -90,6 +89,8 @@ func RunReplace(ctx context.Context, f *util.Factory, out io.Writer, c *ReplaceO
return err
}

vfsContext := f.VFSContext()

for _, f := range c.Filenames {
var contents []byte
if f == "-" {
Expand All @@ -98,7 +99,7 @@ func RunReplace(ctx context.Context, f *util.Factory, out io.Writer, c *ReplaceO
return err
}
} else {
contents, err = vfs.Context.ReadFile(f)
contents, err = vfsContext.ReadFile(f)
if err != nil {
return fmt.Errorf("error reading file %q: %v", f, err)
}
Expand Down
22 changes: 16 additions & 6 deletions cmd/kops/util/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Factory struct {

kubernetesClient kubernetes.Interface
certManagerClient certmanager.Interface
vfsContext *vfs.VFSContext

cachedRESTConfig *rest.Config
dynamicClient dynamic.Interface
Expand Down Expand Up @@ -112,14 +113,15 @@ func (f *Factory) KopsClient() (simple.Clientset, error) {
return nil, fmt.Errorf("error building kops API client: %v", err)
}

f.clientset = &api.RESTClientset{
BaseURL: &url.URL{
f.clientset = api.NewRESTClientset(
f.VFSContext(),
&url.URL{
Scheme: "k8s",
},
KopsClient: kopsClient.Kops(),
}
kopsClient.Kops(),
)
} else {
basePath, err := vfs.Context.BuildVfsPath(registryPath)
basePath, err := f.VFSContext().BuildVfsPath(registryPath)
if err != nil {
return nil, fmt.Errorf("error building path for %q: %v", registryPath, err)
}
Expand All @@ -128,7 +130,7 @@ func (f *Factory) KopsClient() (simple.Clientset, error) {
return nil, field.Invalid(field.NewPath("State Store"), registryPath, INVALID_STATE_ERROR)
}

f.clientset = vfsclientset.NewVFSClientset(basePath)
f.clientset = vfsclientset.NewVFSClientset(f.VFSContext(), basePath)
}
if strings.HasPrefix(registryPath, "file://") {
klog.Warning("The local filesystem state store is not functional for running clusters")
Expand Down Expand Up @@ -221,3 +223,11 @@ func (f *Factory) RESTMapper() (*restmapper.DeferredDiscoveryRESTMapper, error)

return f.restMapper, nil
}

func (f *Factory) VFSContext() *vfs.VFSContext {
if f.vfsContext == nil {
// TODO vfs.NewVFSContext()
f.vfsContext = vfs.Context
}
return f.vfsContext
}
3 changes: 1 addition & 2 deletions cmd/kops/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"k8s.io/kops"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/apis/kops/registry"
"k8s.io/kops/util/pkg/vfs"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
Expand Down Expand Up @@ -116,7 +115,7 @@ func serverVersion(f *util.Factory, options *VersionOptions) string {
if err != nil {
return "could not fetch cluster"
}
configBase, err := vfs.Context.BuildVfsPath(cluster.Spec.ConfigBase)
configBase, err := f.VFSContext().BuildVfsPath(cluster.Spec.ConfigBase)
if err != nil {
return "could not talk to vfs"
}
Expand Down
3 changes: 2 additions & 1 deletion examples/kops-api-example/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (

"k8s.io/kops/pkg/client/simple/vfsclientset"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/util/pkg/vfs"
)

func apply(ctx context.Context) error {
clientset := vfsclientset.NewVFSClientset(registryBase)
clientset := vfsclientset.NewVFSClientset(vfs.Context, registryBase)

cluster, err := clientset.GetCluster(ctx, clusterName)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion examples/kops-api-example/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ import (
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/upup/pkg/fi/utils"
"k8s.io/kops/util/pkg/vfs"
)

func up(ctx context.Context) error {
clientset := vfsclientset.NewVFSClientset(registryBase)
clientset := vfsclientset.NewVFSClientset(vfs.Context, registryBase)

cluster := &api.Cluster{}
cluster.ObjectMeta.Name = clusterName
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 @@ -297,7 +297,7 @@ func mockedPopulateClusterSpec(ctx context.Context, c *kops.Cluster, cloud fi.Cl
if err != nil {
return nil, fmt.Errorf("error building vfspath: %v", err)
}
clientset := vfsclientset.NewVFSClientset(basePath)
clientset := vfsclientset.NewVFSClientset(vfs.Context, basePath)
return cloudup.PopulateClusterSpec(ctx, clientset, c, cloud, assetBuilder)
}

Expand Down
17 changes: 15 additions & 2 deletions pkg/client/simple/api/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,23 @@ import (

// RESTClientset is an implementation of clientset that uses a "real" generated REST client
type RESTClientset struct {
vfsContext *vfs.VFSContext
BaseURL *url.URL
KopsClient kopsinternalversion.KopsInterface
}

func NewRESTClientset(vfsContext *vfs.VFSContext, baseURL *url.URL, kopsClient kopsinternalversion.KopsInterface) *RESTClientset {
return &RESTClientset{
vfsContext: vfsContext,
BaseURL: baseURL,
KopsClient: kopsClient,
}
}

func (c *RESTClientset) VFSContext() *vfs.VFSContext {
return c.vfsContext
}

// GetCluster implements the GetCluster method of Clientset for a kubernetes-API state store
func (c *RESTClientset) GetCluster(ctx context.Context, name string) (*kops.Cluster, error) {
namespace := restNamespaceForClusterName(name)
Expand Down Expand Up @@ -79,11 +92,11 @@ func (c *RESTClientset) UpdateCluster(ctx context.Context, cluster *kops.Cluster
// ConfigBaseFor implements the ConfigBaseFor method of Clientset for a kubernetes-API state store
func (c *RESTClientset) ConfigBaseFor(cluster *kops.Cluster) (vfs.Path, error) {
if cluster.Spec.ConfigBase != "" {
return vfs.Context.BuildVfsPath(cluster.Spec.ConfigBase)
return c.VFSContext().BuildVfsPath(cluster.Spec.ConfigBase)
}
// URL for clusters looks like https://<server>/apis/kops/v1alpha2/namespaces/<cluster>/clusters/<cluster>
// We probably want to add a subresource for full resources
return vfs.Context.BuildVfsPath(c.BaseURL.String())
return c.VFSContext().BuildVfsPath(c.BaseURL.String())
}

// ListClusters implements the ListClusters method of Clientset for a kubernetes-API state store
Expand Down
3 changes: 3 additions & 0 deletions pkg/client/simple/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
)

type Clientset interface {
// VFSContext returns a VFSContext.
VFSContext() *vfs.VFSContext

// GetCluster reads a cluster by name
GetCluster(ctx context.Context, name string) (*kops.Cluster, error)

Expand Down
30 changes: 18 additions & 12 deletions pkg/client/simple/vfsclientset/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ import (
)

type VFSClientset struct {
basePath vfs.Path
vfsContext *vfs.VFSContext
basePath vfs.Path
}

var _ simple.Clientset = &VFSClientset{}

func (c *VFSClientset) VFSContext() *vfs.VFSContext {
return c.vfsContext
}

func (c *VFSClientset) clusters() *ClusterVFS {
return newClusterVFS(c.basePath)
}
Expand Down Expand Up @@ -66,7 +71,7 @@ func (c *VFSClientset) ListClusters(ctx context.Context, options metav1.ListOpti
// ConfigBaseFor implements the ConfigBaseFor method of simple.Clientset for a VFS-backed state store
func (c *VFSClientset) ConfigBaseFor(cluster *kops.Cluster) (vfs.Path, error) {
if cluster.Spec.ConfigBase != "" {
return vfs.Context.BuildVfsPath(cluster.Spec.ConfigBase)
return c.VFSContext().BuildVfsPath(cluster.Spec.ConfigBase)
}
return c.clusters().configBase(cluster.Name)
}
Expand All @@ -89,13 +94,13 @@ func (c *VFSClientset) SecretStore(cluster *kops.Cluster) (fi.SecretStore, error
basedir := configBase.Join("secrets")
return secrets.NewVFSSecretStore(cluster, basedir), nil
} else {
storePath, err := vfs.Context.BuildVfsPath(cluster.Spec.SecretStore)
storePath, err := c.VFSContext().BuildVfsPath(cluster.Spec.SecretStore)
return secrets.NewVFSSecretStore(cluster, storePath), err
}
}

func (c *VFSClientset) KeyStore(cluster *kops.Cluster) (fi.CAStore, error) {
basedir, err := pkiPath(cluster)
basedir, err := c.pkiPath(cluster)
if err != nil {
return nil, err
}
Expand All @@ -106,22 +111,22 @@ func (c *VFSClientset) KeyStore(cluster *kops.Cluster) (fi.CAStore, error) {
}

func (c *VFSClientset) SSHCredentialStore(cluster *kops.Cluster) (fi.SSHCredentialStore, error) {
basedir, err := pkiPath(cluster)
basedir, err := c.pkiPath(cluster)
if err != nil {
return nil, err
}
return fi.NewVFSSSHCredentialStore(cluster, basedir), nil
}

func pkiPath(cluster *kops.Cluster) (vfs.Path, error) {
func (c *VFSClientset) pkiPath(cluster *kops.Cluster) (vfs.Path, error) {
if cluster.Spec.KeyStore == "" {
configBase, err := registry.ConfigBase(cluster)
if err != nil {
return nil, err
}
return configBase.Join("pki"), nil
} else {
storePath, err := vfs.Context.BuildVfsPath(cluster.Spec.KeyStore)
storePath, err := c.VFSContext().BuildVfsPath(cluster.Spec.KeyStore)
return storePath, err
}
}
Expand Down Expand Up @@ -205,7 +210,7 @@ func (c *VFSClientset) DeleteCluster(ctx context.Context, cluster *kops.Cluster)
if cluster.Spec.ServiceAccountIssuerDiscovery != nil {
discoveryStore := cluster.Spec.ServiceAccountIssuerDiscovery.DiscoveryStore
if discoveryStore != "" {
path, err := vfs.Context.BuildVfsPath(discoveryStore)
path, err := c.VFSContext().BuildVfsPath(discoveryStore)
if err != nil {
return err
}
Expand All @@ -223,7 +228,7 @@ func (c *VFSClientset) DeleteCluster(ctx context.Context, cluster *kops.Cluster)

secretStore := cluster.Spec.SecretStore
if secretStore != "" {
path, err := vfs.Context.BuildVfsPath(secretStore)
path, err := c.VFSContext().BuildVfsPath(secretStore)
if err != nil {
return err
}
Expand All @@ -235,7 +240,7 @@ func (c *VFSClientset) DeleteCluster(ctx context.Context, cluster *kops.Cluster)

keyStore := cluster.Spec.KeyStore
if keyStore != "" && keyStore != secretStore {
path, err := vfs.Context.BuildVfsPath(keyStore)
path, err := c.VFSContext().BuildVfsPath(keyStore)
if err != nil {
return err
}
Expand All @@ -253,9 +258,10 @@ func (c *VFSClientset) DeleteCluster(ctx context.Context, cluster *kops.Cluster)
return DeleteAllClusterState(configBase)
}

func NewVFSClientset(basePath vfs.Path) simple.Clientset {
func NewVFSClientset(vfsContext *vfs.VFSContext, basePath vfs.Path) simple.Clientset {
vfsClientset := &VFSClientset{
basePath: basePath,
vfsContext: vfsContext,
basePath: basePath,
}
return vfsClientset
}
10 changes: 8 additions & 2 deletions pkg/client/simple/vfsclientset/clientset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
)

func TestSSHCredentialStoreOnConfigBase(t *testing.T) {
clientset := VFSClientset{
vfsContext: vfs.Context,
}
vfs.Context.ResetMemfsContext(true)
configBase := "memfs://some/config/base"
cluster := &kops.Cluster{
Expand All @@ -32,7 +35,7 @@ func TestSSHCredentialStoreOnConfigBase(t *testing.T) {
},
}

p, err := pkiPath(cluster)
p, err := clientset.pkiPath(cluster)
if err != nil {
t.Errorf("Failed to create ssh path: %v", err)
}
Expand All @@ -46,6 +49,9 @@ func TestSSHCredentialStoreOnConfigBase(t *testing.T) {
}

func TestSSHCredentialStoreOnOwnCFS(t *testing.T) {
clientset := VFSClientset{
vfsContext: vfs.Context,
}
vfs.Context.ResetMemfsContext(true)
configBase := "memfs://some/config/base"
keyPath := "memfs://keys/some/config/base/pki"
Expand All @@ -56,7 +62,7 @@ func TestSSHCredentialStoreOnOwnCFS(t *testing.T) {
},
}

p, err := pkiPath(cluster)
p, err := clientset.pkiPath(cluster)
if err != nil {
t.Errorf("Failed to create ssh path: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/instancegroups/rollingupdate_os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func getTestSetupOS(t *testing.T, ctx context.Context) (*RollingUpdateCluster, *

assetBuilder := assets.NewAssetBuilder(inCluster.Spec.Assets, inCluster.Spec.KubernetesVersion, false)
basePath, _ := vfs.Context.BuildVfsPath(inCluster.Spec.ConfigBase)
clientset := vfsclientset.NewVFSClientset(basePath)
clientset := vfsclientset.NewVFSClientset(vfs.Context, basePath)
cluster, err := cloudup.PopulateClusterSpec(ctx, clientset, inCluster, mockcloud, assetBuilder)
if err != nil {
t.Fatalf("Failed to populate cluster spec: %v", 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 @@ -266,7 +266,7 @@ func (c *ApplyClusterCmd) Run(ctx context.Context) error {

cluster := c.Cluster

configBase, err := vfs.Context.BuildVfsPath(cluster.Spec.ConfigBase)
configBase, err := c.Clientset.VFSContext().BuildVfsPath(cluster.Spec.ConfigBase)
if err != nil {
return fmt.Errorf("error parsing config base %q: %v", cluster.Spec.ConfigBase, err)
}
Expand Down
2 changes: 1 addition & 1 deletion upup/pkg/fi/cloudup/bootstrapchannelbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func runChannelBuilderTest(t *testing.T, key string, addonManifests []string) {
if err != nil {
t.Errorf("error building vfspath: %v", err)
}
clientset := vfsclientset.NewVFSClientset(basePath)
clientset := vfsclientset.NewVFSClientset(vfs.Context, basePath)

secretStore, err := clientset.SecretStore(cluster)
if err != nil {
Expand Down
Loading

0 comments on commit ef284b1

Please sign in to comment.