Skip to content

Commit

Permalink
Liqoctl: pod status check enhanced
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 authored and adamjensenbot committed Apr 28, 2023
1 parent 0a911ed commit e5d98e9
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 142 deletions.
67 changes: 59 additions & 8 deletions pkg/liqoctl/output/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,36 @@ import (
// Section is a section of the output.
type Section interface {
AddSection(title string) Section
AddSectionSuccess(title string) Section
AddSectionFailure(title string) Section
AddSectionInfo(title string) Section
AddSectionWithDetail(title, detail string) Section
AddEntry(key string, values ...string) Section
AddEntryWithoutStyle(key, value string) Section
SprintForBox(printer *Printer) string
}

// entry is a entry of the output.
type entry struct {
key string
values []string
style *pterm.Style
}

type sectionVariant string

const (
success sectionVariant = "success"
failure sectionVariant = "failure"
info sectionVariant = "info"
)

// section is a section of the output.
type section struct {
title, detail string
sections []*section
entries []*entry
variant sectionVariant
}

// NewRootSection create a new Section.
Expand All @@ -51,6 +65,27 @@ func (s *section) AddSection(title string) Section {
return s.AddSectionWithDetail(title, "")
}

// AddSectionSuccess add a new Success Section.
func (s *section) AddSectionSuccess(title string) Section {
section := &section{title: title, detail: "", variant: success}
s.sections = append(s.sections, section)
return section
}

// AddSectionFailure add a new Failure Section.
func (s *section) AddSectionFailure(title string) Section {
section := &section{title: title, detail: "", variant: failure}
s.sections = append(s.sections, section)
return section
}

// AddSectionInfo add a new Info Section.
func (s *section) AddSectionInfo(title string) Section {
section := &section{title: title, detail: "", variant: info}
s.sections = append(s.sections, section)
return section
}

// AddSectionWithDetail add a new Section.
func (s *section) AddSectionWithDetail(title, detail string) Section {
section := &section{title: title, detail: detail}
Expand All @@ -60,7 +95,13 @@ func (s *section) AddSectionWithDetail(title, detail string) Section {

// AddEntry add a new entry.
func (s *section) AddEntry(key string, values ...string) Section {
s.entries = append(s.entries, &entry{key: key, values: values})
s.entries = append(s.entries, &entry{key: key, values: values, style: StatusDataStyle})
return s
}

// AddEntryWithoutStyle add a new entry without style.
func (s *section) AddEntryWithoutStyle(key, value string) Section {
s.entries = append(s.entries, &entry{key: key, values: []string{value}, style: pterm.NewStyle(pterm.FgDefault)})
return s
}

Expand All @@ -72,14 +113,24 @@ func (s *section) SprintForBox(printer *Printer) string {

// String return the string representation of the section.
func (s *section) String() string {
sectionStyle := StatusSectionStyle
switch s.variant {
case success:
sectionStyle = StatusSectionSuccessStyle
case failure:
sectionStyle = StatusSectionFailureStyle
case info:
sectionStyle = StatusSectionInfoStyle
}

if s.detail != "" {
return pterm.Sprintf(
"%s - %s",
StatusSectionStyle.Sprint(s.title),
sectionStyle.Sprint(s.title),
StatusInfoStyle.Sprint(s.detail),
)
}
return StatusSectionStyle.Sprint(s.title)
return sectionStyle.Sprint(s.title)
}

// print print the section.
Expand Down Expand Up @@ -110,25 +161,25 @@ func longestEntryKey(entries []*entry) int {
func (e *entry) print(level int, printer *Printer, longestKey int) {
switch len(e.values) {
case 0:
printer.BulletListAddItemWithoutBullet(pterm.Sprintf("%s", e.key),
printer.BulletListAddItemWithoutBullet(pterm.Bold.Sprint(e.key),
level,
)
case 1:
printer.BulletListAddItemWithoutBullet(pterm.Sprintf("%s: %s%s",
e.key,
pterm.Bold.Sprint(e.key),
strings.Repeat(" ", longestKey-len(e.key)),
StatusDataStyle.Sprint(e.values[0]),
e.style.Sprint(e.values[0]),
),
level,
)
default:
printer.BulletListAddItemWithoutBullet(
pterm.Sprintf("%s:", pterm.Sprint(e.key)),
pterm.Bold.Sprint(e.key),
level,
)
for _, v := range e.values {
printer.BulletListAddItemWithBullet(
StatusDataStyle.Sprint(v),
e.style.Sprint(v),
level,
)
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/liqoctl/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ const (
var (
// StatusSectionStyle is the style of the status section.
StatusSectionStyle = pterm.NewStyle(pterm.FgMagenta, pterm.Bold)
// StatusSectionSuccessStyle is the style of the success status section.
StatusSectionSuccessStyle = pterm.NewStyle(pterm.FgGreen, pterm.Bold)
// StatusSectionFailureStyle is the style of the failure status section.
StatusSectionFailureStyle = pterm.NewStyle(pterm.FgRed, pterm.Bold)
// StatusSectionInfoStyle is the style of the info status section.
StatusSectionInfoStyle = pterm.NewStyle(pterm.FgDefault, pterm.Bold)
// StatusDataStyle is the style of the status data.
StatusDataStyle = pterm.NewStyle(pterm.FgLightYellow, pterm.Bold)
// StatusInfoStyle is the style of the status info.
Expand Down Expand Up @@ -104,6 +110,8 @@ func (p *Printer) BoxSetTitle(title string) {
func (p *Printer) BulletListSprintForBox() string {
// Srender function never throws an error.
text, err := p.BulletList.Srender()
// Flush Items to avoid printing the same list twice.
p.BulletList.Items = []pterm.BulletListItem{}
p.CheckErr(err)
text = strings.TrimRight(text, "\n")
return text
Expand Down
3 changes: 2 additions & 1 deletion pkg/liqoctl/status/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func (o *Options) Run(ctx context.Context) error {

for i, checker := range o.Checkers {
checker.Collect(ctx)
text := checker.Format()
text := ""
text = checker.Format()

if !checker.Silent() || !checker.HasSucceeded() {
o.Printer.BoxSetTitle(checker.GetTitle())
Expand Down
12 changes: 6 additions & 6 deletions pkg/liqoctl/status/local/localinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type LocalInfoChecker struct {
}

const (
localInfoCheckerName = "Local Cluster Information"
localInfoCheckerName = "Local cluster information"
)

// NewLocalInfoChecker returns a new LocalInfoChecker.
Expand All @@ -66,9 +66,9 @@ func (lic *LocalInfoChecker) Collect(ctx context.Context) {
if err != nil {
lic.addCollectionError(fmt.Errorf("unable to get cluster identity: %w", err))
}
clusterIdentitySection := lic.localInfoSection.AddSection("Cluster Identity")
clusterIdentitySection := lic.localInfoSection.AddSection("Cluster identity")
clusterIdentitySection.AddEntry("Cluster ID", clusterIdentity.ClusterID)
clusterIdentitySection.AddEntry("Cluster Name", clusterIdentity.ClusterName)
clusterIdentitySection.AddEntry("Cluster name", clusterIdentity.ClusterName)

ctrlargs, err := liqoctlutils.RetrieveLiqoControllerManagerDeploymentArgs(ctx, lic.options.CRClient, lic.options.LiqoNamespace)
if err != nil {
Expand All @@ -80,7 +80,7 @@ func (lic *LocalInfoChecker) Collect(ctx context.Context) {
if err != nil {
lic.addCollectionError(fmt.Errorf("unable to get cluster labels: %w", err))
}
clusterLabelsSection := clusterIdentitySection.AddSection("Cluster Labels")
clusterLabelsSection := clusterIdentitySection.AddSection("Cluster labels")
for k, v := range clusterLabels {
clusterLabelsSection.AddEntry(k, v)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func (lic *LocalInfoChecker) addEndpointsSection(ctx context.Context) error {
if ep, err = lic.getVpnEndpointLocalAddress(ctx); err != nil {
return fmt.Errorf("unable to get vpn endpoint local address: %w", err)
}
endpointsSection.AddEntry("Network Gateway", ep)
endpointsSection.AddEntry("Network gateway", ep)
}

var aurl string
Expand All @@ -189,6 +189,6 @@ func (lic *LocalInfoChecker) addEndpointsSection(ctx context.Context) error {
if err != nil {
return fmt.Errorf("unable to get api server address: %w", err)
}
endpointsSection.AddEntry("Kubernetes API Server", apiServerAddress)
endpointsSection.AddEntry("Kubernetes API server", apiServerAddress)
return nil
}
8 changes: 4 additions & 4 deletions pkg/liqoctl/status/local/localinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ var _ = Describe("LocalInfo", func() {
pterm.Sprintf("Cluster ID: %s", clusterID),
))
Expect(text).To(ContainSubstring(
pterm.Sprintf("Cluster Name: %s", clusterName),
pterm.Sprintf("Cluster name: %s", clusterName),
))
if args.clusterLabels {
for _, v := range testutil.ClusterLabels {
Expand All @@ -151,7 +151,7 @@ var _ = Describe("LocalInfo", func() {
Expect(text).To(ContainSubstring(v))
}
Expect(text).To(ContainSubstring(
pterm.Sprintf("Network Gateway: udp://%s:%d", testutil.EndpointIP, testutil.VPNGatewayPort),
pterm.Sprintf("Network gateway: udp://%s:%d", testutil.EndpointIP, testutil.VPNGatewayPort),
))
} else {
Expect(text).To(ContainSubstring(pterm.Sprintf("Status: %s", discoveryv1alpha1.PeeringConditionStatusExternal)))
Expand All @@ -161,11 +161,11 @@ var _ = Describe("LocalInfo", func() {
))
if args.net.apiServerOverride {
Expect(text).To(ContainSubstring(
pterm.Sprintf("Kubernetes API Server: %s", fmt.Sprintf("https://%v", testutil.OverrideAPIAddress)),
pterm.Sprintf("Kubernetes API server: %s", fmt.Sprintf("https://%v", testutil.OverrideAPIAddress)),
))
} else {
Expect(text).To(ContainSubstring(
pterm.Sprintf("Kubernetes API Server: %s", fmt.Sprintf("https://%v:6443", testutil.EndpointIP)),
pterm.Sprintf("Kubernetes API server: %s", fmt.Sprintf("https://%v:6443", testutil.EndpointIP)),
))
}

Expand Down
Loading

0 comments on commit e5d98e9

Please sign in to comment.