From 5be087fd7035010c3792962ccf73d1a094736432 Mon Sep 17 00:00:00 2001 From: CrystalChun Date: Tue, 19 Sep 2023 09:19:07 -0700 Subject: [PATCH] MGMT-14838: Required code changes for upgraded dependencies https://issues.redhat.com/browse/MGMT-14838 These code changes are needed after upgrading some dependencies in the previous commit. --- Dockerfile | 8 ++++-- Makefile | 2 +- controllers/agentcluster_controller_test.go | 3 +- controllers/agentmachine_controller.go | 13 ++++----- controllers/agentmachine_controller_test.go | 17 +++++------ controllers/suite_test.go | 5 +--- main.go | 31 +++++++++++++-------- 7 files changed, 44 insertions(+), 35 deletions(-) diff --git a/Dockerfile b/Dockerfile index 773a69f2..533816ab 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,16 @@ # Build the manager binary -FROM golang:1.18 as builder +FROM registry.ci.openshift.org/openshift/release:golang-1.20 + +ENV GOROOT=/usr/local/go +ENV PATH=$PATH:$GOROOT/bin 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"] diff --git a/Makefile b/Makefile index 3c065040..7555f427 100644 --- a/Makefile +++ b/Makefile @@ -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)))) diff --git a/controllers/agentcluster_controller_test.go b/controllers/agentcluster_controller_test.go index 3c13ea44..b2745a4f 100644 --- a/controllers/agentcluster_controller_test.go +++ b/controllers/agentcluster_controller_test.go @@ -147,7 +147,8 @@ var _ = Describe("agentcluster reconcile", func() { ) BeforeEach(func() { - c = fakeclient.NewClientBuilder().WithScheme(scheme.Scheme).Build() + agentCluster := &capiproviderv1.AgentCluster{} + c = fakeclient.NewClientBuilder().WithScheme(scheme.Scheme).WithStatusSubresource(agentCluster).Build() mockCtrl = gomock.NewController(GinkgoT()) acr = &AgentClusterReconciler{ diff --git a/controllers/agentmachine_controller.go b/controllers/agentmachine_controller.go index e334dabf..8cfd00a9 100644 --- a/controllers/agentmachine_controller.go +++ b/controllers/agentmachine_controller.go @@ -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 ( @@ -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(), @@ -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{} } @@ -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(), @@ -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{} } @@ -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) } diff --git a/controllers/agentmachine_controller_test.go b/controllers/agentmachine_controller_test.go index af731204..e2828d4c 100644 --- a/controllers/agentmachine_controller_test.go +++ b/controllers/agentmachine_controller_test.go @@ -24,7 +24,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes/scheme" - k8sutilspointer "k8s.io/utils/pointer" + "k8s.io/utils/ptr" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" clusterutil "sigs.k8s.io/cluster-api/util" "sigs.k8s.io/cluster-api/util/conditions" @@ -86,7 +86,7 @@ func newAgentMachine(name, namespace string, spec capiproviderv1.AgentMachineSpe TLS: ignitionapi.TLS{ CertificateAuthorities: []ignitionapi.Resource{ { - Source: k8sutilspointer.StringPtr("data:text/plain;base64,encodedCACert"), + Source: ptr.To("data:text/plain;base64,encodedCACert"), }, }, }, @@ -94,11 +94,11 @@ func newAgentMachine(name, namespace string, spec capiproviderv1.AgentMachineSpe Config: ignitionapi.IgnitionConfig{ Merge: []ignitionapi.Resource{ { - Source: k8sutilspointer.StringPtr("https://endpoint/ignition"), + Source: ptr.To("https://endpoint/ignition"), HTTPHeaders: []ignitionapi.HTTPHeader{ { Name: "Authorization", - Value: k8sutilspointer.StringPtr("Bearer encodedToken"), + Value: ptr.To("Bearer encodedToken"), }, }, }, @@ -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} @@ -209,7 +209,8 @@ var _ = Describe("agentmachine reconcile", func() { ) BeforeEach(func() { - c = fakeclient.NewClientBuilder().WithScheme(scheme.Scheme).Build() + agentMachine := &capiproviderv1.AgentMachine{} + c = fakeclient.NewClientBuilder().WithScheme(scheme.Scheme).WithStatusSubresource(agentMachine).Build() mockCtrl = gomock.NewController(GinkgoT()) amr = &AgentMachineReconciler{ @@ -616,7 +617,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{ @@ -637,6 +638,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()) }) }) diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 4892cfd9..fa6991f0 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -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" ) @@ -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() { diff --git a/main.go b/main.go index fb3b0a0f..43d4145a 100644 --- a/main.go +++ b/main.go @@ -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 ( @@ -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) @@ -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")