@@ -48,7 +48,6 @@ import (
4848)
4949
5050const (
51- ClusterCertsBasePath = "/tmp/"
5251 kubeconfigPath = "/etc/kubernetes"
5352 kubeconfigFile = "etc/kubernetes/admin.conf"
5453 certPKIPath = "/etc/kubernetes/pki"
@@ -66,15 +65,21 @@ const (
6665)
6766
6867type Config struct {
69- S3 * s3.S3
70- STS * sts.STS
71- S3Uploader * s3manager.Uploader
68+ S3 * s3.S3
69+ STS * sts.STS
70+ S3Uploader * s3manager.Uploader
71+ clusterConfigPath string
7272}
7373
7474func (c * Config ) Create (ctx context.Context , substrate * v1alpha1.Substrate ) (reconcile.Result , error ) {
7575 if substrate .Status .Cluster .APIServerAddress == nil {
7676 return reconcile.Result {Requeue : true }, nil
7777 }
78+ if c .clusterConfigPath == "" {
79+ if err := c .ensureKitEnvDir (); err != nil {
80+ return reconcile.Result {}, fmt .Errorf ("ensuring kit env dir, %w" , err )
81+ }
82+ }
7883 // ensure S3 bucket
7984 if err := c .ensureBucket (ctx , substrate ); err != nil {
8085 return reconcile.Result {}, fmt .Errorf ("ensuring S3 bucket, %w" , err )
@@ -102,11 +107,11 @@ func (c *Config) Create(ctx context.Context, substrate *v1alpha1.Substrate) (rec
102107 }
103108 // upload to s3 bucket
104109 if err := c .S3Uploader .UploadWithIterator (ctx , NewDirectoryIterator (
105- aws .StringValue (discovery .Name (substrate )), path .Join (ClusterCertsBasePath , aws .StringValue (discovery .Name (substrate ))))); err != nil {
110+ aws .StringValue (discovery .Name (substrate )), path .Join (c . clusterConfigPath , aws .StringValue (discovery .Name (substrate ))))); err != nil {
106111 return reconcile.Result {}, fmt .Errorf ("uploading to S3 %w" , err )
107112 }
108113 logging .FromContext (ctx ).Debugf ("Uploaded cluster configuration to s3://%s" , aws .StringValue (discovery .Name (substrate )))
109- substrate .Status .Cluster .KubeConfig = ptr .String (path .Join (ClusterCertsBasePath , aws .StringValue (discovery .Name (substrate )), kubeconfigFile ))
114+ substrate .Status .Cluster .KubeConfig = ptr .String (path .Join (c . clusterConfigPath , aws .StringValue (discovery .Name (substrate )), kubeconfigFile ))
110115 return reconcile.Result {}, nil
111116}
112117
@@ -124,7 +129,7 @@ func (c *Config) Delete(ctx context.Context, substrate *v1alpha1.Substrate) (rec
124129 } else {
125130 logging .FromContext (ctx ).Infof ("Deleted S3 bucket %s" , aws .StringValue (discovery .Name (substrate )))
126131 }
127- return reconcile.Result {}, os .RemoveAll (path .Join (ClusterCertsBasePath , aws .StringValue (discovery .Name (substrate ))))
132+ return reconcile.Result {}, os .RemoveAll (path .Join (c . clusterConfigPath , aws .StringValue (discovery .Name (substrate ))))
128133}
129134
130135func ErrNoSuchBucket (err error ) bool {
@@ -137,7 +142,7 @@ func ErrNoSuchBucket(err error) bool {
137142}
138143
139144func (c * Config ) generateCerts (cfg * kubeadm.InitConfiguration , substrate * v1alpha1.Substrate ) error {
140- cfg .CertificatesDir = path .Join (ClusterCertsBasePath , aws .StringValue (discovery .Name (substrate )), certPKIPath )
145+ cfg .CertificatesDir = path .Join (c . clusterConfigPath , aws .StringValue (discovery .Name (substrate )), certPKIPath )
141146 certTree , err := certs .GetDefaultCertList ().AsMap ().CertTree ()
142147 if err != nil {
143148 return err
@@ -151,7 +156,7 @@ func (c *Config) generateCerts(cfg *kubeadm.InitConfiguration, substrate *v1alph
151156
152157func (c * Config ) kubeConfigs (cfg * kubeadm.InitConfiguration , substrate * v1alpha1.Substrate ) error {
153158 // Generate Kube config files for master components
154- kubeConfigDir := path .Join (ClusterCertsBasePath , aws .StringValue (discovery .Name (substrate )), kubeconfigPath )
159+ kubeConfigDir := path .Join (c . clusterConfigPath , aws .StringValue (discovery .Name (substrate )), kubeconfigPath )
155160 for _ , kubeConfigFileName := range []string {
156161 kubeadmconstants .AdminKubeConfigFileName ,
157162 kubeadmconstants .KubeletKubeConfigFileName ,
@@ -165,7 +170,7 @@ func (c *Config) kubeConfigs(cfg *kubeadm.InitConfiguration, substrate *v1alpha1
165170}
166171
167172func (c * Config ) generateStaticPodManifests (cfg * kubeadm.InitConfiguration , substrate * v1alpha1.Substrate ) error {
168- manifestDir := path .Join (ClusterCertsBasePath , aws .StringValue (discovery .Name (substrate )), clusterManifestPath )
173+ manifestDir := path .Join (c . clusterConfigPath , aws .StringValue (discovery .Name (substrate )), clusterManifestPath )
169174 // etcd phase adds cfg.CertificatesDir to static pod yaml for pods to read the certs from
170175 cfg .CertificatesDir = certPKIPath
171176 if err := etcd .CreateLocalEtcdStaticPodManifestFile (
@@ -176,7 +181,7 @@ func (c *Config) generateStaticPodManifests(cfg *kubeadm.InitConfiguration, subs
176181 kubeadmconstants .KubeAPIServer ,
177182 kubeadmconstants .KubeControllerManager ,
178183 kubeadmconstants .KubeScheduler } {
179- err := controlplane .CreateStaticPodFiles (path .Join (ClusterCertsBasePath , aws .StringValue (discovery .Name (substrate )), clusterManifestPath ), "" ,
184+ err := controlplane .CreateStaticPodFiles (path .Join (c . clusterConfigPath , aws .StringValue (discovery .Name (substrate )), clusterManifestPath ), "" ,
180185 & cfg .ClusterConfiguration , & cfg .LocalAPIEndpoint , false , componentName )
181186 if err != nil {
182187 return fmt .Errorf ("creating static pod file for %v, %w" , componentName , err )
@@ -200,7 +205,7 @@ func (c *Config) ensureBucket(ctx context.Context, substrate *v1alpha1.Substrate
200205}
201206
202207func (c * Config ) kubeletSystemService (cfg * kubeadm.InitConfiguration , substrate * v1alpha1.Substrate ) error {
203- localDir := path .Join (ClusterCertsBasePath , aws .StringValue (discovery .Name (substrate )), kubeletSystemdPath )
208+ localDir := path .Join (c . clusterConfigPath , aws .StringValue (discovery .Name (substrate )), kubeletSystemdPath )
204209 if _ , err := os .Stat (localDir ); err != nil {
205210 if ! os .IsNotExist (err ) {
206211 return err
@@ -295,7 +300,7 @@ func (c *Config) ensureAuthenticatorConfig(ctx context.Context, substrate *v1alp
295300 return fmt .Errorf ("creating authenticator config, %w" , err )
296301 }
297302 logging .FromContext (ctx ).Debugf ("Created config map for authenticator" )
298- configDir := path .Join (ClusterCertsBasePath , aws .StringValue (discovery .Name (substrate )), authenticatorConfigDir )
303+ configDir := path .Join (c . clusterConfigPath , aws .StringValue (discovery .Name (substrate )), authenticatorConfigDir )
299304 if err := os .MkdirAll (configDir , 0700 ); err != nil {
300305 return fmt .Errorf ("failed to create directory, %w" , err )
301306 }
@@ -318,17 +323,30 @@ func (c *Config) staticPodSpecForAuthenticator(ctx context.Context, substrate *v
318323 if err != nil {
319324 return fmt .Errorf ("failed to marshal config map manifest, %w" , err )
320325 }
321- if err := ioutil .WriteFile (path .Join (ClusterCertsBasePath , aws .StringValue (discovery .Name (substrate )),
326+ if err := ioutil .WriteFile (path .Join (c . clusterConfigPath , aws .StringValue (discovery .Name (substrate )),
322327 clusterManifestPath , "aws-iam-authenticator.yaml" ), serialized , 0644 ); err != nil {
323328 return fmt .Errorf ("writing authenticator pod yaml, %w" , err )
324329 }
325330 return nil
326331}
327332
333+ func (c * Config ) ensureKitEnvDir () error {
334+ home , err := os .UserHomeDir ()
335+ if err != nil {
336+ return fmt .Errorf ("finding HOME dir %v" , err )
337+ }
338+ c .clusterConfigPath = filepath .Join (home , ".kit/env" )
339+ if err := os .MkdirAll (c .clusterConfigPath , 0755 ); err != nil {
340+ return fmt .Errorf ("creating .kit/env dir %v" , err )
341+ }
342+ return nil
343+ }
344+
328345// DirectoryIterator represents an iterator of a specified directory
329346type DirectoryIterator struct {
330347 filePaths []string
331348 bucket string
349+ localDir string
332350 next struct {
333351 path string
334352 f * os.File
@@ -351,6 +369,7 @@ func NewDirectoryIterator(bucket, dir string) s3manager.BatchUploadIterator {
351369 return & DirectoryIterator {
352370 filePaths : paths ,
353371 bucket : bucket ,
372+ localDir : dir ,
354373 }
355374}
356375
@@ -373,6 +392,8 @@ func (d *DirectoryIterator) Err() error {
373392
374393// UploadObject uploads a file
375394func (d * DirectoryIterator ) UploadObject () s3manager.BatchUploadObject {
395+ // trim the local path before uploading to S3
396+ d .next .path = strings .TrimPrefix (d .next .path , d .localDir )
376397 return s3manager.BatchUploadObject {
377398 Object : & s3manager.UploadInput {Bucket : & d .bucket , Key : & d .next .path , Body : d .next .f },
378399 After : d .next .f .Close ,
0 commit comments