-
Notifications
You must be signed in to change notification settings - Fork 3
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
Conversation
7666b18
to
17b9a69
Compare
pkg/cmd/controller/config/config.go
Outdated
// 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 |
There was a problem hiding this comment.
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.
2f03a8e
to
392282b
Compare
pkg/cmd/controller/config/config.go
Outdated
} | ||
|
||
// EtcdProxyOptions type is used to pass information from cli to the controller. | ||
type EtcdProxyOptions struct { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
pkg/cmd/controller/config/config.go
Outdated
@@ -0,0 +1,51 @@ | |||
/* |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created issue #17.
pkg/cmd/controller/config/config.go
Outdated
restclient "k8s.io/client-go/rest" | ||
) | ||
|
||
// Config is configuration struct for EtcdProxyController. It's used to wire CLI and the controller. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
godoc format please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
pkg/cmd/controller/config/config.go
Outdated
|
||
// CoreEtcdOptions type is used to wire the core etcd information used by controller to create ReplicaSets. | ||
type CoreEtcdOptions struct { | ||
URL string |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
pkg/cmd/controller/config/config.go
Outdated
ControllerNamespace string | ||
|
||
// KubeconfigPath is used to obtain path to kubeconfig, used to create kubeclients. | ||
KubeconfigPath string |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
pkg/cmd/controller/start.go
Outdated
} | ||
|
||
// initControllerClientSets returns kubernetes clientset and etcdproxy clientset. | ||
func initControllerClientSets(kubeconfig *restclient.Config) (*kubernetes.Clientset, *clientset.Clientset, error) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
pkg/cmd/controller/start.go
Outdated
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 { |
There was a problem hiding this comment.
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 {
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
pkg/cmd/controller/config/config.go
Outdated
) | ||
|
||
// Config is configuration struct for EtcdProxyController. It's used to wire CLI and the controller. | ||
type EtcdProxyController struct { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
pkg/options/options.go
Outdated
} | ||
|
||
func NewCoreEtcdOptions() *CoreEtcdOptions { | ||
return &CoreEtcdOptions{} |
There was a problem hiding this comment.
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",
}
There was a problem hiding this comment.
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?
pkg/options/options.go
Outdated
|
||
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.") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
pkg/options/options.go
Outdated
} | ||
|
||
// EtcdProxyOptions type is used to pass information from cli to the controller. | ||
type EtcdProxyOptions struct { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
pkg/controller/etcdproxy/config.go
Outdated
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strip Options postfix
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just config
pkg/options/options.go
Outdated
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strip Options postfix
9a96c22
to
bdd7604
Compare
26db878
to
f3793a7
Compare
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) |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
pkg/cmd/controller/start.go
Outdated
cmd := &cobra.Command{ | ||
Short: "Start EtcdProxyController", | ||
Long: "Start EtcdProxyController", | ||
RunE: func(c *cobra.Command, args []string) error { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
279b2f9
to
ff75adf
Compare
return utilerrors.NewAggregate(errors) | ||
} | ||
|
||
func (e *EtcdProxyControllerOptions) Config() (*etcdproxy.EtcdProxyControllerConfig, error) { |
There was a problem hiding this comment.
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.
There was a problem hiding this 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
Looks like @sttts comments are addressed. lgtm. merging |
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