Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the main package to use Cobra #15

Merged
merged 5 commits into from
Jun 14, 2018
Merged

Refactor the main package to use Cobra #15

merged 5 commits into from
Jun 14, 2018

Conversation

xmudrii
Copy link
Owner

@xmudrii xmudrii commented Jun 8, 2018

This PR refactors the main package to move logic to helper functions and to utilize Cobra for CLI.
PR #16 updates documentation to reflect changes made in this PR.

Closes #5
Closes #7
Closes #12

// Config is configuration struct for EtcdProxyController. It's used to wire CLI and the controller.
type EtcdProxyController struct {
// EtcdProxy contains information about the controller, such as its namespace and etcd proxy image name.
EtcdProxy *options.EtcdProxyOptions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need a copy of this struct here.

}

// EtcdProxyOptions type is used to pass information from cli to the controller.
type EtcdProxyOptions struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call them config here instead of options

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed and moved to EtcdProxyController package as proposed by @deads2k.

@@ -0,0 +1,51 @@
/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

followup: seems like you should remove this header from the files.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove all license headers from files in a follow-up PR.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created issue #17.

restclient "k8s.io/client-go/rest"
)

// Config is configuration struct for EtcdProxyController. It's used to wire CLI and the controller.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

godoc format please

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


// CoreEtcdOptions type is used to wire the core etcd information used by controller to create ReplicaSets.
type CoreEtcdOptions struct {
URL string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think of these like an API. please doc each field.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

ControllerNamespace string

// KubeconfigPath is used to obtain path to kubeconfig, used to create kubeclients.
KubeconfigPath string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks odd. Why would you have this and the restclient.Config ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to have KubeconfigPath passed from CLI and then once config is built, to get restclient.Config.

I have not done the transition between Options and Config well. I'll pay attention to this.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use only restclient.Config.

}

// initControllerClientSets returns kubernetes clientset and etcdproxy clientset.
func initControllerClientSets(kubeconfig *restclient.Config) (*kubernetes.Clientset, *clientset.Clientset, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: single use, single path helper functions aren't super useful.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated functions for both Clientsets and Informers.

func controllerNamespace(namespace string) (string, error) {
if namespace != "" {
return namespace, nil
} else if data, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no need for else

}
if data, er {
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

)

// Config is configuration struct for EtcdProxyController. It's used to wire CLI and the controller.
type EtcdProxyController struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expected this directly in the controller package.

Copy link
Owner Author

@xmudrii xmudrii Jun 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the controller package, but decided to leave the struct definitions in the config.go file. Is that okay?

@@ -32,6 +32,7 @@ import (

etcdclient "github.com/xmudrii/etcdproxy-controller/pkg/client/clientset/versioned/fake"
etcdlisters "github.com/xmudrii/etcdproxy-controller/pkg/client/listers/etcd/v1alpha1"
"github.com/xmudrii/etcdproxy-controller/pkg/options"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dep strikes me as odd

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Looks like that one caused import cycles as well.

}

func NewCoreEtcdOptions() *CoreEtcdOptions {
return &CoreEtcdOptions{}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the construct of setting the defaults here, so

{
CAConfigMapName: "etcd-coreserving-ca",
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need to rename the function to something like NewCoreEtcdDefaultOptions, or NewCoreEtcdOptions is good enough?


func (e *EtcdProxyOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&e.CoreEtcdOptions.URL, "etcd-core-url", "", "The address of the core etcd server.")
fs.StringVar(&e.CoreEtcdOptions.CAConfigMapName, "etcd-core-ca-configmap", "etcd-coreserving-ca", "The name of the ConfigMap where CA is stored.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you set defaults in the actual object, then this becomes fs.StringVar(&e.CoreEtcdOptions.CAConfigMapName, "etcd-core-ca-configmap", e.CoreEtcdOptions.CAConfigMapName, "The name of the ConfigMap where CA is stored.") and someone can override them.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds much better. Updated.

@xmudrii xmudrii added ready-for-review Indicates that a Pull Request is ready for review. and removed do-not-merge/wip Indicates Work-In-Progress Pull Requests. labels Jun 13, 2018
@xmudrii xmudrii requested a review from sttts June 13, 2018 23:16
}

// EtcdProxyOptions type is used to pass information from cli to the controller.
type EtcdProxyOptions struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question as above: are these the EtcdProxyControllerOptions?

Copy link
Owner Author

@xmudrii xmudrii Jun 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

// EtcdProxyControllerConfig type is used to pass information from cli to the controller.
type EtcdProxyControllerConfig struct {
// CoreEtcdOptions contains information needed to wire up ReplicaSets and the core etcd.
CoreEtcdOptions *CoreEtcdConfig
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strip Options postfix

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, but GitHub doesn't see the change for some reason.

// etcdProxyOptions is used to wire information used by controller to create ReplicaSets.
etcdProxyOptions *EtcdProxyOptions
// etcdProxyControllerConfig is used to wire information used by controller to create ReplicaSets.
etcdProxyControllerConfig *EtcdProxyControllerConfig
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just call it config

@@ -119,7 +101,7 @@ func NewEtcdProxyController(
replicasetsInformer appsinformers.ReplicaSetInformer,
servicesInformer corev1informers.ServiceInformer,
etcdstorageInformer informers.EtcdStorageInformer,
etcdProxyOptions *EtcdProxyOptions) *EtcdProxyController {
etcdProxyControllerConfig *EtcdProxyControllerConfig) *EtcdProxyController {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just config

// EtcdProxyControllerOptions type is used to pass information from cli to the controller.
type EtcdProxyControllerOptions struct {
// CoreEtcdOptions contains information needed to wire up ReplicaSets and the core etcd.
CoreEtcdOptions *CoreEtcdOptions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strip Options postfix

@xmudrii xmudrii force-pushed the cobra branch 2 times, most recently from 9a96c22 to bdd7604 Compare June 14, 2018 12:49
@xmudrii xmudrii force-pushed the cobra branch 3 times, most recently from 26db878 to f3793a7 Compare June 14, 2018 13:45
main.go Outdated
glog.Fatalf("Error detecting controller namespace: %s", err.Error())
cmd := controller.NewCommandEtcdProxyControllerStart(stopCh)
if err := cmd.Execute(); err != nil {
fmt.Errorf("unable to run etcdproxy command: %v", err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually print the error.

fmt.Fprintf(os.Stderr, message)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, reading it, just glog.Fatal(err) will do

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

cmd := &cobra.Command{
Short: "Start EtcdProxyController",
Long: "Start EtcdProxyController",
RunE: func(c *cobra.Command, args []string) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I discovered that this prints a pretty useless usage message. I refactored my command to simply glog.Fatal on errors

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for me. I think this should be fixed now.

@xmudrii xmudrii force-pushed the cobra branch 3 times, most recently from 279b2f9 to ff75adf Compare June 14, 2018 19:19
return utilerrors.NewAggregate(errors)
}

func (e *EtcdProxyControllerOptions) Config() (*etcdproxy.EtcdProxyControllerConfig, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I prefer the ApplyTo construct so that I can easily have multiple options applied to different aspects of the same config type.

Copy link
Collaborator

@deads2k deads2k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the nit. You can update that later

@deads2k
Copy link
Collaborator

deads2k commented Jun 14, 2018

Looks like @sttts comments are addressed.

lgtm. merging

@deads2k deads2k merged commit 8f43b92 into master Jun 14, 2018
@xmudrii xmudrii deleted the cobra branch June 14, 2018 19:37
@xmudrii xmudrii changed the title Refactor the main package to use cobra and update documentation Refactor the main package to use Cobra Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-for-review Indicates that a Pull Request is ready for review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants