Skip to content

Commit

Permalink
Merge pull request #43 from itdove/add-log-statement
Browse files Browse the repository at this point in the history
Add some logs
  • Loading branch information
openshift-merge-robot authored Jul 5, 2021
2 parents ad23688 + 2eb78d5 commit 664568e
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
- Add `clusteradm delete token` [Create cmd to revoke the bootstrap token](https://github.com/open-cluster-management-io/clusteradm/issues/23)
- Add the capability of injecting rendering functions [While rendering a yaml I want to inject my own functions](https://github.com/open-cluster-management-io/clusteradm/issues/37)
- [Enable external cluster config to support oidc](https://github.com/open-cluster-management-io/clusteradm/issues/38)
- Add klog flags [Log will be helpful to contributors](https://github.com/open-cluster-management-io/clusteradm/issues/36)

## Breaking Changes

## Changes
Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ clusteradm <cmd> [subcmd] [flags]
```

- Each cmd/subcmd are in a package, the code is split in 3 files: The [cmd.go](pkg/cmd/version/cmd.go) which creates the cobra command, the [options.go](pkg/cmd/version/options.go) which defines the different option parameters for the command and the the [exec.go](pkg/cmd/version/exec.go) which contains the code to execute the command.
- Each command must support the flag `--dry-run`.
- The command uses [klog V2](https://github.com/kubernetes/klog) as logging package. All messages must be using `klog.V(x)`, in rare exception `klog.Error` and `klog.Warning` can be used.


## Resources
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ See our [Contributing Document](CONTRIBUTING.md) for more information.

## Commands

The commands are composed of a verb and a noun and then a number of parameters.
The commands are composed of a verb and a noun and then a number of parameters.
Logs can be gather by setting the klog flag `-v`.
To get the logs in a separate file:
```
clusteradm <subcommand> -v <level> 2><your_logfile>
```
or
```
clusteradm <subcommand> -v 99 --logtostderr=false --log-file=<your_log_file>
```

### version

Expand Down
5 changes: 4 additions & 1 deletion cmd/clusteradm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
matchVersionKubeConfigFlags.AddFlags(flags)

klog.InitFlags(nil)
root.PersistentFlags().AddGoFlagSet(flag.CommandLine)
flags.AddGoFlagSet(flag.CommandLine)

f := cmdutil.NewFactory(matchVersionKubeConfigFlags)
root.SetGlobalNormalizationFunc(cliflag.WarnWordSepNormalizeFunc)
Expand Down Expand Up @@ -81,6 +81,9 @@ func main() {
}
groups.Add(root)
err := root.Execute()
if err != nil {
klog.V(1).ErrorS(err, "Error:")
}
klog.Flush()
if err != nil {
os.Exit(1)
Expand Down
22 changes: 15 additions & 7 deletions pkg/cmd/accept/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"open-cluster-management.io/clusteradm/pkg/helpers"

"github.com/spf13/cobra"
Expand All @@ -29,6 +30,7 @@ const (
)

func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
klog.V(1).InfoS("accept options:", "dry-run", o.ClusteradmFlags.DryRun, "clusters", o.clusters, "wait", o.wait)
alreadyProvidedCluster := make(map[string]bool)
clusters := make([]string, 0)
if o.clusters != "" {
Expand All @@ -43,7 +45,7 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
} else {
return fmt.Errorf("values or name are missing")
}

klog.V(3).InfoS("values:", "clusters", o.values.clusters)
return nil
}

Expand All @@ -70,10 +72,14 @@ func (o *Options) run() error {
func (o *Options) runWithClient(kubeClient *kubernetes.Clientset, clusterClient *clusterclientset.Clientset) (err error) {
for _, clusterName := range o.values.clusters {
if o.wait == 0 {
_, err = o.accept(kubeClient, clusterClient, clusterName)
var csrApproved bool
csrApproved, err = o.accept(kubeClient, clusterClient, clusterName, false)
if err == nil && !csrApproved {
err = fmt.Errorf("no CSR to approve for cluster %s", clusterName)
}
} else {
err = wait.PollImmediate(1*time.Second, time.Duration(o.wait)*time.Second, func() (bool, error) {
return o.accept(kubeClient, clusterClient, clusterName)
return o.accept(kubeClient, clusterClient, clusterName, true)
})
}
if err != nil {
Expand All @@ -83,8 +89,8 @@ func (o *Options) runWithClient(kubeClient *kubernetes.Clientset, clusterClient
return nil
}

func (o *Options) accept(kubeClient *kubernetes.Clientset, clusterClient *clusterclientset.Clientset, clusterName string) (bool, error) {
csrApproved, err := o.approveCSR(kubeClient, clusterName)
func (o *Options) accept(kubeClient *kubernetes.Clientset, clusterClient *clusterclientset.Clientset, clusterName string, waitMode bool) (bool, error) {
csrApproved, err := o.approveCSR(kubeClient, clusterName, waitMode)
if err != nil {
return false, err
}
Expand All @@ -98,7 +104,7 @@ func (o *Options) accept(kubeClient *kubernetes.Clientset, clusterClient *cluste
return false, nil
}

func (o *Options) approveCSR(kubeClient *kubernetes.Clientset, clusterName string) (bool, error) {
func (o *Options) approveCSR(kubeClient *kubernetes.Clientset, clusterName string, waitMode bool) (bool, error) {
csrs, err := kubeClient.CertificatesV1().CertificateSigningRequests().List(context.TODO(),
metav1.ListOptions{
LabelSelector: fmt.Sprintf("%v = %v", clusterLabel, clusterName),
Expand Down Expand Up @@ -140,7 +146,9 @@ func (o *Options) approveCSR(kubeClient *kubernetes.Clientset, clusterName strin

//no csr found
if csr == nil {
fmt.Printf("no CSR to approve for cluster %s\n", clusterName)
if waitMode {
fmt.Printf("no CSR to approve for cluster %s\n", clusterName)
}
return false, nil
}
//if dry-run don't approve
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/init/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/retry"
"k8s.io/klog/v2"
)

func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
klog.V(1).InfoS("init options:", "dry-run", o.ClusteradmFlags.DryRun, "force", o.force, "output-file", o.outputFile)
o.values = Values{
Hub: Hub{
TokenID: helpers.RandStringRunes_az09(6),
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/join/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/ghodss/yaml"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/klog/v2"

// "k8s.io/apimachinery/pkg/util/wait"

Expand All @@ -22,13 +23,15 @@ import (
)

func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
klog.V(1).InfoS("join options:", "dry-run", o.ClusteradmFlags.DryRun, "cluster", o.clusterName, "api-server", o.hubAPIServer, o.outputFile)

o.values = Values{
ClusterName: o.clusterName,
Hub: Hub{
APIServer: o.hubAPIServer,
},
}
klog.V(3).InfoS("values:", "clusterName", o.values.ClusterName, "hubAPIServer", o.values.Hub.APIServer)
return nil
}

Expand Down

0 comments on commit 664568e

Please sign in to comment.