Skip to content

Commit 664568e

Browse files
Merge pull request #43 from itdove/add-log-statement
Add some logs
2 parents ad23688 + 2eb78d5 commit 664568e

File tree

7 files changed

+38
-9
lines changed

7 files changed

+38
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- Add `clusteradm delete token` [Create cmd to revoke the bootstrap token](https://github.com/open-cluster-management-io/clusteradm/issues/23)
55
- 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)
66
- [Enable external cluster config to support oidc](https://github.com/open-cluster-management-io/clusteradm/issues/38)
7+
- Add klog flags [Log will be helpful to contributors](https://github.com/open-cluster-management-io/clusteradm/issues/36)
8+
79
## Breaking Changes
810

911
## Changes

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ clusteradm <cmd> [subcmd] [flags]
4040
```
4141

4242
- 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.
43+
- Each command must support the flag `--dry-run`.
44+
- 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.
4345

4446

4547
## Resources

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,16 @@ See our [Contributing Document](CONTRIBUTING.md) for more information.
4747

4848
## Commands
4949

50-
The commands are composed of a verb and a noun and then a number of parameters.
50+
The commands are composed of a verb and a noun and then a number of parameters.
51+
Logs can be gather by setting the klog flag `-v`.
52+
To get the logs in a separate file:
53+
```
54+
clusteradm <subcommand> -v <level> 2><your_logfile>
55+
```
56+
or
57+
```
58+
clusteradm <subcommand> -v 99 --logtostderr=false --log-file=<your_log_file>
59+
```
5160

5261
### version
5362

cmd/clusteradm.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func main() {
4444
matchVersionKubeConfigFlags.AddFlags(flags)
4545

4646
klog.InitFlags(nil)
47-
root.PersistentFlags().AddGoFlagSet(flag.CommandLine)
47+
flags.AddGoFlagSet(flag.CommandLine)
4848

4949
f := cmdutil.NewFactory(matchVersionKubeConfigFlags)
5050
root.SetGlobalNormalizationFunc(cliflag.WarnWordSepNormalizeFunc)
@@ -81,6 +81,9 @@ func main() {
8181
}
8282
groups.Add(root)
8383
err := root.Execute()
84+
if err != nil {
85+
klog.V(1).ErrorS(err, "Error:")
86+
}
8487
klog.Flush()
8588
if err != nil {
8689
os.Exit(1)

pkg/cmd/accept/exec.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"k8s.io/apimachinery/pkg/util/sets"
1414
"k8s.io/apimachinery/pkg/util/wait"
1515
"k8s.io/client-go/kubernetes"
16+
"k8s.io/klog/v2"
1617
"open-cluster-management.io/clusteradm/pkg/helpers"
1718

1819
"github.com/spf13/cobra"
@@ -29,6 +30,7 @@ const (
2930
)
3031

3132
func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
33+
klog.V(1).InfoS("accept options:", "dry-run", o.ClusteradmFlags.DryRun, "clusters", o.clusters, "wait", o.wait)
3234
alreadyProvidedCluster := make(map[string]bool)
3335
clusters := make([]string, 0)
3436
if o.clusters != "" {
@@ -43,7 +45,7 @@ func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
4345
} else {
4446
return fmt.Errorf("values or name are missing")
4547
}
46-
48+
klog.V(3).InfoS("values:", "clusters", o.values.clusters)
4749
return nil
4850
}
4951

@@ -70,10 +72,14 @@ func (o *Options) run() error {
7072
func (o *Options) runWithClient(kubeClient *kubernetes.Clientset, clusterClient *clusterclientset.Clientset) (err error) {
7173
for _, clusterName := range o.values.clusters {
7274
if o.wait == 0 {
73-
_, err = o.accept(kubeClient, clusterClient, clusterName)
75+
var csrApproved bool
76+
csrApproved, err = o.accept(kubeClient, clusterClient, clusterName, false)
77+
if err == nil && !csrApproved {
78+
err = fmt.Errorf("no CSR to approve for cluster %s", clusterName)
79+
}
7480
} else {
7581
err = wait.PollImmediate(1*time.Second, time.Duration(o.wait)*time.Second, func() (bool, error) {
76-
return o.accept(kubeClient, clusterClient, clusterName)
82+
return o.accept(kubeClient, clusterClient, clusterName, true)
7783
})
7884
}
7985
if err != nil {
@@ -83,8 +89,8 @@ func (o *Options) runWithClient(kubeClient *kubernetes.Clientset, clusterClient
8389
return nil
8490
}
8591

86-
func (o *Options) accept(kubeClient *kubernetes.Clientset, clusterClient *clusterclientset.Clientset, clusterName string) (bool, error) {
87-
csrApproved, err := o.approveCSR(kubeClient, clusterName)
92+
func (o *Options) accept(kubeClient *kubernetes.Clientset, clusterClient *clusterclientset.Clientset, clusterName string, waitMode bool) (bool, error) {
93+
csrApproved, err := o.approveCSR(kubeClient, clusterName, waitMode)
8894
if err != nil {
8995
return false, err
9096
}
@@ -98,7 +104,7 @@ func (o *Options) accept(kubeClient *kubernetes.Clientset, clusterClient *cluste
98104
return false, nil
99105
}
100106

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

141147
//no csr found
142148
if csr == nil {
143-
fmt.Printf("no CSR to approve for cluster %s\n", clusterName)
149+
if waitMode {
150+
fmt.Printf("no CSR to approve for cluster %s\n", clusterName)
151+
}
144152
return false, nil
145153
}
146154
//if dry-run don't approve

pkg/cmd/init/exec.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import (
1616
"k8s.io/apimachinery/pkg/util/wait"
1717
"k8s.io/client-go/kubernetes"
1818
"k8s.io/client-go/util/retry"
19+
"k8s.io/klog/v2"
1920
)
2021

2122
func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
23+
klog.V(1).InfoS("init options:", "dry-run", o.ClusteradmFlags.DryRun, "force", o.force, "output-file", o.outputFile)
2224
o.values = Values{
2325
Hub: Hub{
2426
TokenID: helpers.RandStringRunes_az09(6),

pkg/cmd/join/exec.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/ghodss/yaml"
99
"k8s.io/apimachinery/pkg/api/errors"
10+
"k8s.io/klog/v2"
1011

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

@@ -22,13 +23,15 @@ import (
2223
)
2324

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

2628
o.values = Values{
2729
ClusterName: o.clusterName,
2830
Hub: Hub{
2931
APIServer: o.hubAPIServer,
3032
},
3133
}
34+
klog.V(3).InfoS("values:", "clusterName", o.values.ClusterName, "hubAPIServer", o.values.Hub.APIServer)
3235
return nil
3336
}
3437

0 commit comments

Comments
 (0)