diff --git a/pkg/work/spoke/spokeagent.go b/pkg/work/spoke/spokeagent.go index 2cbca85c6..dbbb95b0a 100644 --- a/pkg/work/spoke/spokeagent.go +++ b/pkg/work/spoke/spokeagent.go @@ -44,6 +44,8 @@ const ( appliedManifestWorkFinalizeControllerWorkers = 10 manifestWorkFinalizeControllerWorkers = 10 availableStatusControllerWorkers = 10 + + defaultUserAgent = "work-agent" ) type WorkAgentConfig struct { @@ -68,6 +70,10 @@ func (o *WorkAgentConfig) RunWorkloadAgent(ctx context.Context, controllerContex return err } + // copy the config for work and set specific userAgent + spokeRestConfig = rest.CopyConfig(spokeRestConfig) + spokeRestConfig.UserAgent = defaultUserAgent + spokeDynamicClient, err := dynamic.NewForConfig(spokeRestConfig) if err != nil { return err @@ -95,7 +101,7 @@ func (o *WorkAgentConfig) RunWorkloadAgent(ctx context.Context, controllerContex return err } - hubHost, hubWorkClient, hubWorkInformer, err := o.newHubWorkClientAndInformer(ctx, restMapper) + hubHost, hubWorkClient, hubWorkInformer, err := o.newWorkClientAndInformer(ctx, restMapper) if err != nil { return err } @@ -210,7 +216,7 @@ func buildCodecs(codecNames []string, restMapper meta.RESTMapper) []generic.Code return codecs } -func (o *WorkAgentConfig) newHubWorkClientAndInformer( +func (o *WorkAgentConfig) newWorkClientAndInformer( ctx context.Context, restMapper meta.RESTMapper, ) (string, workv1client.ManifestWorkInterface, workv1informers.ManifestWorkInformer, error) { diff --git a/pkg/work/spoke/spokeagent_test.go b/pkg/work/spoke/spokeagent_test.go new file mode 100644 index 000000000..48b6b8e75 --- /dev/null +++ b/pkg/work/spoke/spokeagent_test.go @@ -0,0 +1,95 @@ +package spoke + +import ( + "context" + "os" + "path" + "testing" + + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" + "github.com/openshift/library-go/pkg/controller/controllercmd" + "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/rest" + ocmfeature "open-cluster-management.io/api/feature" + workapiv1 "open-cluster-management.io/api/work/v1" + commonoptions "open-cluster-management.io/ocm/pkg/common/options" + "open-cluster-management.io/ocm/pkg/features" + "open-cluster-management.io/ocm/test/integration/util" + "sigs.k8s.io/controller-runtime/pkg/envtest" + logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" +) + +var testEnv *envtest.Environment +var sourceConfigFileName string +var cfg *rest.Config + +var CRDPaths = []string{ + // hub + "../../../vendor/open-cluster-management.io/api/work/v1/0000_00_work.open-cluster-management.io_manifestworks.crd.yaml", + "../../../vendor/open-cluster-management.io/api/work/v1/0000_01_work.open-cluster-management.io_appliedmanifestworks.crd.yaml", +} + +func TestWorkAgent(t *testing.T) { + gomega.RegisterFailHandler(ginkgo.Fail) + ginkgo.RunSpecs(t, "Work Manager Suite") +} + +var _ = ginkgo.BeforeSuite(func() { + logf.SetLogger(zap.New(zap.WriteTo(ginkgo.GinkgoWriter), zap.UseDevMode(true))) + ginkgo.By("bootstrapping test environment") + var err error + + // start a kube-apiserver + testEnv = &envtest.Environment{ + ErrorIfCRDPathMissing: true, + CRDDirectoryPaths: CRDPaths, + } + cfg, err = testEnv.Start() + gomega.Expect(err).ToNot(gomega.HaveOccurred()) + gomega.Expect(cfg).ToNot(gomega.BeNil()) + + err = workapiv1.Install(scheme.Scheme) + gomega.Expect(err).NotTo(gomega.HaveOccurred()) + + // create kubeconfig file for hub in a tmp dir + tempDir, err := os.MkdirTemp("", "test") + gomega.Expect(err).ToNot(gomega.HaveOccurred()) + gomega.Expect(tempDir).ToNot(gomega.BeEmpty()) + + sourceConfigFileName = path.Join(tempDir, "kubeconfig") + err = util.CreateKubeconfigFile(cfg, sourceConfigFileName) + gomega.Expect(err).ToNot(gomega.HaveOccurred()) + + err = features.SpokeMutableFeatureGate.Add(ocmfeature.DefaultSpokeWorkFeatureGates) + gomega.Expect(err).ToNot(gomega.HaveOccurred()) +}) + +var _ = ginkgo.AfterSuite(func() { + ginkgo.By("tearing down the test environment") + + err := testEnv.Stop() + gomega.Expect(err).ToNot(gomega.HaveOccurred()) +}) + +var _ = ginkgo.Describe("start work agent", func() { + ginkgo.It("start work agent", func() { + ctx, stopAgent := context.WithCancel(context.Background()) + commonOpts := commonoptions.NewAgentOptions() + commonOpts.SpokeClusterName = "cluster1" + opts := NewWorkloadAgentOptions() + opts.WorkloadSourceConfig = sourceConfigFileName + agentConfig := NewWorkAgentConfig(commonOpts, opts) + + // start hub controller + go func() { + err := agentConfig.RunWorkloadAgent(ctx, &controllercmd.ControllerContext{ + KubeConfig: cfg, + EventRecorder: util.NewIntegrationTestEventRecorder("work-agent"), + }) + gomega.Expect(err).NotTo(gomega.HaveOccurred()) + }() + stopAgent() + }) +})