Skip to content

Commit

Permalink
Status: use k8s client to get microshift status
Browse files Browse the repository at this point in the history
In this PR, k8s client api is used to get the node status and provide
this info to status command instead using `systemctl status microshift`
which uses ssh connection.

- https://kubernetes.io/docs/concepts/architecture/nodes/#condition
  • Loading branch information
praveenkumar committed Jun 21, 2023
1 parent 3ac2a2b commit e330d87
Show file tree
Hide file tree
Showing 881 changed files with 108,355 additions and 23 deletions.
41 changes: 41 additions & 0 deletions pkg/crc/cluster/clusteroperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
"time"

clientset "github.com/openshift/client-go/config/clientset/versioned"
k8sclient "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"

"github.com/crc-org/crc/pkg/crc/logging"
openshiftapi "github.com/openshift/api/config/v1"
k8sapi "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -145,6 +147,37 @@ func getStatus(ctx context.Context, lister operatorLister, selector []string) (*
return cs, nil
}

func GetClusterNodeStatus(ctx context.Context, ip string, kubeconfigFilePath string) (*Status, error) {
status := &Status{
Available: true,
}
clientSet, err := kubernetesClient(ip, kubeconfigFilePath)
if err != nil {
return nil, err
}
nodes, err := clientSet.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
var ns k8sapi.ConditionStatus
for _, c := range nodes.Items[0].Status.Conditions {
if c.Type == k8sapi.NodeReady {
ns = c.Status
}
}
switch ns {
case k8sapi.ConditionTrue:
status.Available = true
case k8sapi.ConditionFalse:
status.Progressing = true
case k8sapi.ConditionUnknown:
status.Degraded = true
default:
logging.Debugf("Unexpected node status for %s", ns)
}
return status, nil
}

func contains(value string, list []string) bool {
for _, v := range list {
if v == value {
Expand All @@ -166,6 +199,14 @@ func openshiftClient(ip string, kubeconfigFilePath string) (*clientset.Clientset
return clientset.NewForConfig(config)
}

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

func kubernetesClientConfiguration(ip string, kubeconfigFilePath string) (*restclient.Config, error) {
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigFilePath)
if err != nil {
Expand Down
37 changes: 14 additions & 23 deletions pkg/crc/machine/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/crc-org/crc/pkg/crc/machine/state"
"github.com/crc-org/crc/pkg/crc/machine/types"
"github.com/crc-org/crc/pkg/crc/preset"
"github.com/crc-org/crc/pkg/crc/systemd"
sdState "github.com/crc-org/crc/pkg/crc/systemd/states"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -66,7 +64,7 @@ func (client *client) Status() (*types.ClusterStatusResult, error) {

switch {
case vm.bundle.IsMicroshift():
clusterStatusResult.OpenshiftStatus = getMicroShiftStatus(vm)
clusterStatusResult.OpenshiftStatus = getMicroShiftStatus(context.Background(), ip)
case vm.bundle.IsOpenShift():
clusterStatusResult.OpenshiftStatus = getOpenShiftStatus(context.Background(), ip)
}
Expand Down Expand Up @@ -140,32 +138,25 @@ func getOpenShiftStatus(ctx context.Context, ip string) types.OpenshiftStatus {
logging.Debugf("cannot get OpenShift status: %v", err)
return types.OpenshiftUnreachable
}
switch {
case status.Progressing:
return types.OpenshiftStarting
case status.Degraded:
return types.OpenshiftDegraded
case status.Available:
return types.OpenshiftRunning
}
return types.OpenshiftStopped
return getStatus(status)
}

func getMicroShiftStatus(vm *virtualMachine) types.OpenshiftStatus {
sshRunner, err := vm.SSHRunner()
func getMicroShiftStatus(ctx context.Context, ip string) types.OpenshiftStatus {
status, err := cluster.GetClusterNodeStatus(ctx, ip, constants.KubeconfigFilePath)
if err != nil {
logging.Debugf("cannot get MicroShift status: %v", err)
logging.Debugf("failed to get microshift node status: %v", err)
return types.OpenshiftUnreachable
}
defer sshRunner.Close()
return getStatus(status)
}

sd := systemd.NewInstanceSystemdCommander(sshRunner)
microShiftServiceState, err := sd.Status("microshift")
if err != nil {
logging.Debugf("failed to get microshift service status: %v", err)
return types.OpenshiftUnreachable
}
if microShiftServiceState == sdState.Running {
func getStatus(status *cluster.Status) types.OpenshiftStatus {
switch {
case status.Progressing:
return types.OpenshiftStarting
case status.Degraded:
return types.OpenshiftDegraded
case status.Available:
return types.OpenshiftRunning
}
return types.OpenshiftStopped
Expand Down

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

Loading

0 comments on commit e330d87

Please sign in to comment.