From a481e7c8a3abebee4cc1d344ac0e3bf1d45f15c1 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Sat, 22 Jun 2019 23:42:11 +0300 Subject: [PATCH] hack: include a verify-golint script and fix linting issues --- actuators/actuators.go | 6 +++- actuators/machine.go | 8 +++++- execer/client.go | 6 ++++ hack/verify-golint.sh | 51 +++++++++++++++++++++++++++++++++ kind/actions/cluster_actions.go | 8 +++++- kind/actions/kind.go | 2 ++ kind/kubeadm/config.go | 11 +++++-- 7 files changed, 86 insertions(+), 6 deletions(-) create mode 100755 hack/verify-golint.sh diff --git a/actuators/actuators.go b/actuators/actuators.go index c9ec400..4cfc862 100644 --- a/actuators/actuators.go +++ b/actuators/actuators.go @@ -39,12 +39,15 @@ func getRole(machine *clusterv1.Machine) string { return setValue } +// Cluster defines a cluster actuator object type Cluster struct{} +// NewClusterActuator returns a new cluster actuator object func NewClusterActuator() *Cluster { return &Cluster{} } +// Reconcile setups an external load balancer for the cluster if needed func (c *Cluster) Reconcile(cluster *clusterv1.Cluster) error { elb, err := getExternalLoadBalancerNode(cluster.Name) if err != nil { @@ -72,11 +75,12 @@ func getExternalLoadBalancerNode(clusterName string) (*nodes.Node, error) { return nil, nil } if len(elb) > 1 { - return nil, errors.New("Too many external load balancers.") + return nil, errors.New("too many external load balancers") } return &elb[0], nil } +// Delete can be used to delete a cluster func (c *Cluster) Delete(cluster *clusterv1.Cluster) error { fmt.Println("Cluster delete is not implemented.") return nil diff --git a/actuators/machine.go b/actuators/machine.go index a3e2cea..3eaba1c 100644 --- a/actuators/machine.go +++ b/actuators/machine.go @@ -41,11 +41,13 @@ const ( clusterAPIControlPlaneSetLabel = "controlplane" ) +// Machine defines a machine actuator type type Machine struct { Core corev1.CoreV1Interface ClusterAPI v1alpha1.ClusterV1alpha1Interface } +// NewMachineActuator returns a new machine actuator object func NewMachineActuator(clusterapi v1alpha1.ClusterV1alpha1Interface, core corev1.CoreV1Interface) *Machine { return &Machine{ Core: core, @@ -53,7 +55,8 @@ func NewMachineActuator(clusterapi v1alpha1.ClusterV1alpha1Interface, core corev } } -// Have to print all the errors because cluster-api swallows them +// Create creates a machine for a given cluster +// Note: have to print all the errors because cluster-api swallows them func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clusterv1.Machine) error { old := machine.DeepCopy() fmt.Printf("Creating a machine for cluster %q\n", c.Name) @@ -175,11 +178,13 @@ func (m *Machine) Delete(ctx context.Context, cluster *clusterv1.Cluster, machin return nil } +// Update updates a machine func (m *Machine) Update(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) error { fmt.Println("Update machine is not implemented yet.") return nil } +// Exists returns true if a machine exists in the cluster func (m *Machine) Exists(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) (bool, error) { if machine.Spec.ProviderID != nil { return true, nil @@ -236,6 +241,7 @@ func providerID(name string) string { return fmt.Sprintf("docker://%s", name) } +// CAPIroleToKindRole converts a CAPI role to kind role // TODO there is a better way to do this. func CAPIroleToKindRole(CAPIRole string) string { if CAPIRole == clusterAPIControlPlaneSetLabel { diff --git a/execer/client.go b/execer/client.go index dff17cb..77e88b0 100644 --- a/execer/client.go +++ b/execer/client.go @@ -35,6 +35,7 @@ type Client struct { ExtraEnv []string } +// NewClient returns a new client object func NewClient(command string) *Client { return &Client{ Stdout: os.Stdout, @@ -44,6 +45,9 @@ func NewClient(command string) *Client { } } +// PipeToCommand pipes a standard input stream to a client command, starts the command and waits +// until the client command has finished writing its standard output and standard error to the respective +// client buffers. func (c *Client) PipeToCommand(stdin io.Reader, args ...string) error { cmd := exec.Command(c.Command, args...) cmd.Env = append(os.Environ(), c.ExtraEnv...) @@ -83,6 +87,7 @@ func (c *Client) PipeToCommand(stdin io.Reader, args ...string) error { return nil } +// RunCommandReturnOutput runs a client command and returns its output func (c *Client) RunCommandReturnOutput(args ...string) (string, error) { cmd := exec.Command(c.Command, args...) cmd.Env = append(os.Environ(), c.ExtraEnv...) @@ -126,6 +131,7 @@ func (c *Client) RunCommandReturnOutput(args ...string) (string, error) { } +// RunCommand runs a client command func (c *Client) RunCommand(args ...string) error { cmd := exec.Command(c.Command, args...) cmd.Env = append(os.Environ(), c.ExtraEnv...) diff --git a/hack/verify-golint.sh b/hack/verify-golint.sh new file mode 100755 index 0000000..a0c10a5 --- /dev/null +++ b/hack/verify-golint.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# Copyright 2019 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. + +# CI script to run go lint over our code +set -o errexit +set -o nounset +set -o pipefail + +# shellcheck source=/dev/null +source "$(dirname "$0")/utils.sh" + +# cd to the root path +REPO_PATH=$(get_root_path) + +# create a temporary directory +TMP_DIR=$(mktemp -d) + +# cleanup +exitHandler() ( + echo "Cleaning up..." + rm -rf "${TMP_DIR}" +) +trap exitHandler EXIT + +# pull the source code and build the binary +cd "${TMP_DIR}" +URL="http://github.com/golang/lint" +echo "Cloning ${URL} in ${TMP_DIR}..." +git clone --quiet --depth=1 "${URL}" . +echo "Building golint..." +export GO111MODULE=on +go build -o ./golint/golint ./golint + +# run the binary +cd "${REPO_PATH}" +echo "Running golint..." +git ls-files | grep "\.go" | \ + grep -v "\\/vendor\\/" | \ + xargs -L1 "${TMP_DIR}/golint/golint" -set_exit_status diff --git a/kind/actions/cluster_actions.go b/kind/actions/cluster_actions.go index 31e84ba..a0f1e9d 100644 --- a/kind/actions/cluster_actions.go +++ b/kind/actions/cluster_actions.go @@ -117,6 +117,7 @@ func ConfigureLoadBalancer(clusterName string) error { return errors.WithStack(docker.Kill("SIGHUP", loadBalancerNode.Name())) } +// KubeadmConfig writes the kubeadm config to a node func KubeadmConfig(node *nodes.Node, clusterName, lbip string) error { // get installed kubernetes version from the node image kubeVersion, err := node.KubeVersion() @@ -124,7 +125,7 @@ func KubeadmConfig(node *nodes.Node, clusterName, lbip string) error { return errors.Wrap(err, "failed to get kubernetes version from node") } - kubeadmConfig, err := kubeadm.InitConifguration(kubeVersion, clusterName, fmt.Sprintf("%s:%d", lbip, 6443)) + kubeadmConfig, err := kubeadm.InitConfiguration(kubeVersion, clusterName, fmt.Sprintf("%s:%d", lbip, 6443)) if err != nil { return errors.Wrap(err, "failed to generate kubeadm config content") @@ -137,6 +138,7 @@ func KubeadmConfig(node *nodes.Node, clusterName, lbip string) error { return nil } +// KubeadmInit execute kubeadm init on the boostrap control-plane node of a cluster func KubeadmInit(clusterName string) error { allNodes, err := nodes.List(fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName)) if err != nil { @@ -177,6 +179,7 @@ func KubeadmInit(clusterName string) error { return nil } +// InstallCNI installs a CNI plugin from a node func InstallCNI(node *nodes.Node) error { // read the manifest from the node var raw bytes.Buffer @@ -211,6 +214,7 @@ func InstallCNI(node *nodes.Node) error { return nil } +// KubeadmJoin executes kubeadm join on a node func KubeadmJoin(clusterName string, node *nodes.Node) error { allNodes, err := nodes.List(fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName)) if err != nil { @@ -243,6 +247,7 @@ func KubeadmJoin(clusterName string, node *nodes.Node) error { return nil } +// SetNodeProviderRef patches a node with docker://node-name as the providerID func SetNodeProviderRef(clusterName, nodeName string) error { allNodes, err := nodes.List(fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName)) if err != nil { @@ -274,6 +279,7 @@ func SetNodeProviderRef(clusterName, nodeName string) error { return nil } +// GetNodeRefUID returns the node reference UID func GetNodeRefUID(clusterName, nodeName string) (string, error) { // k get nodes my-cluster-worker -o custom-columns=UID:.metadata.uid --no-headers allNodes, err := nodes.List(fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName)) diff --git a/kind/actions/kind.go b/kind/actions/kind.go index 19b4603..3eb689a 100644 --- a/kind/actions/kind.go +++ b/kind/actions/kind.go @@ -179,6 +179,7 @@ func DeleteControlPlane(clusterName, nodeName string) error { return ConfigureLoadBalancer(clusterName) } +// DeleteWorker removes a worker node from a cluster func DeleteWorker(clusterName, nodeName string) error { nodeList, err := nodes.List( fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName), @@ -202,6 +203,7 @@ func DeleteWorker(clusterName, nodeName string) error { return nodes.Delete(node) } +// ListControlPlanes returns the list of control-plane nodes for a cluster func ListControlPlanes(clusterName string) ([]nodes.Node, error) { return nodes.List( fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName), diff --git a/kind/kubeadm/config.go b/kind/kubeadm/config.go index 0422d16..1656462 100644 --- a/kind/kubeadm/config.go +++ b/kind/kubeadm/config.go @@ -27,12 +27,17 @@ import ( ) const ( - KnownTokenID = "abcdef" + // KnownTokenID is the kubeadm token ID + KnownTokenID = "abcdef" + // KnownTokenSecret is the kubeadm token secret KnownTokenSecret = "0123456789abcdef" - Token = KnownTokenID + "." + KnownTokenSecret + // Token is the kubeadm token formed by combining the token ID and secret + Token = KnownTokenID + "." + KnownTokenSecret ) -func InitConifguration(version, name, controlPlaneEndpoint string) ([]byte, error) { +// InitConfiguration accepts a set of paramenters like Kubernetes version and cluster name, +// and marshals the kubeadm configuration types in a `---` separated JSON document. +func InitConfiguration(version, name, controlPlaneEndpoint string) ([]byte, error) { configuration := &kubeadmv1beta1.InitConfiguration{ TypeMeta: metav1.TypeMeta{ Kind: "InitConfiguration",