-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to use existing k8s cluster instead of minikube (#442)
* created external cluster provisioner * changes made here to support external provisioner that isnt minikube * added testing to the new external cluster provisioner * updated golden files to make sure tests run * pr changes. * removed unused test function * comment made to externalboostrapcluster referencing issue that needs to be addressed soon.
- Loading branch information
1 parent
2ec8be3
commit f17f564
Showing
6 changed files
with
148 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
clusterctl/clusterdeployer/externalclusterprovisioner/externalbootstrapcluster.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package externalclusterprovisioner | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"fmt" | ||
) | ||
|
||
// Represents an actual external cluster being used for bootstrapping, should not be able to | ||
// actually delete or create, but can point to actual kubeconfig file. | ||
type ExternalBootstrapCluster struct { | ||
kubeconfigPath string | ||
kubeconfigFile string | ||
} | ||
|
||
// NewExternalCluster creates a new external k8s bootstrap cluster object | ||
// We should clean up any lingering resources when clusterctl is complete. | ||
// TODO https://github.com/kubernetes-sigs/cluster-api/issues/448 | ||
func NewExternalCluster(kubeconfigPath string) (*ExternalBootstrapCluster, error) { | ||
if _, err := os.Stat(kubeconfigPath); os.IsNotExist(err) { | ||
return nil, fmt.Errorf("file at %s does not exist", kubeconfigPath) | ||
} | ||
|
||
return &ExternalBootstrapCluster{kubeconfigPath:kubeconfigPath}, nil | ||
} | ||
|
||
// Create implements clusterdeployer.ClusterProvisioner interface | ||
func (e *ExternalBootstrapCluster) Create() error { | ||
// noop | ||
return nil | ||
} | ||
// Delete implements clusterdeployer.ClusterProvisioner interface | ||
func (e *ExternalBootstrapCluster) Delete() error { | ||
// noop | ||
return nil | ||
} | ||
|
||
// GetKubeconfig implements clusterdeployer.ClusterProvisioner interface | ||
func (e *ExternalBootstrapCluster) GetKubeconfig() (string, error) { | ||
|
||
if e.kubeconfigFile == "" { | ||
b, err := ioutil.ReadFile(e.kubeconfigPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
e.kubeconfigFile = string(b) | ||
} | ||
|
||
|
||
return e.kubeconfigFile, nil | ||
} |
50 changes: 50 additions & 0 deletions
50
clusterctl/clusterdeployer/externalclusterprovisioner/externalbootstrapcluster_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package externalclusterprovisioner | ||
|
||
import ( | ||
"testing" | ||
"os" | ||
"io/ioutil" | ||
) | ||
|
||
func TestGetKubeconfig(t *testing.T) { | ||
const contents = "dfserfafaew" | ||
f, err := createTempFile(contents) | ||
if err != nil { | ||
t.Fatal("Unable to create test file.") | ||
} | ||
defer os.Remove(f) | ||
|
||
t.Run("invalid path given", func(t *testing.T){ | ||
_, err = NewExternalCluster("") | ||
if err == nil { | ||
t.Fatal("Should not be able create External Cluster Provisioner.") | ||
} | ||
}) | ||
|
||
t.Run("file exists", func(t *testing.T) { | ||
|
||
ec, err := NewExternalCluster(f) | ||
if err != nil { | ||
t.Fatal("Should be able create External Cluster Provisioner.") | ||
} | ||
|
||
c, err := ec.GetKubeconfig() | ||
if err != nil { | ||
t.Fatalf("Unexpected err. Got: %v", err) | ||
return | ||
} | ||
if c != contents { | ||
t.Fatalf("Unexpected contents. Got: %v, Want: %v", c, contents) | ||
} | ||
}) | ||
} | ||
|
||
func createTempFile(contents string) (string, error) { | ||
f, err := ioutil.TempFile("", "") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer f.Close() | ||
f.WriteString(contents) | ||
return f.Name(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters