Skip to content

Commit

Permalink
Check kubeConfig before starting sandbox (#363)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Shuy <[email protected]>

Signed-off-by: Daniel Shuy <[email protected]>
  • Loading branch information
daniel-shuy authored Oct 26, 2022
1 parent b62243b commit 35e67f6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
9 changes: 8 additions & 1 deletion flytectl/pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type K8s interface {

//go:generate mockery -name=ContextOps -case=underscore
type ContextOps interface {
CheckConfig() error
CopyContext(srcConfigAccess clientcmd.ConfigAccess, srcCtxName, targetCtxName string) error
RemoveContext(ctxName string) error
}
Expand Down Expand Up @@ -56,9 +57,15 @@ func GetK8sClient(cfg, master string) (K8s, error) {
return Client, nil
}

// CheckConfig checks if the kubeConfig pointed to by configAccess exists
func (k *ContextManager) CheckConfig() error {
_, err := k.configAccess.GetStartingConfig()
return err
}

// CopyKubeContext copies context srcCtxName part of srcConfigAccess to targetCtxName part of targetConfigAccess.
func (k *ContextManager) CopyContext(srcConfigAccess clientcmd.ConfigAccess, srcCtxName, targetCtxName string) error {
_, err := k.configAccess.GetStartingConfig()
err := k.CheckConfig()
if err != nil {
return err
}
Expand Down
32 changes: 32 additions & 0 deletions flytectl/pkg/k8s/mocks/context_ops.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions flytectl/pkg/sandbox/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,11 @@ func MountVolume(file, destination string) (*mount.Mount, error) {
return nil, nil
}

func UpdateLocalKubeContext(dockerCtx string, contextName string) error {
func UpdateLocalKubeContext(k8sCtxMgr k8s.ContextOps, dockerCtx string, contextName string) error {
srcConfigAccess := &clientcmd.PathOptions{
GlobalFile: docker.Kubeconfig,
LoadingRules: clientcmd.NewDefaultClientConfigLoadingRules(),
}
k8sCtxMgr := k8s.NewK8sContextManager()
return k8sCtxMgr.CopyContext(srcConfigAccess, dockerCtx, contextName)
}

Expand Down Expand Up @@ -240,6 +239,12 @@ func primeFlytekitPod(ctx context.Context, podService corev1.PodInterface) {
}

func StartCluster(ctx context.Context, args []string, sandboxConfig *sandboxCmdConfig.Config, primePod bool, defaultImageName string, defaultImagePrefix string, exposedPorts map[nat.Port]struct{}, portBindings map[nat.Port][]nat.PortBinding, consolePort int) error {
k8sCtxMgr := k8s.NewK8sContextManager()
err := k8sCtxMgr.CheckConfig()
if err != nil {
return err
}

cli, err := docker.GetDockerClient()
if err != nil {
return err
Expand Down Expand Up @@ -267,7 +272,7 @@ func StartCluster(ctx context.Context, args []string, sandboxConfig *sandboxCmdC
if err != nil {
return err
}
if err = UpdateLocalKubeContext(sandboxDockerContext, sandboxContextName); err != nil {
if err = UpdateLocalKubeContext(k8sCtxMgr, sandboxDockerContext, sandboxContextName); err != nil {
return err
}

Expand Down
1 change: 1 addition & 0 deletions flytectl/pkg/sandbox/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func TestStartFunc(t *testing.T) {
sandboxCmdConfig.DefaultConfig.Version = ""
k8s.ContextMgr = mockK8sContextMgr
ghutil.Client = githubMock
mockK8sContextMgr.OnCheckConfig().Return(nil)
mockK8sContextMgr.OnCopyContextMatch(mock.Anything, mock.Anything, mock.Anything).Return(nil)
err = StartSandboxCluster(context.Background(), []string{}, config)
assert.Nil(t, err)
Expand Down

0 comments on commit 35e67f6

Please sign in to comment.