mirror of
https://github.com/open-cluster-management-io/ocm.git
synced 2026-02-14 18:09:57 +00:00
* update vendor to add addon-framework Signed-off-by: zhujian <jiazhu@redhat.com> * Move addon manager from addon-framework to ocm repo Signed-off-by: zhujian <jiazhu@redhat.com> * add integration tests for addon manager Signed-off-by: zhujian <jiazhu@redhat.com> * push addon manager image post commit Signed-off-by: zhujian <jiazhu@redhat.com> * use library-go to refactor addon controllers Signed-off-by: zhujian <jiazhu@redhat.com> --------- Signed-off-by: zhujian <jiazhu@redhat.com>
184 lines
6.0 KiB
Go
184 lines
6.0 KiB
Go
package integration
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
ginkgo "github.com/onsi/ginkgo/v2"
|
|
"github.com/onsi/gomega"
|
|
"github.com/openshift/library-go/pkg/controller/controllercmd"
|
|
certificatesv1 "k8s.io/api/certificates/v1"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
"k8s.io/client-go/kubernetes"
|
|
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
|
|
|
"open-cluster-management.io/addon-framework/pkg/addonmanager"
|
|
"open-cluster-management.io/addon-framework/pkg/agent"
|
|
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
|
|
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"
|
|
clusterv1client "open-cluster-management.io/api/client/cluster/clientset/versioned"
|
|
workclientset "open-cluster-management.io/api/client/work/clientset/versioned"
|
|
clusterv1 "open-cluster-management.io/api/cluster/v1"
|
|
|
|
"open-cluster-management.io/ocm/pkg/addon"
|
|
"open-cluster-management.io/ocm/test/integration/util"
|
|
)
|
|
|
|
const (
|
|
eventuallyTimeout = 30 // seconds
|
|
eventuallyInterval = 1 // seconds
|
|
)
|
|
|
|
var addOnDeploymentConfigGVR = schema.GroupVersionResource{
|
|
Group: "addon.open-cluster-management.io",
|
|
Version: "v1alpha1",
|
|
Resource: "addondeploymentconfigs",
|
|
}
|
|
|
|
var testEnv *envtest.Environment
|
|
var hubWorkClient workclientset.Interface
|
|
var hubClusterClient clusterv1client.Interface
|
|
var hubAddonClient addonv1alpha1client.Interface
|
|
var hubKubeClient kubernetes.Interface
|
|
var testAddonImpl *testAddon
|
|
var testAddOnConfigsImpl *testAddon
|
|
|
|
var cancel context.CancelFunc
|
|
var mgrContext context.Context
|
|
var addonManager addonmanager.AddonManager
|
|
|
|
func TestIntegration(t *testing.T) {
|
|
gomega.RegisterFailHandler(ginkgo.Fail)
|
|
ginkgo.RunSpecs(t, "Integration Suite")
|
|
}
|
|
|
|
var _ = ginkgo.BeforeSuite(func() {
|
|
ginkgo.By("bootstrapping test environment")
|
|
|
|
// start a kube-apiserver
|
|
testEnv = &envtest.Environment{
|
|
ErrorIfCRDPathMissing: true,
|
|
CRDDirectoryPaths: []string{
|
|
filepath.Join(".", "vendor", "open-cluster-management.io", "api", "work", "v1", "0000_00_work.open-cluster-management.io_manifestworks.crd.yaml"),
|
|
filepath.Join(".", "vendor", "open-cluster-management.io", "api", "cluster", "v1"),
|
|
filepath.Join(".", "vendor", "open-cluster-management.io", "api", "cluster", "v1beta1"),
|
|
filepath.Join(".", "vendor", "open-cluster-management.io", "api", "addon", "v1alpha1"),
|
|
},
|
|
}
|
|
|
|
cfg, err := testEnv.Start()
|
|
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
|
gomega.Expect(cfg).ToNot(gomega.BeNil())
|
|
|
|
hubWorkClient, err = workclientset.NewForConfig(cfg)
|
|
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
|
hubClusterClient, err = clusterv1client.NewForConfig(cfg)
|
|
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
|
hubAddonClient, err = addonv1alpha1client.NewForConfig(cfg)
|
|
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
|
hubKubeClient, err = kubernetes.NewForConfig(cfg)
|
|
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
|
|
|
testAddonImpl = &testAddon{
|
|
name: "test",
|
|
manifests: map[string][]runtime.Object{},
|
|
registrations: map[string][]addonapiv1alpha1.RegistrationConfig{},
|
|
}
|
|
|
|
testAddOnConfigsImpl = &testAddon{
|
|
name: "test-addon-configs",
|
|
manifests: map[string][]runtime.Object{},
|
|
registrations: map[string][]addonapiv1alpha1.RegistrationConfig{},
|
|
supportedConfigGVRs: []schema.GroupVersionResource{addOnDeploymentConfigGVR},
|
|
}
|
|
|
|
mgrContext, cancel = context.WithCancel(context.TODO())
|
|
// start hub controller
|
|
go func() {
|
|
addonManager, err = addonmanager.New(cfg)
|
|
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
|
|
|
err = addonManager.AddAgent(testAddonImpl)
|
|
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
|
err = addonManager.AddAgent(testAddOnConfigsImpl)
|
|
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
|
|
|
err = addonManager.Start(mgrContext)
|
|
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
|
|
|
err = addon.RunManager(mgrContext, &controllercmd.ControllerContext{
|
|
KubeConfig: cfg,
|
|
EventRecorder: util.NewIntegrationTestEventRecorder("integration"),
|
|
})
|
|
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
|
}()
|
|
|
|
})
|
|
|
|
var _ = ginkgo.AfterSuite(func() {
|
|
ginkgo.By("tearing down the test environment")
|
|
|
|
cancel()
|
|
err := testEnv.Stop()
|
|
gomega.Expect(err).ToNot(gomega.HaveOccurred())
|
|
})
|
|
|
|
type testAddon struct {
|
|
name string
|
|
manifests map[string][]runtime.Object
|
|
registrations map[string][]addonapiv1alpha1.RegistrationConfig
|
|
approveCSR bool
|
|
cert []byte
|
|
prober *agent.HealthProber
|
|
installStrategy *agent.InstallStrategy
|
|
hostedModeEnabled bool
|
|
supportedConfigGVRs []schema.GroupVersionResource
|
|
}
|
|
|
|
func (t *testAddon) Manifests(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn) ([]runtime.Object, error) {
|
|
return t.manifests[cluster.Name], nil
|
|
}
|
|
|
|
func (t *testAddon) GetAgentAddonOptions() agent.AgentAddonOptions {
|
|
option := agent.AgentAddonOptions{
|
|
AddonName: t.name,
|
|
HealthProber: t.prober,
|
|
InstallStrategy: t.installStrategy,
|
|
HostedModeEnabled: t.hostedModeEnabled,
|
|
SupportedConfigGVRs: t.supportedConfigGVRs,
|
|
}
|
|
|
|
if len(t.registrations) > 0 {
|
|
option.Registration = &agent.RegistrationOption{
|
|
CSRConfigurations: func(cluster *clusterv1.ManagedCluster) []addonapiv1alpha1.RegistrationConfig {
|
|
return t.registrations[cluster.Name]
|
|
},
|
|
CSRApproveCheck: func(cluster *clusterv1.ManagedCluster, addon *addonapiv1alpha1.ManagedClusterAddOn, csr *certificatesv1.CertificateSigningRequest) bool {
|
|
return t.approveCSR
|
|
},
|
|
CSRSign: func(csr *certificatesv1.CertificateSigningRequest) []byte {
|
|
return t.cert
|
|
},
|
|
}
|
|
}
|
|
|
|
return option
|
|
}
|
|
|
|
func newClusterManagementAddon(name string) *addonapiv1alpha1.ClusterManagementAddOn {
|
|
return &addonapiv1alpha1.ClusterManagementAddOn{
|
|
TypeMeta: metav1.TypeMeta{},
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
Name: name,
|
|
},
|
|
Spec: addonapiv1alpha1.ClusterManagementAddOnSpec{
|
|
InstallStrategy: addonapiv1alpha1.InstallStrategy{
|
|
Type: addonapiv1alpha1.AddonInstallStrategyManual,
|
|
},
|
|
},
|
|
}
|
|
}
|