Skip to content

Commit

Permalink
fix: use common utility printer.PrintComponentsCRD to replace printCo…
Browse files Browse the repository at this point in the history
…mponentsCRD, printer.PrintComponentsDeploy to replace printComponentsDelpoyment.

Signed-off-by: ycyaoxdu <[email protected]>
  • Loading branch information
ycyaoxdu committed Mar 14, 2022
1 parent b163921 commit 02f2cf4
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 43 deletions.
166 changes: 166 additions & 0 deletions pkg/cmd/get/hubinfo/exec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright Contributors to the Open Cluster Management project
package hubinfo

import (
"context"
"fmt"

"github.com/spf13/cobra"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
operatorclient "open-cluster-management.io/api/client/operator/clientset/versioned"
v1 "open-cluster-management.io/api/operator/v1"
"open-cluster-management.io/clusteradm/pkg/helpers/printer"
)

func (o *Options) complete(cmd *cobra.Command, args []string) error {
cfg, err := o.ClusteradmFlags.KubectlFactory.ToRESTConfig()
if err != nil {
return err
}
operatorClient, err := operatorclient.NewForConfig(cfg)
if err != nil {
return err
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
return err
}
crdClient, err := clientset.NewForConfig(cfg)
if err != nil {
return err
}

o.kubeClient = kubeClient
o.operatorClient = operatorClient
o.crdClient = crdClient
return nil
}

func (o *Options) validate(args []string) error {
if len(args) != 0 {
return fmt.Errorf("there should be no argument")
}
return nil
}

const (
clusterManagerName = "cluster-manager"
registrationOperatorNamespace = "open-cluster-management"
clusterManagerNameCRD = "clustermanagers.operator.open-cluster-management.io"

componentNameRegistrationController = "cluster-manager-registration-controller"
componentNameRegistrationWebhook = "cluster-manager-registration-webhook"
componentNameWorkWebhook = "cluster-manager-work-webhook"
componentNamePlacementController = "cluster-manager-placement-controller"
)

func (o *Options) run() error {
// printing registration-operator
if err := o.printRegistrationOperator(); err != nil {
return err
}
// printing components
if err := o.printComponents(); err != nil {
return err
}
return nil
}

func (o *Options) printRegistrationOperator() error {
deploy, err := o.kubeClient.AppsV1().
Deployments(registrationOperatorNamespace).
Get(context.TODO(), clusterManagerName, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return err
}
o.printer.Write(printer.LEVEL_0, "Registration Operator:\t<none>\n")
return nil
}
imageName := "<none>"
registrationOperatorExpectedRs := *deploy.Spec.Replicas
registrationOperatorAvailableRs := deploy.Status.AvailableReplicas
for _, container := range deploy.Spec.Template.Spec.Containers {
if container.Name == "registration-operator" {
imageName = container.Image
}
}
crdStatus := make(map[string]string)
cmgrCrd, err := o.crdClient.ApiextensionsV1().
CustomResourceDefinitions().
Get(context.TODO(), clusterManagerNameCRD, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return err
}
}
if cmgrCrd != nil {
crdStatus[clusterManagerNameCRD] = "installed"
} else {
crdStatus[clusterManagerNameCRD] = "absent"
}

o.printer.Write(printer.LEVEL_0, "Registration Operator:\n")
o.printer.Write(printer.LEVEL_1, "Controller:\t(%d/%d) %s\n", registrationOperatorAvailableRs, registrationOperatorExpectedRs, imageName)
o.printer.Write(printer.LEVEL_1, "CustomResourceDefinition:\n")
for name, st := range crdStatus {
o.printer.Write(printer.LEVEL_2, "(%s) %s\n", st, name)
}
return nil
}

func (o *Options) printComponents() error {
cmgr, err := o.operatorClient.OperatorV1().
ClusterManagers().
Get(context.TODO(), clusterManagerName, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return err
}
o.printer.Write(printer.LEVEL_0, "Components:\t<uninstalled>\n")
return nil
}

o.printer.Write(printer.LEVEL_0, "Components:\n")
if err := o.printRegistration(cmgr); err != nil {
return err
}
if err := o.printWork(cmgr); err != nil {
return err
}
if err := o.printPlacement(cmgr); err != nil {
return err
}
if err := o.printComponentsCRD(cmgr); err != nil {
return err
}
return nil
}

func (o *Options) printRegistration(cmgr *v1.ClusterManager) error {
o.printer.Write(printer.LEVEL_1, "Registration:\n")
err := printer.PrintComponentsDeploy(o.printer, o.kubeClient, cmgr.Status.RelatedResources, componentNameRegistrationController)
if err != nil {
return err
}

return printer.PrintComponentsDeploy(o.printer, o.kubeClient, cmgr.Status.RelatedResources, componentNameRegistrationWebhook)
}

func (o *Options) printWork(cmgr *v1.ClusterManager) error {
o.printer.Write(printer.LEVEL_1, "Work:\n")
return printer.PrintComponentsDeploy(o.printer, o.kubeClient, cmgr.Status.RelatedResources, componentNameWorkWebhook)
}

func (o *Options) printPlacement(cmgr *v1.ClusterManager) error {
o.printer.Write(printer.LEVEL_1, "Placement:\n")
return printer.PrintComponentsDeploy(o.printer, o.kubeClient, cmgr.Status.RelatedResources, componentNamePlacementController)
}

func (o *Options) printComponentsCRD(cmgr *v1.ClusterManager) error {
o.printer.Write(printer.LEVEL_1, "CustomResourceDefinition:\n")
return printer.PrintComponentsCRD(o.printer, o.crdClient, cmgr.Status.RelatedResources)
}
45 changes: 3 additions & 42 deletions pkg/cmd/get/klusterletinfo/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"

"github.com/spf13/cobra"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -56,8 +55,6 @@ const (
componentNameWorkAgent = "klusterlet-work-agent"
)

var componentsNamespace string

func (o *Options) run() error {
k, err := o.operatorClient.OperatorV1().Klusterlets().Get(context.TODO(), klusterletName, metav1.GetOptions{})
if err != nil {
Expand All @@ -67,8 +64,6 @@ func (o *Options) run() error {
o.printer.Write(printer.LEVEL_0, "Registration Operator:\t<none>\n")
return nil
}
// set namespace
componentsNamespace = k.Spec.Namespace

o.printer.Write(printer.LEVEL_0, "Klusterlet Conditions:\n")
for _, v := range k.Status.Conditions {
Expand Down Expand Up @@ -161,50 +156,16 @@ func (o *Options) printComponents() error {
}

func (o *Options) printRegistration(klet *v1.Klusterlet) error {
klusterletAgent, err := o.kubeClient.AppsV1().
Deployments(componentsNamespace).
Get(context.TODO(), componentNameRegistrationAgent, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return err
}
return nil
}

controllerExpectedRs := int(*klusterletAgent.Spec.Replicas)
controllerAvailableRs := klusterletAgent.Status.AvailableReplicas

o.printer.Write(printer.LEVEL_1, "Registration:\n")
o.printer.Write(printer.LEVEL_2, "Agent:\t(%d/%d) %s\n", controllerAvailableRs, controllerExpectedRs, getImageName(klusterletAgent))
return nil
return printer.PrintComponentsDeploy(o.printer, o.kubeClient, klet.Status.RelatedResources, componentNameRegistrationAgent)
}

func (o *Options) printWork(klet *v1.Klusterlet) error {
workAgent, err := o.kubeClient.AppsV1().
Deployments(componentsNamespace).
Get(context.TODO(), componentNameWorkAgent, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return err
}
return nil
}

agentExpectedRs := int(*workAgent.Spec.Replicas)
agentAvailableRs := workAgent.Status.AvailableReplicas
o.printer.Write(printer.LEVEL_1, "Work:\n")
o.printer.Write(printer.LEVEL_2, "Agent:\t(%d/%d) %s\n", agentExpectedRs, agentAvailableRs, getImageName(workAgent))
return nil
return printer.PrintComponentsDeploy(o.printer, o.kubeClient, klet.Status.RelatedResources, componentNameWorkAgent)
}

func (o *Options) printComponentsCRD(klet *v1.Klusterlet) error {
o.printer.Write(printer.LEVEL_1, "CustomResourceDefinition:\n")
return printer.PrintComponentsCRD(o.printer, o.crdClient, klet.Status.RelatedResources)
}

func getImageName(deploy *appsv1.Deployment) string {
imageName := "<none>"
for _, container := range deploy.Spec.Template.Spec.Containers {
imageName = container.Image
}
return imageName
}
40 changes: 39 additions & 1 deletion pkg/helpers/printer/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (

"github.com/disiqueira/gotree"
"github.com/fatih/color"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
operatorv1 "open-cluster-management.io/api/operator/v1"
workapiv1 "open-cluster-management.io/api/work/v1"
)
Expand Down Expand Up @@ -85,7 +88,6 @@ func PrintComponentsCRD(printer PrefixWriter, crdClient clientset.Interface, res
}
statuses[name] = st
}
printer.Write(LEVEL_1, "CustomResourceDefinition:\n")
for name, st := range statuses {
versionStr := formatCRDVersion(crdVersions, crdStorageVersion, name)
printer.Write(LEVEL_2, "(%s) %s [%s]\n", st, name, versionStr)
Expand All @@ -105,3 +107,39 @@ func formatCRDVersion(allServingVersions map[string][]string, storageVersion map
}
return strings.Join(outputVersions.List(), "|")
}

func PrintComponentsDeploy(printer PrefixWriter, deployClient kubernetes.Interface, resource []operatorv1.RelatedResourceMeta, name string) error {
var deploy operatorv1.RelatedResourceMeta
for _, item := range resource {
if item.Name == name {
deploy = item
}
}

client, err := deployClient.AppsV1().Deployments(deploy.Namespace).Get(context.TODO(), deploy.Name, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return err
}
return nil
}
var pre string
if strings.HasSuffix(name, "agent") {
pre = "Agent:"
} else if strings.HasSuffix(name, "controller") {
pre = "Controller:"
} else if strings.HasSuffix(name, "webhook") {
pre = "Webhook:"
}
printer.Write(LEVEL_2, "%s\t(%d/%d) %s\n", pre, int(*client.Spec.Replicas), int(client.Status.AvailableReplicas), getImageName(client))

return nil
}

func getImageName(deploy *appsv1.Deployment) string {
imageName := "<none>"
for _, container := range deploy.Spec.Template.Spec.Containers {
imageName = container.Image
}
return imageName
}

0 comments on commit 02f2cf4

Please sign in to comment.