Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 E2E: Have a common function to install BMO and Ironic #1494

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

. "github.com/onsi/gomega"

v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -359,3 +360,96 @@ func IsBootedFromDisk(client *ssh.Client) (bool, error) {

return isDisk, nil
}

// BuildAndApplyKustomizeInput provides input for BuildAndApplyKustomize().
// If WaitForDeployment and/or WatchDeploymentLogs is set to true, then DeploymentName
// and DeploymentNamespace are expected.
type BuildAndApplyKustomizeInput struct {
// Path to the kustomization to build
Kustomization string

ClusterProxy framework.ClusterProxy

// If this is set to true. Perform a wait until the deployment specified by
// DeploymentName and DeploymentNamespace is available or WaitIntervals is timed out
WaitForDeployment bool

// If this is set to true. Set up a log watcher for the deployment specified by
// DeploymentName and DeploymentNamespace
WatchDeploymentLogs bool

// DeploymentName and DeploymentNamespace specified a deployment that will be waited and/or logged
DeploymentName string
DeploymentNamespace string

// Path to store the deployment logs
LogPath string

// Intervals to use in checking and waiting for the deployment
WaitIntervals []interface{}
}

func (input *BuildAndApplyKustomizeInput) validate() error {
// If neither WaitForDeployment nor WatchDeploymentLogs is true, we don't need to validate the input
if !input.WaitForDeployment && !input.WatchDeploymentLogs {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be && or ||, perhaps I am misunderstanding the use case but I would appreciate a comment here in the code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be &&. Only if both of them are false then we don't need to validate the other inputs. I'll add the comment

return nil
}
if input.WaitForDeployment && input.WaitIntervals == nil {
return errors.Errorf("WaitIntervals is expected if WaitForDeployment is set to true")
}
if input.WatchDeploymentLogs && input.LogPath == "" {
return errors.Errorf("LogPath is expected if WatchDeploymentLogs is set to true")
}
if input.DeploymentName == "" || input.DeploymentNamespace == "" {
return errors.Errorf("DeploymentName and DeploymentNamespace are expected if WaitForDeployment or WatchDeploymentLogs is true")
}
return nil
}

// BuildAndApplyKustomize takes input from BuildAndApplyKustomizeInput. It builds the provided kustomization
// and apply it to the cluster provided by clusterProxy
func BuildAndApplyKustomize(ctx context.Context, input *BuildAndApplyKustomizeInput) error {
Expect(input.validate()).To(BeNil())
var err error
kustomization := input.Kustomization
clusterProxy := input.ClusterProxy
manifest, err := buildKustomizeManifest(kustomization)
if err != nil {
return err
}
err = clusterProxy.Apply(ctx, manifest)
if err != nil {
return err
}

if !input.WaitForDeployment && !input.WatchDeploymentLogs {
return nil
}

deployment := &v1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: input.DeploymentName,
Namespace: input.DeploymentNamespace,
},
}

if input.WaitForDeployment {
// Wait for the deployment to become available
framework.WaitForDeploymentsAvailable(ctx, framework.WaitForDeploymentsAvailableInput{
Getter: clusterProxy.GetClient(),
Deployment: deployment,
}, input.WaitIntervals...)
}

if input.WatchDeploymentLogs {
// Set up log watcher
framework.WatchDeploymentLogsByName(ctx, framework.WatchDeploymentLogsByNameInput{
GetLister: clusterProxy.GetClient(),
Cache: clusterProxy.GetCache(ctx),
ClientSet: clusterProxy.GetClientSet(),
Deployment: deployment,
LogPath: input.LogPath,
})
}
return nil
}
71 changes: 22 additions & 49 deletions test/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2"
"sigs.k8s.io/cluster-api/test/framework"
Expand Down Expand Up @@ -116,64 +114,39 @@ var _ = SynchronizedBeforeSuite(func() []byte {
}
}

bmoIronicNamespace := "baremetal-operator-system"

if e2eConfig.GetVariable("DEPLOY_IRONIC") != "false" {
// Install Ironic
By("Installing Ironic")
kustomization := e2eConfig.GetVariable("IRONIC_KUSTOMIZATION")
manifest, err := buildKustomizeManifest(kustomization)
Expect(err).NotTo(HaveOccurred())
err = clusterProxy.Apply(ctx, manifest)
err := BuildAndApplyKustomize(ctx, &BuildAndApplyKustomizeInput{
Kustomization: e2eConfig.GetVariable("IRONIC_KUSTOMIZATION"),
ClusterProxy: clusterProxy,
WaitForDeployment: true,
WatchDeploymentLogs: true,
DeploymentName: "ironic",
DeploymentNamespace: bmoIronicNamespace,
LogPath: filepath.Join(artifactFolder, "logs", bmoIronicNamespace),
WaitIntervals: e2eConfig.GetIntervals("default", "wait-deployment"),
})
Expect(err).NotTo(HaveOccurred())

ironicDeployment := &v1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "ironic",
Namespace: "baremetal-operator-system",
},
}
// Wait for it to become available
framework.WaitForDeploymentsAvailable(ctx, framework.WaitForDeploymentsAvailableInput{
Getter: clusterProxy.GetClient(),
Deployment: ironicDeployment,
}, e2eConfig.GetIntervals("ironic", "wait-deployment")...)
// Set up log watcher
framework.WatchDeploymentLogsByName(ctx, framework.WatchDeploymentLogsByNameInput{
GetLister: clusterProxy.GetClient(),
Cache: clusterProxy.GetCache(ctx),
ClientSet: clusterProxy.GetClientSet(),
Deployment: ironicDeployment,
LogPath: filepath.Join(artifactFolder, "logs", ironicDeployment.GetNamespace()),
})
}

if e2eConfig.GetVariable("DEPLOY_BMO") != "false" {
// Install BMO
By("Installing BMO")
kustomization := e2eConfig.GetVariable("BMO_KUSTOMIZATION")
manifest, err := buildKustomizeManifest(kustomization)
Expect(err).NotTo(HaveOccurred())
err = clusterProxy.Apply(ctx, manifest)
Expect(err).NotTo(HaveOccurred())

bmoDeployment := &v1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "baremetal-operator-controller-manager",
Namespace: "baremetal-operator-system",
},
}
// Wait for it to become available
framework.WaitForDeploymentsAvailable(ctx, framework.WaitForDeploymentsAvailableInput{
Getter: clusterProxy.GetClient(),
Deployment: bmoDeployment,
}, e2eConfig.GetIntervals("default", "wait-deployment")...)
// Set up log watcher
framework.WatchDeploymentLogsByName(ctx, framework.WatchDeploymentLogsByNameInput{
GetLister: clusterProxy.GetClient(),
Cache: clusterProxy.GetCache(ctx),
ClientSet: clusterProxy.GetClientSet(),
Deployment: bmoDeployment,
LogPath: filepath.Join(artifactFolder, "logs", bmoDeployment.GetNamespace()),
err := BuildAndApplyKustomize(ctx, &BuildAndApplyKustomizeInput{
Kustomization: e2eConfig.GetVariable("BMO_KUSTOMIZATION"),
ClusterProxy: clusterProxy,
WaitForDeployment: true,
WatchDeploymentLogs: true,
DeploymentName: "baremetal-operator-controller-manager",
DeploymentNamespace: bmoIronicNamespace,
LogPath: filepath.Join(artifactFolder, "logs", bmoIronicNamespace),
WaitIntervals: e2eConfig.GetIntervals("default", "wait-deployment"),
})
Expect(err).NotTo(HaveOccurred())
}

return []byte(strings.Join([]string{clusterProxy.GetKubeconfigPath()}, ","))
Expand Down