Skip to content

Commit a50809b

Browse files
committed
Adjust Trainer v2 precondition to check JobSet CRD
1 parent eb8fd29 commit a50809b

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

internal/controller/components/trainer/trainer_controller_actions.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package trainer
1919
import (
2020
"context"
2121

22+
"github.com/opendatahub-io/opendatahub-operator/v2/internal/controller/status"
2223
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
24+
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
2325
odherrors "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/errors"
2426
odhtypes "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/types"
2527
)
@@ -33,6 +35,15 @@ func checkPreConditions(ctx context.Context, rr *odhtypes.ReconciliationRequest)
3335
return ErrJobSetOperatorNotInstalled
3436
}
3537

38+
jobset, err := cluster.HasCRD(ctx, rr.Client, gvk.JobSetv1alpha2)
39+
if err != nil {
40+
return odherrors.NewStopError("failed to check %s CRDs version: %w", gvk.JobSetv1alpha2, err)
41+
}
42+
43+
if !jobset {
44+
return odherrors.NewStopError(status.JobSetCRDMissingMessage)
45+
}
46+
3647
return nil
3748
}
3849

internal/controller/components/trainer/trainer_controller_actions_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ package trainer
44
import (
55
"testing"
66

7+
ofapiv2 "github.com/operator-framework/api/pkg/operators/v2"
8+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11+
712
componentApi "github.com/opendatahub-io/opendatahub-operator/v2/api/components/v1alpha1"
813
"github.com/opendatahub-io/opendatahub-operator/v2/internal/controller/status"
14+
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
915
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/conditions"
1016
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/types"
1117
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/utils/test/fakeclient"
18+
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/utils/test/scheme"
1219

1320
. "github.com/onsi/gomega"
1421
)
@@ -34,3 +41,72 @@ func TestCheckPreConditions_Managed_JobSetOperatorNotInstalled(t *testing.T) {
3441
g.Expect(err).Should(HaveOccurred())
3542
g.Expect(err).To(MatchError(ContainSubstring(status.JobSetOperatorNotInstalledMessage)))
3643
}
44+
45+
func TestCheckPreConditions_Managed_JobSetCRDNotInstalled(t *testing.T) {
46+
ctx := t.Context()
47+
g := NewWithT(t)
48+
49+
cli, err := fakeclient.New(
50+
fakeclient.WithObjects(
51+
&ofapiv2.OperatorCondition{ObjectMeta: metav1.ObjectMeta{
52+
Name: jobSetOperator,
53+
}},
54+
),
55+
)
56+
g.Expect(err).ShouldNot(HaveOccurred())
57+
58+
trainer := componentApi.Trainer{
59+
Spec: componentApi.TrainerSpec{},
60+
}
61+
62+
rr := types.ReconciliationRequest{
63+
Client: cli,
64+
Instance: &trainer,
65+
Conditions: conditions.NewManager(&trainer, status.ConditionTypeReady),
66+
}
67+
68+
err = checkPreConditions(ctx, &rr)
69+
g.Expect(err).Should(HaveOccurred())
70+
g.Expect(err).To(MatchError(ContainSubstring(status.JobSetCRDMissingMessage)))
71+
}
72+
73+
func TestCheckPreConditions_Managed_JobSetCRDInstalled(t *testing.T) {
74+
ctx := t.Context()
75+
g := NewWithT(t)
76+
77+
fakeSchema, err := scheme.New()
78+
g.Expect(err).ShouldNot(HaveOccurred())
79+
80+
fakeSchema.AddKnownTypeWithName(gvk.JobSetv1alpha2, &unstructured.Unstructured{})
81+
82+
jobSetCRD := &apiextensionsv1.CustomResourceDefinition{
83+
ObjectMeta: metav1.ObjectMeta{
84+
Name: "jobsets.jobset.x-k8s.io",
85+
},
86+
Status: apiextensionsv1.CustomResourceDefinitionStatus{
87+
StoredVersions: []string{gvk.JobSetv1alpha2.Version},
88+
},
89+
}
90+
jobSetOperatorCondition := &ofapiv2.OperatorCondition{ObjectMeta: metav1.ObjectMeta{
91+
Name: jobSetOperator,
92+
}}
93+
94+
cli, err := fakeclient.New(
95+
fakeclient.WithScheme(fakeSchema),
96+
fakeclient.WithObjects(jobSetCRD, jobSetOperatorCondition),
97+
)
98+
g.Expect(err).ShouldNot(HaveOccurred())
99+
100+
trainer := componentApi.Trainer{
101+
Spec: componentApi.TrainerSpec{},
102+
}
103+
104+
rr := types.ReconciliationRequest{
105+
Client: cli,
106+
Instance: &trainer,
107+
Conditions: conditions.NewManager(&trainer, status.ConditionTypeReady),
108+
}
109+
110+
err = checkPreConditions(ctx, &rr)
111+
g.Expect(err).ShouldNot(HaveOccurred())
112+
}

internal/controller/status/status.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ To uninstall it, you should delete all RayClusters resources from the cluster, d
180180
// For JobSet operator checks.
181181
const (
182182
JobSetOperatorNotInstalledMessage = "JobSet operator not installed, please install it first"
183+
JobSetCRDMissingMessage = "JobSet CRD does not exist, please create JobSetOperator CR to proceed"
183184
)
184185

185186
// setConditions is a helper function to set multiple conditions at once.

pkg/cluster/gvk/gvk.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,4 +637,10 @@ var (
637637
Version: "v1beta1",
638638
Kind: "Kuadrant",
639639
}
640+
641+
JobSetv1alpha2 = schema.GroupVersionKind{
642+
Group: "jobset.x-k8s.io",
643+
Version: "v1alpha2",
644+
Kind: "JobSet",
645+
}
640646
)

tests/e2e/creation_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func dscManagementTestSuite(t *testing.T) {
5151
testCases := []TestCase{
5252
{"Ensure required operators with custom channels are installed", dscTestCtx.ValidateOperatorsWithCustomChannelsInstallation},
5353
{"Ensure required operators are installed", dscTestCtx.ValidateOperatorsInstallation},
54+
{"Ensure required resources are created", dscTestCtx.ValidateResourcesCreation},
5455
{"Validate creation of DSCInitialization instance", dscTestCtx.ValidateDSCICreation},
5556
{"Validate creation of DataScienceCluster instance", dscTestCtx.ValidateDSCCreation},
5657
{"Validate HardwareProfile resource", dscTestCtx.ValidateHardwareProfileCR},
@@ -157,6 +158,17 @@ func (tc *DSCTestCtx) ValidateOperatorsInstallation(t *testing.T) {
157158
RunTestCases(t, testCases, WithParallel())
158159
}
159160

161+
// ValidateResourcesCreation validates the creation of the required resources.
162+
func (tc *DSCTestCtx) ValidateResourcesCreation(t *testing.T) {
163+
t.Helper()
164+
165+
tc.EventuallyResourceCreatedOrUpdated(
166+
WithObjectToCreate(CreateJobSetOperator()),
167+
WithCondition(jq.Match(`.status.conditions[] | select(.type == "Available") | .status == "True"`)),
168+
WithCustomErrorMsg("Failed to create JobSetOperator resource"),
169+
)
170+
}
171+
160172
// ValidateDSCICreation validates the creation of a DSCInitialization.
161173
func (tc *DSCTestCtx) ValidateDSCICreation(t *testing.T) {
162174
t.Helper()

tests/e2e/helper_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,23 @@ func CreateHardwareProfile(name, namespace, apiVersion string) *unstructured.Uns
402402
return hwProfile
403403
}
404404

405+
// CreateJobSetOperator creates a JobSetOperator CR.
406+
func CreateJobSetOperator() *unstructured.Unstructured {
407+
return &unstructured.Unstructured{
408+
Object: map[string]interface{}{
409+
"apiVersion": "operator.openshift.io/v1",
410+
"kind": "JobSetOperator",
411+
"metadata": map[string]interface{}{
412+
"name": "cluster",
413+
},
414+
"spec": map[string]interface{}{
415+
"logLevel": "Normal",
416+
"operatorLogLevel": "Normal",
417+
},
418+
},
419+
}
420+
}
421+
405422
// CreateNamespaceWithLabels creates a namespace manifest with optional labels for use with WithObjectToCreate.
406423
func CreateNamespaceWithLabels(name string, labels map[string]string) *corev1.Namespace {
407424
ns := &corev1.Namespace{

0 commit comments

Comments
 (0)