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

Add boilerplate for clusterctl #119

Merged
merged 2 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clusterctl/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
clusterctl
1 change: 1 addition & 0 deletions clusterctl/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Contributing Guidelines
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it intentional to have empty conributing guidelines for this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I'm happy to remove the file though, but I wanted to link to something from the README.

61 changes: 61 additions & 0 deletions clusterctl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# clusterctl

`clusterctl` is the SIG-cluster-lifecycle sponsored tool that implements the Cluster API.

Read the [experience doc here](https://docs.google.com/document/d/1-sYb3EdkRga49nULH1kSwuQFf1o6GvAw_POrsNo5d8c/edit#).

## Getting Started

### Prerequisites

Follow the steps listed at [CONTRIBUTING.md](https://github.com/kubernetes/kube-deploy/blob/master/cluster-api/clusterctl/CONTRIBUTING.md) to:
Copy link
Contributor

Choose a reason for hiding this comment

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

Wrong repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed


1. Build the `clusterctl` tool
Copy link
Contributor

Choose a reason for hiding this comment

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

probably will want an example of how to build.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added one

3. Create a `machines.yaml` file configured for your cluster. See the provided template for an example.
Copy link
Contributor

Choose a reason for hiding this comment

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

How come it jumped from 1 to 3?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MD renders ordered lists just fine even if the numbers are wrong here. I'll fix them nevertheless.


### Limitation
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit)
Limitations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed



### Creating a cluster

**NOT YET SUPPORTED!**

Copy link
Contributor

Choose a reason for hiding this comment

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

I would cut out everything other than NOT YET SUPPORTED! in the creating a cluster section till it actually is supported. Similar reasoning to flags, it is best to keep the implementation and documentation in sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair.

In order to create a cluster with the Cluster API, the user will supply these:

* Cluster which defines the spec common across the entire cluster.
* Machine which defines the spec of a machine. Further abstractions of
MachineSets and MachineClass and MachineDeployments are supported as well.
* Extras (optional) spec extras.yaml file with specs of all controllers
(ConfigMaps) that the cluster needs. This would include the Machine controller,
MachineSet controller, Machine Setup ConfigMap etc. Note that this is not a new API
object. There will be defaults running. This will make the tool easily pluggage
(change the controller spec) instead of changing the tool or mucking with flags.

1. Create a cluster: `./clusterctl create cluster -c cluster.yaml -m machines.yaml -e extras.yaml`
Copy link
Contributor

Choose a reason for hiding this comment

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

I would leave the create cluster details out. They can be filled in as the implementation is checked in. That way the docs and reality keep in sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure


### Interacting with your cluster

Once you have created a cluster, you can interact with the cluster and machine
resources using kubectl:

```
$ kubectl get clusters
$ kubectl get machines
$ kubectl get machines -o yaml
```

#### Scaling your cluster

**NOT YET SUPPORTED!**

#### Upgrading your cluster

**NOT YET SUPPORTED!**

#### Node repair

**NOT YET SUPPORTED!**

### Deleting a cluster

**NOT YET SUPPORTED!**
71 changes: 71 additions & 0 deletions clusterctl/cmd/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

Copy link
Contributor

Choose a reason for hiding this comment

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

Would recommend the cluster subcommand files be named with the cluster prefix. That way we do not have confusion when we have other NOUNS that happen to have a similar verb eg. create.

cluster_create.go or clustercreate.go instead of create.go

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now that we are going with the kubectl/kops model of VERB NOUN, this comment is moot?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep. Moot.

import (
"os"

//"fmt"
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it intentional to have a commented out import?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed it. Typo

"github.com/golang/glog"
"github.com/spf13/cobra"
"sigs.k8s.io/cluster-api/errors"
)

type CreateOptions struct {
Cluster string
Machine string
Extras string
}

var co = &CreateOptions{}

var createCmd = &cobra.Command{
Use: "create",
Short: "Create kubernetes cluster",
Long: `Create a kubernetes cluster with one command`,
Run: func(cmd *cobra.Command, args []string) {
if co.Cluster == "" {
glog.Error("Please provide yaml file for cluster definition.")
cmd.Help()
os.Exit(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

We are doing the following:

cmd.Help()
os.Exit(...)

in 4 locations in the Create command and there are similar sections in Delete and Register commands. Theoretically this number will grow as the scope of this application increases. Is there a simplification we can make so that it is only called in a single place for the various error scenarios?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. DOne

}
if co.Machine == "" {
glog.Error("Please provide yaml file for machine definition.")
cmd.Help()
os.Exit(1)
}
if co.Extras == "" {
glog.Error("Please provide a yaml file for extra definitions (controllers, Addons etc).")
cmd.Help()
os.Exit(1)
}
if err := RunCreate(co); err != nil {
glog.Exit(err)
}
},
}

func RunCreate(co *CreateOptions) error {
return errors.NotImplementedError
}
func init() {
createCmd.Flags().StringVarP(&co.Cluster, "cluster", "c", "", "cluster yaml file")
createCmd.Flags().StringVarP(&co.Machine, "machines", "m", "", "machine yaml file")
createCmd.Flags().StringVarP(&co.Extras, "extras", "e", "", "extras yaml file")

RootCmd.AddCommand(createCmd)
Copy link
Contributor

Choose a reason for hiding this comment

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

I thought the goal was something like:

./clusterctl create cluster .....

This seems to produce:

./clusterctl create ....

Or did I miss something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Based on community feedback, we decided to align ourselves with the rest of the tooling -- This is also reflected in https://docs.google.com/document/d/1-sYb3EdkRga49nULH1kSwuQFf1o6GvAw_POrsNo5d8c/edit#.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we are doing VERB NOUN right now. This makes me expect
clusterctl create cluster .....
to be the create commmand instead of
clusterctl create ....
which is what this seems to be doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Added the subcommands

}
56 changes: 56 additions & 0 deletions clusterctl/cmd/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"github.com/golang/glog"
"github.com/spf13/cobra"
"os"
"sigs.k8s.io/cluster-api/errors"
)

type DeleteOptions struct {
ClusterName string
}

var do = &DeleteOptions{}

var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete kubernetes cluster",
Long: `Delete a kubernetes cluster with one command`,
Run: func(cmd *cobra.Command, args []string) {
if do.ClusterName == "" {
glog.Error("Please provide cluster name.")
cmd.Help()
os.Exit(1)
}
if err := RunDelete(); err != nil {
glog.Exit(err)
}
},
}

func RunDelete() error {
return errors.NotImplementedError
}

func init() {
deleteCmd.Flags().StringVarP(&do.ClusterName, "cluster-name", "n", "", "cluster name")

RootCmd.AddCommand(deleteCmd)
}
57 changes: 57 additions & 0 deletions clusterctl/cmd/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"os"

"github.com/golang/glog"
"github.com/spf13/cobra"
"sigs.k8s.io/cluster-api/errors"
)

type RegisterOptions struct {
RegistryEndpoint string
}

var ro = &RegisterOptions{}

var registerCmd = &cobra.Command{
Use: "register",
Copy link
Contributor

Choose a reason for hiding this comment

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

Should register also follow VERB NOUN? ie. "register cluster" instead of just "register"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whenever register is implemented, it can be branched out. For now, I'm leaving it as is.

Copy link
Contributor

Choose a reason for hiding this comment

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

I do not think this is the right approach.

Either we structure it as it is supposed to be structured ie. VERB NOUN or we leave it out entirely and when it is implemented, it is added with the right structure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have decided to remove it and leave it up to implementer to add this cmd as well.

Short: "Register a kubernetes cluster with a Cluster Registry",
Long: `Register a kubernetes cluster with a Cluster Registry`,
Run: func(cmd *cobra.Command, args []string) {
if ro.RegistryEndpoint == "" {
glog.Error("Please provide yaml file for cluster definition.")
cmd.Help()
os.Exit(1)
}
if err := RunRegister(ro); err != nil {
glog.Exit(err)
}
},
}

func RunRegister(co *RegisterOptions) error {
return errors.NotImplementedError
}

func init() {
registerCmd.Flags().StringVarP(&ro.RegistryEndpoint, "registry-endpoint", "r", "", "registry endpoint")

RootCmd.AddCommand(registerCmd)
}
85 changes: 85 additions & 0 deletions clusterctl/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"flag"
"io/ioutil"

"github.com/ghodss/yaml"
"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/apiserver/pkg/util/logs"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"sigs.k8s.io/cluster-api/util"
)

var RootCmd = &cobra.Command{
Use: "clusterctl",
Short: "cluster management",
Long: `Simple kubernetes cluster management`,
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
cmd.Help()
},
}

var (
Copy link
Contributor

Choose a reason for hiding this comment

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

On my previous comment on flags, I do mean all flags on all commands should not be implemented yet. They can be implemented when there is actually functionality to back it. This way the usage and description is accurate to how it is actually used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The flags have been removed, I guess I missed the var.

Copy link
Contributor

Choose a reason for hiding this comment

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

The var seems to still be here. There also still seem to be flags on some of the commands that are not backed by implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Weird my linter didn't complain.

kubeConfig string
)

func Execute() {
if err := RootCmd.Execute(); err != nil {
glog.Exit(err)
}
}

func parseClusterYaml(file string) (*clusterv1.Cluster, error) {
bytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}

cluster := &clusterv1.Cluster{}
err = yaml.Unmarshal(bytes, cluster)
if err != nil {
return nil, err
}

return cluster, nil
}

func parseMachinesYaml(file string) ([]*clusterv1.Machine, error) {
bytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}

machines := &clusterv1.MachineList{}
err = yaml.Unmarshal(bytes, &machines)
if err != nil {
return nil, err
}

return util.MachineP(machines.Items), nil
}

func init() {
RootCmd.PersistentFlags().StringVarP(&kubeConfig, "kubeconfig", "k", "", "location for the kubernetes config file. If not provided, $HOME/.kube/config is used")
Copy link
Contributor

Choose a reason for hiding this comment

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

Please leave all flags out of the skeleton. They can be filled in as code uses them. This ensures the the flags actually do something when they appear and also ensure they are defined in a way that works and is accurate to the behavior of the code.

For example, I already know this definition of kubeconfig will not be accurate for bootstrapping.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

flag.CommandLine.Parse([]string{})
logs.InitLogs()
}
23 changes: 23 additions & 0 deletions clusterctl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import "sigs.k8s.io/cluster-api/clusterctl/cmd"

func main() {
cmd.Execute()
}
23 changes: 23 additions & 0 deletions errors/deployer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package errors

import (
"fmt"
)

var NotImplementedError = fmt.Errorf("not implemented")