Skip to content

Commit

Permalink
Merge pull request #496 from kamlendu1982/osd-19746-svclog
Browse files Browse the repository at this point in the history
adding servicelog check, interactive acceptance criteria, fixing inte…
  • Loading branch information
openshift-merge-bot[bot] authored Jan 5, 2024
2 parents 1519644 + f333a14 commit 4db4a49
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
4 changes: 3 additions & 1 deletion cmd/cluster/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"sync"
"time"

v1 "github.com/openshift-online/ocm-sdk-go/servicelogs/v1"

Expand Down Expand Up @@ -372,7 +373,8 @@ func (o *contextOptions) generateContextData() (*contextData, []error) {
if o.verbose {
fmt.Fprintln(os.Stderr, "Getting Service Logs...")
}
data.ServiceLogs, err = servicelog.GetServiceLogsSince(cluster.ID(), o.days, false, false)
timeToCheckSvcLogs := time.Now().AddDate(0, 0, -o.days)
data.ServiceLogs, err = servicelog.GetServiceLogsSince(cluster.ID(), timeToCheckSvcLogs, false, false)
if err != nil {
errors = append(errors, fmt.Errorf("Error while getting the service logs: %v", err))
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/org/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"strconv"
"sync"
"time"

pd "github.com/PagerDuty/go-pagerduty"
"github.com/andygrunwald/go-jira"
Expand Down Expand Up @@ -285,7 +286,8 @@ func addLimitedSupportReasons(clusterInfo *ClusterInfo, ocmClient *sdk.Connectio

func addServiceLogs(clusterInfo *ClusterInfo) error {
var err error
clusterInfo.ServiceLogs, err = servicelog.GetServiceLogsSince(clusterInfo.ID, ServiceLogDaysSince, false, false)
timeToCheckSvcLogs := time.Now().AddDate(0, 0, -ServiceLogDaysSince)
clusterInfo.ServiceLogs, err = servicelog.GetServiceLogsSince(clusterInfo.ID, timeToCheckSvcLogs, false, false)
if err != nil {
return fmt.Errorf("failed to fetch service logs for cluster %v: %w", clusterInfo.ID, err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/servicelog/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ func validateBadResponse(body []byte) (badReply *servicelog.BadReply, err error)
// time.Now() and time.Now()-duration. the first parameter will contain a slice
// of the service logs from the given time period, while the second return value
// indicates if an error has happened.
func GetServiceLogsSince(clusterID string, days int, allMessages bool, internalOnly bool) ([]*v1.LogEntry, error) {
// time.Now().Sub() returns the duration between two times, so we negate the duration in Add()
earliestTime := time.Now().AddDate(0, 0, -days)
func GetServiceLogsSince(clusterID string, timeSince time.Time, allMessages bool, internalOnly bool) ([]*v1.LogEntry, error) {
earliestTime := timeSince

slResponse, err := FetchServiceLogs(clusterID, allMessages, internalOnly)
if err != nil {
Expand Down
26 changes: 25 additions & 1 deletion cmd/servicelog/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/utils/strings/slices"
Expand All @@ -24,6 +25,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/term"
)

type PostCmdOptions struct {
Expand Down Expand Up @@ -89,14 +91,36 @@ func (o *PostCmdOptions) Validate() error {
return nil
}

func (o *PostCmdOptions) CheckServiceLogsLastHour() bool {
getAllMessages := false // we need just manual entries
getInternalLogsOnly := false // we need all messages
numberOfHours := 1 // number of hours we need to wait for svc logs
timeStampToCompare := time.Now().Add(-time.Hour * time.Duration(numberOfHours))
serviceLogs, err := GetServiceLogsSince(o.ClusterId, timeStampToCompare, getAllMessages, getInternalLogsOnly)
if err != nil {
log.Fatalf("failed to fetch service logs: %q", err)
}
if len(serviceLogs) > 0 {
for _, svclog := range serviceLogs {
fmt.Println("Below service Log has been subitted in last 60 minutes\nDescription: ", svclog.Description())
}
return true
}
return false
}

func (o *PostCmdOptions) Run() error {
if err := o.Init(); err != nil {
return err
}
if err := o.Validate(); err != nil {
return err
}

if term.IsTerminal(int(os.Stdout.Fd())) && o.CheckServiceLogsLastHour() {
if !ocmutils.ConfirmPrompt() {
return nil
}
}
o.parseUserParameters() // parse all the '-p' user flags
o.readFilterFile() // parse the ocm filters in file provided via '-f' flag
o.readTemplate() // parse the given JSON template provided via '-t' flag
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ require (
github.com/spf13/viper v1.18.1
go.uber.org/mock v0.3.0
golang.org/x/sync v0.5.0
golang.org/x/term v0.15.0
google.golang.org/api v0.153.0
google.golang.org/genproto v0.0.0-20231106174013-bbf56f31fb17
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -177,7 +178,6 @@ require (
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
Expand Down

0 comments on commit 4db4a49

Please sign in to comment.