Skip to content

Commit

Permalink
Use kubeconfig from instance machine dir instead from metadata
Browse files Browse the repository at this point in the history
Bundle metadata kubeconfig is created during initial bundle creation
and this will be outdated when kubeconfig is generated for each
fresh crc start.
  • Loading branch information
praveenkumar committed Jul 2, 2021
1 parent 8928cf1 commit 466c4da
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
13 changes: 6 additions & 7 deletions pkg/crc/cluster/clusteroperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"
"time"

"github.com/code-ready/crc/pkg/crc/machine/bundle"
clientset "github.com/openshift/client-go/config/clientset/versioned"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -80,8 +79,8 @@ func (status *Status) IsReady() bool {
return status.Available && !status.Progressing && !status.Degraded && !status.Disabled
}

func GetClusterOperatorsStatus(ctx context.Context, ip string, bundle *bundle.CrcBundleInfo) (*Status, error) {
lister, err := kubernetesClient(ip, bundle)
func GetClusterOperatorsStatus(ctx context.Context, ip string, kubeconfigFilePath string) (*Status, error) {
lister, err := kubernetesClient(ip, kubeconfigFilePath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -157,16 +156,16 @@ type operatorLister interface {
List(ctx context.Context, opts metav1.ListOptions) (*openshiftapi.ClusterOperatorList, error)
}

func kubernetesClient(ip string, bundle *bundle.CrcBundleInfo) (*clientset.Clientset, error) {
config, err := kubernetesClientConfiguration(ip, bundle)
func kubernetesClient(ip string, kubeconfigFilePath string) (*clientset.Clientset, error) {
config, err := kubernetesClientConfiguration(ip, kubeconfigFilePath)
if err != nil {
return nil, err
}
return clientset.NewForConfig(config)
}

func kubernetesClientConfiguration(ip string, bundle *bundle.CrcBundleInfo) (*restclient.Config, error) {
config, err := clientcmd.BuildConfigFromFlags("", bundle.GetKubeConfigPath())
func kubernetesClientConfiguration(ip string, kubeconfigFilePath string) (*restclient.Config, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigFilePath)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/crc/cluster/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"time"

"github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/crc/pkg/crc/machine/bundle"
)

// WaitForClusterStable checks that the cluster is running a number of consecutive times
func WaitForClusterStable(ctx context.Context, ip string, bundle *bundle.CrcBundleInfo) error {
func WaitForClusterStable(ctx context.Context, ip string, kubeconfigFilePath string) error {
if ctx.Err() != nil {
return ctx.Err()
}
Expand All @@ -24,7 +23,7 @@ func WaitForClusterStable(ctx context.Context, ip string, bundle *bundle.CrcBund
var count int // holds num of consecutive matches

for i := 0; i < retryCount; i++ {
status, err := GetClusterOperatorsStatus(ctx, ip, bundle)
status, err := GetClusterOperatorsStatus(ctx, ip, kubeconfigFilePath)
if err == nil {
// update counter for consecutive matches
if status.IsReady() {
Expand Down
1 change: 1 addition & 0 deletions pkg/crc/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ var (
MachineInstanceDir = filepath.Join(MachineBaseDir, "machines")
DefaultBundlePath = defaultBundlePath()
DaemonSocketPath = filepath.Join(CrcBaseDir, "crc.sock")
KubeconfigFilePath = filepath.Join(MachineInstanceDir, DefaultName, "kubeconfig")
)

func defaultBundlePath() string {
Expand Down
12 changes: 5 additions & 7 deletions pkg/crc/machine/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
}

logging.Info("Starting OpenShift cluster... [waiting for the cluster to stabilize]")

if err := cluster.WaitForClusterStable(ctx, instanceIP, crcBundleMetadata); err != nil {
if err := cluster.WaitForClusterStable(ctx, instanceIP, constants.KubeconfigFilePath); err != nil {
logging.Errorf("Cluster is not ready: %v", err)
}

Expand Down Expand Up @@ -616,16 +615,15 @@ func updateSSHKeyPair(sshRunner *crcssh.Runner) error {
}

func copyKubeconfig(name string, machineConfig config.MachineConfig) error {
kubeConfigFilePath := filepath.Join(constants.MachineInstanceDir, name, "kubeconfig")
if _, err := os.Stat(kubeConfigFilePath); err == nil {
if _, err := os.Stat(constants.KubeconfigFilePath); err == nil {
return nil
}

// Copy Kubeconfig file from bundle extract path to machine directory.
// In our case it would be ~/.crc/machines/crc/
// Copy Kubeconfig file content from bundle extract path to machine directory.
// In our case it would be `constants.KubeconfigFilePath`
logging.Info("Copying kubeconfig file to instance dir...")
err := crcos.CopyFileContents(machineConfig.KubeConfig,
kubeConfigFilePath,
constants.KubeconfigFilePath,
0644)
if err != nil {
return fmt.Errorf("Error copying kubeconfig file to instance dir: %v", err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/crc/machine/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (client *client) Status() (*types.ClusterStatusResult, error) {
diskSize, diskUse := client.getDiskDetails(ip, crcBundleMetadata)
return &types.ClusterStatusResult{
CrcStatus: state.Running,
OpenshiftStatus: getOpenShiftStatus(context.Background(), ip, crcBundleMetadata),
OpenshiftStatus: getOpenShiftStatus(context.Background(), ip),
OpenshiftVersion: crcBundleMetadata.GetOpenshiftVersion(),
DiskUse: diskUse,
DiskSize: diskSize,
Expand All @@ -82,8 +82,8 @@ func (client *client) getDiskDetails(ip string, bundle *bundle.CrcBundleInfo) (i
return disk.([]int64)[0], disk.([]int64)[1]
}

func getOpenShiftStatus(ctx context.Context, ip string, bundle *bundle.CrcBundleInfo) types.OpenshiftStatus {
status, err := cluster.GetClusterOperatorsStatus(ctx, ip, bundle)
func getOpenShiftStatus(ctx context.Context, ip string) types.OpenshiftStatus {
status, err := cluster.GetClusterOperatorsStatus(ctx, ip, constants.KubeconfigFilePath)
if err != nil {
logging.Debugf("cannot get OpenShift status: %v", err)
return types.OpenshiftUnreachable
Expand Down

0 comments on commit 466c4da

Please sign in to comment.