Skip to content

Commit

Permalink
Merge pull request #192 from fahlmant/OSD-10259
Browse files Browse the repository at this point in the history
Create loggingCheck comamnd to show logging support for a target cluster
  • Loading branch information
openshift-merge-robot authored Mar 9, 2022
2 parents 9ff52fd + 82eb31f commit aaa1cd7
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cmd/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
func NewCmdCluster(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, globalOpts *globalflags.GlobalOptions) *cobra.Command {
clusterCmd := &cobra.Command{
Use: "cluster",
Short: "Provides vitals of an AWS cluster",
Short: "Provides information for a specified cluster",
Args: cobra.NoArgs,
DisableAutoGenTag: true,
Run: help,
}

clusterCmd.AddCommand(newCmdHealth(streams, flags, globalOpts))
clusterCmd.AddCommand(newCmdloggingCheck(streams, flags, globalOpts))
return clusterCmd
}

Expand Down
123 changes: 123 additions & 0 deletions cmd/cluster/loggincheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package cluster

import (
"context"
"fmt"
"log"
"os"

sdk "github.com/openshift-online/ocm-sdk-go"
"github.com/openshift/osdctl/internal/utils/globalflags"
"github.com/openshift/osdctl/pkg/utils"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"

cmdutil "k8s.io/kubectl/pkg/cmd/util"
)

const loggingLabel string = "ext-managed.openshift.io/extended-logging-support"

// loggingCheckOptions defines the struct for running loggingCheck command
// This command requires the ocm API Token https://cloud.redhat.com/openshift/token be available in the OCM_TOKEN env variable.

type loggingCheckOptions struct {
output string
verbose bool
clusterID string

genericclioptions.IOStreams
GlobalOptions *globalflags.GlobalOptions
}

// newCmdloggingCheck implements the loggingCheck command to show the logging support status of a cluster
func newCmdloggingCheck(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, globalOpts *globalflags.GlobalOptions) *cobra.Command {
ops := newloggingCheckOptions(streams, flags, globalOpts)
loggingCheckCmd := &cobra.Command{
Use: "loggingCheck",
Short: "Shows the logging support status of a specified cluster",
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(ops.complete(cmd, args))
cmdutil.CheckErr(ops.run())
},
}
loggingCheckCmd.Flags().BoolVarP(&ops.verbose, "verbose", "", false, "Verbose output")

return loggingCheckCmd
}

func newloggingCheckOptions(streams genericclioptions.IOStreams, flags *genericclioptions.ConfigFlags, globalOpts *globalflags.GlobalOptions) *loggingCheckOptions {
return &loggingCheckOptions{
IOStreams: streams,
GlobalOptions: globalOpts,
}
}

func (o *loggingCheckOptions) complete(cmd *cobra.Command, args []string) error {

if len(args) != 1 {
return cmdutil.UsageErrorf(cmd, "Provide exactly one cluster ID")
}

o.clusterID = args[0]
o.output = o.GlobalOptions.Output

return nil
}

func (o *loggingCheckOptions) run() error {

// Create a context:
ctx := context.Background()
//The ocm
token := os.Getenv("OCM_TOKEN")
if token == "" {
ocmToken, err := utils.GetOCMAccessToken()
if err != nil {
log.Fatalf("OCM token not set. Please configure it using the OCM_TOKEN evnironment variable or the ocm cli")
os.Exit(1)
}
token = *ocmToken
}
connection, err := sdk.NewConnectionBuilder().
Tokens(token).
Build()
if err != nil {
fmt.Fprintf(os.Stderr, "Can't build connection: %v\n", err)
os.Exit(1)
}
defer connection.Close()

// Get the client for the resource that manages the collection of clusters:
collection := connection.ClustersMgmt().V1().Clusters()
// Get the labels externally available for the cluster
resource := collection.Cluster(o.clusterID).ExternalConfiguration().Labels()
// Send the request to retrieve the list of external cluster labels:
response, err := resource.List().SendContext(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't retrieve cluster labels: %v\n", err)
os.Exit(1)
}

labels, ok := response.GetItems()
// If there are no labels, then logging is not SREP supported
if !ok {
fmt.Printf("Cluster logging not SREP supported\n")
return nil
}

for _, label := range labels.Slice() {
if l, ok := label.GetKey(); ok {
// If the label is found as the key, we know its an SREP supported logging stack
if l == loggingLabel {
fmt.Printf("Cluster logging SREP supported for the target cluster\n")
return nil
}
}
}

fmt.Printf("Cluster logging not SREP supported\n")

return nil
}

0 comments on commit aaa1cd7

Please sign in to comment.