Skip to content

Commit

Permalink
operator/pkg: test init and deinit
Browse files Browse the repository at this point in the history
In this commit, we unit test the init and deinit workflows
in the operator pkg.

Signed-off-by: Mohamed Awnallah <mohamedmohey2352@gmail.com>
  • Loading branch information
mohamedawnallah committed Sep 27, 2024
1 parent 4c8bcd4 commit 7f481a7
Show file tree
Hide file tree
Showing 4 changed files with 637 additions and 26 deletions.
2 changes: 1 addition & 1 deletion operator/pkg/controller/karmada/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewPlannerFor(karmada *operatorv1alpha1.Karmada, c client.Client, config *r
}

options := operator.NewJobInitOptions(opts...)
job = operator.NewInitJob(options)
job = operator.NewInitJob(options, operator.DefaultInitTasks)

case DeInitAction:
opts := []operator.DeInitOpt{
Expand Down
66 changes: 43 additions & 23 deletions operator/pkg/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"sync"

corev1 "k8s.io/api/core/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
utilversion "k8s.io/apimachinery/pkg/util/version"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand All @@ -39,6 +38,24 @@ import (
)

var (
// DefaultInitTasks contains the default tasks to be executed during the initialization process.
DefaultInitTasks = []workflow.Task{
tasks.NewPrepareCrdsTask(),
tasks.NewCertTask(),
tasks.NewNamespaceTask(),
tasks.NewUploadCertsTask(),
tasks.NewEtcdTask(),
tasks.NewKarmadaApiserverTask(),
tasks.NewUploadKubeconfigTask(),
tasks.NewKarmadaAggregatedApiserverTask(),
tasks.NewCheckApiserverHealthTask(),
tasks.NewKarmadaResourcesTask(),
tasks.NewRBACTask(),
tasks.NewComponentTask(),
tasks.NewWaitControlPlaneTask(),
}

// defaultCrdURL is the URL for fetching CRDs.
defaultCrdURL = "https://github.com/karmada-io/karmada/releases/download/%s/crds.tar.gz"
)

Expand All @@ -55,16 +72,17 @@ type InitOptions struct {

// Validate is used to validate the initOptions before creating initJob.
func (opt *InitOptions) Validate() error {
var errs []error

if len(opt.Name) == 0 || len(opt.Namespace) == 0 {
return errors.New("unexpected empty name or namespace")
}
if opt.CRDTarball.HTTPSource != nil {
if _, err := url.Parse(opt.CRDTarball.HTTPSource.URL); err != nil {
return fmt.Errorf("unexpected invalid crds remote url %s", opt.CRDTarball.HTTPSource.URL)
return fmt.Errorf("unexpected invalid crds remote url %s, err: %w", opt.CRDTarball.HTTPSource.URL, err)
}
}
if opt.Karmada == nil || opt.Karmada.Spec.Components == nil || opt.Karmada.Spec.Components.KarmadaAPIServer == nil {
return fmt.Errorf("invalid Karmada configuration: Karmada, Karmada components, and Karmada API server must be defined")
}
if !util.IsInCluster(opt.Karmada.Spec.HostCluster) && opt.Karmada.Spec.Components.KarmadaAPIServer.ServiceType == corev1.ServiceTypeClusterIP {
return fmt.Errorf("if karmada is installed in a remote cluster, the service type of karmada-apiserver must be either NodePort or LoadBalancer")
}
Expand All @@ -73,15 +91,15 @@ func (opt *InitOptions) Validate() error {
return fmt.Errorf("unexpected karmada invalid version %s", opt.KarmadaVersion)
}

if opt.Karmada.Spec.Components.Etcd.Local != nil && opt.Karmada.Spec.Components.Etcd.Local.CommonSettings.Replicas != nil {
if opt.Karmada.Spec.Components.Etcd != nil && opt.Karmada.Spec.Components.Etcd.Local != nil && opt.Karmada.Spec.Components.Etcd.Local.CommonSettings.Replicas != nil {
replicas := *opt.Karmada.Spec.Components.Etcd.Local.CommonSettings.Replicas

if (replicas % 2) == 0 {
klog.Warningf("invalid etcd replicas %d, expected an odd number", replicas)
}
}

return utilerrors.NewAggregate(errs)
return nil
}

// InitOpt defines a type of function to set InitOptions values.
Expand Down Expand Up @@ -111,23 +129,13 @@ type initData struct {

// NewInitJob initializes a job with list of init sub-task. and build
// init runData object.
func NewInitJob(opt *InitOptions) *workflow.Job {
func NewInitJob(opt *InitOptions, initTasks []workflow.Task) *workflow.Job {
initJob := workflow.NewJob()

// add the all tasks to the init job workflow.
initJob.AppendTask(tasks.NewPrepareCrdsTask())
initJob.AppendTask(tasks.NewCertTask())
initJob.AppendTask(tasks.NewNamespaceTask())
initJob.AppendTask(tasks.NewUploadCertsTask())
initJob.AppendTask(tasks.NewEtcdTask())
initJob.AppendTask(tasks.NewKarmadaApiserverTask())
initJob.AppendTask(tasks.NewUploadKubeconfigTask())
initJob.AppendTask(tasks.NewKarmadaAggregatedApiserverTask())
initJob.AppendTask(tasks.NewCheckApiserverHealthTask())
initJob.AppendTask(tasks.NewKarmadaResourcesTask())
initJob.AppendTask(tasks.NewRBACTask())
initJob.AppendTask(tasks.NewComponentTask())
initJob.AppendTask(tasks.NewWaitControlPlaneTask())
for _, task := range initTasks {
initJob.AppendTask(task)
}

initJob.SetDataInitializer(func() (workflow.RunData, error) {
return newRunData(opt)
Expand All @@ -136,12 +144,19 @@ func NewInitJob(opt *InitOptions) *workflow.Job {
return initJob
}

var clientFactory = func(kubeconfig *rest.Config) (clientset.Interface, error) {
return clientset.NewForConfig(kubeconfig)
}
var buildClientFromSecretRefFactory = func(client clientset.Interface, ref *operatorv1alpha1.LocalSecretReference) (clientset.Interface, error) {
return util.BuildClientFromSecretRef(client, ref)
}

func newRunData(opt *InitOptions) (*initData, error) {
if err := opt.Validate(); err != nil {
return nil, err
}

localClusterClient, err := clientset.NewForConfig(opt.Kubeconfig)
localClusterClient, err := clientFactory(opt.Kubeconfig)
if err != nil {
return nil, fmt.Errorf("error when creating local cluster client, err: %w", err)
}
Expand All @@ -152,7 +167,7 @@ func newRunData(opt *InitOptions) (*initData, error) {
if util.IsInCluster(opt.Karmada.Spec.HostCluster) {
remoteClient = localClusterClient
} else {
remoteClient, err = util.BuildClientFromSecretRef(localClusterClient, opt.Karmada.Spec.HostCluster.SecretRef)
remoteClient, err = buildClientFromSecretRefFactory(localClusterClient, opt.Karmada.Spec.HostCluster.SecretRef)
if err != nil {
return nil, fmt.Errorf("error when creating cluster client to install karmada, err: %w", err)
}
Expand All @@ -177,6 +192,11 @@ func newRunData(opt *InitOptions) (*initData, error) {
}
}

var hostClusterDNSDomain string
if opt.Karmada.Spec.HostCluster != nil && opt.Karmada.Spec.HostCluster.Networking != nil && opt.Karmada.Spec.HostCluster.Networking.DNSDomain != nil {
hostClusterDNSDomain = *opt.Karmada.Spec.HostCluster.Networking.DNSDomain
}

return &initData{
name: opt.Name,
namespace: opt.Namespace,
Expand All @@ -188,7 +208,7 @@ func newRunData(opt *InitOptions) (*initData, error) {
privateRegistry: privateRegistry,
components: opt.Karmada.Spec.Components,
featureGates: opt.Karmada.Spec.FeatureGates,
dnsDomain: *opt.Karmada.Spec.HostCluster.Networking.DNSDomain,
dnsDomain: hostClusterDNSDomain,
CertStore: certs.NewCertStore(),
}, nil
}
Expand Down
Loading

0 comments on commit 7f481a7

Please sign in to comment.