Skip to content

Commit

Permalink
MGMT-14838: Required code changes for upgraded dependencies
Browse files Browse the repository at this point in the history
https://issues.redhat.com/browse/MGMT-14838
These code changes are needed after upgrading some
dependencies in the previous commit.
  • Loading branch information
CrystalChun committed Sep 23, 2023
1 parent e2eb88d commit 1d65362
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# Build the manager binary
FROM golang:1.18 as builder
FROM registry.ci.openshift.org/openshift/release:golang-1.20

WORKDIR /workspace
COPY . .
# Build
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -a -o manager main.go

FROM registry.ci.openshift.org/ocp/4.12:base
WORKDIR /
COPY --from=builder /workspace/manager .
RUN cp /workspace/manager /manager
USER 65532:65532

ENTRYPOINT ["/manager"]
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mockgen: ## Download mockgen locally if necessary.

GOLINT = $(shell pwd)/bin/golangci-lint
golint: ## Download golangci-lint locally if necessary.
$(call go-get-tool,$(GOLINT),github.com/golangci/golangci-lint/cmd/golangci-lint@v1.44.2)
$(call go-get-tool,$(GOLINT),github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.0)

# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
Expand Down
13 changes: 6 additions & 7 deletions controllers/agentmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

const (
Expand Down Expand Up @@ -640,7 +639,7 @@ func getAddresses(foundAgent *aiv1beta1.Agent) []clusterv1.MachineAddress {
return machineAddresses
}

func (r *AgentMachineReconciler) mapMachineToAgentMachine(machine client.Object) []reconcile.Request {
func (r *AgentMachineReconciler) mapMachineToAgentMachine(ctx context.Context, machine client.Object) []reconcile.Request {
log := r.Log.WithFields(
logrus.Fields{
"machine": machine.GetName(),
Expand All @@ -652,7 +651,7 @@ func (r *AgentMachineReconciler) mapMachineToAgentMachine(machine client.Object)
opts := &client.ListOptions{
Namespace: machine.GetNamespace(),
}
if err := r.List(context.Background(), amList, opts); err != nil {
if err := r.List(ctx, amList, opts); err != nil {
log.Debugf("failed to list agent machines")
return []reconcile.Request{}
}
Expand All @@ -677,7 +676,7 @@ func (r *AgentMachineReconciler) mapMachineToAgentMachine(machine client.Object)

// SetupWithManager sets up the controller with the Manager.
func (r *AgentMachineReconciler) SetupWithManager(mgr ctrl.Manager, agentNamespace string) error {
mapAgentToAgentMachine := func(agent client.Object) []reconcile.Request {
mapAgentToAgentMachine := func(ctx context.Context, agent client.Object) []reconcile.Request {
log := r.Log.WithFields(
logrus.Fields{
"agent": agent.GetName(),
Expand All @@ -694,7 +693,7 @@ func (r *AgentMachineReconciler) SetupWithManager(mgr ctrl.Manager, agentNamespa
Namespace: namespace,
}

if err := r.List(context.Background(), amList, opts); err != nil {
if err := r.List(ctx, amList, opts); err != nil {
log.WithError(err).Error("failed to list agent machines")
return []reconcile.Request{}
}
Expand All @@ -716,7 +715,7 @@ func (r *AgentMachineReconciler) SetupWithManager(mgr ctrl.Manager, agentNamespa

return ctrl.NewControllerManagedBy(mgr).
For(&capiproviderv1.AgentMachine{}).
Watches(&source.Kind{Type: &aiv1beta1.Agent{}}, handler.EnqueueRequestsFromMapFunc(mapAgentToAgentMachine)).
Watches(&source.Kind{Type: &clusterv1.Machine{}}, handler.EnqueueRequestsFromMapFunc(r.mapMachineToAgentMachine)).
Watches(&aiv1beta1.Agent{}, handler.EnqueueRequestsFromMapFunc(mapAgentToAgentMachine)).
Watches(&clusterv1.Machine{}, handler.EnqueueRequestsFromMapFunc(r.mapMachineToAgentMachine)).
Complete(r)
}
6 changes: 3 additions & 3 deletions controllers/agentmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func newAgentMachine(name, namespace string, spec capiproviderv1.AgentMachineSpe
},
Status: clusterv1.MachineStatus{},
}
machine.ObjectMeta.Labels[clusterv1.ClusterLabelName] = cluster.Name
machine.ObjectMeta.Labels[clusterv1.ClusterNameLabel] = cluster.Name
Expect(c.Create(ctx, &machine)).To(BeNil())

machineOwnerRef := metav1.OwnerReference{APIVersion: "cluster.x-k8s.io/v1beta1", Kind: "Machine", Name: machine.Name}
Expand Down Expand Up @@ -616,7 +616,7 @@ var _ = Describe("mapMachineToAgentMachine", func() {
agentMachine, machine := newAgentMachine("agentMachine-1", testNamespace, capiproviderv1.AgentMachineSpec{}, ctx, c)
Expect(c.Create(ctx, agentMachine)).To(Succeed())

requests := amr.mapMachineToAgentMachine(machine)
requests := amr.mapMachineToAgentMachine(ctx, machine)
Expect(len(requests)).To(Equal(1))

agentMachineKey := types.NamespacedName{
Expand All @@ -637,6 +637,6 @@ var _ = Describe("mapMachineToAgentMachine", func() {
machine := clusterv1.Machine{}
Expect(c.Get(ctx, key, &machine)).To(Succeed())

Expect(amr.mapMachineToAgentMachine(&machine)).To(BeEmpty())
Expect(amr.mapMachineToAgentMachine(ctx, &machine)).To(BeEmpty())
})
})
5 changes: 1 addition & 4 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
Expand All @@ -40,9 +39,7 @@ var testEnv *envtest.Environment
func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecsWithDefaultAndCustomReporters(t,
"Controller Suite",
[]Reporter{printer.NewlineReporter{}})
RunSpecs(t, "Controller Suite")
}

var _ = BeforeSuite(func() {
Expand Down
31 changes: 20 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
)

var (
Expand Down Expand Up @@ -78,19 +79,21 @@ func main() {
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
scheme = controllers.GetKubeClientSchemes(scheme)
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
},
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "7605f49b.agent-install.openshift.io",
NewCache: cache.BuilderWithOptions(cache.Options{
DefaultSelector: cache.ObjectSelector{Field: fields.OneTermEqualSelector("metadata.namespace", watchNamespace)},
SelectorsByObject: cache.SelectorsByObject{
Cache: cache.Options{
DefaultFieldSelector: fields.OneTermEqualSelector("metadata.namespace", watchNamespace),
ByObject: map[client.Object]cache.ByObject{
&aiv1beta1.Agent{}: {Field: fields.OneTermEqualSelector("metadata.namespace", agentsNamespace)},
},
}),
})
},
},
)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
Expand All @@ -101,10 +104,16 @@ func main() {
if agentsNamespace != "" {
setupLog.Info("Watching Agents objects only in namespace for reconciliation", "agent-namespace", agentsNamespace)
agentMgr, err2 := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
MetricsBindAddress: "0",
Scheme: scheme,
LeaderElection: false,
Namespace: agentsNamespace,
Metrics: metricsserver.Options{
BindAddress: "0",
},
Scheme: scheme,
LeaderElection: false,
Cache: cache.Options{
ByObject: map[client.Object]cache.ByObject{
&aiv1beta1.Agent{}: {Field: fields.OneTermEqualSelector("metadata.namespace", agentsNamespace)},
},
},
})
if err2 != nil {
setupLog.Error(err, "unable to start Agent manager")
Expand Down

0 comments on commit 1d65362

Please sign in to comment.