Skip to content

Regroups code to use subpackages. Finishes issue #66#145

Merged
knative-prow-robot merged 1 commit intoknative:masterfrom
maximilien:subpackages
Jun 5, 2019
Merged

Regroups code to use subpackages. Finishes issue #66#145
knative-prow-robot merged 1 commit intoknative:masterfrom
maximilien:subpackages

Conversation

@maximilien
Copy link
Copy Markdown
Contributor

Creates three subpackages for now:

  1. kn/commands/common - inludes types.go for common
    struct. Also includes humman_readable_flags.go
    which could be split into the specific command
    package. But I would do this as a refactor later
  2. kn/commands/service - all 'kn service *' commands
  3. kn/commands/revision - all 'kn revision *' commands

@googlebot googlebot added the cla: yes Indicates the PR's author has signed the CLA. label May 28, 2019
@knative-prow-robot knative-prow-robot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label May 28, 2019
@maximilien
Copy link
Copy Markdown
Contributor Author

maximilien commented May 28, 2019

/assign @sixolet @cppforlife
cc: @rhuss @navidshaikh

OK so as promised this regroups things. Overall it should be a good start. I have two issues with it that we can solve later during refactors. If you agree. Here they are:

  1. I had to export KnCfgFile in pkg/kn/commands/common/types.go and don't like that. The main reason I had to do it this way is this var is needed when setting common flag in pkg/kn/commands/root.go. Would be better not to make it public... but how to pass in this:
	rootCmd.PersistentFlags().StringVar(&common.KubeCfgFile, "kubeconfig", "", "kubectl config file (default is $HOME/.kube/config)")

without having to make it public? Might have to have two vars and sync them, which is also not nice.

  1. The file pkg/kn/commands/namespaced.go clearly belongs in the common package. However, the file pkg/kn/commands/human_readable_flags.go aggregates functions for both service and revision subcommands. Not clear that these functions would be useful separated into their appropriate sub packages. I tried to keep only common stuff but then ran into all kinds of dependency issues. To avoid too many changes I consolidated and figured we could refactor after.

Anyhow, let me know your thoughts.

Also need to add tests for pkg/kn/commands/human_readable_flags.go or whatever we end up with. It currently has none.

@rhuss
Copy link
Copy Markdown
Contributor

rhuss commented May 29, 2019

Thanks a lot ! I will have a look tomorrow (hopefully), sorry won't get to it today.

@rhuss
Copy link
Copy Markdown
Contributor

rhuss commented May 29, 2019

BTW, tests (unit and integration) are failing.

@maximilien
Copy link
Copy Markdown
Contributor Author

maximilien commented May 29, 2019

Thanks a lot ! I will have a look tomorrow (hopefully), sorry won't get to it today.

Still making changes. Tests failures require some more fiddling with packages. Wait for next update.

Copy link
Copy Markdown
Contributor

@sixolet sixolet left a comment

Choose a reason for hiding this comment

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

I think the main thing I'm worried about is division of responsiblity for flags.

Comment thread pkg/kn/commands/common/human_readable_flags.go Outdated
Comment thread pkg/kn/commands/common/human_readable_flags.go Outdated
Comment thread pkg/kn/commands/common/human_readable_flags.go Outdated
@maximilien maximilien force-pushed the subpackages branch 3 times, most recently from 4ac1c77 to 27a8537 Compare May 29, 2019 23:52
@maximilien
Copy link
Copy Markdown
Contributor Author

maximilien commented May 30, 2019

OK all. Current version does pass tests. I had to refactor all tests to use an additional pkg/kn/utils package that includes some tests utilities. In there I had to redo a test version of the NewKnCommand otherwise since the original needs to add the subcommands it creates a cycle in the tests.

The end result works. The code is tiddy and well packaged and done (except for the refactoring I need to do for the HummanReadableFlag as per discussion with @sixolet above) but otherwise is well structured IMO.

The tests suffered a bit since I had to duplicate some code in the test version of the NewKnCommand, specifically see file: pkg/kn/utils/test_utils.go.

Also we now have two packages (core and utils) that have one file :( Could not combine nor move w/o creating cycle nightmares. Generally, I am OK since perhaps in time we will have more needs for both so they won't be degenerate packages forever.

Let me know your thoughts: @cppforlife @sixolet @rhuss

@maximilien
Copy link
Copy Markdown
Contributor Author

/test pull-knative-client-integration-tests

Comment thread pkg/kn/commands/revision/revision_describe_test.go Outdated
Comment thread pkg/kn/commands/service/service.go
Copy link
Copy Markdown
Contributor

@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

The PR looks good to me, nice job @maximilien !

I have some minor comments about naming and the final structure, though:

  • Why do we need the kn directory level below pkg/ ? I think "commands" can be moved up one level and kn removed.
  • Can't we put "root.go", "completion.go" and "version.go" directly into the "commands" package ? This would get rid of the 'etc/' and 'core/' directories/packages.
  • Can we please rename the package for service and revision to command_revision and command_service (or cmd_revision / cmd_service). This would make it clear from the package name that its about a command and dedicated libs (without references to cobra commands) can be named like 'serving' and 'eventing' without adding to much of confusion.

After this bigger restructuring, we can still fine-tune and e.g. refactor the utils/test_helper.go which imo shouldn't be there, too.

As another refactoring tasks which I see after this move are:

  • Move as much functionality as possible out of the commands in cobra independent libraries (i.e. into the serving package) so that they can be reused easily. E.g. 'KnServiceCreateCommand' contains too much logic (which even tends to grow e.g. when #156 is added). It should be only a thin layer of a more generic lib imo
  • Refactor tests to become more unit tests. E.g. commands should be tested only that they propagate the proper info to the library calls (e.g. in serving), the library calls should then use the serving mock library. Only a suggestion, but tests currently are not easy to maintain.

But as mentioned, I would be very happy if this PR gets merged soonish.

@rhuss
Copy link
Copy Markdown
Contributor

rhuss commented Jun 3, 2019

@maximilien The decoupling works nicely, just as a comment for another approach which might be useful, too, would be to reverse the direction of the commands registration (i.e. letting sub-commands register themselves). It's more a syntactic thing, and I think it can also break dep cycles more easily (maybe even without the introduction of third common package for the KnParams definition).

That's the idea (not fully tested):

  • root.go
package commands

// That unnamed import is crucial here
import _ cmd_service

typ KnParams struct {
  // ...
}

var SubCommandFactories = make([]func(*KnParams) *cobra.Command,0)

func NewKnCommand(params ...common.KnParams) *cobra.Command {

	for _, fact := range SubCommandFactories {
       rootCmd.AddCommand(fact.newCommand(p))
    }
}
  • service.go
package cmd_service

import commands

func init() {
    commands.SubCommandFactories = append(commands.SubCommandFactories, newServiceCreateFactor)
}

func newServiceCreateFactory(p *commands.KnParams) *cobra.Command {
  ////
}

You then can easily add new commands by adding an unnamed import which is used to call its init() method.

I've seen that pattern in the wild quite a bit (I darkly remember its used also for collecting k8s resource schemes), so might make sense here, too.

@maximilien
Copy link
Copy Markdown
Contributor Author

I've seen that pattern in the wild quite a bit (I darkly remember its used also for collecting k8s resource schemes), so might make sense here, too.

Let's explore this after we agree on the package naming and whether commons and etc stay or we move things to commands. I see this as an evolution.

Also, most importantly, I need to resolve various conflicts as we merge the PRs newer to this one...

Best.

@maximilien
Copy link
Copy Markdown
Contributor Author

OK @sixolet and @rhuss this last pushed code addresses almost everything that we discussed today. Missing are:

  1. simplify tests and removing kn/commands/test_helper.go and I have issue Improve unit tests #161 to address that
  2. simplify command creation and remove package kn/commands/core/root.go for which I opened issue Refactor root command creation to avoid it knowing about packages of sub-commands #164. You cannot put it in commands as it creates a cycle. We need to refactor that code to pass factories or similar indirection as @rhuss mentioned above
  3. need to rebase to include latest commits since this PR. I plan to do that once you give me your OK on where are at. Did not want to go to the rebase / merge pain and only find out we have more to address.

Let me know if you are OK with this so I can rebase and submit last version of this PR :)

cc: @cppforlife did not address your test comment. Will do so in #161

Copy link
Copy Markdown
Contributor

@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

Cool, thanks a ton @maximilien ! Lgtm (with some minor nit-picks), I'm good with going forward.

return &HumanPrintFlags{}
}

// Private functions
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When the following functions are private, why are the names uppercased ? (very likely in the original code, so not really part of the PR. nevertheless ;-)

// See the License for the specific language governing permissions and
// limitations under the License.

package commands
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would call the file helper_test.go so that it gets compiled and used only during testing.

@rhuss
Copy link
Copy Markdown
Contributor

rhuss commented Jun 5, 2019

@maximilien btw, rebasing on upstream/master was surprisingly easy, only some minor conflicts in import statements. I attach a patch with the rebased changed (could be applied on top of your branch) for your reference, but wouldn't apply it (just as a reference) as you would come into troubles with a rebase.

I could push my rebased branch to yours (if you allow), but I'm pretty sure doing the rebase yourself is easy, too.

Thanks again for the quick update!

rebase.txt

@maximilien
Copy link
Copy Markdown
Contributor Author

Thanks @rhuss that makes me looking forward to it now :)

Will do it in a few hours. Got a few company stuff to complete in the AM.

More soon. Best, Max

@maximilien
Copy link
Copy Markdown
Contributor Author

Looks like a back end infra failure

/test pull-knative-client-unit-tests

@maximilien
Copy link
Copy Markdown
Contributor Author

/retest

Creates four subpackages for now:

1. kn/commands - inludes types.go for common
   struct and other common files and misc commands
2. kn/commands/service - all 'kn service *' commands
3. kn/commands/revision - all 'kn revision *' commands
4. kn/core - contains the root.go and other top level
   for code and testing
5. refactors:
   a. split .../commons/human_readable_flags.go into three
   b. modifies the HumanReadableFlags.ToPrinter to get pass
      a function that sets the columns and fields

Had to refactor all tests to avoid cycles.
@knative-metrics-robot
Copy link
Copy Markdown

The following is the coverage report on pkg/.
Say /test pull-knative-client-go-coverage to re-run this coverage report

File Old Coverage New Coverage Delta
pkg/kn/commands/human_readable_flags.go 83.3% 0.0% -83.3
pkg/kn/commands/revision/human_readable_flags.go Do not exist 94.7%
pkg/kn/commands/revision/revision.go Do not exist 100.0%
pkg/kn/commands/revision/revision_describe.go Do not exist 79.2%
pkg/kn/commands/revision/revision_get.go Do not exist 80.0%
pkg/kn/commands/revision/revision_get_flags.go Do not exist 46.7%
pkg/kn/commands/service/configuration_edit_flags.go Do not exist 80.4%
pkg/kn/commands/service/human_readable_flags.go Do not exist 95.0%
pkg/kn/commands/service/service.go Do not exist 100.0%
pkg/kn/commands/service/service_create.go Do not exist 66.7%
pkg/kn/commands/service/service_delete.go Do not exist 18.8%
pkg/kn/commands/service/service_describe.go Do not exist 79.2%
pkg/kn/commands/service/service_get.go Do not exist 80.0%
pkg/kn/commands/service/service_get_flags.go Do not exist 46.7%
pkg/kn/commands/service/service_update.go Do not exist 75.0%
pkg/kn/commands/test_helper.go Do not exist 0.0%
pkg/kn/commands/types.go Do not exist 0.0%
pkg/kn/commands/version.go 25.0% 0.0% -25.0

@maximilien
Copy link
Copy Markdown
Contributor Author

maximilien commented Jun 5, 2019

So right @rhuss, rebasing was a breeze 🏖

All green so good to merge @sixolet and @cppforlife

@sixolet
Copy link
Copy Markdown
Contributor

sixolet commented Jun 5, 2019

/approve
/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Jun 5, 2019
@knative-prow-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: maximilien, rhuss, sixolet

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 5, 2019
@knative-prow-robot knative-prow-robot merged commit 0800d7c into knative:master Jun 5, 2019
@rhuss rhuss added this to the v0.2.0 milestone Jun 9, 2019
@maximilien maximilien deleted the subpackages branch June 10, 2019 18:46
navidshaikh pushed a commit to navidshaikh/client that referenced this pull request Nov 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants