Skip to content
This repository has been archived by the owner on Sep 24, 2021. It is now read-only.

Commit

Permalink
hack: include a verify-golint script and fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
neolit123 committed Jun 24, 2019
1 parent daafa3a commit a481e7c
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 6 deletions.
6 changes: 5 additions & 1 deletion actuators/actuators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion actuators/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@ 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,
ClusterAPI: clusterapi,
}
}

// 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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions execer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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...)
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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...)
Expand Down
51 changes: 51 additions & 0 deletions hack/verify-golint.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion kind/actions/cluster_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,15 @@ 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()
if err != nil {
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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions kind/actions/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down
11 changes: 8 additions & 3 deletions kind/kubeadm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a481e7c

Please sign in to comment.