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

feat: add support for kueue to work with VAP on OCP 4.16+ #1480

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ spec:
- admissionregistration.k8s.io
resources:
- mutatingwebhookconfigurations
- validatingadmissionpolicies
- validatingadmissionpolicybindings
- validatingwebhookconfigurations
verbs:
- create
Expand Down
2 changes: 2 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ rules:
- admissionregistration.k8s.io
resources:
- mutatingwebhookconfigurations
- validatingadmissionpolicies
- validatingadmissionpolicybindings
- validatingwebhookconfigurations
verbs:
- create
Expand Down
17 changes: 13 additions & 4 deletions controllers/components/kueue/kueue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import (
"context"

"github.com/blang/semver/v4"
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -29,6 +30,8 @@
ctrl "sigs.k8s.io/controller-runtime"

componentApi "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1alpha1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/gc"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
Expand All @@ -41,7 +44,7 @@
)

func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
_, err := reconciler.ReconcilerFor(mgr, &componentApi.Kueue{}).
b := reconciler.ReconcilerFor(mgr, &componentApi.Kueue{}).

Check warning on line 47 in controllers/components/kueue/kueue_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/components/kueue/kueue_controller.go#L47

Added line #L47 was not covered by tests
// customized Owns() for Component with new predicates
Owns(&corev1.ConfigMap{}).
Owns(&corev1.Secret{}).
Expand Down Expand Up @@ -72,15 +75,21 @@
kustomize.WithLabel(labels.ODH.Component(LegacyComponentName), labels.True),
kustomize.WithLabel(labels.K8SCommon.PartOf, LegacyComponentName),
)).
WithAction(customizeResources).

Check warning on line 78 in controllers/components/kueue/kueue_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/components/kueue/kueue_controller.go#L78

Added line #L78 was not covered by tests
WithAction(deploy.NewAction(
deploy.WithCache(),
)).
WithAction(updatestatus.NewAction()).
// must be the final action
WithAction(gc.NewAction()).
Build(ctx)
WithAction(gc.NewAction())

Check warning on line 84 in controllers/components/kueue/kueue_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/components/kueue/kueue_controller.go#L84

Added line #L84 was not covered by tests

if err != nil {
if cluster.GetClusterInfo().Version.GTE(semver.MustParse("4.17.0")) {
b = b.OwnsGVK(gvk.ValidatingAdmissionPolicy) // "own" VAP, because we want it has owner so when kueue is removed it gets cleaned.
b = b.WatchesGVK(gvk.ValidatingAdmissionPolicyBinding) // "watch" VAPB, because we want it to be configable by user and it can be left behind when kueue is remov
b = b.WithAction(extraInitialize)
}

Check warning on line 90 in controllers/components/kueue/kueue_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/components/kueue/kueue_controller.go#L86-L90

Added lines #L86 - L90 were not covered by tests

if _, err := b.Build(ctx); err != nil {

Check warning on line 92 in controllers/components/kueue/kueue_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/components/kueue/kueue_controller.go#L92

Added line #L92 was not covered by tests
return err // no need customize error, it is done in the caller main
}

Expand Down
19 changes: 19 additions & 0 deletions controllers/components/kueue/kueue_controller_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@
"fmt"

componentApi "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1alpha1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
odhtypes "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/types"
odhdeploy "github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/resources"
)

func initialize(_ context.Context, rr *odhtypes.ReconciliationRequest) error {
rr.Manifests = append(rr.Manifests, manifestsPath())
return nil

Check warning on line 17 in controllers/components/kueue/kueue_controller_actions.go

View check run for this annotation

Codecov / codecov/patch

controllers/components/kueue/kueue_controller_actions.go#L17

Added line #L17 was not covered by tests
}

func extraInitialize(_ context.Context, rr *odhtypes.ReconciliationRequest) error {
// Add specific manifests if OCP is greater or equal 4.17.
rr.Manifests = append(rr.Manifests, extramanifestsPath())

Check warning on line 22 in controllers/components/kueue/kueue_controller_actions.go

View check run for this annotation

Codecov / codecov/patch

controllers/components/kueue/kueue_controller_actions.go#L20-L22

Added lines #L20 - L22 were not covered by tests
return nil
}

Expand Down Expand Up @@ -42,3 +50,14 @@

return nil
}

func customizeResources(_ context.Context, rr *odhtypes.ReconciliationRequest) error {
for i := range rr.Resources {
if rr.Resources[i].GroupVersionKind() == gvk.ValidatingAdmissionPolicyBinding {
// admin can update this resource
resources.SetAnnotation(&rr.Resources[i], annotations.ManagedByODHOperator, "false")
break // fast exist function

Check warning on line 59 in controllers/components/kueue/kueue_controller_actions.go

View check run for this annotation

Codecov / codecov/patch

controllers/components/kueue/kueue_controller_actions.go#L54-L59

Added lines #L54 - L59 were not covered by tests
}
}
return nil

Check warning on line 62 in controllers/components/kueue/kueue_controller_actions.go

View check run for this annotation

Codecov / codecov/patch

controllers/components/kueue/kueue_controller_actions.go#L62

Added line #L62 was not covered by tests
}
8 changes: 8 additions & 0 deletions controllers/components/kueue/kueue_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@
SourcePath: "rhoai",
}
}

func extramanifestsPath() odhtypes.ManifestInfo {
return odhtypes.ManifestInfo{
Path: odhdeploy.DefaultManifestPath,
ContextDir: ComponentName,
SourcePath: "rhoai/ocp-4.17-addons",
lburgazzoli marked this conversation as resolved.
Show resolved Hide resolved
}

Check warning on line 42 in controllers/components/kueue/kueue_support.go

View check run for this annotation

Codecov / codecov/patch

controllers/components/kueue/kueue_support.go#L37-L42

Added lines #L37 - L42 were not covered by tests
}
4 changes: 3 additions & 1 deletion controllers/datasciencecluster/kubebuilder_rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ package datasciencecluster
// +kubebuilder:rbac:groups=components.platform.opendatahub.io,resources=kueues/finalizers,verbs=update
// +kubebuilder:rbac:groups="monitoring.coreos.com",resources=prometheusrules,verbs=get;create;patch;delete;deletecollection;list;watch
// +kubebuilder:rbac:groups="monitoring.coreos.com",resources=podmonitors,verbs=get;create;delete;update;watch;list;patch
// +kubebuilder:rbac:groups="admissionregistration.k8s.io",resources=validatingadmissionpolicybindings,verbs=get;create;delete;update;watch;list;patch
// +kubebuilder:rbac:groups="admissionregistration.k8s.io",resources=validatingadmissionpolicies,verbs=get;create;delete;update;watch;list;patch

// CFO
//+kubebuilder:rbac:groups=components.platform.opendatahub.io,resources=codeflares,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -191,7 +193,7 @@ package datasciencecluster
// +kubebuilder:rbac:groups="operator.knative.dev",resources=knativeservings,verbs=*
// +kubebuilder:rbac:groups="config.openshift.io",resources=ingresses,verbs=get

// TODO: WB
// WB
// +kubebuilder:rbac:groups=components.platform.opendatahub.io,resources=workbenches,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=components.platform.opendatahub.io,resources=workbenches/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=components.platform.opendatahub.io,resources=workbenches/finalizers,verbs=update
Expand Down
57 changes: 53 additions & 4 deletions pkg/cluster/cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,15 @@
Version version.OperatorVersion `json:"version,omitempty"`
}

type ClusterInfo struct {
Type string `json:"type,omitempty"` // openshift , TODO: can be other value if we later support other type
Version version.OperatorVersion `json:"version,omitempty"`
}

var clusterConfig struct {
Namespace string
Release Release
Namespace string
Release Release
ClusterInfo ClusterInfo
}

// Init initializes cluster configuration variables on startup
Expand All @@ -54,15 +60,21 @@
return err
}

clusterConfig.ClusterInfo, err = getClusterInfo(ctx, cli)
if err != nil {
return err
}

Check warning on line 66 in pkg/cluster/cluster_config.go

View check run for this annotation

Codecov / codecov/patch

pkg/cluster/cluster_config.go#L63-L66

Added lines #L63 - L66 were not covered by tests

printClusterConfig(log)

return nil
}

func printClusterConfig(log logr.Logger) {
log.Info("Cluster config",
"Namespace", clusterConfig.Namespace,
"Release", clusterConfig.Release)
"Operator Namespace", clusterConfig.Namespace,
"Release", clusterConfig.Release,
"Cluster", clusterConfig.ClusterInfo)

Check warning on line 77 in pkg/cluster/cluster_config.go

View check run for this annotation

Codecov / codecov/patch

pkg/cluster/cluster_config.go#L75-L77

Added lines #L75 - L77 were not covered by tests
}

func GetOperatorNamespace() (string, error) {
Expand All @@ -76,6 +88,10 @@
return clusterConfig.Release
}

func GetClusterInfo() ClusterInfo {
return clusterConfig.ClusterInfo

Check warning on line 92 in pkg/cluster/cluster_config.go

View check run for this annotation

Codecov / codecov/patch

pkg/cluster/cluster_config.go#L91-L92

Added lines #L91 - L92 were not covered by tests
}

func GetDomain(ctx context.Context, c client.Client) (string, error) {
ingress := &unstructured.Unstructured{}
ingress.SetGroupVersionKind(gvk.OpenshiftIngress)
Expand All @@ -95,6 +111,21 @@
return domain, err
}

// This is an openshift speicifc implementation.
func getOCPVersion(ctx context.Context, c client.Client) (version.OperatorVersion, error) {
clusterVersion := &configv1.ClusterVersion{}
if err := c.Get(ctx, client.ObjectKey{
Name: OpenShiftVersionObj,
}, clusterVersion); err != nil {
return version.OperatorVersion{}, errors.New("unable to get OCP version")
}
v, err := semver.ParseTolerant(clusterVersion.Status.History[0].Version)
if err != nil {
return version.OperatorVersion{}, errors.New("unable to parse OCP version")
}
return version.OperatorVersion{Version: v}, nil

Check warning on line 126 in pkg/cluster/cluster_config.go

View check run for this annotation

Codecov / codecov/patch

pkg/cluster/cluster_config.go#L115-L126

Added lines #L115 - L126 were not covered by tests
}

func getOperatorNamespace() (string, error) {
operatorNS, exist := os.LookupEnv("OPERATOR_NAMESPACE")
if exist && operatorNS != "" {
Expand Down Expand Up @@ -199,6 +230,7 @@
Version: semver.Version{},
},
}

Check warning on line 233 in pkg/cluster/cluster_config.go

View check run for this annotation

Codecov / codecov/patch

pkg/cluster/cluster_config.go#L233

Added line #L233 was not covered by tests
// Set platform
platform, err := getPlatform(ctx, cli)
if err != nil {
Expand Down Expand Up @@ -230,6 +262,23 @@
return initRelease, nil
}

func getClusterInfo(ctx context.Context, cli client.Client) (ClusterInfo, error) {
c := ClusterInfo{
Version: version.OperatorVersion{
Version: semver.Version{},
},
Type: "OpenShift",
}
// Set OCP
ocpVersion, err := getOCPVersion(ctx, cli)
if err != nil {
return c, err
}
c.Version = ocpVersion

return c, nil

Check warning on line 279 in pkg/cluster/cluster_config.go

View check run for this annotation

Codecov / codecov/patch

pkg/cluster/cluster_config.go#L265-L279

Added lines #L265 - L279 were not covered by tests
}

// IsDefaultAuthMethod returns true if the default authentication method is IntegratedOAuth or empty.
// This will give indication that Operator should create userGroups or not in the cluster.
func IsDefaultAuthMethod(ctx context.Context, cli client.Client) (bool, error) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/cluster/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ const (

// Default cluster-scope Authentication CR name.
ClusterAuthenticationObj = "cluster"

// Default OpenShift version CR name.
OpenShiftVersionObj = "version"
)
12 changes: 12 additions & 0 deletions pkg/cluster/gvk/gvk.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,16 @@ var (
Version: "v1alpha1",
Kind: "Auth",
}

ValidatingAdmissionPolicy = schema.GroupVersionKind{
Group: "admissionregistration.k8s.io",
Version: "v1",
Kind: "ValidatingAdmissionPolicy",
}

ValidatingAdmissionPolicyBinding = schema.GroupVersionKind{
Group: "admissionregistration.k8s.io",
Version: "v1",
Kind: "ValidatingAdmissionPolicyBinding",
}
)
36 changes: 36 additions & 0 deletions tests/e2e/kueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ package e2e_test
import (
"testing"

"github.com/blang/semver/v4"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"

componentApi "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1alpha1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/resources"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/utils/test/matchers/jq"

. "github.com/onsi/gomega"
)

func kueueTestSuite(t *testing.T) {
Expand All @@ -20,10 +30,36 @@ func kueueTestSuite(t *testing.T) {

t.Run("Validate component enabled", componentCtx.ValidateComponentEnabled)
t.Run("Validate operands have OwnerReferences", componentCtx.ValidateOperandsOwnerReferences)
t.Run("Validate Kueue Dynamically create VAP", componentCtx.validateKueueVAPReady)
t.Run("Validate update operand resources", componentCtx.ValidateUpdateDeploymentsResources)
t.Run("Validate component disabled", componentCtx.ValidateComponentDisabled)
}

type KueueTestCtx struct {
*ComponentTestCtx
}

func (tc *KueueTestCtx) validateKueueVAPReady(t *testing.T) {
g := tc.NewWithT(t)
if cluster.GetClusterInfo().Version.GTE(semver.MustParse("4.17.0")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this check shold probably me moved to the t.Run block, like:

t.Run("Validate Kueue Dynamically create VAP", func(t *testing.T) {
        if !cluster.GetClusterInfo().Version.GTE(semver.MustParse("4.17.0") {
            t.Skip("Disabled as requires OpenShift >= 4.17")
            return
        }
        
	err = kueueCtx.validateVAPReady()
	require.NoError(t, err, "Kueue instance is not Ready")
})

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, doing rebase now, need local test before push the final change

g.Get(gvk.ValidatingAdmissionPolicy, types.NamespacedName{Name: "kueue-validating-admission-policy"}).Eventually().Should(
jq.Match(`.metadata.ownerReferences == "%s"`, componentApi.KueueInstanceName),
)
vapb, err := g.Get(gvk.ValidatingAdmissionPolicyBinding, types.NamespacedName{Name: "kueue-validating-admission-policy-binding"}).Get()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(vapb.GetOwnerReferences()).Should(BeEmpty())
return
}
scheme := runtime.NewScheme()
vap := &unstructured.Unstructured{}
vap.SetKind(gvk.ValidatingAdmissionPolicy.Kind)
err := resources.EnsureGroupVersionKind(scheme, vap)
g.Expect(err).To(HaveOccurred())
g.Expect(err.Error()).To(ContainSubstring("failed to get GVK"))

vapb := &unstructured.Unstructured{}
vapb.SetKind(gvk.ValidatingAdmissionPolicyBinding.Kind)
err = resources.EnsureGroupVersionKind(scheme, vapb)
g.Expect(err).To(HaveOccurred())
g.Expect(err.Error()).To(ContainSubstring("failed to get GVK"))
}
Loading