Skip to content

Commit dc2d7dd

Browse files
authored
Cleanup (#55)
* go 1.17 has deprecated installing executables with `go get` Use go install instead Set GOFLAGS to empty string to overcome the `cannot query module due to -mod=vendor` error we get in presubmit job * Updated kustomize version to a version that support go install See kubernetes-sigs/kustomize#3618 * Install golangci-lint from source See golangci/golangci-lint#2374 * Fix lint issue See golangci/golangci-lint#2601 * Refactor - don't pass around origRes and origErr
1 parent e6fa01e commit dc2d7dd

File tree

3 files changed

+32
-36
lines changed

3 files changed

+32
-36
lines changed

Makefile

+5-9
Original file line numberDiff line numberDiff line change
@@ -153,31 +153,27 @@ controller-gen: ## Download controller-gen locally if necessary.
153153

154154
KUSTOMIZE = $(shell pwd)/bin/kustomize
155155
kustomize: ## Download kustomize locally if necessary.
156-
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
156+
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
157157

158158
ENVTEST = $(shell pwd)/bin/setup-envtest
159159
envtest: ## Download envtest-setup locally if necessary.
160160
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
161161

162162
MOCKGEN = $(shell pwd)/bin/mockgen
163163
mockgen: ## Download mockgen locally if necessary.
164-
$(call go-get-tool,$(MOCKGEN),github.com/golang/mock/mockgen)
164+
$(call go-get-tool,$(MOCKGEN),github.com/golang/mock/mockgen@latest)
165165

166166
GOLINT = $(shell pwd)/bin/golangci-lint
167167
golint: ## Download golangci-lint locally if necessary.
168-
@[ -f $(GOLINT) ] || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell pwd)/bin v1.43.0
168+
$(call go-get-tool,$(GOLINT),github.com/golangci/golangci-lint/cmd/golangci-lint@v1.44.2)
169169

170170
# go-get-tool will 'go get' any package $2 and install it to $1.
171171
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
172172
define go-get-tool
173173
@[ -f $(1) ] || { \
174174
set -e ;\
175-
TMP_DIR=$$(mktemp -d) ;\
176-
cd $$TMP_DIR ;\
177-
go mod init tmp ;\
178-
echo "Downloading $(2)" ;\
179-
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
180-
rm -rf $$TMP_DIR ;\
175+
echo "Installing $(2)" ;\
176+
GOFLAGS="" GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
181177
}
182178
endef
183179

controllers/agentmachine_controller.go

+27-25
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (r *AgentMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request
101101
}
102102
if machine == nil {
103103
log.Info("Waiting for Machine Controller to set OwnerRef on AgentMachine")
104-
return r.updateStatus(ctx, log, agentMachine, ctrl.Result{}, nil)
104+
return ctrl.Result{}, r.updateStatus(ctx, log, agentMachine, nil)
105105
}
106106

107107
res, err := r.handleDeletionHook(ctx, log, agentMachine, machine)
@@ -120,7 +120,9 @@ func (r *AgentMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request
120120
}
121121

122122
if agentCluster.Status.ClusterDeploymentRef.Name == "" {
123-
return r.updateStatus(ctx, log, agentMachine, ctrl.Result{}, fmt.Errorf("No cluster deployment reference on agentCluster %s", agentCluster.GetName()))
123+
err = fmt.Errorf("No cluster deployment reference on agentCluster %s", agentCluster.GetName())
124+
log.Warning(err.Error())
125+
return ctrl.Result{}, r.updateStatus(ctx, log, agentMachine, err)
124126
}
125127

126128
machineConfigPool, ignitionTokenSecretRef, err := r.processBootstrapDataSecret(ctx, log, machine)
@@ -130,20 +132,21 @@ func (r *AgentMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request
130132

131133
// If the AgentMachine doesn't have an agent, find one and set the agentRef
132134
if agentMachine.Status.AgentRef == nil {
133-
foundAgent, err := r.findAgent(ctx, log, agentMachine, agentCluster.Status.ClusterDeploymentRef, machineConfigPool, ignitionTokenSecretRef)
135+
var foundAgent *aiv1beta1.Agent
136+
foundAgent, err = r.findAgent(ctx, log, agentMachine, agentCluster.Status.ClusterDeploymentRef, machineConfigPool, ignitionTokenSecretRef)
134137
if foundAgent == nil || err != nil {
135-
return r.updateStatus(ctx, log, agentMachine, ctrl.Result{}, err)
138+
return ctrl.Result{}, r.updateStatus(ctx, log, agentMachine, err)
136139
}
137140

138141
err = r.updateAgentMachineWithFoundAgent(ctx, log, agentMachine, foundAgent)
139142
if err != nil {
140143
log.WithError(err).Error("failed to update AgentMachine with found agent")
141-
return r.updateStatus(ctx, log, agentMachine, ctrl.Result{}, err)
144+
return ctrl.Result{}, r.updateStatus(ctx, log, agentMachine, err)
142145
}
143146
}
144147

145148
// If the AgentMachine has an agent, check its conditions and update ready/error
146-
return r.updateStatus(ctx, log, agentMachine, ctrl.Result{}, nil)
149+
return ctrl.Result{}, r.updateStatus(ctx, log, agentMachine, nil)
147150
}
148151

149152
func (r *AgentMachineReconciler) removeMachineDeletionHookAnnotation(ctx context.Context, machine *clusterv1.Machine) (err error) {
@@ -479,27 +482,27 @@ func isValidAgent(agent *aiv1beta1.Agent, agentMachine *capiproviderv1alpha1.Age
479482
return true
480483
}
481484

482-
func (r *AgentMachineReconciler) updateStatus(ctx context.Context, log logrus.FieldLogger, agentMachine *capiproviderv1alpha1.AgentMachine, origRes ctrl.Result, origErr error) (ctrl.Result, error) {
483-
installationInProgress := false
484-
conditionPassed := setAgentReservedCondition(agentMachine, origErr)
485+
func (r *AgentMachineReconciler) updateStatus(ctx context.Context, log logrus.FieldLogger, agentMachine *capiproviderv1alpha1.AgentMachine, err error) error {
486+
conditionPassed := setAgentReservedCondition(agentMachine, err)
485487
if !conditionPassed {
486488
conditions.MarkFalse(agentMachine, capiproviderv1alpha1.AgentSpecSyncedCondition, capiproviderv1alpha1.AgentNotYetFoundReason, clusterv1.ConditionSeverityInfo, "Agent not yet reserved")
487489
conditions.MarkFalse(agentMachine, capiproviderv1alpha1.AgentValidatedCondition, capiproviderv1alpha1.AgentNotYetFoundReason, clusterv1.ConditionSeverityInfo, "Agent not yet reserved")
488490
conditions.MarkFalse(agentMachine, capiproviderv1alpha1.AgentRequirementsMetCondition, capiproviderv1alpha1.AgentNotYetFoundReason, clusterv1.ConditionSeverityInfo, "Agent not yet reserved")
489491
conditions.MarkFalse(agentMachine, capiproviderv1alpha1.InstalledCondition, capiproviderv1alpha1.AgentNotYetFoundReason, clusterv1.ConditionSeverityInfo, "Agent not yet reserved")
490-
return r.setStatus(ctx, log, agentMachine, origRes, origErr, installationInProgress)
492+
err = r.setStatus(ctx, log, agentMachine)
493+
return err
491494
}
492495

493-
agent, err := r.getAgent(ctx, log, agentMachine)
494-
if err != nil {
495-
return ctrl.Result{}, err
496+
agent, getErr := r.getAgent(ctx, log, agentMachine)
497+
if getErr != nil {
498+
return getErr
496499
}
497-
498500
setConditionByAgentCondition(agentMachine, agent, capiproviderv1alpha1.AgentSpecSyncedCondition, aiv1beta1.SpecSyncedCondition, clusterv1.ConditionSeverityError)
499501
setConditionByAgentCondition(agentMachine, agent, capiproviderv1alpha1.AgentValidatedCondition, aiv1beta1.ValidatedCondition, clusterv1.ConditionSeverityError)
500502
setConditionByAgentCondition(agentMachine, agent, capiproviderv1alpha1.AgentRequirementsMetCondition, aiv1beta1.RequirementsMetCondition, clusterv1.ConditionSeverityError)
501-
installationInProgress = !setConditionByAgentCondition(agentMachine, agent, capiproviderv1alpha1.InstalledCondition, aiv1beta1.InstalledCondition, clusterv1.ConditionSeverityInfo)
502-
return r.setStatus(ctx, log, agentMachine, origRes, origErr, installationInProgress)
503+
setConditionByAgentCondition(agentMachine, agent, capiproviderv1alpha1.InstalledCondition, aiv1beta1.InstalledCondition, clusterv1.ConditionSeverityInfo)
504+
err = r.setStatus(ctx, log, agentMachine)
505+
return err
503506
}
504507

505508
func (r *AgentMachineReconciler) getAgent(ctx context.Context, log logrus.FieldLogger, agentMachine *capiproviderv1alpha1.AgentMachine) (*aiv1beta1.Agent, error) {
@@ -532,12 +535,12 @@ func setConditionByAgentCondition(agentMachine *capiproviderv1alpha1.AgentMachin
532535
return false
533536
}
534537

535-
func setAgentReservedCondition(agentMachine *capiproviderv1alpha1.AgentMachine, origErr error) bool {
538+
func setAgentReservedCondition(agentMachine *capiproviderv1alpha1.AgentMachine, err error) bool {
536539
if agentMachine.Status.AgentRef == nil {
537-
if origErr == nil {
540+
if err == nil {
538541
conditions.MarkFalse(agentMachine, capiproviderv1alpha1.AgentReservedCondition, capiproviderv1alpha1.NoSuitableAgentsReason, clusterv1.ConditionSeverityWarning, "")
539542
} else {
540-
conditions.MarkFalse(agentMachine, capiproviderv1alpha1.AgentReservedCondition, capiproviderv1alpha1.AgentNotYetFoundReason, clusterv1.ConditionSeverityInfo, origErr.Error())
543+
conditions.MarkFalse(agentMachine, capiproviderv1alpha1.AgentReservedCondition, capiproviderv1alpha1.AgentNotYetFoundReason, clusterv1.ConditionSeverityInfo, err.Error())
541544
}
542545
return false
543546
}
@@ -546,7 +549,7 @@ func setAgentReservedCondition(agentMachine *capiproviderv1alpha1.AgentMachine,
546549
return true
547550
}
548551

549-
func (r *AgentMachineReconciler) setStatus(ctx context.Context, log logrus.FieldLogger, agentMachine *capiproviderv1alpha1.AgentMachine, origRes ctrl.Result, origErr error, installationInProgress bool) (ctrl.Result, error) {
552+
func (r *AgentMachineReconciler) setStatus(ctx context.Context, log logrus.FieldLogger, agentMachine *capiproviderv1alpha1.AgentMachine) error {
550553
conditions.SetSummary(agentMachine,
551554
conditions.WithConditions(capiproviderv1alpha1.AgentReservedCondition,
552555
capiproviderv1alpha1.AgentSpecSyncedCondition,
@@ -559,12 +562,11 @@ func (r *AgentMachineReconciler) setStatus(ctx context.Context, log logrus.Field
559562

560563
agentMachine.Status.Ready, _ = strconv.ParseBool(string(conditions.Get(agentMachine, clusterv1.ReadyCondition).Status))
561564

562-
if updateErr := r.Status().Update(ctx, agentMachine); updateErr != nil {
563-
log.WithError(updateErr).Error("failed to update AgentMachine Status")
564-
return ctrl.Result{}, updateErr
565+
err := r.Status().Update(ctx, agentMachine)
566+
if err != nil {
567+
log.WithError(err).Error("failed to update AgentMachine Status")
565568
}
566-
567-
return origRes, origErr
569+
return err
568570
}
569571

570572
func getAddresses(foundAgent *aiv1beta1.Agent) []clusterv1.MachineAddress {

main.go

-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import (
2929
"k8s.io/apimachinery/pkg/runtime"
3030
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3131
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
32-
33-
// to ensure that exec-entrypoint and run can make use of them.
3432
_ "k8s.io/client-go/plugin/pkg/client/auth"
3533
ctrl "sigs.k8s.io/controller-runtime"
3634
"sigs.k8s.io/controller-runtime/pkg/cache"

0 commit comments

Comments
 (0)