Skip to content

Commit

Permalink
Improve Go formatting in CAPI codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
abhay-krishna committed Mar 23, 2023
1 parent e88eccb commit 7ce65c0
Show file tree
Hide file tree
Showing 212 changed files with 927 additions and 888 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ linters:
- gocritic
- godot
- gofmt
- gofumpt
- goimports
- goprintffuncname
- gosec
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ generate-go-conversions-docker-infrastructure: $(CONVERSION_GEN) ## Generate con
generate-go-openapi: $(OPENAPI_GEN) $(CONTROLLER_GEN) ## Generate openapi go code for runtime SDK
@mkdir -p ./tmp/sigs.k8s.io; ln -s $(ROOT_DIR) ./tmp/sigs.k8s.io/; cd ./tmp; \
for pkg in "api/v1beta1" "$(EXP_DIR)/runtime/hooks/api/v1alpha1"; do \
$(MAKE) clean-generated-openapi-definitions SRC_DIRS="./$${pkg}"; \
$(MAKE) clean-generated-openapi-definitions -C sigs.k8s.io/cluster-api SRC_DIRS="./$${pkg}"; \
echo "** Generating openapi schema for types in ./$${pkg} **"; \
$(OPENAPI_GEN) \
--input-dirs=sigs.k8s.io/cluster-api/$${pkg} \
Expand Down
6 changes: 2 additions & 4 deletions api/v1alpha4/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ const (
TemplateSuffix = "Template"
)

var (
// ZeroDuration is a zero value of the metav1.Duration type.
ZeroDuration = metav1.Duration{}
)
// ZeroDuration is a zero value of the metav1.Duration type.
var ZeroDuration = metav1.Duration{}

const (
// MachineNodeNameIndex is used by the Machine Controller to index Machines by Node name, and add a watch on Nodes.
Expand Down
6 changes: 2 additions & 4 deletions api/v1beta1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,8 @@ const (
TemplateSuffix = "Template"
)

var (
// ZeroDuration is a zero value of the metav1.Duration type.
ZeroDuration = metav1.Duration{}
)
// ZeroDuration is a zero value of the metav1.Duration type.
var ZeroDuration = metav1.Duration{}

// MachineAddressType describes a valid MachineAddress type.
type MachineAddressType string
Expand Down
6 changes: 4 additions & 2 deletions api/v1beta1/machine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ func (m *Machine) SetupWebhookWithManager(mgr ctrl.Manager) error {
// +kubebuilder:webhook:verbs=create;update,path=/validate-cluster-x-k8s-io-v1beta1-machine,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster.x-k8s.io,resources=machines,versions=v1beta1,name=validation.machine.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-cluster-x-k8s-io-v1beta1-machine,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster.x-k8s.io,resources=machines,versions=v1beta1,name=default.machine.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var _ webhook.Validator = &Machine{}
var _ webhook.Defaulter = &Machine{}
var (
_ webhook.Validator = &Machine{}
_ webhook.Defaulter = &Machine{}
)

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (m *Machine) Default() {
Expand Down
8 changes: 5 additions & 3 deletions api/v1beta1/machinedeployment_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ func (m *MachineDeployment) SetupWebhookWithManager(mgr ctrl.Manager) error {
// +kubebuilder:webhook:verbs=create;update,path=/validate-cluster-x-k8s-io-v1beta1-machinedeployment,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster.x-k8s.io,resources=machinedeployments,versions=v1beta1,name=validation.machinedeployment.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-cluster-x-k8s-io-v1beta1-machinedeployment,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster.x-k8s.io,resources=machinedeployments,versions=v1beta1,name=default.machinedeployment.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var _ webhook.CustomDefaulter = &machineDeploymentDefaulter{}
var _ webhook.Validator = &MachineDeployment{}
var (
_ webhook.CustomDefaulter = &machineDeploymentDefaulter{}
_ webhook.Validator = &MachineDeployment{}
)

// MachineDeploymentDefaulter creates a new CustomDefaulter for MachineDeployments.
func MachineDeploymentDefaulter(scheme *runtime.Scheme) webhook.CustomDefaulter {
Expand Down Expand Up @@ -304,7 +306,7 @@ const (
// Notes:
// - While the min size and max size annotations of the autoscaler provide the best UX, other autoscalers can use the
// DefaultReplicasAnnotation if they have similar use cases.
func calculateMachineDeploymentReplicas(ctx context.Context, oldMD *MachineDeployment, newMD *MachineDeployment, dryRun bool) (int32, error) {
func calculateMachineDeploymentReplicas(ctx context.Context, oldMD, newMD *MachineDeployment, dryRun bool) (int32, error) {
// If replicas is already set => Keep the current value.
if newMD.Spec.Replicas != nil {
return *newMD.Spec.Replicas, nil
Expand Down
1 change: 0 additions & 1 deletion api/v1beta1/machinedeployment_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,6 @@ func TestMachineDeploymentVersionValidation(t *testing.T) {

md := &MachineDeployment{
Spec: MachineDeploymentSpec{

Template: MachineTemplateSpec{
Spec: MachineSpec{
Version: pointer.String(tt.version),
Expand Down
6 changes: 4 additions & 2 deletions api/v1beta1/machinehealthcheck_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ func (m *MachineHealthCheck) SetupWebhookWithManager(mgr ctrl.Manager) error {
// +kubebuilder:webhook:verbs=create;update,path=/validate-cluster-x-k8s-io-v1beta1-machinehealthcheck,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster.x-k8s.io,resources=machinehealthchecks,versions=v1beta1,name=validation.machinehealthcheck.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-cluster-x-k8s-io-v1beta1-machinehealthcheck,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster.x-k8s.io,resources=machinehealthchecks,versions=v1beta1,name=default.machinehealthcheck.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var _ webhook.Defaulter = &MachineHealthCheck{}
var _ webhook.Validator = &MachineHealthCheck{}
var (
_ webhook.Defaulter = &MachineHealthCheck{}
_ webhook.Validator = &MachineHealthCheck{}
)

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (m *MachineHealthCheck) Default() {
Expand Down
6 changes: 4 additions & 2 deletions api/v1beta1/machineset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ func (m *MachineSet) SetupWebhookWithManager(mgr ctrl.Manager) error {
// +kubebuilder:webhook:verbs=create;update,path=/validate-cluster-x-k8s-io-v1beta1-machineset,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster.x-k8s.io,resources=machinesets,versions=v1beta1,name=validation.machineset.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-cluster-x-k8s-io-v1beta1-machineset,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=cluster.x-k8s.io,resources=machinesets,versions=v1beta1,name=default.machineset.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var _ webhook.Defaulter = &MachineSet{}
var _ webhook.Validator = &MachineSet{}
var (
_ webhook.Defaulter = &MachineSet{}
_ webhook.Validator = &MachineSet{}
)

// Default sets default MachineSet field values.
func (m *MachineSet) Default() {
Expand Down
4 changes: 1 addition & 3 deletions bootstrap/kubeadm/api/v1alpha4/kubeadm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ type ImageMeta struct {
// +optional
ImageTag string `json:"imageTag,omitempty"`

//TODO: evaluate if we need also a ImageName based on user feedbacks
// TODO: evaluate if we need also a ImageName based on user feedbacks
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down Expand Up @@ -205,7 +205,6 @@ type APIEndpoint struct {

// NodeRegistrationOptions holds fields that relate to registering a new control-plane or node to the cluster, either via "kubeadm init" or "kubeadm join".
type NodeRegistrationOptions struct {

// Name is the `.Metadata.Name` field of the Node API object that will be created in this `kubeadm init` or `kubeadm join` operation.
// This field is also used in the CommonName field of the kubelet's client certificate to the API server.
// Defaults to the hostname of the node if not provided.
Expand Down Expand Up @@ -278,7 +277,6 @@ type BootstrapToken struct {

// Etcd contains elements describing Etcd configuration.
type Etcd struct {

// Local provides configuration knobs for configuring the local etcd instance
// Local and External are mutually exclusive
// +optional
Expand Down
10 changes: 5 additions & 5 deletions bootstrap/kubeadm/api/v1alpha4/kubeadm_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func TestMarshalJSON(t *testing.T) {
var tests = []struct {
tests := []struct {
bts BootstrapTokenString
expected string
}{
Expand All @@ -46,7 +46,7 @@ func TestMarshalJSON(t *testing.T) {
}

func TestUnmarshalJSON(t *testing.T) {
var tests = []struct {
tests := []struct {
input string
bts *BootstrapTokenString
expectedError bool
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestUnmarshalJSON(t *testing.T) {
}

func TestJSONRoundtrip(t *testing.T) {
var tests = []struct {
tests := []struct {
input string
bts *BootstrapTokenString
}{
Expand Down Expand Up @@ -132,7 +132,7 @@ func roundtrip(input string, bts *BootstrapTokenString) error {
}

func TestTokenFromIDAndSecret(t *testing.T) {
var tests = []struct {
tests := []struct {
bts BootstrapTokenString
expected string
}{
Expand All @@ -150,7 +150,7 @@ func TestTokenFromIDAndSecret(t *testing.T) {
}

func TestNewBootstrapTokenString(t *testing.T) {
var tests = []struct {
tests := []struct {
token string
expectedError bool
bts *BootstrapTokenString
Expand Down
4 changes: 1 addition & 3 deletions bootstrap/kubeadm/api/v1beta1/kubeadm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ type ImageMeta struct {
// +optional
ImageTag string `json:"imageTag,omitempty"`

//TODO: evaluate if we need also a ImageName based on user feedbacks
// TODO: evaluate if we need also a ImageName based on user feedbacks
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down Expand Up @@ -225,7 +225,6 @@ type APIEndpoint struct {
// NodeRegistrationOptions holds fields that relate to registering a new control-plane or node to the cluster, either via "kubeadm init" or "kubeadm join".
// Note: The NodeRegistrationOptions struct has to be kept in sync with the structs in MarshalJSON.
type NodeRegistrationOptions struct {

// Name is the `.Metadata.Name` field of the Node API object that will be created in this `kubeadm init` or `kubeadm join` operation.
// This field is also used in the CommonName field of the kubelet's client certificate to the API server.
// Defaults to the hostname of the node if not provided.
Expand Down Expand Up @@ -355,7 +354,6 @@ type BootstrapToken struct {

// Etcd contains elements describing Etcd configuration.
type Etcd struct {

// Local provides configuration knobs for configuring the local etcd instance
// Local and External are mutually exclusive
// +optional
Expand Down
12 changes: 6 additions & 6 deletions bootstrap/kubeadm/api/v1beta1/kubeadm_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

func TestNodeRegistrationOptionsMarshalJSON(t *testing.T) {
var tests = []struct {
tests := []struct {
name string
opts NodeRegistrationOptions
expected string
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestNodeRegistrationOptionsMarshalJSON(t *testing.T) {
}

func TestBootstrapTokenStringMarshalJSON(t *testing.T) {
var tests = []struct {
tests := []struct {
bts BootstrapTokenString
expected string
}{
Expand All @@ -104,7 +104,7 @@ func TestBootstrapTokenStringMarshalJSON(t *testing.T) {
}

func TestBootstrapTokenStringUnmarshalJSON(t *testing.T) {
var tests = []struct {
tests := []struct {
input string
bts *BootstrapTokenString
expectedError bool
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestBootstrapTokenStringUnmarshalJSON(t *testing.T) {
}

func TestBootstrapTokenStringJSONRoundtrip(t *testing.T) {
var tests = []struct {
tests := []struct {
input string
bts *BootstrapTokenString
}{
Expand Down Expand Up @@ -190,7 +190,7 @@ func roundtrip(input string, bts *BootstrapTokenString) error {
}

func TestBootstrapTokenStringTokenFromIDAndSecret(t *testing.T) {
var tests = []struct {
tests := []struct {
bts BootstrapTokenString
expected string
}{
Expand All @@ -208,7 +208,7 @@ func TestBootstrapTokenStringTokenFromIDAndSecret(t *testing.T) {
}

func TestNewBootstrapTokenString(t *testing.T) {
var tests = []struct {
tests := []struct {
token string
expectedError bool
bts *BootstrapTokenString
Expand Down
8 changes: 3 additions & 5 deletions bootstrap/kubeadm/internal/cloudinit/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (input *BaseUserData) prepare() error {
return nil
}

func generate(kind string, tpl string, data interface{}) ([]byte, error) {
func generate(kind, tpl string, data interface{}) ([]byte, error) {
tm := template.New(kind).Funcs(defaultTemplateFuncMap)
if _, err := tm.Parse(filesTemplate); err != nil {
return nil, errors.Wrap(err, "failed to parse files template")
Expand Down Expand Up @@ -117,10 +117,8 @@ func generate(kind string, tpl string, data interface{}) ([]byte, error) {
return out.Bytes(), nil
}

var (
//go:embed kubeadm-bootstrap-script.sh
kubeadmBootstrapScript string
)
//go:embed kubeadm-bootstrap-script.sh
var kubeadmBootstrapScript string

func generateBootstrapScript(input interface{}) (*bootstrapv1.File, error) {
joinScript, err := generate("JoinScript", kubeadmBootstrapScript, input)
Expand Down
8 changes: 3 additions & 5 deletions bootstrap/kubeadm/internal/cloudinit/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import (
"text/template"
)

var (
defaultTemplateFuncMap = template.FuncMap{
"Indent": templateYAMLIndent,
}
)
var defaultTemplateFuncMap = template.FuncMap{
"Indent": templateYAMLIndent,
}

func templateYAMLIndent(i int, input string) string {
split := strings.Split(input, "\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ func TestKubeadmConfigReconciler_TestSecretOwnerReferenceReconciliation(t *testi
Name: machine.Name,
UID: machine.UID,
Controller: pointer.Bool(true),
}})
},
})
g.Expect(myclient.Update(ctx, actual)).To(Succeed())

_, err = k.Reconcile(ctx, request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ const (
clusterNamespace = "test-namespace"
)

var (
ctx = ctrl.SetupSignalHandler()
)
var ctx = ctrl.SetupSignalHandler()

func TestControlPlaneInitMutex_Lock(t *testing.T) {
g := NewWithT(t)
Expand Down Expand Up @@ -145,10 +143,12 @@ func TestControlPlaneInitMutex_LockWithMachineDeletion(t *testing.T) {
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName(clusterName),
Namespace: clusterNamespace},
Namespace: clusterNamespace,
},
Data: map[string]string{
"lock-information": "{\"machineName\":\"existent-machine\"}",
}},
},
},
&clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "existent-machine",
Expand All @@ -166,10 +166,12 @@ func TestControlPlaneInitMutex_LockWithMachineDeletion(t *testing.T) {
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName(clusterName),
Namespace: clusterNamespace},
Namespace: clusterNamespace,
},
Data: map[string]string{
"lock-information": "{\"machineName\":\"non-existent-machine\"}",
}},
},
},
).Build(),
},
expectedMachineName: newMachineName,
Expand Down
Loading

0 comments on commit 7ce65c0

Please sign in to comment.